Search in sources :

Example 21 with ContentChannel

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

the class MbusServer method handleMessage.

@Override
public void handleMessage(final Message msg) {
    if (!running.get()) {
        dispatchErrorReply(msg, ErrorCode.SESSION_BUSY, "Session temporarily closed.");
        return;
    }
    if (msg.getTrace().shouldTrace(6)) {
        msg.getTrace().trace(6, "Message received by MbusServer.");
    }
    Request request = null;
    ContentChannel content = null;
    try {
        request = new MbusRequest(container, uri, msg);
        content = request.connect(new ServerResponseHandler(msg));
    } catch (final RuntimeException e) {
        dispatchErrorReply(msg, ErrorCode.APP_FATAL_ERROR, e.toString());
    } finally {
        if (request != null) {
            request.release();
        }
    }
    if (content != null) {
        content.close(IgnoredCompletionHandler.INSTANCE);
    }
}
Also used : ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Request(com.yahoo.jdisc.Request)

Example 22 with ContentChannel

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

the class ReferenceCountingRequestHandler method handleRequest.

@Override
public ContentChannel handleRequest(Request request, ResponseHandler responseHandler) {
    try (final ResourceReference requestReference = request.refer()) {
        ContentChannel contentChannel;
        final ReferenceCountingResponseHandler referenceCountingResponseHandler = new ReferenceCountingResponseHandler(request, new NullContentResponseHandler(responseHandler));
        try {
            contentChannel = delegate.handleRequest(request, referenceCountingResponseHandler);
            Objects.requireNonNull(contentChannel, "contentChannel");
        } catch (Throwable t) {
            try {
                // The response handler might never be invoked, due to the exception thrown from handleRequest().
                referenceCountingResponseHandler.unrefer();
            } catch (Throwable thrownFromUnrefer) {
                log.log(Level.WARNING, "Unexpected problem", thrownFromUnrefer);
            }
            throw t;
        }
        return new ReferenceCountingContentChannel(request, contentChannel);
    }
}
Also used : ContentChannel(com.yahoo.jdisc.handler.ContentChannel) ResourceReference(com.yahoo.jdisc.ResourceReference)

Example 23 with ContentChannel

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

the class TestDriverTestCase method requireThatConnectRequestWorks.

@Test
public void requireThatConnectRequestWorks() {
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    MyRequestHandler requestHandler = new MyRequestHandler(new MyContentChannel());
    ContainerBuilder builder = driver.newContainerBuilder();
    builder.serverBindings().bind("scheme://host/path", requestHandler);
    driver.activateContainer(builder);
    ContentChannel content = driver.connectRequest("scheme://host/path", new MyResponseHandler());
    assertNotNull(content);
    content.close(null);
    assertNotNull(requestHandler.handler);
    requestHandler.handler.handleResponse(new Response(Response.Status.OK)).close(null);
    assertTrue(driver.close());
}
Also used : Response(com.yahoo.jdisc.Response) ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) Test(org.junit.Test)

Example 24 with ContentChannel

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

the class FilteringRequestHandler method handleRequest.

@Override
public ContentChannel handleRequest(Request request, ResponseHandler originalResponseHandler) {
    Preconditions.checkArgument(request instanceof HttpRequest, "Expected HttpRequest, got " + request);
    Objects.requireNonNull(originalResponseHandler, "responseHandler");
    RequestFilter requestFilter = requestFilters.resolve(request.getUri());
    ResponseFilter responseFilter = responseFilters.resolve(request.getUri());
    // Not using request.connect() here - it adds logic for error handling that we'd rather leave to the framework.
    RequestHandler resolvedRequestHandler = request.container().resolveHandler(request);
    if (resolvedRequestHandler == null) {
        throw new BindingNotFoundException(request.getUri());
    }
    RequestHandler requestHandler = new ReferenceCountingRequestHandler(resolvedRequestHandler);
    ResponseHandler responseHandler;
    if (responseFilter != null) {
        responseHandler = new FilteringResponseHandler(originalResponseHandler, responseFilter, request);
    } else {
        responseHandler = originalResponseHandler;
    }
    if (requestFilter != null) {
        InterceptingResponseHandler interceptingResponseHandler = new InterceptingResponseHandler(responseHandler);
        requestFilter.filter(HttpRequest.class.cast(request), interceptingResponseHandler);
        if (interceptingResponseHandler.hasProducedResponse()) {
            return COMPLETING_CONTENT_CHANNEL;
        }
    }
    ContentChannel contentChannel = requestHandler.handleRequest(request, responseHandler);
    if (contentChannel == null) {
        throw new RequestDeniedException(request);
    }
    return contentChannel;
}
Also used : HttpRequest(com.yahoo.jdisc.http.HttpRequest) RequestHandler(com.yahoo.jdisc.handler.RequestHandler) AbstractRequestHandler(com.yahoo.jdisc.handler.AbstractRequestHandler) ResponseHandler(com.yahoo.jdisc.handler.ResponseHandler) RequestDeniedException(com.yahoo.jdisc.handler.RequestDeniedException) ContentChannel(com.yahoo.jdisc.handler.ContentChannel) ResponseFilter(com.yahoo.jdisc.http.filter.ResponseFilter) BindingNotFoundException(com.yahoo.jdisc.handler.BindingNotFoundException) RequestFilter(com.yahoo.jdisc.http.filter.RequestFilter)

Example 25 with ContentChannel

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

the class MockClient method handleRequest.

@Override
public ContentChannel handleRequest(Request request, ResponseHandler handler) {
    counter.incrementAndGet();
    final ContentChannel responseContentChannel = handler.handleResponse(new Response(200));
    responseContentChannel.close(NOOP_COMPLETION_HANDLER);
    return null;
}
Also used : Response(com.yahoo.jdisc.Response) ContentChannel(com.yahoo.jdisc.handler.ContentChannel)

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