Search in sources :

Example 31 with RoutingException

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

the class DefaultDocumentationRequestHandler method processDocumentationRequest.

@Override
@SuppressWarnings("fallthrough")
public RestResponse processDocumentationRequest(Request request) {
    final String path = request.getURI().getRawPath();
    final List<UriComponent.PathSegment> pathSegments = UriComponent.decodePath(path, true);
    String prefixSegment = null;
    String actionSegment = null;
    String typeSegment = null;
    String objectSegment = null;
    switch(pathSegments.size()) {
        case 5:
            objectSegment = pathSegments.get(4).getPath();
        case 4:
            typeSegment = pathSegments.get(3).getPath();
        case 3:
            actionSegment = pathSegments.get(2).getPath();
        case 2:
            prefixSegment = pathSegments.get(1).getPath();
    }
    assert (prefixSegment.equals(DOC_PREFIX) || (HttpMethod.valueOf(request.getMethod()) == HttpMethod.OPTIONS));
    final ByteArrayOutputStream out = new ByteArrayOutputStream(BAOS_BUFFER_SIZE);
    final RestLiDocumentationRenderer renderer;
    if (HttpMethod.valueOf(request.getMethod()) == HttpMethod.OPTIONS) {
        renderer = _jsonRenderer;
        renderer.renderResource(prefixSegment, out);
    } else if (HttpMethod.valueOf(request.getMethod()) == HttpMethod.GET) {
        if (!DOC_VIEW_DOCS_ACTION.equals(actionSegment)) {
            throw createRoutingError(path);
        }
        final MultivaluedMap queryMap = UriComponent.decodeQuery(request.getURI().getQuery(), false);
        final List<String> formatList = queryMap.get("format");
        if (formatList == null) {
            renderer = _htmlRenderer;
        } else if (formatList.size() > 1) {
            throw new RoutingException(String.format("\"format\" query parameter must be unique, where multiple are specified: %s", Arrays.toString(formatList.toArray())), HttpStatus.S_400_BAD_REQUEST.getCode());
        } else {
            renderer = (formatList.contains(DOC_JSON_FORMAT) ? _jsonRenderer : _htmlRenderer);
        }
        if (renderer == _htmlRenderer) {
            _htmlRenderer.setJsonFormatUri(UriBuilder.fromUri(request.getURI()).queryParam("format", DOC_JSON_FORMAT).build());
        }
        try {
            if (typeSegment == null || typeSegment.isEmpty()) {
                renderer.renderHome(out);
            } else {
                if (DOC_RESOURCE_TYPE.equals(typeSegment)) {
                    if (objectSegment == null || objectSegment.isEmpty()) {
                        renderer.renderResourceHome(out);
                    } else {
                        renderer.renderResource(objectSegment, out);
                    }
                } else if (DOC_DATA_TYPE.equals(typeSegment)) {
                    if (objectSegment == null || objectSegment.isEmpty()) {
                        renderer.renderDataModelHome(out);
                    } else {
                        renderer.renderDataModel(objectSegment, out);
                    }
                } else {
                    throw createRoutingError(path);
                }
            }
        } catch (RuntimeException e) {
            if (!renderer.handleException(e, out)) {
                throw e;
            }
        }
    } else {
        throw new RoutingException(HttpStatus.S_405_METHOD_NOT_ALLOWED.getCode());
    }
    return new RestResponseBuilder().setStatus(HttpStatus.S_200_OK.getCode()).setHeader(RestConstants.HEADER_CONTENT_TYPE, renderer.getMIMEType()).setEntity(out.toByteArray()).build();
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MultivaluedMap(com.linkedin.jersey.core.util.MultivaluedMap)

Example 32 with RoutingException

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

the class TestCreateArgumentBuilder method testFailure.

@Test(dataProvider = "failureEntityData", dataProviderClass = RestLiArgumentBuilderTestHelper.class)
public void testFailure(String entity) {
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, entity, 1);
    ResourceModel model = RestLiArgumentBuilderTestHelper.getMockResourceModel(MyComplexKey.class, null, false);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(model, 1, null);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, null, 0);
    RestLiArgumentBuilder argumentBuilder = new CreateArgumentBuilder();
    try {
        argumentBuilder.extractRequestData(routingResult, request);
        fail("Expected RoutingException");
    } catch (RoutingException e) {
        assertTrue(e.getMessage().contains("Error parsing entity body"));
    }
    verify(request, model, descriptor, routingResult);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RoutingException(com.linkedin.restli.server.RoutingException) RestRequest(com.linkedin.r2.message.rest.RestRequest) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) Test(org.testng.annotations.Test)

Example 33 with RoutingException

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

the class TestActionArgumentBuilder method testExtractRequestDataFailure.

