/// <summary> /// Maps the properties of a <see cref="ServiceEndpoint"/> instance into a /// <see cref="HttpEndpointCommunication"/> instance. /// </summary> /// <param name="endpoint">The endpoint to map.</param> /// <param name="commsEvent">The instance to populate. If <see langword="null"/> a new instance will be created.</param> /// <returns>The updated <paramref name="commsEvent"/>.</returns> internal HttpEndpointCommunication ToHttpEndpointCommunication(ServiceEndpoint endpoint, HttpEndpointCommunication commsEvent = null) { commsEvent = commsEvent ?? new HttpEndpointCommunication(); commsEvent.ServiceEndpointId = endpoint.Id; commsEvent.Timestamp = DateTime.Now; commsEvent.RequestUrl = endpoint.Url; commsEvent.RequestMethod = endpoint.Method; commsEvent.RequestFormat = endpoint.RequestFormat; commsEvent.ResponseFormat = endpoint.ResponseFormat; return commsEvent; }
public string SendRequest(ServiceEndpoint endpoint, ServiceEndpointUtilisation utilisation, Application application, ApplicationData updateTarget) { if (this.type == "VALIDATE") { string validateStr = AssemblyResourceReader.ReadAsString(string.Format("Test_Data.EndpointValidation.VALIDATE-{0}.json", endpoint.Id.PadLeft(2, '0'))); return validateStr; } return null; }
/// <summary> /// Creates a new <see cref="HttpWebRequest" />. /// </summary> /// <param name="endpoint">Contains information on what sort of request to create.</param> /// <param name="application">The application containing values to map.</param> /// <param name="requestFieldMap">The list of request fields to map.</param> /// <param name="obfuscatedRequestBody">The request body.</param> /// <returns>A new <see cref="HttpWebRequest" />.</returns> internal HttpWebRequest Create(ServiceEndpoint endpoint, Application application, MappedFieldList requestFieldMap, out string obfuscatedRequestBody) { obfuscatedRequestBody = string.Empty; if (endpoint.Method == HttpRequestMethod.GET && endpoint.RequestFormat != ServiceDataFormat.HttpVariables) { throw new InvalidOperationException(string.Format(ExceptionMessages.IncompatibleRequestFormat, endpoint.RequestFormat, endpoint.Method)); } return (endpoint.Method == HttpRequestMethod.GET) ? this.CreateGetRequest(endpoint.Url, application, requestFieldMap, out obfuscatedRequestBody) : this.CreatePostRequest(endpoint, application, requestFieldMap, out obfuscatedRequestBody); }
public void ValidateImportNonExistingEndpoint() { ServiceEndpoint serviceEndpoint = new ServiceEndpoint { Id = "48387627348jfksdfjmfkjd" }; MigrationData migrationData = new MigrationData(); migrationData.ServiceEndpoints.Add(serviceEndpoint); migrationData.SystemVersion = new Version(16, 5); MigrationDataResults results = this.validator.Validate(migrationData); MigrationDataMessageList messages = results.GetNotificationsFor(serviceEndpoint); Assert.AreEqual(0, messages.Count()); }
/// <summary> /// Creates a returns a POST request. /// </summary> /// <param name="endpoint">Contains information on what sort of request to create.</param> /// <param name="application">The application.</param> /// <param name="requestFieldMap">The list of request fields to map.</param> /// <param name="obfuscatedRequestBody">The request body.</param> /// <returns>A new <see cref="HttpWebRequest" /> uses the POST method.</returns> private HttpWebRequest CreatePostRequest(ServiceEndpoint endpoint, Application application, MappedFieldList requestFieldMap, out string obfuscatedRequestBody) { string content = string.Empty; string contentType = string.Empty; obfuscatedRequestBody = string.Empty; switch (endpoint.RequestFormat) { case ServiceDataFormat.HttpVariables: contentType = InternetMediaType.ApplicationXFormEncodedData; content = this.GetHttpVariableRequestContent(application, requestFieldMap).ToHttpVariables(); obfuscatedRequestBody = this.ObfuscateRequest(content, HttpResources.HttpRequestFieldValueLocator, requestFieldMap); break; case ServiceDataFormat.Json: contentType = InternetMediaType.ApplicationJson; content = this.GetJsonRequestContent(application, requestFieldMap); obfuscatedRequestBody = this.ObfuscateRequest(content, HttpResources.JsonRequestFieldValueLocator, requestFieldMap); break; case ServiceDataFormat.Xml: contentType = InternetMediaType.TextXml; content = this.GetXmlRequestContent(endpoint.RequestRoot, application, requestFieldMap); obfuscatedRequestBody = this.ObfuscateRequest(content, HttpResources.XmlRequestFieldValueLocator, requestFieldMap); break; } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint.Url); request.Method = WebRequestMethods.Http.Post; request.ContentType = contentType; byte[] bytes = Encoding.UTF8.GetBytes(content); using (Stream stream = request.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); } return request; }
/// <summary> /// Sends a request to a service endpoint and returns the response /// mapped to a JSON string. /// </summary> /// <param name="communicator">The endpoint communicator.</param> /// <param name="endpoint">The endpoint definition.</param> /// <param name="utilisation">Provides a data field mapping for the request / response.</param> /// <param name="applicationData">The application data.</param> /// <param name="updateTarget">The update target.</param> /// <returns> /// A mapped JSON string of the response from the service endpoint. /// </returns> public static string SendRequest(this IServiceEndpointCommunicator communicator, ServiceEndpoint endpoint, ServiceEndpointUtilisation utilisation, ApplicationData applicationData, ApplicationData updateTarget) { return communicator.SendRequest(endpoint, utilisation, new Application(applicationData), updateTarget); }
/// <summary> /// Sends a request to a service endpoint and returns the response /// mapped to a JSON string. /// </summary> /// <param name="communicator">The endpoint communicator.</param> /// <param name="endpoint">The endpoint definition.</param> /// <param name="utilisation">Provides a data field mapping for the request / response.</param> /// <param name="applicationData">The application data, which is also the target when mapping back.</param> /// <returns> /// A mapped JSON string of the response from the service endpoint. /// </returns> public static string SendRequest(this IServiceEndpointCommunicator communicator, ServiceEndpoint endpoint, ServiceEndpointUtilisation utilisation, ApplicationData applicationData) { return communicator.SendRequest(endpoint, utilisation, applicationData, applicationData); }
public string SendRequest(ServiceEndpoint endpoint, ServiceEndpointUtilisation utilisation, Application application, ApplicationData updateTarget) { throw new NotImplementedException(); }
/// <summary> /// Sends a request to a service endpoint and returns the response /// mapped to a JSON string. /// </summary> /// <param name="endpoint">The endpoint definition.</param> /// <param name="utilisation">Provides a data field mapping for the request / response.</param> /// <param name="application">The application to map.</param> /// <param name="updateTarget">The update target.</param> /// <returns>A mapped JSON string of the response from the service endpoint.</returns> public string SendRequest(ServiceEndpoint endpoint, ServiceEndpointUtilisation utilisation, Application application, ApplicationData updateTarget) { MappedFieldList consolidatedMap = new MappedFieldList(endpoint.RequestFieldMap); consolidatedMap.MergeOnTarget(utilisation.RequestFieldMap); if (utilisation.MapAllFieldsInRequest) { MappedFieldList allFieldMap = new MappedFieldList(); allFieldMap.AddRange(application.ApplicationData.Select(val => new MappedField { MapType = MapType.Field, Target = val.Key, Source = val.Key })); consolidatedMap.MergeOnTarget(allFieldMap); } bool cached = consolidatedMap.Count == 0 && this.cachedResponse.ContainsKey(endpoint.Url); HttpEndpointCommunication commsEvent = new HttpEndpointCommunicationMapper().ToHttpEndpointCommunication(endpoint); HttpWebResponse response = null; string responseBody = string.Empty; try { if (cached) { response = this.cachedResponse[endpoint.Url]; responseBody = this.cachedResponseBody[endpoint.Url]; } else { response = this.DoSendRequest(endpoint, commsEvent, application, consolidatedMap); responseBody = this.ReadResponseContent(response); if (consolidatedMap.Count == 0) { this.cachedResponse.Add(endpoint.Url, response); this.cachedResponseBody.Add(endpoint.Url, responseBody); } } JToken token = new HttpWebResponseReader().ConvertToJson(responseBody, endpoint.ResponseFormat, endpoint.ResponseRoot); if (utilisation.ResponseFieldsMatchRequest) { MappedFieldList allFieldMap = new MappedFieldList(); allFieldMap.AddRange(consolidatedMap.Select(mappedField => new MappedField { MapType = MapType.Content, Source = mappedField.Target, Target = mappedField.Source })); utilisation.ResponseFieldMap.MergeOnTarget(allFieldMap); } if (utilisation.ResponseFieldMap != null && utilisation.ResponseFieldMap.Count > 0) { token = new HttpEndpointResponseMapper().MapToken(token, response, utilisation.ResponseFieldMap, updateTarget, utilisation.MaxResults); } return (token ?? string.Empty).ToString(); } catch (WebException e) { response = (HttpWebResponse)e.Response; responseBody = this.ReadResponseContent(response); commsEvent.ExceptionMessage = e.Message; throw; } catch (Exception e) { commsEvent.ExceptionMessage = e.Message; throw; } finally { if (response != null) { commsEvent.ResponseStatusCode = (int)response.StatusCode; commsEvent.ResponseContent = responseBody; response.Close(); } if (!cached) { this.logger.LogCommunication(commsEvent); } } }
/// <summary> /// Sends a request to a service endpoint and returns the <see cref="HttpWebResponse"/>. /// </summary> /// <param name="endpoint">The endpoint definition.</param> /// <param name="commsEvent">Tracks the communication event.</param> /// <param name="application">The application to map.</param> /// <param name="requestFieldMap">The list of mapped fields for the request.</param> /// <returns>The <see cref="HttpWebResponse"/>.</returns> private HttpWebResponse DoSendRequest(ServiceEndpoint endpoint, HttpEndpointCommunication commsEvent, Application application, MappedFieldList requestFieldMap) { string requestBody; HttpWebRequest request = new HttpWebRequestFactory().Create(endpoint, application, requestFieldMap, out requestBody); request.Timeout = Convert.ToInt32(TimeSpan.FromSeconds(this.TimeoutSeconds).TotalMilliseconds); commsEvent.RequestHeaders = request.Headers.ToDictionary(); commsEvent.RequestContent = requestBody; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); commsEvent.ResponseHeaders = response.Headers.ToDictionary(); return response; }
public void ValidateNotOverwriteExistingServiceEndpoint() { const string id = "53858ea7f9d8ee351cff376a"; ServiceEndpoint serviceEndpoint = new ServiceEndpoint { Id = id }; MigrationData migrationData = new MigrationData(); migrationData.ServiceEndpoints.Add(serviceEndpoint); migrationData.SystemVersion = new Version(16, 5); MigrationDataResults results = this.validator.Validate(migrationData); MigrationDataMessageList messages = results.GetNotificationsFor(serviceEndpoint); Assert.AreEqual(1, messages.Count()); MigrationDataMessage message = messages.First(); Assert.AreEqual(MessageType.Error, message.MessageType); Assert.AreEqual(string.Format("Service Endpoint \"Existing Service Endpoint\" with ID \"{0}\" exists - Service Endpoint will not be imported", id), message.Message); }