internal async Task <IRestResponse> HandleRequest(RestServerRequest req) { if (!req.HttpServerRequest.IsComplete || req.HttpServerRequest.Method == HttpMethod.Unsupported) { return(_responseFactory.CreateBadRequest()); } var restMethods = _restMethodCollection.Where(r => r.Match(req.HttpServerRequest.Uri)); if (!restMethods.Any()) { return(_responseFactory.CreateBadRequest()); } var restMethod = restMethods.SingleOrDefault(r => r.Verb == req.HttpServerRequest.Method); if (restMethod == null) { return(new MethodNotAllowedResponse(restMethods.Select(r => r.Verb))); } var restMethodExecutor = _methodExecuteFactory.Create(restMethod); try { return(await restMethodExecutor.ExecuteMethodAsync(restMethod, req)); } catch { return(_responseFactory.CreateBadRequest()); } }
private HttpServerResponse GetPostResponse(PostResponse response, RestServerRequest restReq) { var serverResponse = GetDefaultContentResponse(response, restReq); if (response.Status == PostResponse.ResponseStatus.Created) { serverResponse.Location = new Uri(response.LocationRedirect, UriKind.RelativeOrAbsolute); } return(serverResponse); }
private HttpServerResponse GetPostResponse(PostResponse response, RestServerRequest restReq) { var serverResponse = GetDefaultContentResponse(response, restReq); var locationRedirect = response.LocationRedirect; if (response.Status == PostResponse.ResponseStatus.Created && !string.IsNullOrWhiteSpace(locationRedirect)) { serverResponse.Location = new Uri(locationRedirect, UriKind.RelativeOrAbsolute); } return(serverResponse); }
private HttpServerResponse GetDefaultContentResponse(IContentRestResponse response, RestServerRequest restReq) { var defaultResponse = GetDefaultResponse(response); if (response.ContentData != null) { defaultResponse.ContentType = GetMediaTypeAsString(restReq.AcceptMediaType); defaultResponse.ContentCharset = restReq.AcceptCharset; defaultResponse.Content = _contentSerializer.ToAcceptContent(response.ContentData, restReq); } return defaultResponse; }
internal HttpServerResponse ConvertToHttpResponse(IRestResponse restResponse, RestServerRequest restServerRequest) { var methodNotAllowedResponse = restResponse as MethodNotAllowedResponse; if (methodNotAllowedResponse != null) return GetMethodNotAllowedResponse(methodNotAllowedResponse); var postResponse = restResponse as PostResponse; if (postResponse != null) return GetPostResponse(postResponse, restServerRequest); var response = restResponse as IContentRestResponse; if (response != null) return GetDefaultContentResponse(response, restServerRequest); return GetDefaultResponse(restResponse); }
protected override object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri) { var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType); object[] parameters; try { parameters = info.GetParametersFromUri(requestUri).ToArray(); } catch (FormatException) { return _responseFactory.CreateBadRequest(); } return info.MethodInfo.Invoke( instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()), parameters); }
internal byte[] ToAcceptContent(object contentObject, RestServerRequest req) { if (contentObject == null) { return new byte[0]; } if (req.AcceptMediaType == MediaType.JSON) { return req.AcceptEncoding.GetBytes(JsonConvert.SerializeObject(contentObject)); } else if (req.AcceptMediaType == MediaType.XML) { return req.AcceptEncoding.GetBytes(XmlSerializeObject(contentObject)); } return new byte[0]; }
internal byte[] ToAcceptContent(object contentObject, RestServerRequest req) { if (contentObject == null) { return(new byte[0]); } if (req.AcceptMediaType == MediaType.JSON) { return(req.AcceptEncoding.GetBytes(JsonConvert.SerializeObject(contentObject))); } else if (req.AcceptMediaType == MediaType.XML) { return(req.AcceptEncoding.GetBytes(XmlSerializeObject(contentObject))); } return(new byte[0]); }
private object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request) { var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType); object[] parameters = null; try { parameters = info.GetParametersFromUri(request.HttpServerRequest.Uri).ToArray(); } catch (FormatException) { return(_responseFactory.CreateBadRequest()); } return(info.MethodInfo.Invoke( instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()), parameters)); }
internal async Task <IRestResponse> HandleRequestAsync(RestServerRequest req) { if (!req.HttpServerRequest.IsComplete || req.HttpServerRequest.Method == HttpMethod.Unsupported) { return(_responseFactory.CreateBadRequest()); } ParsedUri parsedUri; var incomingUriAsString = req.HttpServerRequest.Uri.ToRelativeString(); if (!_uriParser.TryParse(incomingUriAsString, out parsedUri)) { throw new Exception($"Could not parse uri: {incomingUriAsString}"); } var restMethods = _restMethodCollection.Where(r => r.Match(parsedUri)).ToList(); if (!restMethods.Any()) { return(_responseFactory.CreateBadRequest()); } var restMethod = restMethods.FirstOrDefault(r => r.Verb == req.HttpServerRequest.Method); if (restMethod == null) { return(new MethodNotAllowedResponse(restMethods.Select(r => r.Verb))); } var restMethodExecutor = _methodExecuteFactory.Create(restMethod); try { var task = restMethodExecutor.ExecuteMethodAsync(restMethod, req, parsedUri); return(await task); } catch (Exception ex) { return(_responseFactory.CreateInternalServerErrorResponse(ex)); } }
private object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request) { var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType); object contentObj = null; try { if (request.HttpServerRequest.Content != null) { contentObj = _contentSerializer.FromContent( request.ContentEncoding.GetString(request.HttpServerRequest.Content), request.ContentMediaType, info.ContentParameterType); } } catch (JsonReaderException) { return(_responseFactory.CreateBadRequest()); } catch (InvalidOperationException) { return(_responseFactory.CreateBadRequest()); } object[] parameters = null; try { parameters = info.GetParametersFromUri(request.HttpServerRequest.Uri).Concat(new[] { contentObj }).ToArray(); } catch (FormatException) { return(_responseFactory.CreateBadRequest()); } return(info.MethodInfo.Invoke( instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()), parameters)); }
protected override object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri) { var instantiator = InstanceCreatorCache.Default.GetCreator(info.MethodInfo.DeclaringType); object contentObj = null; try { if (request.HttpServerRequest.Content != null) { contentObj = _contentSerializer.FromContent( request.ContentEncoding.GetString(request.HttpServerRequest.Content), request.ContentMediaType, info.ContentParameterType); } } catch (JsonReaderException) { return _responseFactory.CreateBadRequest(); } catch (InvalidOperationException) { return _responseFactory.CreateBadRequest(); } object[] parameters = null; try { parameters = info.GetParametersFromUri(requestUri).Concat(new[] { contentObj }).ToArray(); } catch (FormatException) { return _responseFactory.CreateBadRequest(); } return info.MethodInfo.Invoke( instantiator.Create(info.MethodInfo.DeclaringType, info.ControllerConstructorArgs()), parameters); }
private HttpServerResponse GetDefaultContentResponse(IContentRestResponse response, RestServerRequest restReq) { var defaultResponse = GetDefaultResponse(response); if (response.ContentData != null) { defaultResponse.ContentType = GetMediaTypeAsString(restReq.AcceptMediaType); defaultResponse.ContentCharset = restReq.AcceptCharset; defaultResponse.Content = _contentSerializer.ToAcceptContent(response.ContentData, restReq); } return(defaultResponse); }
public async Task <IRestResponse> ExecuteMethodAsync(RestControllerMethodInfo info, RestServerRequest request) { var methodInvokeResult = ExecuteAnonymousMethod(info, request); if (!info.IsAsync) { return(await Task.Run(() => (IRestResponse)methodInvokeResult)); } return(await(dynamic) methodInvokeResult); }
internal async Task <IRestResponse> HandleRequestAsync(RestServerRequest req, IAuthorizationProvider authorizationProvider) { if (!req.HttpServerRequest.IsComplete || req.HttpServerRequest.Method == HttpMethod.Unsupported) { return(_responseFactory.CreateBadRequest()); } ParsedUri parsedUri; var incomingUriAsString = req.HttpServerRequest.Uri.ToRelativeString(); if (!_uriParser.TryParse(incomingUriAsString, out parsedUri)) { throw new Exception($"Could not parse uri: {incomingUriAsString}"); } var restMethods = _restMethodCollection.Where(r => r.Match(parsedUri)).ToList(); if (!restMethods.Any()) { return(_responseFactory.CreateBadRequest()); } var restMethod = restMethods.FirstOrDefault(r => r.Verb == req.HttpServerRequest.Method); if (restMethod == null) { return(new MethodNotAllowedResponse(restMethods.Select(r => r.Verb))); } // check if authentication is required AuthorizeAttribute authAttribute = null; // first check on controller level if (restMethod.MethodInfo.DeclaringType.GetTypeInfo().IsDefined(typeof(AuthorizeAttribute))) { authAttribute = restMethod.MethodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes <AuthorizeAttribute>().Single(); } // otherwise check on method level else if (restMethod.MethodInfo.IsDefined(typeof(AuthorizeAttribute))) { authAttribute = restMethod.MethodInfo.GetCustomAttributes <AuthorizeAttribute>().Single(); } if (authAttribute != null) // need to check authentication { if (authorizationProvider == null) { _log.Error("HandleRequestAsync|AuthenticationProvider not configured"); return(_responseFactory.CreateInternalServerError()); } var authResult = authorizationProvider.Authorize(req.HttpServerRequest); if (authResult == HttpResponseStatus.Unauthorized) { return(_responseFactory.CreateWwwAuthenticate(authorizationProvider.Realm)); } } var restMethodExecutor = _methodExecuteFactory.Create(restMethod); try { return(await restMethodExecutor.ExecuteMethodAsync(restMethod, req, parsedUri)); } catch { return(_responseFactory.CreateBadRequest()); } }
internal Task <IRestResponse> HandleRequestAsync(RestServerRequest req) { return(HandleRequestAsync(req, null)); }
private HttpServerResponse GetPostResponse(PostResponse response, RestServerRequest restReq) { var serverResponse = GetDefaultContentResponse(response, restReq); var locationRedirect = response.LocationRedirect; if (response.Status == PostResponse.ResponseStatus.Created && !string.IsNullOrWhiteSpace(locationRedirect)) { serverResponse.Location = new Uri(locationRedirect, UriKind.RelativeOrAbsolute); } return serverResponse; }
internal HttpServerResponse ConvertToHttpResponse(IRestResponse restResponse, RestServerRequest restServerRequest) { var methodNotAllowedResponse = restResponse as MethodNotAllowedResponse; if (methodNotAllowedResponse != null) { return(GetMethodNotAllowedResponse(methodNotAllowedResponse)); } var postResponse = restResponse as PostResponse; if (postResponse != null) { return(GetPostResponse(postResponse, restServerRequest)); } var response = restResponse as IContentRestResponse; if (response != null) { return(GetDefaultContentResponse(response, restServerRequest)); } return(GetDefaultResponse(restResponse)); }
protected abstract object ExecuteAnonymousMethod(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri);
public async Task <IRestResponse> ExecuteMethodAsync(RestControllerMethodInfo info, RestServerRequest request, ParsedUri requestUri) { var methodInvokeResult = ExecuteAnonymousMethod(info, request, requestUri); switch (info.ReturnTypeWrapper) { case RestControllerMethodInfo.TypeWrapper.None: return(await Task.FromResult((IRestResponse)methodInvokeResult)); case RestControllerMethodInfo.TypeWrapper.AsyncOperation: return(await ConvertToTask((dynamic)methodInvokeResult)); case RestControllerMethodInfo.TypeWrapper.Task: return(await(dynamic) methodInvokeResult); } throw new Exception($"ReturnTypeWrapper of type {info.ReturnTypeWrapper} not known."); }
private static HttpServerResponse GetMethodNotAllowedResponse(MethodNotAllowedResponse methodNotAllowedResponse, RestServerRequest restReq) { var serverResponse = GetDefaultResponse(methodNotAllowedResponse); serverResponse.Allow = methodNotAllowedResponse.Allows; return(serverResponse); }