Search in sources :

Example 1 with FilterRequestContextInternalImpl

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

the class TestRestLiMethodInvocation method checkInvocation.

private void checkInvocation(Object resource, ResourceMethodDescriptor resourceMethodDescriptor, String httpMethod, ProtocolVersion version, String uri, String entityBody, MutablePathKeys pathkeys, final RequestExecutionCallback<RestResponse> callback, final boolean isDebugMode, final boolean expectRoutingException, final RestLiAttachmentReader expectedRequestAttachments, final RestLiResponseAttachments expectedResponseAttachments) throws URISyntaxException, RestLiSyntaxException {
    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));
        }
        RestRequest request = builder.build();
        final ResourceContext resourceContext = new ResourceContextImpl(pathkeys, request, new RequestContext(), true, expectedRequestAttachments);
        resourceContext.setResponseAttachments(expectedResponseAttachments);
        RoutingResult routingResult = new RoutingResult(resourceContext, resourceMethodDescriptor);
        FilterRequestContextInternal filterContext = new FilterRequestContextInternalImpl((ServerResourceContext) routingResult.getContext(), resourceMethodDescriptor);
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch expectedRoutingExceptionLatch = new CountDownLatch(1);
        RestLiArgumentBuilder adapter = _methodAdapterRegistry.getArgumentBuilder(resourceMethodDescriptor.getType());
        RestLiRequestData requestData = adapter.extractRequestData(routingResult, request);
        filterContext.setRequestData(requestData);
        RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler.Builder().build();
        RequestExecutionReportBuilder requestExecutionReportBuilder = null;
        if (isDebugMode) {
            requestExecutionReportBuilder = new RequestExecutionReportBuilder();
        }
        RequestExecutionCallback<RestResponse> executionCallback = new RequestExecutionCallback<RestResponse>() {

            @Override
            public void onError(Throwable e, RequestExecutionReport executionReport, RestLiAttachmentReader requestAttachmentReader, RestLiResponseAttachments responseAttachments) {
                if (isDebugMode) {
                    Assert.assertNotNull(executionReport);
                } else {
                    Assert.assertNull(executionReport);
                }
                if (e.getCause().getCause() instanceof RoutingException) {
                    expectedRoutingExceptionLatch.countDown();
                }
                if (callback != null) {
                    callback.onError(e, executionReport, null, null);
                }
                Assert.assertEquals(requestAttachmentReader, expectedRequestAttachments);
                Assert.assertEquals(responseAttachments, expectedResponseAttachments);
                latch.countDown();
            }

            @Override
            public void onSuccess(final RestResponse result, RequestExecutionReport executionReport, RestLiResponseAttachments responseAttachments) {
                if (isDebugMode) {
                    Assert.assertNotNull(executionReport);
                } else {
                    Assert.assertNull(executionReport);
                }
                if (callback != null) {
                    callback.onSuccess(result, executionReport, null);
                }
                Assert.assertEquals(responseAttachments, expectedResponseAttachments);
                latch.countDown();
            }
        };
        FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(routingResult, _invoker, adapter, requestExecutionReportBuilder, expectedRequestAttachments, restLiResponseHandler, executionCallback);
        final RestLiCallback<Object> outerCallback = new RestLiCallback<Object>(filterContext, new RestLiFilterResponseContextFactory<Object>(request, routingResult, restLiResponseHandler), new RestLiFilterChain(null, filterChainCallback));
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
        filterContext.setRequestData(adapter.extractRequestData(routingResult, request));
        _invoker.invoke(filterContext.getRequestData(), routingResult, adapter, outerCallback, requestExecutionReportBuilder);
        try {
            latch.await();
            if (expectRoutingException) {
                expectedRoutingExceptionLatch.await();
            }
        } catch (InterruptedException e) {
        // Ignore
        }
        EasyMock.verify(resource);
        Assert.assertEquals(((ServerResourceContext) 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) ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) URI(java.net.URI) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) RequestExecutionCallback(com.linkedin.restli.server.RequestExecutionCallback) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) 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) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) RequestExecutionReportBuilder(com.linkedin.restli.server.RequestExecutionReportBuilder) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) RestRequest(com.linkedin.r2.message.rest.RestRequest) FilterRequestContextInternal(com.linkedin.restli.internal.server.filter.FilterRequestContextInternal) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) EasyMock.anyObject(org.easymock.EasyMock.anyObject) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData)

