Search in sources :

Example 1 with FilterChainDispatcherImpl

use of com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl in project rest.li by linkedin.

the class TestRestLiMethodInvocation method checkInvocation.

private void checkInvocation(Object resource, RequestContext requestContext, ResourceMethodDescriptor resourceMethodDescriptor, ResourceMethodConfig resourceMethodConfig, String httpMethod, ProtocolVersion version, String uri, String entityBody, MutablePathKeys pathkeys, final Callback<RestResponse> callback, final boolean isDebugMode, final boolean expectRoutingException, final RestLiAttachmentReader expectedRequestAttachments, final RestLiResponseAttachments expectedResponseAttachments) throws Exception {
    assertNotNull(resource);
    assertNotNull(resourceMethodDescriptor);
    try {
        EasyMock.replay(resource);
        RestRequestBuilder builder = new RestRequestBuilder(new URI(uri)).setMethod(httpMethod).addHeaderValue("Accept", "application/json").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
        if (entityBody != null) {
            builder.setEntity(entityBody.getBytes(Data.UTF_8_CHARSET));
        }
        if (expectedResponseAttachments != null) {
            builder.addHeaderValue(RestConstants.HEADER_ACCEPT, RestConstants.HEADER_VALUE_MULTIPART_RELATED);
        }
        RestRequest request = builder.build();
        if (isDebugMode) {
            requestContext.putLocalAttr(RestLiMethodInvoker.ATTRIBUTE_PROMISE_LISTENER, new PromiseListener<Object>() {

                @Override
                public void onResolved(Promise<Object> promise) {
                    // PromiseListener is invoked with a task.
                    if (promise instanceof Task) {
                        requestContext.putLocalAttr(ATTRIBUTE_PARSEQ_TRACE, ((Task<?>) promise).getTrace());
                    }
                }
            });
        }
        final ServerResourceContext resourceContext = new ResourceContextImpl(pathkeys, request, requestContext);
        resourceContext.setRequestAttachmentReader(expectedRequestAttachments);
        if (expectedResponseAttachments != null) {
            resourceContext.setResponseAttachments(expectedResponseAttachments);
        }
        RoutingResult routingResult = new RoutingResult(resourceContext, resourceMethodDescriptor, resourceMethodConfig);
        RestLiArgumentBuilder argumentBuilder = _methodAdapterProvider.getArgumentBuilder(resourceMethodDescriptor.getType());
        RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, entityBody != null && !entityBody.isEmpty() ? DataMapUtils.readMapWithExceptions(request) : null);
        FilterRequestContext filterContext = new FilterRequestContextInternalImpl(routingResult.getContext(), resourceMethodDescriptor, requestData);
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch expectedRoutingExceptionLatch = new CountDownLatch(1);
        RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler(_methodAdapterProvider, _errorResponseBuilder);
        Callback<RestLiResponse> executionCallback = new Callback<RestLiResponse>() {

            @Override
            public void onError(Throwable e) {
                if (e.getCause() != null && e.getCause().getCause() instanceof RoutingException) {
                    expectedRoutingExceptionLatch.countDown();
                }
                if (callback != null) {
                    callback.onError(e);
                }
                Assert.assertEquals(resourceContext.getRequestAttachmentReader(), expectedRequestAttachments);
                Assert.assertEquals(resourceContext.getResponseAttachments(), expectedResponseAttachments);
                latch.countDown();
            }

            @Override
            public void onSuccess(final RestLiResponse result) {
                if (callback != null) {
                    callback.onSuccess(ResponseUtils.buildResponse(routingResult, result));
                }
                Assert.assertEquals(resourceContext.getResponseAttachments(), expectedResponseAttachments);
                latch.countDown();
            }
        };
        FilterChainDispatcher filterChainDispatcher = new FilterChainDispatcherImpl(routingResult, _invoker, argumentBuilder);
        FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(routingResult, restLiResponseHandler, executionCallback, _errorResponseBuilder);
        final RestLiCallback outerCallback = new RestLiCallback(filterContext, new RestLiFilterResponseContextFactory(request, routingResult, restLiResponseHandler), new RestLiFilterChain(null, filterChainDispatcher, filterChainCallback));
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), Collections.emptySet(), routingResult.getContext());
        _invoker.invoke(requestData, routingResult, argumentBuilder, outerCallback);
        try {
            latch.await();
            if (expectRoutingException) {
                expectedRoutingExceptionLatch.await();
            }
        } catch (InterruptedException e) {
        // Ignore
        }
        EasyMock.verify(resource);
        Assert.assertEquals((routingResult.getContext()).getResponseMimeType(), "application/json");
    } catch (RestLiSyntaxException e) {
        throw new RoutingException("syntax exception", 400);
    } finally {
        EasyMock.reset(resource);
        EasyMock.makeThreadSafe(resource, true);
    }
}
Also used : FilterRequestContextInternalImpl(com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl) RoutingException(com.linkedin.restli.server.RoutingException) Task(com.linkedin.parseq.Task) BaseTask(com.linkedin.parseq.BaseTask) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) FilterChainDispatcherImpl(com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl) URI(java.net.URI) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) FilterChainDispatcher(com.linkedin.restli.internal.server.filter.FilterChainDispatcher) RestLiResponseHandler(com.linkedin.restli.internal.server.response.RestLiResponseHandler) FilterChainCallbackImpl(com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) RestLiFilterResponseContextFactory(com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData)

