/// <summary> /// Returns the response stream by calling REST service. /// </summary> /// <param name="request">The request.</param> /// <returns>Response from REST service.</returns> public override byte[] GetResponseStream(HttpWebRequest request) { FaultHandler handler = new FaultHandler(this.context); byte[] receivedBytes = new byte[0]; try { // Check whether the retryPolicy is null. if (this.context.IppConfiguration.RetryPolicy == null) { // If yes then call the rest service without retry framework enabled. receivedBytes = GetRestServiceCallResponseStream(request); } else { // If no then call the rest service using the execute action of retry framework. this.context.IppConfiguration.RetryPolicy.ExecuteAction(() => { receivedBytes = GetRestServiceCallResponseStream(request); }); } } catch (WebException webException) { // System.Net.HttpWebRequest.Abort() was previously called.-or- The time-out // period for the request expired.-or- An error occurred while processing the request. bool isIps = false; if (this.context.ServiceType == IntuitServicesType.IPS) { isIps = true; } IdsException idsException = handler.ParseResponseAndThrowException(webException, isIps); if (idsException != null) { this.context.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Error, idsException.ToString()); CoreHelper.AdvancedLogging.Log(idsException.ToString()); throw idsException; } } finally { this.context.RequestId = null; } return(receivedBytes); }
/// <summary> /// Creates the Event Args for Exception/Fault responses from server. /// </summary> /// <param name="exception">The exception class.</param> /// <returns>Async CallCompletedEvent Arguments.</returns> private AsyncCallCompletedEventArgs CreateEventArgsForException(Exception exception) { AsyncCallCompletedEventArgs resultArguments = null; WebException webException = exception as WebException; if (webException != null) { bool isIps = false; if (this.context.ServiceType == IntuitServicesType.IPS) { isIps = true; } FaultHandler handler = new FaultHandler(this.context); IdsException idsException = handler.ParseResponseAndThrowException(webException, isIps); if (idsException != null) { this.context.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Error, idsException.ToString()); CoreHelper.AdvancedLogging.Log(idsException.ToString()); resultArguments = new AsyncCallCompletedEventArgs(null, idsException); } } else { IdsException idsException = exception as IdsException; if (idsException != null) { this.context.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Error, idsException.ToString()); CoreHelper.AdvancedLogging.Log(idsException.ToString()); resultArguments = new AsyncCallCompletedEventArgs(null, idsException); } else { this.context.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Error, idsException.ToString()); CoreHelper.AdvancedLogging.Log(idsException.ToString()); resultArguments = new AsyncCallCompletedEventArgs(null, new IdsException("Exception has been generated.", exception)); } } return(resultArguments); }
/// <summary> /// Returns the response by calling REST service. /// </summary> /// <param name="request">The request.</param> /// <returns>Response from REST service.</returns> public override string GetResponse(HttpWebRequest request) { FaultHandler handler = new FaultHandler(this.context); // Create a variable for storing the response. string response = string.Empty; try { // Check whether the retryPolicy is null. if (this.context.IppConfiguration.RetryPolicy == null) { // If yes then call the rest service without retry framework enabled. response = this.CallRestService(request); } else { // If no then call the rest service using the execute action of retry framework. this.context.IppConfiguration.RetryPolicy.ExecuteAction(() => { response = this.CallRestService(request); }); } if (request != null && request.RequestUri != null && request.RequestUri.Segments != null) { if (System.Array.IndexOf(request.RequestUri.Segments, "reports/") >= 0) { if (!response.StartsWith("{\"Report\":")) { response = "{\"Report\":" + response + "}"; } } } if (request != null && request.RequestUri != null && request.RequestUri.Segments != null) { if (System.Array.IndexOf(request.RequestUri.Segments, "taxservice/") >= 0) { //This if condition was added as Json serialization was failing for the FaultResponse bcoz of missing TaxService seriliazation tag on AnyIntuitObject in Fms.cs class if (!response.Contains("Fault")) { if (!response.StartsWith("{\"TaxService\":")) { response = "{\"TaxService\":" + response + "}"; } } } } } catch (RetryExceededException retryExceededException) { // System.Net.HttpWebRequest.Abort() was previously called.-or- The time-out // period for the request expired.-or- An error occurred while processing the request. bool isIps = false; if (this.context.ServiceType == IntuitServicesType.IPS) { isIps = true; } this.context.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Error, retryExceededException.ToString()); CoreHelper.AdvancedLogging.Log(retryExceededException.ToString()); throw; } catch (WebException webException) { // System.Net.HttpWebRequest.Abort() was previously called.-or- The time-out // period for the request expired.-or- An error occurred while processing the request. bool isIps = false; if (this.context.ServiceType == IntuitServicesType.IPS) { isIps = true; } IdsException idsException = handler.ParseResponseAndThrowException(webException, isIps); if (idsException != null) { this.context.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Error, idsException.ToString()); CoreHelper.AdvancedLogging.Log(idsException.ToString()); throw idsException; } } finally { this.context.RequestId = null; } if (this.context.ServiceType == IntuitServicesType.IPS) { // Handle errors here Utility.IntuitErrorHandler.HandleErrors(response); } else { // Check the response if there are any fault tags and throw appropriate exceptions. IdsException exception = handler.ParseErrorResponseAndPrepareException(response); if (exception != null) { throw exception; } } // Return the response. return(response); }
private AsyncCallCompletedEventArgs CreateEventArgsForRequest(IAsyncResult asyncResult) { IdsException exception = null; AsyncCallCompletedEventArgs resultArguments = null; byte[] receiveBytes = new byte[0]; bool isResponsePdf = false; // Get the async state of the web request. HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState; // Invoke the end method of the asynchronous call. HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult); string resultString = string.Empty; if (!string.IsNullOrWhiteSpace(response.ContentEncoding) && this.ResponseCompressor != null) { using (var responseStream = response.GetResponseStream()) { using (var decompressedStream = this.ResponseCompressor.Decompress(responseStream)) { if (response.ContentType.ToLower().Contains(CoreConstants.CONTENTTYPE_APPLICATIONPDF.ToLower())) { receiveBytes = ConvertResponseStreamToBytes(decompressedStream); isResponsePdf = true; } else { // Get the response stream. StreamReader reader = new StreamReader(decompressedStream); // Read the Stream resultString = reader.ReadToEnd(); // Close reader reader.Close(); } } } } else { using (Stream responseStream = response.GetResponseStream()) { //get the response in bytes if the conten-type is application/pdf if (response.ContentType.ToLower().Contains(CoreConstants.CONTENTTYPE_APPLICATIONPDF.ToLower())) { receiveBytes = ConvertResponseStreamToBytes(responseStream); isResponsePdf = true; } else { // Get the response stream. StreamReader reader = new StreamReader(responseStream); // Read the Stream resultString = reader.ReadToEnd(); // Close reader reader.Close(); } } } string response_intuit_tid_header = ""; //get intuit_tid header for (int i = 0; i < response.Headers.Count; ++i) { if (response.Headers.Keys[i] == "intuit_tid") { response_intuit_tid_header = response.Headers[i]; } } // Log the response to Disk. this.RequestLogging.LogPlatformRequests(" Response Intuit_Tid header: " + response_intuit_tid_header + ", Response Payload: " + resultString, false); // Log response to Serilog CoreHelper.AdvancedLogging.Log(" Response Intuit_Tid header: " + response_intuit_tid_header + ", Response Payload: " + resultString); //log response to logs TraceSwitch traceSwitch = new TraceSwitch("IPPTraceSwitch", "IPP Trace Switch"); this.context.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Info, (int)traceSwitch.Level > (int)TraceLevel.Info ? "Got the response from service.\n Start Dump: \n" + resultString : "Got the response from service."); CoreHelper.AdvancedLogging.Log("Got the response from service.\n Start Dump: \n" + resultString); //if response is of not type pdf do as usual if (!isResponsePdf) { // If the response string is null then there was a communication mismatch with the server. So throw exception. if (string.IsNullOrWhiteSpace(resultString)) { exception = new IdsException(Resources.CommunicationErrorMessage, new CommunicationException(Resources.ResponseStreamNullOrEmptyMessage)); resultArguments = new AsyncCallCompletedEventArgs(null, exception); } else { if (this.context.ServiceType == IntuitServicesType.IPS) { // Handle errors here resultArguments = this.HandleErrors(resultString); } else { FaultHandler handler = new FaultHandler(this.context); IdsException idsException = handler.ParseErrorResponseAndPrepareException(resultString); if (idsException != null) { this.context.IppConfiguration.Logger.CustomLogger.Log(TraceLevel.Error, idsException.ToString()); CoreHelper.AdvancedLogging.Log(idsException.ToString()); resultArguments = new AsyncCallCompletedEventArgs(null, idsException); } else { resultArguments = new AsyncCallCompletedEventArgs(resultString, null); } } } } else //if response is of type pdf act accordingly { if (receiveBytes.Length <= 0) { //response equivalent to nullorwhitespace above so act in similar way exception = new IdsException(Resources.CommunicationErrorMessage, new CommunicationException(Resources.ResponseStreamNullOrEmptyMessage)); resultArguments = new AsyncCallCompletedEventArgs(null, exception); } //faults not applicable here since we are expecting only pdf in binary resultArguments = new AsyncCallCompletedEventArgs(null, null, receiveBytes); } return(resultArguments); }