Search in sources :

Example 1 with RequestDeniedException

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

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

the class MbusClientTestCase method requireThatNonMbusRequestIsDenied.

@Test
public void requireThatNonMbusRequestIsDenied() throws InterruptedException {
    ClientTestDriver driver = ClientTestDriver.newInstance();
    Request serverReq = null;
    Request clientReq = null;
    try {
        serverReq = driver.newServerRequest();
        clientReq = new Request(serverReq, URI.create("mbus://host/path"));
        clientReq.connect(MyResponseHandler.newInstance());
        fail();
    } catch (RequestDeniedException e) {
        System.out.println(e.getMessage());
    } finally {
        if (serverReq != null) {
            serverReq.release();
        }
        if (clientReq != null) {
            clientReq.release();
        }
    }
    assertTrue(driver.close());
}
Also used : ClientTestDriver(com.yahoo.messagebus.jdisc.test.ClientTestDriver) RequestDeniedException(com.yahoo.jdisc.handler.RequestDeniedException) Request(com.yahoo.jdisc.Request) Test(org.junit.Test)

Example 3 with RequestDeniedException

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

the class TestDriverTestCase method requireThatFailedRequestConnectDoesNotBlockClose.

@Test
public void requireThatFailedRequestConnectDoesNotBlockClose() {
    TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
    ContainerBuilder builder = driver.newContainerBuilder();
    builder.serverBindings().bind("scheme://host/path", new MyRequestHandler(null));
    driver.activateContainer(builder);
    try {
        driver.connectRequest("scheme://host/path", new MyResponseHandler());
        fail();
    } catch (RequestDeniedException e) {
    }
    assertTrue(driver.close());
}
Also used : ContainerBuilder(com.yahoo.jdisc.application.ContainerBuilder) RequestDeniedException(com.yahoo.jdisc.handler.RequestDeniedException) Test(org.junit.Test)

Example 4 with RequestDeniedException

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

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

the class MbusClient method handleRequest.

@Override
public ContentChannel handleRequest(final Request request, final ResponseHandler handler) {
    if (!(request instanceof MbusRequest)) {
        throw new RequestDeniedException(request);
    }
    final Message msg = ((MbusRequest) request).getMessage();
    msg.getTrace().trace(6, "Request received by MbusClient.");
    // save user context
    msg.pushHandler(null);
    final Long timeout = request.timeRemaining(TimeUnit.MILLISECONDS);
    if (timeout != null) {
        msg.setTimeReceivedNow();
        // negative or zero timeout has semantics
        msg.setTimeRemaining(Math.max(1, timeout));
    }
    msg.setContext(handler);
    msg.pushHandler(this);
    queue.add((MbusRequest) request);
    return null;
}
Also used : RequestDeniedException(com.yahoo.jdisc.handler.RequestDeniedException) Message(com.yahoo.messagebus.Message)

Aggregations

RequestDeniedException (com.yahoo.jdisc.handler.RequestDeniedException)5 BindingNotFoundException (com.yahoo.jdisc.handler.BindingNotFoundException)2 ContentChannel (com.yahoo.jdisc.handler.ContentChannel)2 RequestHandler (com.yahoo.jdisc.handler.RequestHandler)2 Test (org.junit.Test)2 Request (com.yahoo.jdisc.Request)1 ContainerBuilder (com.yahoo.jdisc.application.ContainerBuilder)1 AbstractRequestHandler (com.yahoo.jdisc.handler.AbstractRequestHandler)1 ResponseHandler (com.yahoo.jdisc.handler.ResponseHandler)1 HttpRequest (com.yahoo.jdisc.http.HttpRequest)1 RequestFilter (com.yahoo.jdisc.http.filter.RequestFilter)1 ResponseFilter (com.yahoo.jdisc.http.filter.ResponseFilter)1 Message (com.yahoo.messagebus.Message)1 ClientTestDriver (com.yahoo.messagebus.jdisc.test.ClientTestDriver)1