Search in sources :

Example 1 with DefaultMethodAdapterProvider

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

the class TestCustomMethodAdapterProvider method initClass.

@BeforeClass
public void initClass() throws Exception {
    RestLiConfig config = new RestLiConfig();
    config.setMethodAdapterProvider(new DefaultMethodAdapterProvider(new ErrorResponseBuilder(ErrorResponseFormat.MESSAGE_AND_SERVICECODE)) {

        @Override
        public RestLiArgumentBuilder getArgumentBuilder(ResourceMethod resourceMethod) {
            // Override the behavior of the CREATE argument builder
            if (resourceMethod == ResourceMethod.CREATE) {
                return new CreateArgumentBuilder() {

                    @Override
                    public RestLiRequestData extractRequestData(RoutingResult routingResult, DataMap dataMap) {
                        // Always use the FRIENDLY record regardless of the actual data
                        return new RestLiRequestDataImpl.Builder().entity(FRIENDLY).build();
                    }
                };
            } else {
                return super.getArgumentBuilder(resourceMethod);
            }
        }
    });
    super.init(false, config);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) DefaultMethodAdapterProvider(com.linkedin.restli.internal.server.methods.DefaultMethodAdapterProvider) CreateArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.CreateArgumentBuilder) ErrorResponseBuilder(com.linkedin.restli.internal.server.response.ErrorResponseBuilder) RestLiRequestDataImpl(com.linkedin.restli.server.RestLiRequestDataImpl) RestLiArgumentBuilder(com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder) RestLiConfig(com.linkedin.restli.server.RestLiConfig) ResourceMethod(com.linkedin.restli.common.ResourceMethod) DataMap(com.linkedin.data.DataMap) RestLiRequestData(com.linkedin.restli.server.RestLiRequestData) BeforeClass(org.testng.annotations.BeforeClass)

Example 2 with DefaultMethodAdapterProvider

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

the class TestResponseMetadata method testMetadata.

