use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class StreamingGreetings method respondWithResponseAttachment.
private void respondWithResponseAttachment(final Callback<UpdateResponse> callback) {
if (getContext().responseAttachmentsSupported()) {
//Echo the bytes back from the header
final String headerValue = getContext().getRequestHeaders().get("getHeader");
final GreetingWriter greetingWriter = new GreetingWriter(ByteString.copy(headerValue.getBytes()));
final RestLiResponseAttachments streamingAttachments = new RestLiResponseAttachments.Builder().appendSingleAttachment(greetingWriter).build();
getContext().setResponseAttachments(streamingAttachments);
callback.onSuccess(new UpdateResponse(HttpStatus.S_200_OK));
}
callback.onError(new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "You must be able to receive attachments!"));
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class StreamingGreetings method get.
@Override
public void get(Long key, @CallbackParam Callback<Greeting> callback) {
if (getContext().responseAttachmentsSupported()) {
final GreetingWriter greetingWriter = new GreetingWriter(ByteString.copy(greetingBytes));
final RestLiResponseAttachments streamingAttachments = new RestLiResponseAttachments.Builder().appendSingleAttachment(greetingWriter).build();
getContext().setResponseAttachments(streamingAttachments);
final String headerValue = getContext().getRequestHeaders().get("getHeader");
getContext().setResponseHeader("getHeader", headerValue);
callback.onSuccess(new Greeting().setMessage("Your greeting has an attachment since you were kind and " + "decided you wanted to read it!").setId(key));
}
callback.onError(new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "You must be able to receive attachments!"));
}
use of com.linkedin.restli.server.RestLiServiceException in project rest.li by linkedin.
the class ValidationDemoResource method batchUpdate.
@RestMethod.BatchUpdate
public BatchUpdateResult<Integer, ValidationDemo> batchUpdate(final BatchUpdateRequest<Integer, ValidationDemo> entities, @ValidatorParam RestLiDataValidator validator) {
Map<Integer, UpdateResponse> results = new HashMap<Integer, UpdateResponse>();
Map<Integer, RestLiServiceException> errors = new HashMap<Integer, RestLiServiceException>();
for (Map.Entry<Integer, ValidationDemo> entry : entities.getData().entrySet()) {
Integer key = entry.getKey();
ValidationDemo entity = entry.getValue();
ValidationResult result = validator.validateInput(entity);
if (result.isValid()) {
results.put(key, new UpdateResponse(HttpStatus.S_204_NO_CONTENT));
} else {
errors.put(key, new RestLiServiceException(HttpStatus.S_422_UNPROCESSABLE_ENTITY, result.getMessages().toString()));
}
}
return new BatchUpdateResult<Integer, ValidationDemo>(results, errors);
}
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);
}
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);
}
Aggregations