Example 2 with FilterChainDispatcherImpl

use of com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testInvokeWithUnsupportedAcceptMimeType.

@Test
public void testInvokeWithUnsupportedAcceptMimeType() throws Exception {
    RestRequestBuilder builder = new RestRequestBuilder(new URI("")).addHeaderValue("Accept", "text/plain").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
    RestRequest request = builder.build();
    final RestLiAttachmentReader attachmentReader = new RestLiAttachmentReader(null);
    final CountDownLatch latch = new CountDownLatch(1);
    RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler(_methodAdapterProvider, _errorResponseBuilder);
    ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), new RestRequestBuilder(URI.create("")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.LATEST_PROTOCOL_VERSION.toString()).build(), new RequestContext());
    resourceContext.setRequestAttachmentReader(attachmentReader);
    Callback<RestLiResponse> executionCallback = new Callback<RestLiResponse>() {

        @Override
        public void onError(Throwable e) {
            latch.countDown();
            Assert.assertTrue(e instanceof RestException);
            RestException ex = (RestException) e;
            Assert.assertEquals(ex.getResponse().getStatus(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
            Assert.assertEquals(resourceContext.getRequestAttachmentReader(), attachmentReader);
            Assert.assertNull(resourceContext.getResponseAttachments());
        }

        @Override
        public void onSuccess(RestLiResponse result) {
            Assert.fail();
        }
    };
    try {
        RoutingResult routingResult = new RoutingResult(resourceContext, null);
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), Collections.emptySet(), routingResult.getContext());
        FilterChainDispatcher filterChainDispatcher = new FilterChainDispatcherImpl(routingResult, _invoker, null);
        FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(null, restLiResponseHandler, executionCallback, _errorResponseBuilder);
        final RestLiCallback callback = new RestLiCallback(null, new RestLiFilterResponseContextFactory(request, null, restLiResponseHandler), new RestLiFilterChain(null, filterChainDispatcher, filterChainCallback));
        _invoker.invoke(null, routingResult, null, callback);
        latch.await();
    } catch (Exception e) {
        // exception is expected
        Assert.assertTrue(e instanceof RestLiServiceException);
    }
    Assert.assertNull(resourceContext.getResponseMimeType());
}
Also used : FilterChainDispatcherImpl(com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl) URI(java.net.URI) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) FilterChainDispatcher(com.linkedin.restli.internal.server.filter.FilterChainDispatcher) RestLiResponseHandler(com.linkedin.restli.internal.server.response.RestLiResponseHandler) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) FilterChainCallbackImpl(com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) RestException(com.linkedin.r2.message.rest.RestException) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) CountDownLatch(java.util.concurrent.CountDownLatch) RestException(com.linkedin.r2.message.rest.RestException) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RoutingException(com.linkedin.restli.server.RoutingException) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) RestLiFilterResponseContextFactory(com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with FilterChainDispatcherImpl

use of com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl in project rest.li by linkedin.

the class BaseRestLiServer method handleResourceRequest.

/**
 * Handles a request by building arguments and invoking the Rest.li resource method. All the arguments are processed
 * by the filters in the filter chain before invoking the resource method. The result is also processed by the
 * filters after invoking the resource method.
 *
 * @param request   The request to handle. Only the URI, method, and the headers can be accessed from this request. The
 *                  body should have already been parse to a DataMap.
 * @param callback
 */
protected final void handleResourceRequest(Request request, RoutingResult routingResult, DataMap entityDataMap, Callback<RestLiResponse> callback) {
    ServerResourceContext context = routingResult.getContext();
    ResourceMethodDescriptor method = routingResult.getResourceMethod();
    FilterRequestContext filterContext;
    RestLiArgumentBuilder argumentBuilder;
    try {
        argumentBuilder = lookupArgumentBuilder(method);
        // Unstructured data is not available in the Rest.Li filters
        RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, entityDataMap);
        filterContext = new FilterRequestContextInternalImpl(context, method, requestData);
    } catch (Exception e) {
        // would not trigger response filters because request filters haven't run yet
        callback.onError(buildPreRoutingError(e, request));
        return;
    }
    RestLiFilterResponseContextFactory filterResponseContextFactory = new RestLiFilterResponseContextFactory(request, routingResult, _responseHandler);
    FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(routingResult, _responseHandler, callback, _errorResponseBuilder);
    FilterChainDispatcher filterChainDispatcher = new FilterChainDispatcherImpl(routingResult, _methodInvoker, argumentBuilder);
    RestLiFilterChain filterChain = new RestLiFilterChain(_filters, filterChainDispatcher, filterChainCallback);
    TimingContextUtil.beginTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_REQUEST_RESTLI_FILTER_CHAIN.key());
    filterChain.onRequest(filterContext, filterResponseContextFactory);
}
Also used : FilterRequestContextInternalImpl(com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl) FilterChainDispatcherImpl(com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) RestLiResponseException(com.linkedin.restli.internal.server.response.RestLiResponseException) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RestLiFilterResponseContextFactory(com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory) FilterChainDispatcher(com.linkedin.restli.internal.server.filter.FilterChainDispatcher) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) FilterChainCallbackImpl(com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext)

