public GenericHandler(string contentType, RequestAttributes handlerAttributes, Feature format) { this.HandlerContentType = contentType; this.ContentTypeAttribute = ContentFormat.GetEndpointAttributes(contentType); this.HandlerAttributes = handlerAttributes; this.format = format; }
public StreamDeserializerDelegate GetStreamDeserializer(string contentType) { StreamDeserializerDelegate streamReader; var realContentType = ContentFormat.GetRealContentType(contentType); if (this.ContentTypeDeserializers.TryGetValue(realContentType, out streamReader)) { return(streamReader); } var contentTypeAttr = ContentFormat.GetEndpointAttributes(contentType); switch (contentTypeAttr) { case RequestAttributes.Xml: case RequestAttributes.Soap11: //"text/xml; charset=utf-8" also matches xml return(XmlSerializer.DeserializeFromStream); case RequestAttributes.Json: return(JsonDataContractSerializer.Instance.DeserializeFromStream); case RequestAttributes.Jsv: return(TypeSerializer.DeserializeFromStream); } return(null); }
public StreamDeserializerDelegate GetStreamDeserializer(string contentType) { StreamDeserializerDelegate streamReader; var realContentType = contentType.Split(';')[0].Trim(); if (this.ContentTypeDeserializers.TryGetValue(realContentType, out streamReader)) { return(streamReader); } var contentTypeAttr = ContentFormat.GetEndpointAttributes(contentType); switch (contentTypeAttr) { case RequestAttributes.Xml: return(XmlSerializer.DeserializeFromStream); case RequestAttributes.Json: return(JsonDataContractSerializer.Instance.DeserializeFromStream); case RequestAttributes.Jsv: return(TypeSerializer.DeserializeFromStream); } return(null); }
public StreamSerializerDelegate GetStreamSerializer(string contentType) { StreamSerializerDelegate responseWriter; if (this.ContentTypeSerializers.TryGetValue(contentType, out responseWriter) || this.ContentTypeSerializers.TryGetValue(ContentFormat.GetRealContentType(contentType), out responseWriter)) { return(responseWriter); } var contentTypeAttr = ContentFormat.GetEndpointAttributes(contentType); switch (contentTypeAttr) { case RequestAttributes.Xml: return((r, o, s) => XmlSerializer.SerializeToStream(o, s)); case RequestAttributes.Json: return((r, o, s) => JsonDataContractSerializer.Instance.SerializeToStream(o, s)); case RequestAttributes.Jsv: return((r, o, s) => TypeSerializer.SerializeToStream(o, s)); case RequestAttributes.Soap11: return(SoapHandler.SerializeSoap11ToStream); case RequestAttributes.Soap12: return(SoapHandler.SerializeSoap12ToStream); } return(null); }
public override object GetResponse(IHttpRequest httpReq, IHttpResponse httpRes, object request) { var requestContentType = ContentFormat.GetEndpointAttributes(httpReq.ResponseContentType); return(ExecuteService(request, HandlerAttributes | requestContentType | httpReq.GetAttributes(), httpReq, httpRes)); }
public override object GetResponse(IRequest request, object requestDto) { var requestContentType = ContentFormat.GetEndpointAttributes(request.ResponseContentType); request.RequestAttributes |= HandlerAttributes | requestContentType; return(ExecuteService(requestDto, request)); }
public string SerializeToString(IRequest requestContext, object response) { var contentType = requestContext.ResponseContentType; StreamSerializerDelegate responseStreamWriter; if (this.ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) || this.ContentTypeSerializers.TryGetValue(ContentFormat.GetRealContentType(contentType), out responseStreamWriter)) { using (var ms = new MemoryStream()) { responseStreamWriter(requestContext, response, ms); ms.Position = 0; var result = new StreamReader(ms, UTF8EncodingWithoutBom).ReadToEnd(); return(result); } } ResponseSerializerDelegate responseWriter; if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) || this.ContentTypeResponseSerializers.TryGetValue(ContentFormat.GetRealContentType(contentType), out responseWriter)) { using (var ms = new MemoryStream()) { var httpRes = new HttpResponseStreamWrapper(ms) { KeepOpen = true, //Don't let view engines close the OutputStream }; responseWriter(requestContext, response, httpRes); var bytes = ms.ToArray(); var result = bytes.FromUtf8Bytes(); httpRes.ForceClose(); //Manually close the OutputStream return(result); } } var contentTypeAttr = ContentFormat.GetEndpointAttributes(contentType); switch (contentTypeAttr) { case RequestAttributes.Xml: return(XmlSerializer.SerializeToString(response)); case RequestAttributes.Json: return(JsonDataContractSerializer.Instance.SerializeToString(response)); case RequestAttributes.Jsv: return(TypeSerializer.SerializeToString(response)); } throw new NotSupportedException("ContentType not supported: " + contentType); }
public string SerializeToString(IRequest req, object response) { var contentType = req.ResponseContentType; if (this.ContentTypeSerializers.TryGetValue(contentType, out var responseStreamWriter) || this.ContentTypeSerializers.TryGetValue(ContentFormat.GetRealContentType(contentType), out responseStreamWriter)) { using (var ms = MemoryStreamFactory.GetStream()) { responseStreamWriter(req, response, ms); ms.Position = 0; var result = new StreamReader(ms, UTF8EncodingWithoutBom).ReadToEnd(); return(result); } } if (this.ContentTypeSerializersAsync.TryGetValue(contentType, out var responseWriter) || this.ContentTypeSerializersAsync.TryGetValue(ContentFormat.GetRealContentType(contentType), out responseWriter)) { using (var ms = MemoryStreamFactory.GetStream()) { responseWriter(req, response, ms).Wait(); var bytes = ms.ToArray(); var result = bytes.FromUtf8Bytes(); return(result); } } var contentTypeAttr = ContentFormat.GetEndpointAttributes(contentType); switch (contentTypeAttr) { case RequestAttributes.Xml: return(XmlSerializer.SerializeToString(response)); case RequestAttributes.Json: return(JsonDataContractSerializer.Instance.SerializeToString(response)); case RequestAttributes.Jsv: return(TypeSerializer.SerializeToString(response)); #if !NETSTANDARD1_6 case RequestAttributes.Soap11: return(SoapHandler.SerializeSoap11ToBytes(req, response).FromUtf8Bytes()); case RequestAttributes.Soap12: return(SoapHandler.SerializeSoap12ToBytes(req, response).FromUtf8Bytes()); #endif } throw new NotSupportedException("ContentType not supported: " + contentType); }
public byte[] SerializeToBytes(IRequest req, object response) { var contentType = req.ResponseContentType; StreamSerializerDelegate responseStreamWriter; if (this.ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) || this.ContentTypeSerializers.TryGetValue(ContentFormat.GetRealContentType(contentType), out responseStreamWriter)) { using (var ms = MemoryStreamFactory.GetStream()) { responseStreamWriter(req, response, ms); ms.Position = 0; return(ms.ToArray()); } } ResponseSerializerDelegate responseWriter; if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) || this.ContentTypeResponseSerializers.TryGetValue(ContentFormat.GetRealContentType(contentType), out responseWriter)) { using (var ms = MemoryStreamFactory.GetStream()) { var httpRes = new HttpResponseStreamWrapper(ms, req); responseWriter(req, response, httpRes); ms.Position = 0; return(ms.ToArray()); } } var contentTypeAttr = ContentFormat.GetEndpointAttributes(contentType); switch (contentTypeAttr) { case RequestAttributes.Xml: return(XmlSerializer.SerializeToString(response).ToUtf8Bytes()); case RequestAttributes.Json: return(JsonDataContractSerializer.Instance.SerializeToString(response).ToUtf8Bytes()); case RequestAttributes.Jsv: return(TypeSerializer.SerializeToString(response).ToUtf8Bytes()); #if !NETSTANDARD1_6 case RequestAttributes.Soap11: return(SoapHandler.SerializeSoap11ToBytes(req, response)); case RequestAttributes.Soap12: return(SoapHandler.SerializeSoap12ToBytes(req, response)); #endif } throw new NotSupportedException("ContentType not supported: " + contentType); }
public override async Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName) { try { var restPath = httpReq.GetRoute(); if (restPath == null) throw new NotSupportedException("No RestPath found for: " + httpReq.Verb + " " + httpReq.PathInfo); httpReq.OperationName = operationName = restPath.RequestType.GetOperationName(); if (appHost.ApplyPreRequestFilters(httpReq, httpRes)) return; appHost.AssertContentType(httpReq.ResponseContentType); var request = httpReq.Dto = await CreateRequestAsync(httpReq, restPath); await appHost.ApplyRequestFiltersAsync(httpReq, httpRes, request); if (httpRes.IsClosed) return; var requestContentType = ContentFormat.GetEndpointAttributes(httpReq.ResponseContentType); httpReq.RequestAttributes |= HandlerAttributes | requestContentType; var rawResponse = await GetResponseAsync(httpReq, request); if (httpRes.IsClosed) return; await HandleResponse(httpReq, httpRes, rawResponse); } //sync with GenericHandler catch (TaskCanceledException) { httpRes.StatusCode = (int)HttpStatusCode.PartialContent; httpRes.EndRequest(); } catch (Exception ex) { if (!appHost.Config.WriteErrorsToResponse) { await appHost.ApplyResponseConvertersAsync(httpReq, ex); httpRes.EndRequest(); } else { await HandleException(httpReq, httpRes, operationName, await appHost.ApplyResponseConvertersAsync(httpReq, ex) as Exception ?? ex); } } }
public object DeserializeFromString(string contentType, Type type, string request) { var contentTypeAttr = ContentFormat.GetEndpointAttributes(contentType); switch (contentTypeAttr) { case RequestAttributes.Xml: return(XmlSerializer.DeserializeFromString(request, type)); case RequestAttributes.Json: return(JsonDataContractSerializer.Instance.DeserializeFromString(request, type)); case RequestAttributes.Jsv: return(TypeSerializer.DeserializeFromString(request, type)); default: throw new NotSupportedException("ContentType not supported: " + contentType); } }