@Test(dataProvider = "dataProvider")
public void testMetadata(ResourceMethod resourceMethod, Object responseObject, boolean hasEntity, String responseString) throws Exception {
    final RestRequest mockRequest = mock(RestRequest.class);
    final RoutingResult mockRoutingResult = mock(RoutingResult.class);
    final ResourceMethodDescriptor mockResourceMethodDescriptor = mock(ResourceMethodDescriptor.class);
    final ServerResourceContext mockContext = mock(ServerResourceContext.class);
    final ProtocolVersion mockProtocolVersion = AllProtocolVersions.LATEST_PROTOCOL_VERSION;
    final URI mockURI = new URI("http://fake.com");
    when(mockRequest.getURI()).thenReturn(mockURI);
    when(mockResourceMethodDescriptor.getMethodType()).thenReturn(resourceMethod);
    when(mockRoutingResult.getResourceMethod()).thenReturn(mockResourceMethodDescriptor);
    when(mockResourceMethodDescriptor.getType()).thenReturn(resourceMethod);
    when(mockRoutingResult.getContext()).thenReturn(mockContext);
    when(mockContext.getRestliProtocolVersion()).thenReturn(mockProtocolVersion);
    when(mockContext.getRawRequestContext()).thenReturn(new RequestContext());
    final ErrorResponseBuilder errorResponseBuilder = new ErrorResponseBuilder();
    final RestLiResponseHandler responseHandler = new RestLiResponseHandler(new DefaultMethodAdapterProvider(errorResponseBuilder), errorResponseBuilder);
    // Test success path
    RestLiResponseData<?> responseData = responseHandler.buildRestLiResponseData(mockRequest, mockRoutingResult, responseObject);
    responseData.getResponseEnvelope().getResponseMetadata().put(TEST_META_DATA_ELEMENT_KEY, TEST_META_DATA_ELEMENT);
    RestLiResponse response = responseHandler.buildPartialResponse(mockRoutingResult, responseData);
    assertEquals(response.getEntity() != null, hasEntity);
    if (hasEntity) {
        if (response.getEntity() instanceof IdResponse) {
            assertNotNull(((IdResponse) response.getEntity()).getId());
        } else {
            assertEquals(response.getEntity().data().get(RestConstants.METADATA_RESERVED_FIELD), TEST_META_DATA);
            assertEquals(response.getEntity().toString(), responseString);
        }
    }
    // Verify metadata is cleared when an exception is set by a filter
    responseData.getResponseEnvelope().setExceptionInternal(new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR));
    assertEquals(responseData.getResponseEnvelope().getResponseMetadata().size(), 0);
    RestLiResponse errorResponse = responseHandler.buildPartialResponse(mockRoutingResult, responseData);
    assertNull(errorResponse.getEntity().data().get(RestConstants.METADATA_RESERVED_FIELD));
    // Test case where resource method returns exception path
    RestLiResponseData<?> errorResponseData = responseHandler.buildExceptionResponseData(mockRoutingResult, new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR), new HashMap<>(), new ArrayList<>());
    responseData.getResponseEnvelope().getResponseMetadata().put(TEST_META_DATA_ELEMENT_KEY, TEST_META_DATA_ELEMENT);
    errorResponse = responseHandler.buildPartialResponse(mockRoutingResult, responseData);
    assertNull(errorResponse.getEntity().data().get(RestConstants.METADATA_RESERVED_FIELD));
}
Also used : DefaultMethodAdapterProvider(com.linkedin.restli.internal.server.methods.DefaultMethodAdapterProvider) IdResponse(com.linkedin.restli.common.IdResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 3 with DefaultMethodAdapterProvider

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

the class TestRestLiMethodInvocation method setUp.

@BeforeTest
protected void setUp() {
    _scheduler = Executors.newSingleThreadScheduledExecutor();
    _engine = new EngineBuilder().setTaskExecutor(_scheduler).setTimerScheduler(_scheduler).build();
    _resourceFactory = new EasyMockResourceFactory();
    _errorResponseBuilder = new ErrorResponseBuilder();
    _methodAdapterProvider = new DefaultMethodAdapterProvider(_errorResponseBuilder);
    // Add filters to the invoker.
    _invoker = new RestLiMethodInvoker(_resourceFactory, _engine, ErrorResponseBuilder.DEFAULT_INTERNAL_ERROR_MESSAGE);
}
Also used : DefaultMethodAdapterProvider(com.linkedin.restli.internal.server.methods.DefaultMethodAdapterProvider) RestLiMethodInvoker(com.linkedin.restli.internal.server.RestLiMethodInvoker) ErrorResponseBuilder(com.linkedin.restli.internal.server.response.ErrorResponseBuilder) EngineBuilder(com.linkedin.parseq.EngineBuilder) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

DefaultMethodAdapterProvider (com.linkedin.restli.internal.server.methods.DefaultMethodAdapterProvider)3 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)2 ErrorResponseBuilder (com.linkedin.restli.internal.server.response.ErrorResponseBuilder)2 DataMap (com.linkedin.data.DataMap)1 EngineBuilder (com.linkedin.parseq.EngineBuilder)1 RequestContext (com.linkedin.r2.message.RequestContext)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 IdResponse (com.linkedin.restli.common.IdResponse)1 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)1 ResourceMethod (com.linkedin.restli.common.ResourceMethod)1 RestLiMethodInvoker (com.linkedin.restli.internal.server.RestLiMethodInvoker)1 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)1 CreateArgumentBuilder (com.linkedin.restli.internal.server.methods.arguments.CreateArgumentBuilder)1 RestLiArgumentBuilder (com.linkedin.restli.internal.server.methods.arguments.RestLiArgumentBuilder)1 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)1 RestLiConfig (com.linkedin.restli.server.RestLiConfig)1 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)1 RestLiRequestDataImpl (com.linkedin.restli.server.RestLiRequestDataImpl)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 URI (java.net.URI)1