Example 4 with FilterChainDispatcherImpl

use of com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testInvokeWithInvalidAcceptMimeType.

@Test
public void testInvokeWithInvalidAcceptMimeType() throws Exception {
    RestRequestBuilder builder = new RestRequestBuilder(new URI("")).addHeaderValue("Accept", "foo").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
    RestRequest request = builder.build();
    final CountDownLatch latch = new CountDownLatch(1);
    RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler(_methodAdapterProvider, _errorResponseBuilder);
    Callback<RestLiResponse> executionCallback = new Callback<RestLiResponse>() {

        @Override
        public void onError(Throwable e) {
            latch.countDown();
            Assert.assertTrue(e instanceof RestException);
            RestException ex = (RestException) e;
            Assert.assertEquals(ex.getResponse().getStatus(), HttpStatus.S_400_BAD_REQUEST.getCode());
        }

        @Override
        public void onSuccess(RestLiResponse result) {
        }
    };
    ServerResourceContext context = new ResourceContextImpl();
    try {
        RoutingResult routingResult = new RoutingResult(context, null);
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), Collections.emptySet(), routingResult.getContext());
        FilterChainDispatcher filterChainDispatcher = new FilterChainDispatcherImpl(routingResult, _invoker, null);
        FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(null, restLiResponseHandler, executionCallback, _errorResponseBuilder);
        final RestLiCallback callback = new RestLiCallback(null, new RestLiFilterResponseContextFactory(request, null, restLiResponseHandler), new RestLiFilterChain(null, filterChainDispatcher, filterChainCallback));
        _invoker.invoke(null, routingResult, null, callback);
        latch.await();
    } catch (Exception e) {
        // exception is expected
        Assert.assertTrue(e instanceof RestLiServiceException);
    }
    Assert.assertNull(context.getResponseMimeType());
}
Also used : FilterChainDispatcherImpl(com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl) RestException(com.linkedin.r2.message.rest.RestException) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) RestException(com.linkedin.r2.message.rest.RestException) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RoutingException(com.linkedin.restli.server.RoutingException) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RestLiFilterResponseContextFactory(com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory) FilterChainDispatcher(com.linkedin.restli.internal.server.filter.FilterChainDispatcher) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiResponseHandler(com.linkedin.restli.internal.server.response.RestLiResponseHandler) Callback(com.linkedin.common.callback.Callback) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) FilterChainCallbackImpl(com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with FilterChainDispatcherImpl

use of com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl in project rest.li by linkedin.

the class TestRestLiCallback method setUp.

@BeforeTest
protected void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    _filterResponseContextFactory = new RestLiFilterResponseContextFactory(_restRequest, _routingResult, _responseHandler);
    when(_routingResult.getContext()).thenReturn(new ResourceContextImpl());
    ErrorResponseBuilder errorResponseBuilder = new ErrorResponseBuilder();
    FilterChainDispatcher filterChainDispatcher = new FilterChainDispatcherImpl(_routingResult, _methodInvoker, _argumentBuilder);
    FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(_routingResult, _responseHandler, _callback, errorResponseBuilder);
    _zeroFilterChain = new RestLiFilterChain(null, filterChainDispatcher, filterChainCallback);
    _oneFilterChain = new RestLiFilterChain(Arrays.asList(_filter), filterChainDispatcher, filterChainCallback);
    _twoFilterChain = new RestLiFilterChain(Arrays.asList(_filter, _filter), filterChainDispatcher, filterChainCallback);
    _noFilterRestLiCallback = new RestLiCallback(_filterRequestContext, _filterResponseContextFactory, _zeroFilterChain);
    _oneFilterRestLiCallback = new RestLiCallback(_filterRequestContext, _filterResponseContextFactory, _oneFilterChain);
    _twoFilterRestLiCallback = new RestLiCallback(_filterRequestContext, _filterResponseContextFactory, _twoFilterChain);
}
Also used : FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RestLiFilterResponseContextFactory(com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory) FilterChainDispatcher(com.linkedin.restli.internal.server.filter.FilterChainDispatcher) FilterChainDispatcherImpl(com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl) FilterChainCallbackImpl(com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl) RestLiCallback(com.linkedin.restli.internal.server.RestLiCallback) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)5 FilterChainCallbackImpl (com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl)5 FilterChainDispatcher (com.linkedin.restli.internal.server.filter.FilterChainDispatcher)5 FilterChainDispatcherImpl (com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl)5 RestLiFilterChain (com.linkedin.restli.internal.server.filter.RestLiFilterChain)5 RestLiFilterResponseContextFactory (com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory)5 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)4 RestLiCallback (com.linkedin.restli.internal.server.RestLiCallback)4 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)4 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)4 Callback (com.linkedin.common.callback.Callback)3 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)3 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)3 RestLiResponse (com.linkedin.restli.internal.server.response.RestLiResponse)3 RestLiResponseHandler (com.linkedin.restli.internal.server.response.RestLiResponseHandler)3 RoutingException (com.linkedin.restli.server.RoutingException)3 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)3 URI (java.net.URI)3 CountDownLatch (java.util.concurrent.CountDownLatch)3