Beispiel #1
0
        public override void ProcessResponse(ref System.ServiceModel.Channels.Message request, ref System.ServiceModel.Channels.Message response)
        {
            // Don't do anything if there was no error
            Exception ex = null;

            if (!OperationContext.Current.OutgoingMessageProperties.ContainsKey(StatusCodeProperty))
            {
                return;
            }

            HttpStatusCode statusCode = (HttpStatusCode)OperationContext.Current.OutgoingMessageProperties[StatusCodeProperty];
            //object error = OperationContext.Current.OutgoingMessageProperties[ErrorObjectProperty];

            // TODO: add text message to output
            HttpResponseMessage responseMessage = request.ToHttpRequestMessage().CreateResponse(statusCode);



            //var httpMessageProperty = OperationContext.Current.IncomingMessageProperties[HttpMessageProperty.Name] as HttpMessageProperty;
            //var httpRequest = httpMessageProperty.Request as HttpRequestMessage;
            var httpRequest          = request.ToHttpRequestMessage();
            var endpoint             = OperationContext.Current.Host.Description.Endpoints.Find(OperationContext.Current.EndpointDispatcher.EndpointAddress.Uri);
            var uriMatch             = httpRequest.Properties.First(p => p.GetType() == typeof(UriTemplateMatch)) as UriTemplateMatch;
            var dispatchOperation    = OperationContext.Current.EndpointDispatcher.DispatchRuntime.Operations.Where(op => op.Name == uriMatch.Data).First();
            var operationDescription = endpoint.Contract.Operations.Find(dispatchOperation.Name);
            var httpBehavoir         = endpoint.Behaviors.Find <HttpEndpointBehavior>();
            var processors           = httpBehavoir.GetResponseProcessors(operationDescription.ToHttpOperationDescription()).ToList <Processor>();

            foreach (var processor in processors)
            {
                var mediaTypeProcessor = processor as MediaTypeProcessor;
                if (mediaTypeProcessor == null)
                {
                    continue;
                }

                if (mediaTypeProcessor.SupportedMediaTypes.Contains <string>("application/json"))
                {
                    ErrorObject errorObject = new ErrorObject()
                    {
                        ErrorCode = -1, Message = OperationContext.Current.OutgoingMessageProperties[ErrorObjectProperty].ToString(), StatusCode = Convert.ToInt32(OperationContext.Current.OutgoingMessageProperties[StatusCodeProperty])
                    };

                    if (ex != null)
                    {
                        errorObject.Ex = ex;
                    }


                    responseMessage.Content = HttpContent.Create(s => mediaTypeProcessor.WriteToStream(errorObject, s, httpRequest));
                    break;
                }
            }



            //	responseMessage.Content = HttpContent.Create( OperationContext.Current.OutgoingMessageProperties[ErrorObjectProperty].ToString() , "application/json");
            response = responseMessage.ToMessage();
        }
Beispiel #2
0
        public override void ProcessRequest(ref System.ServiceModel.Channels.Message request)
        {
            try
            {
                HttpRequestMessage httpRequestMessage = request.ToHttpRequestMessage();

                if (CheckSession)
                {
                    UriTemplateMatch uriTemplateMatch = (UriTemplateMatch)httpRequestMessage.Properties.Where(prop => prop.GetType() == typeof(UriTemplateMatch)).First();

                    if (uriTemplateMatch.Data.ToString().ToUpper() != LogIn.ToUpper())
                    {
                        if (httpRequestMessage.Headers.ContainsKey(SessionHeader))
                        {
                            int    userCode;
                            string session = httpRequestMessage.Headers[SessionHeader];
                            if (String.IsNullOrEmpty(session) || !IsSessionValid(session, out userCode))
                            {
                                ErrorMessageInterceptor.ThrowError(HttpStatusCode.Forbidden, "Invalid session information.");
                                throw new InvalidOperationException();
                            }
                            else
                            {
                                OperationContext.Current.IncomingMessageProperties.Add("edge-user-id", userCode);
                            }
                        }
                        else
                        {
                            ErrorMessageInterceptor.ThrowError(HttpStatusCode.Forbidden, "Invalid session information.");
                            throw new InvalidOperationException();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message != "Invalid session information.")
                {
                    ErrorMessageInterceptor.ThrowError(HttpStatusCode.Forbidden, ex.Message);
                }
                else
                {
                    ErrorMessageInterceptor.ThrowError(HttpStatusCode.Forbidden, ex.Message);
                }
                throw;
            }
        }
Beispiel #3
0
        private static Message ConfigureRequestMessage(Message message)
        {
            if (message == null)
            {
                return(null);
            }

            HttpRequestMessageProperty requestProperty = message.GetHttpRequestMessageProperty();

            if (requestProperty == null)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              SR.RequestMissingHttpRequestMessageProperty,
                              HttpRequestMessageProperty.Name,
                              typeof(HttpRequestMessageProperty).FullName));
            }

            Uri uri = message.Headers.To;

            if (uri == null)
            {
                throw new InvalidOperationException(SR.RequestMissingToHeader);
            }

            HttpRequestMessage httpRequestMessage = message.ToHttpRequestMessage();

            if (httpRequestMessage == null)
            {
                httpRequestMessage         = new HttpRequestMessage();
                httpRequestMessage.Content = new StringContent(String.Empty);
                httpRequestMessage.Content.Headers.ContentLength = 0;
                message.Close();
                message = httpRequestMessage.ToMessage();
            }
            else
            {
                message.Headers.Clear();
                message.Properties.Clear();
                httpRequestMessage.Headers.Clear();
                httpRequestMessage.GetProperties().Clear();
            }

            message.Headers.To = uri;

            httpRequestMessage.RequestUri = uri;
            httpRequestMessage.Method     = new HttpMethod(requestProperty.Method);

            foreach (var headerName in requestProperty.Headers.AllKeys)
            {
                if (headerName.StartsWith("content-", StringComparison.OrdinalIgnoreCase) ||
                    headerName.Equals("Allow", StringComparison.OrdinalIgnoreCase) ||
                    headerName.Equals("Expires") ||
                    headerName.Equals("Expires", StringComparison.OrdinalIgnoreCase))
                {
                    httpRequestMessage.Content.Headers.Remove(headerName);
                    httpRequestMessage.Content.Headers.Add(headerName, requestProperty.Headers[headerName]);
                    continue;
                }

                httpRequestMessage.Headers.Remove(headerName);
                httpRequestMessage.Headers.Add(headerName, requestProperty.Headers[headerName]);
            }

            return(message);
        }