Search in sources :

Example 21 with RestLiServiceException

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

the class CustomTypesResource3 method batchUpdate.

@Override
public BatchUpdateResult<CompoundKey, Greeting> batchUpdate(BatchUpdateRequest<CompoundKey, Greeting> entities) {
    Set<CompoundKey> keys = entities.getData().keySet();
    Map<CompoundKey, UpdateResponse> responseMap = new HashMap<>();
    Map<CompoundKey, RestLiServiceException> errorMap = new HashMap<>();
    for (CompoundKey key : keys) {
        responseMap.put(key, new UpdateResponse(HttpStatus.S_201_CREATED));
    }
    return new BatchUpdateResult<>(responseMap);
}
Also used : UpdateResponse(com.linkedin.restli.server.UpdateResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) BatchUpdateResult(com.linkedin.restli.server.BatchUpdateResult) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap)

Example 22 with RestLiServiceException

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

the class ExceptionsResource method create.

/**
 * Responds with an error for requests to create insulting greetings, responds
 * with 201 created for all other requests.
 */
@RestMethod.Create
public CreateResponse create(Greeting g) {
    if (g.hasTone() && g.getTone() == Tone.INSULTING) {
        RestLiServiceException notAcceptableException = new RestLiServiceException(HttpStatus.S_406_NOT_ACCEPTABLE, "I will not tolerate your insolence!");
        DataMap details = new DataMap();
        details.put("reason", "insultingGreeting");
        notAcceptableException.setErrorDetails(new EmptyRecord(details));
        notAcceptableException.setServiceErrorCode(999);
        throw notAcceptableException;
    } else {
        return new CreateResponse(g.getId(), HttpStatus.S_201_CREATED);
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) DataMap(com.linkedin.data.DataMap)

Example 23 with RestLiServiceException

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

the class TestRequestCompression method initClass.

@BeforeClass
public void initClass() throws Exception {
    class CheckRequestCompressionFilter implements RestFilter {

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Map<String, String> requestHeaders = req.getHeaders();
            if (requestHeaders.containsKey(TEST_HELP_HEADER)) {
                String contentEncodingHeader = requestHeaders.get(HttpConstants.CONTENT_ENCODING);
                if (requestHeaders.get(TEST_HELP_HEADER).equals(EXPECT_COMPRESSION)) {
                    if (contentEncodingHeader == null) {
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is not compressed when it should be.");
                    } else if (!contentEncodingHeader.equals("x-snappy-framed")) {
                        // which is always snappy in this test.
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is compressed with " + contentEncodingHeader + " instead of x-snappy-framed.");
                    }
                } else {
                    if (contentEncodingHeader != null) {
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is compressed when it shouldn't be.");
                    }
                }
            }
            nextFilter.onRequest(req, requestContext, wireAttrs);
        }
    }
    // Check that Content-Encoding and Content-Length headers are set correctly by ServerCompressionFilter.
    class CheckHeadersFilter implements RestFilter {

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            if (req.getHeaders().containsKey(HttpConstants.CONTENT_ENCODING)) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Content-Encoding header not removed.");
            }
            if (req.getEntity().length() != Integer.parseInt(req.getHeader(HttpConstants.CONTENT_LENGTH))) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Content-Length header incorrect.");
            }
            nextFilter.onRequest(req, requestContext, wireAttrs);
        }
    }
    final FilterChain fc = FilterChains.empty().addLastRest(new CheckRequestCompressionFilter()).addLastRest(new ServerCompressionFilter(RestLiIntTestServer.supportedCompression)).addLastRest(new CheckHeadersFilter()).addLastRest(new SimpleLoggingFilter());
    super.init(null, fc, false);
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) NextFilter(com.linkedin.r2.filter.NextFilter) ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) FilterChain(com.linkedin.r2.filter.FilterChain) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) BeforeClass(org.testng.annotations.BeforeClass)

Example 24 with RestLiServiceException

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

