Search in sources :

Example 11 with RequestHandler

use of act.handler.RequestHandler in project actframework by actframework.

the class NetworkHandler method handle.

public void handle(final ActionContext ctx, final NetworkDispatcher dispatcher) {
    if (isDestroyed()) {
        return;
    }
    Exception refreshError = null;
    if (Act.isDev()) {
        try {
            boolean updated = app.checkUpdates(false);
            if (updated && !app.hasBlockIssue()) {
                app.jobManager().on(SysEventId.POST_START, new Runnable() {

                    @Override
                    public void run() {
                        handle(ctx, dispatcher);
                    }
                }, true);
                dispatcher.keep();
                return;
            }
        } catch (Exception e) {
            refreshError = e;
        }
    }
    final H.Request req = ctx.req();
    String url = req.url();
    H.Method method = req.method();
    url = contentSuffixProcessor.apply(req, url);
    try {
        url = urlContextProcessor.apply(req, url);
    } catch (NotFound notFound) {
        ctx.handler(AlwaysNotFound.INSTANCE);
        ctx.saveLocal();
        AlwaysNotFound.INSTANCE.apply(ctx);
        return;
    }
    Timer timer = metric.startTimer(MetricInfo.ROUTING);
    final RequestHandler requestHandler = router().getInvoker(method, url, ctx);
    ctx.handler(requestHandler);
    timer.stop();
    boolean resourceGetter = requestHandler instanceof ResourceGetter || requestHandler instanceof FileGetter;
    if (null != refreshError && !resourceGetter) {
        ctx.saveLocal();
        handleException(refreshError, ctx, "Error refreshing app");
        ActionContext.clearCurrent();
        return;
    }
    NetworkJob job = new NetworkJob() {

        @Override
        public void run() {
            Timer timer = Metric.NULL_METRIC.startTimer("null");
            if (metric != Metric.NULL_METRIC) {
                String key = S.concat(MetricInfo.HTTP_HANDLER, ":", requestHandler.toString());
                timer = metric.startTimer(key);
            }
            ctx.saveLocal();
            EventBus eventBus = app.eventBus();
            try {
                eventBus.emit(new PreHandle(ctx));
                requestHandler.handle(ctx);
            } catch (Result r) {
                if (isError(r)) {
                    ctx.handler(FastRequestHandler.DUMB);
                }
                try {
                    r = RequestHandlerProxy.GLOBAL_AFTER_INTERCEPTOR.apply(r, ctx);
                } catch (Exception e) {
                    logger.error(e, "Error calling global after interceptor");
                    r = ActErrorResult.of(e);
                }
                if (null == ctx.handler() || isError(r)) {
                    ctx.handler(FastRequestHandler.DUMB);
                }
                H.Format fmt = req.accept();
                if (H.Format.UNKNOWN == fmt) {
                    fmt = req.contentType();
                }
                ctx.prepareRespForWrite().addHeaderIfNotAdded(H.Header.Names.CONTENT_TYPE, fmt.contentType());
                r.apply(req, ctx.prepareRespForWrite());
            } catch (Exception e) {
                handleException(e, ctx, "Error handling network request");
            } finally {
                // we don't destroy ctx here in case it's been passed to
                // another thread
                eventBus.emit(new PostHandle(ctx));
                ActionContext.clearCurrent();
                timer.stop();
            }
        }
    };
    if (method.unsafe() || !requestHandler.express(ctx)) {
        dispatcher.dispatch(job);
    } else {
        job.run();
    }
}
Also used : H(org.osgl.http.H) PostHandle(act.handler.event.PostHandle) EventBus(act.event.EventBus) ResourceGetter(act.handler.builtin.ResourceGetter) NotAppliedException(org.osgl.exception.NotAppliedException) PreHandle(act.handler.event.PreHandle) FileGetter(act.handler.builtin.FileGetter) Result(org.osgl.mvc.result.Result) ErrorResult(org.osgl.mvc.result.ErrorResult) ActErrorResult(act.view.ActErrorResult) Timer(act.metric.Timer) FastRequestHandler(act.handler.builtin.controller.FastRequestHandler) RequestHandler(act.handler.RequestHandler) AlwaysNotFound(act.handler.builtin.AlwaysNotFound) NotFound(org.osgl.mvc.result.NotFound)

