Search in sources :

Example 6 with RestLiArgumentBuilder

use of com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testAction_BadArrayElements.

@Test
public void testAction_BadArrayElements() throws Exception {
    ResourceModel accountsResourceModel = buildResourceModel(TwitterAccountsResource.class);
    ResourceMethodDescriptor methodDescriptor;
    // #1 no defaults provided
    methodDescriptor = accountsResourceModel.findActionMethod("spamTweets", ResourceLevel.COLLECTION);
    String jsonEntityBody = RestLiTestHelper.doubleQuote("{'statuses':[1,2,3]}");
    RestRequest request = new RestRequestBuilder(new URI("/accounts?action=spamTweets")).setMethod("POST").setEntity(jsonEntityBody.getBytes(Data.UTF_8_CHARSET)).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString()).build();
    RoutingResult routingResult = new RoutingResult(new ResourceContextImpl(null, request, new RequestContext()), methodDescriptor);
    final FilterRequestContextInternal filterContext = new FilterRequestContextInternalImpl((ServerResourceContext) routingResult.getContext(), routingResult.getResourceMethod());
    RestLiArgumentBuilder adapter = _methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getType());
    try {
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
        filterContext.setRequestData(adapter.extractRequestData(routingResult, request));
        _invoker.invoke(filterContext.getRequestData(), routingResult, _methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getMethodType()), null, null);
        Assert.fail("expected routing exception");
    } catch (RoutingException e) {
        Assert.assertEquals(e.getStatus(), 400);
    }
}
Also used : FilterRequestContextInternalImpl(com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl) RoutingException(com.linkedin.restli.server.RoutingException) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ByteString(com.linkedin.data.ByteString) CustomString(com.linkedin.restli.server.custom.types.CustomString) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) FilterRequestContextInternal(com.linkedin.restli.internal.server.filter.FilterRequestContextInternal) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with RestLiArgumentBuilder

use of com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder in project rest.li by linkedin.

the class RestLiServerUtils method buildRestLiArgumentBuilder.

/**
   * Builds a RestLiArgument builder
   *
   * @param method
   *          {@link RoutingResult}
   * @param errorResponseBuilder
   *          {@link ErrorResponseBuilder}
   * @return
   */
public static RestLiArgumentBuilder buildRestLiArgumentBuilder(RoutingResult method, ErrorResponseBuilder errorResponseBuilder) {
    ResourceMethodDescriptor resourceMethodDescriptor = method.getResourceMethod();
    RestLiArgumentBuilder adapter = new MethodAdapterRegistry(errorResponseBuilder).getArgumentBuilder(resourceMethodDescriptor.getType());
    if (adapter == null) {
        throw new IllegalArgumentException("Unsupported method type: " + resourceMethodDescriptor.getType());
    }
    return adapter;
}
Also used : ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) MethodAdapterRegistry(com.linkedin.restli.internal.server.methods.MethodAdapterRegistry)

Example 8 with RestLiArgumentBuilder

use of com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder 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 URISyntaxException {
    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);
        RequestExecutionReportBuilder requestExecutionReportBuilder = null;
        if (isDebugMode) {
            requestExecutionReportBuilder = new RequestExecutionReportBuilder();
        }
        RestLiArgumentBuilder adapter = _methodAdapterRegistry.getArgumentBuilder(methodDescriptor.getType());
        RestLiRequestData requestData = adapter.extractRequestData(routingResult, request);
        RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), (ServerResourceContext) routingResult.getContext());
        _invoker.invoke(requestData, routingResult, adapter, callback, requestExecutionReportBuilder);
        EasyMock.verify(resource);
        EasyMock.verify(callback);
        Assert.assertEquals(((ServerResourceContext) routingResult.getContext()).getResponseMimeType(), "application/x-pson");
    } catch (RestLiSyntaxException e) {
        throw new RoutingException("syntax exception", 400);
    } finally {
        EasyMock.reset(callback, resource);
        callback.onSuccess(EasyMock.anyObject(), isDebugMode ? EasyMock.isA(RequestExecutionReport.class) : EasyMock.<RequestExecutionReport>isNull(), EasyMock.anyObject(RestLiResponseAttachments.class));
        EasyMock.expectLastCall().once();
        EasyMock.replay(callback);
    }
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) RequestExecutionReportBuilder(com.linkedin.restli.server.RequestExecutionReportBuilder) RestLiSyntaxException(com.linkedin.restli.internal.server.util.RestLiSyntaxException) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) URI(java.net.URI) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) 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) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData)

Aggregations

RestLiArgumentBuilder (com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder)8 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)5 RequestContext (com.linkedin.r2.message.RequestContext)4 RestRequest (com.linkedin.r2.message.rest.RestRequest)4 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)4 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)4 FilterRequestContextInternal (com.linkedin.restli.internal.server.filter.FilterRequestContextInternal)4 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)4 RoutingException (com.linkedin.restli.server.RoutingException)4 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)4 URI (java.net.URI)4 FilterChainCallback (com.linkedin.restli.internal.server.filter.FilterChainCallback)3 FilterRequestContextInternalImpl (com.linkedin.restli.internal.server.filter.FilterRequestContextInternalImpl)3 RestLiFilterChain (com.linkedin.restli.internal.server.filter.RestLiFilterChain)3 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)3 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)3 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)3 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)3 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)3 ByteString (com.linkedin.data.ByteString)2