private Ret <Entity> ExecuteMethod(HttpContext httpContext, MethodInfo method) { try { var target = injector.CreateInstance(PaperType); try { var ret = CreateParameters(httpContext, method); if (ret.IsFault()) { return(Ret.Throw(ret)); } object[] parameters = ret.Data; method.Invoke(target, parameters); } finally { (target as IDisposable)?.Dispose(); } return(Ret.Ok()); } catch (Exception ex) { return(Ret.Fail(ex)); } }
public async Task <Ret <byte[]> > TransferBytesAsync( UriString route, string method = MethodNames.Get, byte[] requestData = null ) { try { Ret ret = null; await DoRequestAsync(route, method, "application/octet-stream", requestData, async webResponse => { if (webResponse.StatusCode == HttpStatusCode.OK) { using (var stream = webResponse.GetResponseStream()) using (var memory = new MemoryStream()) { await stream.CopyToAsync(memory); ret = Ret.Ok(memory.ToArray(), webResponse.StatusCode); } } else { ret = Ret.Fail(webResponse.StatusCode); } }); return(ret); } catch (Exception ex) { ex.Trace(); return(ex); } }
public async Task <Ret <Result> > TransferAsync( UriString route, string method = MethodNames.Get, Entity requestData = null) { try { Ret ret = null; await DoRequestAsync(route, method, null, requestData, webResponse => { var isRedirect = ((int)webResponse.StatusCode / 100) == 3; if (isRedirect) { var location = webResponse.Headers[HttpResponseHeader.Location]; var uri = new Uri(location, UriKind.RelativeOrAbsolute); ret = Ret.Ok(uri, webResponse.StatusCode); } else { using (var stream = webResponse.GetResponseStream()) { var entity = EntityParser.ParseEntity(stream); ret = Ret.Ok(entity, webResponse.StatusCode); } } }); return(ret); } catch (Exception ex) { ex.Trace(); return(ex); } }
public async Task <Ret> CheckConnectivityAsync() { try { var uri = new UriString(Settings.Endpoint).Combine(ApiInfo.Prefix, "/Status"); var client = WebRequest.CreateHttp(uri.ToString()); using (var response = await client.GetResponseAsync()) using (var stream = response.GetResponseStream()) { try { // Checando se a resposta é uma entidade (Entity) válida. EntityParser.ParseEntity(stream); // TODO: Futuramente a entidade pode ser investigada para // determinar se é uma entidade compatível com a esperada // para a consulta de status. return(Ret.Ok()); } catch (Exception ex) { ex.Trace(); return(Ret.Ok(HttpStatusCode.UnsupportedMediaType)); } } } catch (Exception ex) { return(ex); } }
public static Ret <Entity> Create(Route route, HttpStatusCode status, string message, Exception exception) { var causes = EnumerateCauses(message, exception).Distinct(); var description = causes.Any() ? string.Join(Environment.NewLine, causes) : null; var entity = new Entity(); entity.Class = ClassNames.Status; if ((int)status >= 400) { entity.Class.Add(ClassNames.Error); } entity.Properties = new PropertyCollection(); entity.Properties.Add("Code", (int)status); entity.Properties.Add("Status", status.ToString()); if (description != null) { entity.Properties.Add("Description", description); } if (exception != null) { entity.Properties.Add("StackTrace", exception.GetStackTrace()); } entity.Links = new LinkCollection(); entity.Links.Add(new Link { Href = route.ToString(), Rel = RelNames.Self }); return(Ret.Ok(entity, status)); }
private async Task SendProxyAsync(HttpContext httpContext) { var req = httpContext.Request; var query = httpContext.Request.Query; var path = (string)query["path"]; if (path != null) { Proxy proxy = registry.FindExact(path); if (proxy == null) { var ret = HttpEntity.Create( route: req.GetRequestUri(), status: HttpStatusCode.NotFound, message: $"Proxy não registrado: {path}" ); await SendStatusAsync(httpContext, ret); } else { var href = req.GetRequestUri(); var allProxiesHref = new Route(href).UnsetArgs("path"); var entity = new Entity(); entity.Title = $"Configuração de proxy: {proxy.Path}"; entity.Class = KnownClasses.Single; entity.Links = new LinkCollection(); entity.Links.AddSelf(href); entity.Links.Add("link", "Todas as configurações de proxy", allProxiesHref); entity.Properties = new PropertyCollection(); entity.Properties.AddFromGraph(proxy); entity.Properties.AddDataHeadersFromGraph <Proxy>(); await SendStatusAsync(httpContext, Ret.Ok(entity)); } } else { var entity = new Entity(); entity.Class = KnownClasses.Rows; entity.Title = "Todas as configurações de proxy"; entity.Links = new LinkCollection(); entity.Links.AddSelf(req.GetRequestUri()); entity.Entities = new EntityCollection(); entity.Properties = new PropertyCollection(); entity.Properties.AddRowsHeadersFromGraph <Proxy>(); foreach (var knownPath in registry.Paths) { var proxy = registry.FindExact(knownPath); var href = new Route(req.GetRequestUri()) .SetArg("path", proxy.Path) .ToString(); var row = new Entity(); row.Title = $"Configuração de proxy: {proxy.Path}"; row.Class = KnownClasses.Row; row.Rel = KnownRelations.Row; row.Links = new LinkCollection(); row.Links.AddSelf(href); row.Properties = new PropertyCollection(); row.Properties.AddFromGraph(proxy); entity.Entities.Add(row); } await SendStatusAsync(httpContext, Ret.Ok(entity)); } }