Search in sources :

Example 26 with ResourceContextImpl

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

the class TestRestLiResponseHandler method buildRoutingResult.

private RoutingResult buildRoutingResult(ResourceMethod resourceMethod, RestRequest request, Map<String, String> acceptHeaders, Set<String> customTypes) throws SecurityException, NoSuchMethodException, RestLiSyntaxException {
    Method method = ProjectionTestFixture.class.getMethod("batchGet", Set.class);
    ResourceModel model = RestLiTestHelper.buildResourceModel(StatusCollectionResource.class);
    ResourceMethodDescriptor methodDescriptor = ResourceMethodDescriptor.createForRestful(resourceMethod, method, InterfaceType.SYNC);
    model.addResourceMethodDescriptor(methodDescriptor);
    ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptHeaders, customTypes, context);
    return new RoutingResult(context, methodDescriptor);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Method(java.lang.reflect.Method) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 27 with ResourceContextImpl

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

the class BaseRestLiServer method getRoutingResult.

protected RoutingResult getRoutingResult(Request request, RequestContext requestContext) {
    ensureRequestUsesValidRestliProtocol(request, requestContext);
    try {
        ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, requestContext);
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), _supportedAcceptTypes, _customContentTypes, context, requestContext);
        ResourceMethodDescriptor method = _router.process(context);
        ResourceMethodConfig methodConfig = _methodConfigProvider.apply(method);
        context.setAlwaysProjectedFields(methodConfig.getAlwaysProjectedFields().getValue());
        context.setFillInDefaultValues(_fillInDefaultValueConfigured);
        return new RoutingResult(context, method, methodConfig);
    } catch (RestLiSyntaxException e) {
        throw new RoutingException(e.getMessage(), HttpStatus.S_400_BAD_REQUEST.getCode());
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceMethodConfig(com.linkedin.restli.server.config.ResourceMethodConfig) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl)

Example 28 with ResourceContextImpl

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

the class TestRestLiMethodInvocation method checkAsyncInvocation.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void checkAsyncInvocation(BaseResource resource, RestLiCallback callback, ResourceMethodDescriptor methodDescriptor, String httpMethod, ProtocolVersion version, String uri, String entityBody, MutablePathKeys pathkeys, boolean isDebugMode) throws Exception {
    try {
        RestRequestBuilder builder = new RestRequestBuilder(new URI(uri)).setMethod(httpMethod).addHeaderValue("Accept", "application/x-pson").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
        if (entityBody != null) {
            builder.setEntity(entityBody.getBytes(Data.UTF_8_CHARSET));
        }
        RestRequest request = builder.build();
        RoutingResult routingResult = new RoutingResult(new ResourceContextImpl(pathkeys, request, new RequestContext()), methodDescriptor);
        RestLiArgumentBuilder argumentBuilder = _methodAdapterProvider.getArgumentBuilder(methodDescriptor.getType());
        RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, entityBody != null ? DataMapUtils.readMapWithExceptions(request) : null);
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), Collections.emptySet(), routingResult.getContext());
        _invoker.invoke(requestData, routingResult, argumentBuilder, callback);
        EasyMock.verify(resource);
        EasyMock.verify(callback);
        Assert.assertEquals((routingResult.getContext()).getResponseMimeType(), "application/x-pson");
    } catch (RestLiSyntaxException e) {
        throw new RoutingException("syntax exception", 400);
    } finally {
        EasyMock.reset(callback, resource);
        callback.onSuccess(EasyMock.anyObject());
        EasyMock.expectLastCall().once();
        EasyMock.replay(callback);
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RoutingException(com.linkedin.restli.server.RoutingException) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) URI(java.net.URI) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData)

Example 29 with ResourceContextImpl

use of com.linkedin.restli.internal.server.ResourceContextImpl 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 30 with ResourceContextImpl

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

the class TestResourceContext method testStreamingDataResourceContext.

@Test
public void testStreamingDataResourceContext() throws Exception {
    RestRequest request = new RestRequestBuilder(URI.create("foobar")).addHeaderValue(RestConstants.HEADER_ACCEPT, RestConstants.HEADER_VALUE_MULTIPART_RELATED).build();
    ServerResourceContext fullyStreamingResourceContext = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    fullyStreamingResourceContext.setRequestAttachmentReader(new RestLiAttachmentReader(null));
    Assert.assertTrue(fullyStreamingResourceContext.responseAttachmentsSupported());
    Assert.assertNotNull(fullyStreamingResourceContext.getRequestAttachmentReader());
    // Now set and get response attachments
    final RestLiResponseAttachments restLiResponseAttachments = new RestLiResponseAttachments.Builder().build();
    fullyStreamingResourceContext.setResponseAttachments(restLiResponseAttachments);
    Assert.assertEquals(fullyStreamingResourceContext.getResponseAttachments(), restLiResponseAttachments);
    ServerResourceContext responseAllowedNoRequestAttachmentsPresent = new ResourceContextImpl(new PathKeysImpl(), request, new RequestContext());
    Assert.assertTrue(responseAllowedNoRequestAttachmentsPresent.responseAttachmentsSupported());
    Assert.assertNull(responseAllowedNoRequestAttachmentsPresent.getRequestAttachmentReader());
    // Now set and get response attachments
    responseAllowedNoRequestAttachmentsPresent.setResponseAttachments(restLiResponseAttachments);
    Assert.assertEquals(responseAllowedNoRequestAttachmentsPresent.getResponseAttachments(), restLiResponseAttachments);
    ServerResourceContext noResponseAllowedRequestAttachmentsPresent = new ResourceContextImpl(new PathKeysImpl(), new MockRequest(URI.create("foobar"), Collections.emptyMap()), new RequestContext());
    noResponseAllowedRequestAttachmentsPresent.setRequestAttachmentReader(new RestLiAttachmentReader(null));
    Assert.assertFalse(noResponseAllowedRequestAttachmentsPresent.responseAttachmentsSupported());
    Assert.assertNotNull(noResponseAllowedRequestAttachmentsPresent.getRequestAttachmentReader());
    // Now try to set and make sure we fail
    try {
        noResponseAllowedRequestAttachmentsPresent.setResponseAttachments(restLiResponseAttachments);
        Assert.fail();
    } catch (IllegalStateException illegalStateException) {
    // pass
    }
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RestLiAttachmentReader(com.linkedin.restli.common.attachments.RestLiAttachmentReader) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) Test(org.testng.annotations.Test)

Aggregations

ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)52 RequestContext (com.linkedin.r2.message.RequestContext)41 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)40 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)34 Test (org.testng.annotations.Test)31 RestRequest (com.linkedin.r2.message.rest.RestRequest)29 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)29 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)25 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)20 URI (java.net.URI)15 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)14 RestLiRouter (com.linkedin.restli.internal.server.RestLiRouter)14 RestLiConfig (com.linkedin.restli.server.RestLiConfig)14 ByteString (com.linkedin.data.ByteString)11 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)10 PathKeys (com.linkedin.restli.server.PathKeys)10 RoutingException (com.linkedin.restli.server.RoutingException)9 DataMap (com.linkedin.data.DataMap)7 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)7