Search in sources :

Example 6 with MethodMeta

use of org.webpieces.router.api.routes.MethodMeta in project webpieces by deanhiller.

the class StreamProxy method openStream.

@Override
public RouterStreamRef openStream(MethodMeta meta, ProxyStreamHandle handle) {
    RequestContext requestCtx = meta.getCtx();
    LoadedController loadedController = meta.getLoadedController();
    Object instance = loadedController.getControllerInstance();
    Method controllerMethod = loadedController.getControllerMethod();
    Parameter[] parameters = loadedController.getParameters();
    if (parameters.length != 1)
        throw new IllegalArgumentException("Your method='" + controllerMethod + "' MUST one parameter and does not.  It needs to take a RouterStreamHandler");
    else if (!ResponseStreamHandle.class.equals(parameters[0].getType()))
        throw new IllegalArgumentException("The single parameter must be RouterStreamHandle and was not for this method='" + controllerMethod + "'");
    else if (!StreamRef.class.equals(controllerMethod.getReturnType()))
        throw new IllegalArgumentException("The return value must be a subclass of StreamRef and was not for this method='" + controllerMethod + "'");
    StreamRef streamRef = invokeStream(meta, controllerMethod, instance, requestCtx, handle);
    XFuture<StreamWriter> writer = streamRef.getWriter();
    XFuture<StreamWriter> newFuture = futureUtil.catchBlockWrap(() -> writer, (t) -> convert(loadedController, t));
    Function<CancelReason, XFuture<Void>> cancelFunc = (reason) -> streamRef.cancel(reason);
    return new RouterStreamRef("streamProxy", newFuture, cancelFunc);
}
Also used : LoadedController(org.webpieces.router.impl.loader.LoadedController) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Function(java.util.function.Function) FutureHelper(org.webpieces.util.futures.FutureHelper) ResponseStreamHandle(com.webpieces.http2.api.streaming.ResponseStreamHandle) ProxyStreamHandle(org.webpieces.router.impl.proxyout.ProxyStreamHandle) ControllerException(org.webpieces.router.api.exceptions.ControllerException) MethodMeta(org.webpieces.router.api.routes.MethodMeta) XFuture(org.webpieces.util.futures.XFuture) RequestContext(org.webpieces.ctx.api.RequestContext) StreamRef(com.webpieces.http2.api.streaming.StreamRef) Parameter(java.lang.reflect.Parameter) ServiceInvoker(org.webpieces.router.impl.routeinvoker.ServiceInvoker) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) StreamService(org.webpieces.router.api.streams.StreamService) Method(java.lang.reflect.Method) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) WebpiecesException(org.webpieces.util.exceptions.WebpiecesException) LoadedController(org.webpieces.router.impl.loader.LoadedController) XFuture(org.webpieces.util.futures.XFuture) StreamWriter(com.webpieces.http2.api.streaming.StreamWriter) Method(java.lang.reflect.Method) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) StreamRef(com.webpieces.http2.api.streaming.StreamRef) RouterStreamRef(org.webpieces.router.impl.routeinvoker.RouterStreamRef) CancelReason(com.webpieces.http2.api.dto.lowlevel.CancelReason) Parameter(java.lang.reflect.Parameter) RequestContext(org.webpieces.ctx.api.RequestContext)

Example 7 with MethodMeta

use of org.webpieces.router.api.routes.MethodMeta in project webpieces by deanhiller.

the class JacksonCatchAllFilter method printPreRequestLog.

protected void printPreRequestLog(MethodMeta meta) {
    Method method = meta.getLoadedController().getControllerMethod();
    RouterRequest request = meta.getCtx().getRequest();
    // Use this fancifully-named logger so that you can filter these out by controller method in your logging config
    final Logger preRequestLog = LoggerFactory.getLogger(getClass().getSimpleName() + "." + method.getDeclaringClass().getName() + "." + method.getName());
    String httpMethod = request.method.getCode();
    String endpoint = httpMethod + " " + request.domain + ":" + request.port + request.relativePath;
    List<String> headers = meta.getCtx().getRequest().originalRequest.getHeaders().stream().map(h -> h.getName() + ": " + h.getValue()).collect(Collectors.toList());
    String json = new String(request.body.createByteArray());
    // Print a pre-request log that describes the request
    preRequestLog.info("The following log is the original request body and its headers. If this log is spammy or " + "unnecessary you can disable it in your logging config by filtering out this logger: {}", preRequestLog.getName());
    preRequestLog.info("{}:\n\n" + "Headers: {}\n\n" + "Request Body JSON:\n{}", endpoint, headers, json);
}
Also used : Action(org.webpieces.router.api.controller.actions.Action) RouteFilter(org.webpieces.router.api.routes.RouteFilter) MimeTypeResult(org.webpieces.router.impl.compression.MimeTypes.MimeTypeResult) StatusCode(org.webpieces.http.StatusCode) Logger(org.slf4j.Logger) Service(org.webpieces.util.filters.Service) LoggerFactory(org.slf4j.LoggerFactory) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) KnownStatusCode(org.webpieces.httpparser.api.dto.KnownStatusCode) RenderContent(org.webpieces.router.api.controller.actions.RenderContent) StandardCharsets(java.nio.charset.StandardCharsets) Inject(javax.inject.Inject) RouterRequest(org.webpieces.ctx.api.RouterRequest) List(java.util.List) MethodMeta(org.webpieces.router.api.routes.MethodMeta) Matcher(java.util.regex.Matcher) XFuture(org.webpieces.util.futures.XFuture) Pattern(java.util.regex.Pattern) HttpException(org.webpieces.http.exception.HttpException) Violation(org.webpieces.http.exception.Violation) Method(java.lang.reflect.Method) BadRequestException(org.webpieces.http.exception.BadRequestException) Method(java.lang.reflect.Method) Logger(org.slf4j.Logger) RouterRequest(org.webpieces.ctx.api.RouterRequest)

Aggregations

MethodMeta (org.webpieces.router.api.routes.MethodMeta)7 Action (org.webpieces.router.api.controller.actions.Action)4 RequestContext (org.webpieces.ctx.api.RequestContext)3 LoadedController (org.webpieces.router.impl.loader.LoadedController)3 Method (java.lang.reflect.Method)2 RouteFilter (org.webpieces.router.api.routes.RouteFilter)2 StreamService (org.webpieces.router.api.streams.StreamService)2 ProxyStreamHandle (org.webpieces.router.impl.proxyout.ProxyStreamHandle)2 AbstractRouter (org.webpieces.router.impl.routers.AbstractRouter)2 XFuture (org.webpieces.util.futures.XFuture)2 Injector (com.google.inject.Injector)1 CancelReason (com.webpieces.http2.api.dto.lowlevel.CancelReason)1 ResponseStreamHandle (com.webpieces.http2.api.streaming.ResponseStreamHandle)1 StreamRef (com.webpieces.http2.api.streaming.StreamRef)1 StreamWriter (com.webpieces.http2.api.streaming.StreamWriter)1 Parameter (java.lang.reflect.Parameter)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Function (java.util.function.Function)1