Example 12 with RequestHandler

use of act.handler.RequestHandler in project actframework by actframework.

the class RouteTableRouterBuilderTest method payloadContainsBlank.

@Test
public void payloadContainsBlank() {
    addRouteMap("GET /magic_words echo: Hello world!");
    RequestHandler h = router.getInvoker(GET, "/magic_words", ctx);
    yes(h instanceof Echo);
    eq("Hello world!", ((Echo) h).readContent());
}
Also used : RequestHandler(act.handler.RequestHandler) Echo(act.handler.builtin.Echo) Test(org.junit.Test)

Example 13 with RequestHandler

use of act.handler.RequestHandler in project actframework by actframework.

the class RouterBenchmark method osgl.

void osgl(H.Method method, String url, Object... args) {
    H.Request req = new MockRequest(config, method, url);
    ctx = ActionContext.create(app, req, new MockResponse());
    if (args.length > 0) {
        url = S.fmt(url, args);
    }
    RequestHandler handler = router.getInvoker(method, url, ctx);
    if (AlwaysBadRequest.INSTANCE == handler || AlwaysNotFound.INSTANCE == handler) {
        throw NotFound.get();
    }
}
Also used : RequestHandler(act.handler.RequestHandler) H(org.osgl.http.H)

Example 14 with RequestHandler

use of act.handler.RequestHandler in project actframework by actframework.

the class RouterTest method searchPathEndsWithIgnoreNotation.

@Test
public void searchPathEndsWithIgnoreNotation() {
    router.addMapping(GET, "/foo/bar/...", controller);
    router.addMapping(GET, "/svc/{id}/...", controller);
    H.Request req = Mockito.mock(H.Request.class);
    when(ctx.req()).thenReturn(req);
    when(req.path()).thenReturn("/foo/bar/something-should-be-ignored");
    RequestHandler handler = router.getInvoker(GET, "/foo/bar/something-should-be-ignored", ctx);
    same(controller, handler);
    when(req.path()).thenReturn("/svc/123/another-thing-should-be-ignored/and-whatever/else");
    router.getInvoker(GET, "/svc/123/another-thing-should-be-ignored/and-whatever/else", ctx).handle(ctx);
    verify(ctx).urlPathParam("id", "123");
}
Also used : RequestHandler(act.handler.RequestHandler) H(org.osgl.http.H) Test(org.junit.Test)

Example 15 with RequestHandler

use of act.handler.RequestHandler in project actframework by actframework.

the class RouterTest method _regExtTests.

private void _regExtTests(String pattern) {
    router.addMapping(GET, "/int/" + pattern, controller);
    H.Request req = Mockito.mock(H.Request.class);
    when(ctx.req()).thenReturn(req);
    when(req.path()).thenReturn("/int/33");
    RequestHandler handler = router.getInvoker(GET, "/int/33", ctx);
    same(controller, handler);
    verify(ctx).urlPathParam("n", "33");
}
Also used : RequestHandler(act.handler.RequestHandler) H(org.osgl.http.H)

Aggregations

RequestHandler (act.handler.RequestHandler)16 Test (org.junit.Test)7 H (org.osgl.http.H)7 FileGetter (act.handler.builtin.FileGetter)5 File (java.io.File)4 FastRequestHandler (act.handler.builtin.controller.FastRequestHandler)2 PreHandle (act.handler.event.PreHandle)2 EventBus (act.event.EventBus)1 AlwaysNotFound (act.handler.builtin.AlwaysNotFound)1 Echo (act.handler.builtin.Echo)1 ResourceGetter (act.handler.builtin.ResourceGetter)1 PostHandle (act.handler.event.PostHandle)1 Timer (act.metric.Timer)1 CORS (act.security.CORS)1 ActErrorResult (act.view.ActErrorResult)1 UndertowRequest (act.xio.undertow.UndertowRequest)1 URL (java.net.URL)1 NotAppliedException (org.osgl.exception.NotAppliedException)1 ErrorResult (org.osgl.mvc.result.ErrorResult)1 NotFound (org.osgl.mvc.result.NotFound)1