@Test(dataProvider = "failureData")
public void testExtractRequestDataFailure(String entity, List<Parameter<?>> params, String errorRegEx) {
    RecordDataSchema dataSchema = DynamicRecordMetadata.buildSchema("testAction", params);
    RestRequest request = RestLiArgumentBuilderTestHelper.getMockRequest(false, entity, 3);
    ResourceMethodDescriptor descriptor = RestLiArgumentBuilderTestHelper.getMockResourceMethodDescriptor(null, null, "testAction", dataSchema);
    RoutingResult routingResult = RestLiArgumentBuilderTestHelper.getMockRoutingResult(descriptor, 1, null, 1);
    RestLiArgumentBuilder argumentBuilder = new ActionArgumentBuilder();
    try {
        argumentBuilder.extractRequestData(routingResult, request);
        fail("Expected RoutingException");
    } catch (RoutingException e) {
        assertTrue(e.getMessage().matches(errorRegEx));
    }
    verify(request, descriptor, routingResult);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RoutingException(com.linkedin.restli.server.RoutingException) RestRequest(com.linkedin.r2.message.rest.RestRequest) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Test(org.testng.annotations.Test)

Example 34 with RoutingException

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

the class RestLiJSONDocumentationRenderer method renderResource.

@Override
public void renderResource(String resourceName, OutputStream out) {
    final ResourceSchema resourceSchema = _relationships.getResourceSchemaCollection().getResource(resourceName);
    if (resourceSchema == null) {
        throw new RoutingException(String.format("Resource named '%s' does not exist", resourceName), 404);
    }
    final DataMap outputMap = createEmptyOutput();
    try {
        renderResource(resourceSchema, outputMap);
        _codec.writeMap(outputMap, out);
    } catch (IOException e) {
        throw new RestLiInternalException(e);
    }
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) ResourceSchema(com.linkedin.restli.restspec.ResourceSchema) RestLiInternalException(com.linkedin.restli.internal.server.RestLiInternalException) IOException(java.io.IOException) DataMap(com.linkedin.data.DataMap)

Example 35 with RoutingException

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

the class TestRestLiCallback method testOnErrorOtherExceptionNoFilters.

@SuppressWarnings("unchecked")
@Test(dataProvider = "provideExceptions")
public void testOnErrorOtherExceptionNoFilters(Exception ex) throws Exception {
    ArgumentCaptor<RestLiServiceException> exCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
    RequestExecutionReport executionReport = new RequestExecutionReportBuilder().build();
    RestLiResponseAttachments responseAttachments = new RestLiResponseAttachments.Builder().build();
    PartialRestResponse partialResponse = new PartialRestResponse.Builder().build();
    RestLiServiceException wrappedEx = new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, ex);
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(wrappedEx, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
    responseData.setResponseEnvelope(new GetResponseEnvelope(new EmptyRecord(), responseData));
    RestException restException = new RestException(new RestResponseBuilder().build());
    Map<String, String> inputHeaders = Maps.newHashMap();
    inputHeaders.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, "2.0.0");
    // Set up.
    when(_requestExecutionReportBuilder.build()).thenReturn(executionReport);
    when(_restRequest.getHeaders()).thenReturn(inputHeaders);
    when(_responseHandler.buildExceptionResponseData(eq(_restRequest), eq(_routingResult), exCapture.capture(), anyMap(), anyList())).thenReturn(responseData);
    when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
    when(_responseHandler.buildRestException(wrappedEx, partialResponse)).thenReturn(restException);
    // Invoke.
    _noFilterRestLiCallback.onError(ex, executionReport, _requestAttachmentReader, responseAttachments);
    // Verify.
    verify(_responseHandler).buildExceptionResponseData(eq(_restRequest), eq(_routingResult), exCapture.capture(), anyMap(), anyList());
    verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
    verify(_responseHandler).buildRestException(wrappedEx, partialResponse);
    verify(_callback).onError(restException, executionReport, _requestAttachmentReader, responseAttachments);
    verify(_restRequest, times(1)).getHeaders();
    verifyZeroInteractions(_routingResult);
    verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
    RestLiServiceException restliEx = exCapture.getValue();
    assertNotNull(restliEx);
    if (ex instanceof RoutingException) {
        assertEquals(HttpStatus.fromCode(((RoutingException) ex).getStatus()), restliEx.getStatus());
    } else if (ex instanceof RestLiServiceException) {
        assertEquals(((RestLiServiceException) ex).getStatus(), restliEx.getStatus());
    } else {
        assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx.getStatus());
    }
    assertEquals(ex.getMessage(), restliEx.getMessage());
    if (ex instanceof RestLiServiceException) {
        assertEquals(ex, restliEx);
    } else {
        assertEquals(ex, restliEx.getCause());
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RoutingException(com.linkedin.restli.server.RoutingException) RequestExecutionReportBuilder(com.linkedin.restli.server.RequestExecutionReportBuilder) RestException(com.linkedin.r2.message.rest.RestException) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RequestExecutionReport(com.linkedin.restli.server.RequestExecutionReport) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestLiResponseAttachments(com.linkedin.restli.server.RestLiResponseAttachments) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

RoutingException (com.linkedin.restli.server.RoutingException)41 RestRequest (com.linkedin.r2.message.rest.RestRequest)15 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)15 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)15 Test (org.testng.annotations.Test)15 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)12 DataMap (com.linkedin.data.DataMap)7 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)7 TemplateRuntimeException (com.linkedin.data.template.TemplateRuntimeException)6 ResourceContext (com.linkedin.restli.server.ResourceContext)6 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)6 RequestContext (com.linkedin.r2.message.RequestContext)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 CompoundKey (com.linkedin.restli.common.CompoundKey)5 RestLiRequestData (com.linkedin.restli.server.RestLiRequestData)5 ByteString (com.linkedin.data.ByteString)4 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)4 RestLiInternalException (com.linkedin.restli.internal.server.RestLiInternalException)4 CustomString (com.linkedin.restli.server.custom.types.CustomString)4 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)4