public static void WriteHttpHeaderSettingFile(SharedHttpHeaderSettingEntity settingObj, HostEntity host) { if (!System.IO.Directory.Exists(HTTP_BASIC_SETTING_DIRECTORY)) { System.IO.Directory.CreateDirectory(HTTP_BASIC_SETTING_DIRECTORY); } if (host == null) { return; } if (settingObj == null) { return; } string fileName = String.Format("{0}\\{1}_{2}", HTTP_BASIC_SETTING_DIRECTORY, host.Name, "httpHeadingSetting.txt").ToLower(); //{content-Type}|||{appId}|||{appSecret}|||{GettingTokenRequestUrl}|||{GetttingTokenRequestBody}|||{AccessToken}|||{agency}|||{environment}|||{accesskey} string pattern = "{0}|||{1}|||{2}|||{3}|||{4}|||{5}|||{6}|||{7}|||{8}"; string content = String.Format(pattern, settingObj.ContentType, settingObj.AppId, settingObj.AppSecret, settingObj.GetTokenRequestUrl, settingObj.GetTokenRequestBody, settingObj.AccessToken, settingObj.Agency, settingObj.Environment, settingObj.AccessKey); Stream stream = File.Open(fileName, FileMode.Create); StreamWriter sw = new StreamWriter(stream); try { sw.WriteLine(content); } catch (Exception ex) { throw ex; } finally { sw.Close(); } }
public static void WriteHttpHeaderSettingFile(SharedHttpHeaderSettingEntity settingObj, HostEntity host) { if (!System.IO.Directory.Exists(HTTP_BASIC_SETTING_DIRECTORY)) { System.IO.Directory.CreateDirectory(HTTP_BASIC_SETTING_DIRECTORY); } if (host == null) return; if (settingObj == null) return; string fileName = String.Format("{0}\\{1}_{2}", HTTP_BASIC_SETTING_DIRECTORY, host.Name, "httpHeadingSetting.txt").ToLower(); //{content-Type}|||{appId}|||{appSecret}|||{GettingTokenRequestUrl}|||{GetttingTokenRequestBody}|||{AccessToken}|||{agency}|||{environment}|||{accesskey} string pattern = "{0}|||{1}|||{2}|||{3}|||{4}|||{5}|||{6}|||{7}|||{8}"; string content = String.Format(pattern, settingObj.ContentType, settingObj.AppId, settingObj.AppSecret, settingObj.GetTokenRequestUrl, settingObj.GetTokenRequestBody, settingObj.AccessToken, settingObj.Agency,settingObj.Environment,settingObj.AccessKey); Stream stream = File.Open(fileName, FileMode.Create); StreamWriter sw = new StreamWriter(stream); try { sw.WriteLine(content); } catch (Exception ex) { throw ex; } finally { sw.Close(); } }
public static SharedHttpHeaderSettingEntity ReadHttpHeaderSettingFile(HostEntity host) { if (!System.IO.Directory.Exists(HTTP_BASIC_SETTING_DIRECTORY)) { return null; } if (host == null) return null; string fileName =String.Format("{0}\\{1}_{2}", HTTP_BASIC_SETTING_DIRECTORY,host.Name,"httpHeadingSetting.txt"); if (!File.Exists(fileName)) { return null; } try { StreamReader sr = new StreamReader(fileName); string content = sr.ReadToEnd(); sr.Close(); if(String.IsNullOrWhiteSpace(content)) { return null; } content = content.Replace("\r\n", ""); var segments = content.Split(new string[]{"|||"},StringSplitOptions.None); //if(segments.Length != 9) //{ // return null; //} //{content-Type}|||{appId}|||{appSecret}|||{GettingTokenRequestUrl}|||{GetttingTokenRequestBody}|||{AccessToken}|||{agency}|||{environment}|||{accesskey} //string pattern = "{0}|||{1}|||{2}|||{3}|||{4}|||{5}|||{6}|||{7}|||{8}"; SharedHttpHeaderSettingEntity settingEntity = new SharedHttpHeaderSettingEntity(); if (segments.Length == 0) return null; settingEntity.ContentType = segments[0]; if(segments.Length > 1) settingEntity.AppId = segments[1]; if (segments.Length > 2) settingEntity.AppSecret = segments[2]; if (segments.Length > 3) settingEntity.GetTokenRequestUrl = segments[3]; if (segments.Length > 4) settingEntity.GetTokenRequestBody = segments[4]; if (segments.Length > 5) settingEntity.AccessToken = segments[5]; if (segments.Length > 6) settingEntity.Agency = segments[6]; if (segments.Length > 7) settingEntity.Environment = segments[7]; if (segments.Length > 8) settingEntity.AccessKey = segments[8]; return settingEntity; } catch (Exception ex) { throw ex; } }
private void GenerateToken(string requestURL, string requestBody) { if (String.IsNullOrWhiteSpace(requestURL) || String.IsNullOrWhiteSpace(requestBody)) { throw new ArgumentNullException("requestURL or requestBody"); } // send request to get access token ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return (true); }; HttpClient client = new HttpClient(); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, requestURL); requestMessage.Headers.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded"); requestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/x-www-form-urlencoded"); var task = client.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead); task.ContinueWith((response) => { if (response.IsCompleted && response.Status == TaskStatus.RanToCompletion) { if (SHARED_HTTP_HEADER_SETTING == null) SHARED_HTTP_HEADER_SETTING = new SharedHttpHeaderSettingEntity(); SHARED_HTTP_HEADER_SETTING.AccessToken = ParseToken(response.Result.Content.ReadAsStringAsync().Result); } else { MessageBox.Show("Failed to generate access token, please try again."); return; } }); }
private async void btnGenerateToken_Click(object sender, EventArgs e) { try { SharedHttpHeaderSettingEntity settingObj = new SharedHttpHeaderSettingEntity(); settingObj.ContentType = this.txtContentType.Text.Trim().Replace("\r\n", String.Empty); settingObj.AppId = this.txtAppID.Text.Trim().Replace("\r\n", String.Empty); settingObj.AppSecret = txtAppSecret.Text.Trim().Replace("\r\n", String.Empty); settingObj.GetTokenRequestUrl = this.txtRequestUrlForGettingToken.Text.Trim().Replace("\r\n", String.Empty); settingObj.GetTokenRequestBody = txtRequestBodyForGettingToken.Text.Trim().Replace("\r\n", String.Empty); settingObj.Agency = txtAgency.Text.Trim().Replace("\r\n", String.Empty); settingObj.Environment = txtEnvironment.Text.Trim().Replace("\r\n",String.Empty); settingObj.AccessKey = txtAccessKey.Text.Trim().Replace("\r\n", String.Empty); if(string.IsNullOrWhiteSpace(settingObj.ContentType) || string.IsNullOrWhiteSpace(settingObj.AppId) || string.IsNullOrWhiteSpace(settingObj.AppSecret) || string.IsNullOrWhiteSpace(settingObj.GetTokenRequestUrl) || string.IsNullOrWhiteSpace(settingObj.GetTokenRequestBody) || string.IsNullOrWhiteSpace(settingObj.Agency) || string.IsNullOrWhiteSpace(settingObj.Environment) || this.cmbHostsTab2.SelectedItem == null ) { MessageBox.Show("Please enter all required fields."); return; } var selectedHost = this.cmbHostsTab2.SelectedItem as HostEntity; FileUtility.WriteHttpHeaderSettingFile(settingObj, selectedHost); SHARED_HTTP_HEADER_SETTING = settingObj; // send request to get access token ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return (true); }; HttpClient client = new HttpClient(); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, settingObj.GetTokenRequestUrl); requestMessage.Headers.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded"); requestMessage.Content = new StringContent(settingObj.GetTokenRequestBody, Encoding.UTF8, "application/x-www-form-urlencoded"); var task = client.SendAsync(requestMessage,HttpCompletionOption.ResponseContentRead); await task.ContinueWith((requestTask) => { if (requestTask.IsCompleted && requestTask.Status == TaskStatus.RanToCompletion) { settingObj.AccessToken = this.txtResponseToken.Text =ParseToken( requestTask.Result.Content.ReadAsStringAsync().Result); FileUtility.WriteHttpHeaderSettingFile(settingObj, selectedHost); } else { MessageBox.Show("Failed to generate access token, please try again."); return; } }); } catch(Exception ex) { MessageBox.Show(ex.Message); } }
private void btnSaveSettings_Click(object sender, EventArgs e) { try { SharedHttpHeaderSettingEntity settingObj = new SharedHttpHeaderSettingEntity(); settingObj.ContentType = this.txtContentType.Text.Trim().Replace("\r\n", String.Empty); settingObj.AppId = this.txtAppID.Text.Trim().Replace("\r\n", String.Empty); settingObj.AppSecret = txtAppSecret.Text.Trim().Replace("\r\n", String.Empty); settingObj.GetTokenRequestUrl = this.txtRequestUrlForGettingToken.Text.Trim().Replace("\r\n", String.Empty); settingObj.GetTokenRequestBody = txtRequestBodyForGettingToken.Text.Trim().Replace("\r\n", String.Empty); settingObj.Agency = txtAgency.Text.Trim().Replace("\r\n", String.Empty); settingObj.Environment = txtEnvironment.Text.Trim().Replace("\r\n", String.Empty); settingObj.AccessKey = txtAccessKey.Text.Trim().Replace("\r\n", String.Empty); if (string.IsNullOrWhiteSpace(settingObj.ContentType) || string.IsNullOrWhiteSpace(settingObj.AppId) || string.IsNullOrWhiteSpace(settingObj.AppSecret) || string.IsNullOrWhiteSpace(settingObj.GetTokenRequestUrl) || string.IsNullOrWhiteSpace(settingObj.GetTokenRequestBody) || string.IsNullOrWhiteSpace(settingObj.Agency) || string.IsNullOrWhiteSpace(settingObj.Environment) || this.cmbHostsTab2.SelectedItem == null ) { MessageBox.Show("Please enter all required fields."); return; } var selectedHost = this.cmbHostsTab2.SelectedItem as HostEntity; FileUtility.WriteHttpHeaderSettingFile(settingObj, selectedHost); MessageBox.Show("Update Successfully."); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private NameValueCollection GetHeaders(bool usingSharedHeader, SharedHttpHeaderSettingEntity sharedSetting) { NameValueCollection headerList = new NameValueCollection(); string headers = this.txtRequestHeader.Text.Trim(); string[] arr = headers.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string s in arr) { if (!String.IsNullOrWhiteSpace(s)) { string[] kv = s.Split(new char[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries); if (kv.Length > 1) { string key = kv[0].Trim().ToLower(); if (!key.StartsWith("//")) { headerList.Add(key, kv[1].Trim()); } } } } if (usingSharedHeader && sharedSetting != null) { if (headerList.AllKeys.Contains("content-type")) { headerList["content-type"] = sharedSetting.ContentType; } else { headerList.Add("content-type", sharedSetting.ContentType); } if (headerList.AllKeys.Contains("x-accela-appid")) { headerList["x-accela-appid"] = sharedSetting.AppId; } else { headerList.Add("x-accela-appid", sharedSetting.AppId); } if (headerList.AllKeys.Contains("x-accela-appsecret")) { headerList["x-accela-appsecret"] = sharedSetting.AppSecret; } else { headerList.Add("x-accela-appsecret", sharedSetting.AppSecret); } if (headerList.AllKeys.Contains("x-accela-agency")) { headerList["x-accela-agency"] = sharedSetting.Agency; } else { headerList.Add("x-accela-agency", sharedSetting.Agency); } if (headerList.AllKeys.Contains("x-accela-environment")) { headerList["x-accela-environment"] = sharedSetting.Environment; } else { headerList.Add("x-accela-environment", sharedSetting.Environment); } if (headerList.AllKeys.Contains("authorization")) { headerList["authorization"] = sharedSetting.AccessToken; } else { headerList.Add("authorization", sharedSetting.AccessToken); } if (headerList.AllKeys.Contains("x-accela-subsystem-accesskey")) { headerList["x-accela-subsystem-accesskey"] = sharedSetting.AccessKey; } else { headerList.Add("x-accela-subsystem-accesskey", sharedSetting.AccessKey); } } //if (sharedSetting != null) //{ // headerList.AllKeys.Contains("Content-Type") // var enumerator = headerList.GetEnumerator(); // while(enumerator.MoveNext()) // { // //enumerator.Current. // } //} return headerList; }
private void FillSharedHttpHeaderSettings(SharedHttpHeaderSettingEntity entity) { if(entity == null) { this.txtAgency.Clear(); this.txtAppID.Clear(); this.txtAppSecret.Clear(); this.txtContentType.Clear(); this.txtEnvironment.Clear(); this.txtRequestBodyForGettingToken.Clear(); this.txtRequestUrlForGettingToken.Clear(); this.txtResponseToken.Clear(); this.txtAccessKey.Clear(); } else { this.txtAgency.Text = entity.Agency; this.txtAppID.Text = entity.AppId; this.txtAppSecret.Text = entity.AppSecret; this.txtContentType.Text = entity.ContentType; this.txtEnvironment.Text = entity.Environment; this.txtRequestBodyForGettingToken.Text = entity.GetTokenRequestBody; this.txtRequestUrlForGettingToken.Text = entity.GetTokenRequestUrl; this.txtResponseToken.Text = entity.AccessToken; this.txtAccessKey.Text = entity.AccessKey; } }
private void LoadSharedHttpHeaderSetting(HostEntity host) { try { if (host == null) return; // open application, init a token //btnGenerateToken_Click(null, null); SHARED_HTTP_HEADER_SETTING = FileUtility.ReadHttpHeaderSettingFile(host); FillSharedHttpHeaderSettings(SHARED_HTTP_HEADER_SETTING); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }
public static SharedHttpHeaderSettingEntity ReadHttpHeaderSettingFile(HostEntity host) { if (!System.IO.Directory.Exists(HTTP_BASIC_SETTING_DIRECTORY)) { return(null); } if (host == null) { return(null); } string fileName = String.Format("{0}\\{1}_{2}", HTTP_BASIC_SETTING_DIRECTORY, host.Name, "httpHeadingSetting.txt"); if (!File.Exists(fileName)) { return(null); } try { StreamReader sr = new StreamReader(fileName); string content = sr.ReadToEnd(); sr.Close(); if (String.IsNullOrWhiteSpace(content)) { return(null); } content = content.Replace("\r\n", ""); var segments = content.Split(new string[] { "|||" }, StringSplitOptions.None); //if(segments.Length != 9) //{ // return null; //} //{content-Type}|||{appId}|||{appSecret}|||{GettingTokenRequestUrl}|||{GetttingTokenRequestBody}|||{AccessToken}|||{agency}|||{environment}|||{accesskey} //string pattern = "{0}|||{1}|||{2}|||{3}|||{4}|||{5}|||{6}|||{7}|||{8}"; SharedHttpHeaderSettingEntity settingEntity = new SharedHttpHeaderSettingEntity(); if (segments.Length == 0) { return(null); } settingEntity.ContentType = segments[0]; if (segments.Length > 1) { settingEntity.AppId = segments[1]; } if (segments.Length > 2) { settingEntity.AppSecret = segments[2]; } if (segments.Length > 3) { settingEntity.GetTokenRequestUrl = segments[3]; } if (segments.Length > 4) { settingEntity.GetTokenRequestBody = segments[4]; } if (segments.Length > 5) { settingEntity.AccessToken = segments[5]; } if (segments.Length > 6) { settingEntity.Agency = segments[6]; } if (segments.Length > 7) { settingEntity.Environment = segments[7]; } if (segments.Length > 8) { settingEntity.AccessKey = segments[8]; } return(settingEntity); } catch (Exception ex) { throw ex; } }