Example 2 with FilterRequestContextInternalImpl

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

the class RestLiServer method handleResourceRequest.

private void handleResourceRequest(final RestRequest request, final RequestContext requestContext, final RequestExecutionCallback<RestResponse> callback, final RestLiAttachmentReader attachmentReader, final boolean isDebugMode) {
    try {
        ensureRequestUsesValidRestliProtocol(request);
    } catch (RestLiServiceException e) {
        respondWithPreRoutingError(e, request, attachmentReader, callback);
        return;
    }
    final RoutingResult method;
    try {
        method = _router.process(request, requestContext, attachmentReader);
    } catch (Exception e) {
        respondWithPreRoutingError(e, request, attachmentReader, callback);
        return;
    }
    final RequestExecutionCallback<RestResponse> wrappedCallback = notifyInvokeAwares(method, callback);
    RequestExecutionReportBuilder requestExecutionReportBuilder = null;
    if (isDebugMode) {
        requestExecutionReportBuilder = new RequestExecutionReportBuilder();
    }
    final FilterRequestContextInternal filterContext = new FilterRequestContextInternalImpl((ServerResourceContext) method.getContext(), method.getResourceMethod());
    RestLiArgumentBuilder adapter;
    try {
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) method.getContext());
        adapter = buildRestLiArgumentBuilder(method, _errorResponseBuilder);
        filterContext.setRequestData(adapter.extractRequestData(method, request));
    } catch (Exception e) {
        // would not trigger response filters because request filters haven't run yet
        wrappedCallback.onError(e, requestExecutionReportBuilder == null ? null : requestExecutionReportBuilder.build(), ((ServerResourceContext) method.getContext()).getRequestAttachmentReader(), null);
        return;
    }
    RestLiFilterResponseContextFactory<Object> filterResponseContextFactory = new RestLiFilterResponseContextFactory<Object>(request, method, _responseHandler);
    FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(method, _methodInvoker, adapter, requestExecutionReportBuilder, attachmentReader, _responseHandler, wrappedCallback);
    RestLiFilterChain filterChain = new RestLiFilterChain(_filters, filterChainCallback);
    filterChain.onRequest(filterContext, filterResponseContextFactory);
}
Also used : FilterRequestContextInternalImpl(com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) MultiPartIllegalFormatException(com.linkedin.multipart.exceptions.MultiPartIllegalFormatException) ParseException(javax.mail.internet.ParseException) RestException(com.linkedin.r2.message.rest.RestException) RestLiAttachmentReaderException(com.linkedin.restli.common.attachments.RestLiAttachmentReaderException) RestLiFilterChain(com.linkedin.restli.internal.server.filter.RestLiFilterChain) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) FilterChainCallback(com.linkedin.restli.internal.server.filter.FilterChainCallback) RestLiFilterResponseContextFactory(com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory) FilterRequestContextInternal(com.linkedin.restli.internal.server.filter.FilterRequestContextInternal) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) FilterChainCallbackImpl(com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl)

Example 3 with FilterRequestContextInternalImpl

use of com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl 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 4 with FilterRequestContextInternalImpl

use of com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl 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)

Aggregations

ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)4 FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)4 FilterChainCallbackImpl (com.linkedin.restli.internal.server.filter.FilterChainCallbackImpl)4 FilterRequestContextInternalImpl (com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl)4 RestLiFilterChain (com.linkedin.restli.internal.server.filter.RestLiFilterChain)4 RestLiArgumentBuilder (com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder)4 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)3 RestLiFilterResponseContextFactory (com.linkedin.restli.internal.server.filter.RestLiFilterResponseContextFactory)3 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)3 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)3 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)2 RestLiCallback (com.linkedin.restli.internal.server.RestLiCallback)2 FilterChainDispatcher (com.linkedin.restli.internal.server.filter.FilterChainDispatcher)2 FilterChainDispatcherImpl (com.linkedin.restli.internal.server.filter.FilterChainDispatcherImpl)2 FilterRequestContextInternal (com.linkedin.restli.internal.server.filter.FilterRequestContextInternal)2 RestLiResponseHandler (com.linkedin.restli.internal.server.response.RestLiResponseHandler)2 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)2