the class TestResponseCompression method initClass.

@BeforeClass
public void initClass() throws Exception {
    class TestHelperFilter implements Filter {

        @Override
        public CompletableFuture<Void> onRequest(FilterRequestContext requestContext) {
            Map<String, String> requestHeaders = requestContext.getRequestHeaders();
            if (requestHeaders.containsKey(EXPECTED_ACCEPT_ENCODING)) {
                String expected = requestHeaders.get(EXPECTED_ACCEPT_ENCODING);
                if (expected.equals(NONE)) {
                    if (requestHeaders.containsKey(HttpConstants.ACCEPT_ENCODING)) {
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Accept-Encoding header should not be present.");
                    }
                } else {
                    if (!expected.equals(requestHeaders.get(HttpConstants.ACCEPT_ENCODING))) {
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Accept-Encoding header should be " + expected + ", but received " + requestHeaders.get(HttpConstants.ACCEPT_ENCODING));
                    }
                }
            }
            if (requestHeaders.containsKey(EXPECTED_COMPRESSION_THRESHOLD)) {
                if (!requestHeaders.get(EXPECTED_COMPRESSION_THRESHOLD).equals(requestHeaders.get(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD))) {
                    throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Expected " + HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD + " " + requestHeaders.get(EXPECTED_COMPRESSION_THRESHOLD) + ", but received " + requestHeaders.get(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD));
                }
            }
            return CompletableFuture.completedFuture(null);
        }
    }
    // The default compression threshold is between tiny and huge threshold.
    final FilterChain fc = FilterChains.empty().addLastRest(new TestCompressionServer.SaveContentEncodingHeaderFilter()).addLastRest(new ServerCompressionFilter("x-snappy-framed,snappy,gzip,deflate", new CompressionConfig(10000))).addLastRest(new SimpleLoggingFilter());
    super.init(Arrays.asList(new TestHelperFilter()), fc, false);
}
Also used : ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) FilterChain(com.linkedin.r2.filter.FilterChain) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) Filter(com.linkedin.restli.server.filter.Filter) ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) BeforeClass(org.testng.annotations.BeforeClass)

Example 25 with RestLiServiceException

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

the class TestResourceContextImpl method testReturnEntityParameter.

@Test(dataProvider = "returnEntityParameterData")
public void testReturnEntityParameter(String uri, boolean expectReturnEntity, boolean expectException) throws RestLiSyntaxException {
    final ResourceContextImpl context = new ResourceContextImpl(new PathKeysImpl(), new RestRequestBuilder(URI.create(uri)).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.LATEST_PROTOCOL_VERSION.toString()).build(), new RequestContext());
    try {
        final boolean returnEntity = context.isReturnEntityRequested();
        if (expectException) {
            Assert.fail("Exception should be thrown for URI: " + uri);
        }
        Assert.assertEquals(returnEntity, expectReturnEntity, "Resource context was wrong about whether the URI \"" + uri + "\" indicates that the entity should be returned.");
    } catch (RestLiServiceException e) {
        if (!expectException) {
            Assert.fail("Exception should not be thrown for URI: " + uri);
        }
    }
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Aggregations

RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)145 Test (org.testng.annotations.Test)55 HashMap (java.util.HashMap)39 DataMap (com.linkedin.data.DataMap)29 RecordTemplate (com.linkedin.data.template.RecordTemplate)21 Map (java.util.Map)21 RoutingException (com.linkedin.restli.server.RoutingException)20 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)18 UpdateResponse (com.linkedin.restli.server.UpdateResponse)18 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)17 BeforeTest (org.testng.annotations.BeforeTest)17 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)16 ArrayList (java.util.ArrayList)16 RestException (com.linkedin.r2.message.rest.RestException)14 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)13 RestRequest (com.linkedin.r2.message.rest.RestRequest)12 ByteString (com.linkedin.data.ByteString)11 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)11 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)11 BatchResult (com.linkedin.restli.server.BatchResult)11