|
| 1 | +using Common; |
| 2 | +using CxViewerAction.Entities; |
| 3 | +using CxViewerAction.Helpers; |
| 4 | +using CxViewerAction.WebPortal; |
| 5 | +using System; |
| 6 | +using System.IO; |
| 7 | +using System.Net; |
| 8 | +using System.Text; |
| 9 | +using System.Web.Script.Serialization; |
| 10 | + |
| 11 | +namespace CxViewerAction.Services |
| 12 | +{ |
| 13 | + public class CxRESTApiNoneConfiguration |
| 14 | + { |
| 15 | + #region Fields |
| 16 | + |
| 17 | + private string _restAPIRelativePath = "/cxrestapi/Configurations/None"; |
| 18 | + private const string requestContentType = "application/json, text/plain, */*"; |
| 19 | + |
| 20 | + #endregion |
| 21 | + |
| 22 | + #region API |
| 23 | + |
| 24 | + public void InitNoneBaseUrl() |
| 25 | + { |
| 26 | + try |
| 27 | + { |
| 28 | + LoginHelper.PortalConfiguration = new CxPortalConfiguration(); |
| 29 | + Uri uri = GetLoginUri(); |
| 30 | + HttpWebRequest webRequest = GetWebRequest(uri); |
| 31 | + CxPortalConfiguration result = GetWebResponse(webRequest); |
| 32 | + LoginHelper.PortalConfiguration.WebServer = GetWebServerBaseUrl(result.WebServer); |
| 33 | + } |
| 34 | + catch (System.Net.WebException ex) |
| 35 | + { |
| 36 | + var response = (System.Net.HttpWebResponse)ex.Response; |
| 37 | + |
| 38 | + if (response.StatusCode == HttpStatusCode.NotFound) |
| 39 | + { |
| 40 | + LoginHelper.PortalConfiguration.WebServer = LoginHelper.ServerBaseUrl; |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + Logger.Create().Error("CxRESTApiPortalConfiguration->GetPortalBaseUrl: " + ex.ToString()); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public CxPortalConfiguration InitNoneConfigurationDetails() |
| 50 | + { |
| 51 | + try |
| 52 | + { |
| 53 | + LoginHelper.PortalConfiguration = new CxPortalConfiguration(); |
| 54 | + Uri uri = GetLoginUri(); |
| 55 | + HttpWebRequest webRequest = GetWebRequest(uri); |
| 56 | + return GetWebResponse(webRequest); |
| 57 | + } |
| 58 | + catch (System.Net.WebException ex) |
| 59 | + { |
| 60 | + var response = (System.Net.HttpWebResponse)ex.Response; |
| 61 | + |
| 62 | + if (response.StatusCode == HttpStatusCode.NotFound) |
| 63 | + { |
| 64 | + LoginHelper.PortalConfiguration.WebServer = LoginHelper.ServerBaseUrl; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + Logger.Create().Error("CxRESTApiPortalConfiguration->InitPortalConfigurationDetails: " + ex.ToString()); |
| 69 | + } |
| 70 | + } |
| 71 | + return null; |
| 72 | + } |
| 73 | + #endregion |
| 74 | + |
| 75 | + #region Private methods |
| 76 | + |
| 77 | + private string GetWebServerBaseUrl(string url) |
| 78 | + { |
| 79 | + if (string.IsNullOrEmpty(url)) |
| 80 | + { |
| 81 | + return LoginHelper.ServerBaseUrl; |
| 82 | + } |
| 83 | + |
| 84 | + Uri uri = new Uri(LoginHelper.ServerBaseUrl); |
| 85 | + |
| 86 | + if (!url.StartsWith(uri.Scheme)) |
| 87 | + { |
| 88 | + return string.Format("{0}://{1}", uri.Scheme, url); |
| 89 | + } |
| 90 | + |
| 91 | + return url; |
| 92 | + } |
| 93 | + |
| 94 | + private HttpWebRequest GetWebRequest(Uri uri) |
| 95 | + { |
| 96 | + HttpWebRequest webRequest = new CxRESTApiWebRequestCore().Create(uri, "GET"); |
| 97 | + webRequest.Accept = requestContentType; |
| 98 | + OidcLoginData oidcLoginData = OidcLoginData.GetOidcLoginDataInstance(); |
| 99 | + if (CxVSWebServiceWrapper.IsTokenExpired(oidcLoginData)) |
| 100 | + { |
| 101 | + //get the login data again with the new access token |
| 102 | + oidcLoginData = OidcLoginData.GetOidcLoginDataInstance(); |
| 103 | + }; |
| 104 | + webRequest.Headers.Clear(); |
| 105 | + webRequest.Headers.Add(Constants.AUTHORIZATION_HEADER, Constants.BEARER + oidcLoginData.AccessToken); |
| 106 | + return webRequest; |
| 107 | + } |
| 108 | + |
| 109 | + private CxPortalConfiguration GetWebResponse(HttpWebRequest webRequest) |
| 110 | + { |
| 111 | + string responseText = string.Empty; |
| 112 | + CxPortalConfiguration portalConfiguration = new CxPortalConfiguration(); |
| 113 | + HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); |
| 114 | + |
| 115 | + if (webResponse.StatusCode != HttpStatusCode.OK) |
| 116 | + { |
| 117 | + Logger.Create().Error("CxRESTApiPortalConfiguration->HandleWebResponse->Rest API, status message: " + webResponse.StatusDescription); |
| 118 | + } |
| 119 | + |
| 120 | + using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), ASCIIEncoding.ASCII)) |
| 121 | + { |
| 122 | + responseText = reader.ReadToEnd(); |
| 123 | + } |
| 124 | + |
| 125 | + if (!string.IsNullOrEmpty(responseText)) |
| 126 | + { |
| 127 | + JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); |
| 128 | + portalConfiguration = (CxPortalConfiguration)javaScriptSerializer.Deserialize(responseText, typeof(CxPortalConfiguration)); |
| 129 | + } |
| 130 | + |
| 131 | + return portalConfiguration; |
| 132 | + } |
| 133 | + |
| 134 | + private Uri GetLoginUri() |
| 135 | + { |
| 136 | + string url = string.Format("{0}{1}", |
| 137 | + Common.Text.Text.RemoveTrailingSlash(LoginHelper.ServerBaseUrl), |
| 138 | + _restAPIRelativePath); |
| 139 | + |
| 140 | + return new Uri(url); |
| 141 | + } |
| 142 | + |
| 143 | + #endregion |
| 144 | + } |
| 145 | +} |
0 commit comments