/// <summary> /// Function used to detect the desired response format and return the provider error response /// </summary> /// <param name="request"></param> /// <param name="code"></param> /// <param name="_responseEnvelope"></param> /// <returns></returns> public HttpResponseMessage FormatProviderErrorResponse(HttpRequestMessage request, HttpStatusCode code, ProviderAuthenticationResponse _responseEnvelope) { HttpResponseMessage response = new HttpResponseMessage(); MediaTypeWithQualityHeaderValue responseType = null; //Check the requested response format if (request.GetRequestContext() != null && request.GetRequestContext().Url != null && request.GetRequestContext().Url.Request != null && request.GetRequestContext().Url.Request.Headers != null) responseType = request.GetRequestContext().Url.Request.Headers.Accept.LastOrDefault(); if (responseType != null && responseType.ToString().ToLower().Equals("application/xml")) response.Content = new ObjectContent<ProviderAuthenticationResponse>(_responseEnvelope, new XmlMediaTypeFormatter()); else response.Content = new ObjectContent<ProviderAuthenticationResponse>(_responseEnvelope, new JsonMediaTypeFormatter()); return response; }
protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { LoggingUtility log = LoggerFactory.GetLogger(); ErrorUtil errorUtil = new ErrorUtil(); LogObject logObject = new LogObject(); var tsc = new TaskCompletionSource<HttpResponseMessage>(); string _requestId = request.Properties["requestId"].ToString(); ProviderAuthenticationResponse _responseEnvelope = new ProviderAuthenticationResponse(_requestId); //Initialize the error to 0 to be able to check later if there is any error in the request int Error = 0; //Get the authentication credentials AuthenticationObject authenticationObject = new CommonMethods().GetAuthenticationHeader(request); //If header does not contain the required credentials then add an error to the response envelope else if (authenticationObject == null || authenticationObject.AuthenticationType == null || authenticationObject.ApiUser == null || authenticationObject.SharedSecret == null) { Error = ErrorKey.ERR_PROVIDER_AUTHENTICATION_FAILED; authenticationObject = new AuthenticationObject(); } //Authenticate with pmp else { Error = PmpAuth(authenticationObject, _requestId); } //Error different then 0, send back the request with an error message if (Error != 0) { Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters.Add("ApiUser", authenticationObject.ApiUser); //Add the error in the response envelope //Edit for R184 _responseEnvelope.Data.Errors.Add(new ErrorObject(Error, parameters)); //R185 Modification //_responseEnvelope.Data.Errors.Add(new ErrorObject(Error, parameters)); //Log the error //Edit for R184 logObject = new CommonMethods().Authentication_ToLogObject(_requestId, authenticationObject.ApiUser, OperationType, OperationName, authenticationObject, _responseEnvelope.Data.Errors); //R185 Modification //logObject = new Methods().Authentication_ToLogObject(_requestId, authenticationObject.ApiUser, OperationType, OperationName, authenticationObject, _responseEnvelope.Data.Errors); log.InfoJson(logObject); var response = new MethodsApi().FormatProviderErrorResponse(request, HttpStatusCode.OK, _responseEnvelope); //Return back the results tsc.SetResult(response); return tsc.Task; } //Authentication succeeded...continue with the request else { //Authentication and authorization were successful, log the request and continue processing //Edit for R184 logObject = new CommonMethods().Authentication_ToLogObject(_requestId, authenticationObject.ApiUser, OperationType, OperationName, authenticationObject, _responseEnvelope.Data.Errors); //R185 Modification //logObject = new Methods().Authentication_ToLogObject(_requestId, authenticationObject.ApiUser, OperationType, OperationName, authenticationObject, _responseEnvelope.Data.Errors); log.InfoJson(logObject); // This piece of code has been added to unit test this handler // In case of success we are returning an empty ProviderAuthenticationResponse if (xunit) { tsc.SetResult(new MethodsApi().FormatProviderErrorResponse(request, HttpStatusCode.OK, _responseEnvelope)); return tsc.Task; } var providerResponse = base.SendAsync(request, cancellationToken); //Check if request has missing action name (e.g. does not specify the action for liveoffers) if (providerResponse.Result.StatusCode == HttpStatusCode.InternalServerError) { _responseEnvelope.Data.Errors.Add(new ErrorObject(ErrorKey.ERR_PROVIDER_MISSING_ACTION)); var response = new MethodsApi().FormatProviderErrorResponse(request, HttpStatusCode.OK, _responseEnvelope); //Log the error Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters.Add("URL", providerResponse.Result.RequestMessage.RequestUri.AbsolutePath); log.InfoJson(new Methods().Error_ToLogObject(_requestId, authenticationObject.ApiUser, OperationType, operationName.ApiCall.ToString(), parameters, _responseEnvelope.Data.Errors)); tsc.SetResult(response); return tsc.Task; } return providerResponse; } }