Search in sources :

Example 1 with ContentChannel

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

the class SynchronousRequestResponseHandler method handleRequest.

Response handleRequest(Request request, TestDriver driver) {
    BlockingResponseHandler responseHandler = new BlockingResponseHandler();
    ContentChannel inputRequestChannel = connectRequest(request, driver, responseHandler);
    writeRequestBody(request, inputRequestChannel);
    return responseHandler.getResponse();
}
Also used : ContentChannel(com.yahoo.jdisc.handler.ContentChannel)

Example 2 with ContentChannel

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

the class Request method connect.

/**
 * <p>Attempts to resolve and connect to the {@link RequestHandler} appropriate for the {@link URI} of this Request.
 * An exception is thrown if this operation fails at any point. This method is exception-safe.</p>
 *
 * @param responseHandler The handler to pass the corresponding {@link Response} to.
 * @return The {@link ContentChannel} to write the Request content to.
 * @throws NullPointerException     If the {@link ResponseHandler} is null.
 * @throws BindingNotFoundException If the corresponding call to {@link Container#resolveHandler(Request)} returns
 *                                  null.
 */
public ContentChannel connect(final ResponseHandler responseHandler) {
    try {
        Objects.requireNonNull(responseHandler, "responseHandler");
        RequestHandler requestHandler = container().resolveHandler(this);
        if (requestHandler == null) {
            throw new BindingNotFoundException(uri);
        }
        requestHandler = new ProxyRequestHandler(requestHandler);
        ContentChannel content = requestHandler.handleRequest(this, responseHandler);
        if (content == null) {
            throw new RequestDeniedException(this);
        }
        return content;
    } catch (Throwable t) {
        cancel();
        throw t;
    }
}
Also used : RequestHandler(com.yahoo.jdisc.handler.RequestHandler) RequestDeniedException(com.yahoo.jdisc.handler.RequestDeniedException) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) BindingNotFoundException(com.yahoo.jdisc.handler.BindingNotFoundException)

Example 3 with ContentChannel

use of com.yahoo.jdisc.handler.ContentChannel 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 4 with ContentChannel

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

the class ContainerShutdownTestCase method requireThatRequestContentWriteExceptionDoesNotBlockTermination.

@Test
public void requireThatRequestContentWriteExceptionDoesNotBlockTermination() {
    MyRequestHandler requestHandler = MyRequestHandler.newContentWriteExceptionWithEagerCompletion();
    Context ctx = Context.newPendingRequest(requestHandler);
    ContentChannel requestContent = ctx.request.connect(MyResponseHandler.newEagerCompletion());
    try {
        requestContent.write(ByteBuffer.allocate(69), null);
        fail();
    } catch (MyException e) {
    // ignore
    }
    requestContent.close(null);
    ctx.request.release();
    requestHandler.respond().close(null);
    assertTrue(ctx.shutdown());
    assertTrue(ctx.driver.close());
}
Also used : ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Test(org.junit.Test)

Example 5 with ContentChannel

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

the class ContainerShutdownTestCase method requireThatRequestContentCloseExceptionWithCompletionDoesNotBlockTermination.

@Test
public void requireThatRequestContentCloseExceptionWithCompletionDoesNotBlockTermination() {
    MyRequestHandler requestHandler = MyRequestHandler.newContentCloseException();
    Context ctx = Context.newPendingRequest(requestHandler);
    ContentChannel requestContent = ctx.request.connect(MyResponseHandler.newEagerCompletion());
    try {
        requestContent.close(MyCompletion.newInstance());
        fail();
    } catch (MyException e) {
    // ignore
    }
    ctx.request.release();
    requestHandler.respond().close(null);
    assertTrue(ctx.shutdown());
    assertTrue(ctx.driver.close());
}
Also used : ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Test(org.junit.Test)

Aggregations

ContentChannel (com.yahoo.jdisc.handler.ContentChannel)48 Test (org.junit.Test)31 Request (com.yahoo.jdisc.Request)10 Response (com.yahoo.jdisc.Response)10 TestDriver (com.yahoo.jdisc.test.TestDriver)10 ByteBuffer (java.nio.ByteBuffer)7 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)6 HttpRequest (com.yahoo.jdisc.http.HttpRequest)4 ResourceReference (com.yahoo.jdisc.ResourceReference)3 RequestHandler (com.yahoo.jdisc.handler.RequestHandler)3 Callable (java.util.concurrent.Callable)3 ContainerBuilder (com.yahoo.jdisc.application.ContainerBuilder)2 BindingNotFoundException (com.yahoo.jdisc.handler.BindingNotFoundException)2 RequestDeniedException (com.yahoo.jdisc.handler.RequestDeniedException)2 NonWorkingRequest (com.yahoo.jdisc.test.NonWorkingRequest)2 AbstractRequestHandler (com.yahoo.jdisc.handler.AbstractRequestHandler)1 BufferedContentChannel (com.yahoo.jdisc.handler.BufferedContentChannel)1 ReadableContentChannel (com.yahoo.jdisc.handler.ReadableContentChannel)1 RequestDispatch (com.yahoo.jdisc.handler.RequestDispatch)1 HttpResponse (com.yahoo.jdisc.http.HttpResponse)1