Search in sources :

Example 1 with ResponseHandler

use of com.yahoo.jdisc.handler.ResponseHandler in project vespa by vespa-engine.

the class StateHandlerTest method requestAsString.

private String requestAsString(String requestUri) throws Exception {
    final BufferedContentChannel content = new BufferedContentChannel();
    Response response = driver.dispatchRequest(requestUri, new ResponseHandler() {

        @Override
        public ContentChannel handleResponse(Response response) {
            return content;
        }
    }).get(60, TimeUnit.SECONDS);
    assertNotNull(response);
    assertEquals(Response.Status.OK, response.getStatus());
    StringBuilder str = new StringBuilder();
    Reader in = new InputStreamReader(content.toStream(), StandardCharsets.UTF_8);
    for (int c; (c = in.read()) != -1; ) {
        str.append((char) c);
    }
    return str.toString();
}
Also used : Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedContentChannel(com.yahoo.jdisc.handler.BufferedContentChannel)

Example 2 with ResponseHandler

use of com.yahoo.jdisc.handler.ResponseHandler in project vespa by vespa-engine.

the class ProxyRequestHandler method handleRequest.

@Override
public ContentChannel handleRequest(Request request, ResponseHandler responseHandler) {
    try (final ResourceReference requestReference = request.refer()) {
        ContentChannel contentChannel;
        final ResponseHandler proxyResponseHandler = new ProxyResponseHandler(request, new NullContentResponseHandler(responseHandler));
        try {
            contentChannel = delegate.handleRequest(request, proxyResponseHandler);
            Objects.requireNonNull(contentChannel, "contentChannel");
        } catch (Throwable t) {
            try {
                proxyResponseHandler.handleResponse(new Response(Response.Status.INTERNAL_SERVER_ERROR, t)).close(IGNORED_COMPLETION);
            } catch (Throwable ignored) {
            // empty
            }
            throw t;
        }
        return new ProxyContentChannel(request, contentChannel);
    }
}
Also used : ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) ContentChannel(com.yahoo.jdisc.handler.ContentChannel)

Example 3 with ResponseHandler

use of com.yahoo.jdisc.handler.ResponseHandler in project vespa by vespa-engine.

the class ServerProviderConformanceTest method testRequestExceptionAfterResponseWriteWithAsyncHandleResponse.

private <T extends ServerProvider, U, V> void testRequestExceptionAfterResponseWriteWithAsyncHandleResponse(final Adapter<T, U, V> adapter, final Module... config) throws Throwable {
    runTest(adapter, Modules.combine(config), RequestType.WITHOUT_CONTENT, new TestRequestHandler() {

        @Override
        public ContentChannel handle(final Request request, final ResponseHandler handler) {
            callInOtherThread(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    final ContentChannel out = handler.handleResponse(new Response(Response.Status.OK));
                    writeResponse(out);
                    closeResponse(out);
                    return null;
                }
            });
            responseWritten.await();
            throw new ConformanceException();
        }
    });
}
Also used : Response(com.yahoo.jdisc.Response) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Request(com.yahoo.jdisc.Request) Callable(java.util.concurrent.Callable)

Example 4 with ResponseHandler

use of com.yahoo.jdisc.handler.ResponseHandler in project vespa by vespa-engine.

the class JDiscFilterForServletTest method bindings.

private Module bindings(BindingRepository<RequestFilter> requestFilters, BindingRepository<ResponseFilter> responseFilters) {
    return Modules.combine(new AbstractModule() {

        @Override
        protected void configure() {
            bind(FilterBindings.class).toInstance(new FilterBindings(requestFilters, responseFilters));
            bind(FilterInvoker.class).toInstance(new FilterInvoker() {

                @Override
                public HttpServletRequest invokeRequestFilterChain(RequestFilter requestFilter, URI uri, HttpServletRequest httpRequest, ResponseHandler responseHandler) {
                    TestRequestFilter filter = (TestRequestFilter) requestFilter;
                    filter.runAsSecurityFilter(httpRequest, responseHandler);
                    return httpRequest;
                }

                @Override
                public void invokeResponseFilterChain(ResponseFilter responseFilter, URI uri, HttpServletRequest request, HttpServletResponse response) {
                    TestResponseFilter filter = (TestResponseFilter) responseFilter;
                    filter.runAsSecurityFilter(request, response);
                }
            });
        }
    }, guiceModule());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) FilterInvoker(com.yahoo.jdisc.http.server.jetty.FilterInvoker) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterBindings(com.yahoo.jdisc.http.server.FilterBindings) URI(java.net.URI) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter) AbstractModule(com.google.inject.AbstractModule)

Example 5 with ResponseHandler

use of com.yahoo.jdisc.handler.ResponseHandler in project vespa by vespa-engine.

the class MbusClient method handleReply.

@Override
public void handleReply(final Reply reply) {
    reply.getTrace().trace(6, "Reply received by MbusClient.");
    final ResponseHandler handler = (ResponseHandler) reply.getContext();
    // restore user context
    reply.popHandler();
    try {
        handler.handleResponse(new MbusResponse(StatusCodes.fromMbusReply(reply), reply)).close(IgnoredCompletionHandler.INSTANCE);
    } catch (final Exception e) {
        log.log(LogLevel.WARNING, "Ignoring exception thrown by ResponseHandler.", e);
    }
}
Also used : ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) RequestDeniedException(com.yahoo.jdisc.handler.RequestDeniedException)

Aggregations

ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)15 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)7 RequestFilter (com.yahoo.jdisc.http.filter.RequestFilter)7 Response (com.yahoo.jdisc.Response)6 HttpRequest (com.yahoo.jdisc.http.HttpRequest)6 Test (org.testng.annotations.Test)4 Request (com.yahoo.jdisc.Request)3 Callable (java.util.concurrent.Callable)3 RequestDeniedException (com.yahoo.jdisc.handler.RequestDeniedException)2 HttpResponse (com.yahoo.jdisc.http.HttpResponse)2 ResponseFilter (com.yahoo.jdisc.http.filter.ResponseFilter)2 AbstractModule (com.google.inject.AbstractModule)1 AbstractRequestHandler (com.yahoo.jdisc.handler.AbstractRequestHandler)1 BindingNotFoundException (com.yahoo.jdisc.handler.BindingNotFoundException)1 BufferedContentChannel (com.yahoo.jdisc.handler.BufferedContentChannel)1 RequestHandler (com.yahoo.jdisc.handler.RequestHandler)1 FilterBindings (com.yahoo.jdisc.http.server.FilterBindings)1 FilterInvoker (com.yahoo.jdisc.http.server.jetty.FilterInvoker)1 TestDriver (com.yahoo.jdisc.test.TestDriver)1 IOException (java.io.IOException)1