@Override public RestResponse sendRequestImpl(RestRequest request) throws IOException { Future<RestResponse> responseFuture = client.restRequest(request); RestResponse response; try { response = responseFuture.get(); } catch (InterruptedException | ExecutionException e) { // The service may choose to throw an exception as a way to report error Throwable t = e.getCause(); if (t != null && t instanceof RestException) { response = ((RestException) t).getResponse(); } else { throw new IOException(e); } } return response; }
nextFilter.onError(new RestException(restResponse, e), requestContext, wireAttrs);
/** * Create a StreamException based on the RestException * @param restException the rest Exception * @return the StreamException that's created based on rest exception */ public static StreamException toStreamException(final RestException restException) { return new StreamException(toStreamResponse(restException.getResponse()), restException.getMessage(), restException.getCause()); }
@Override public void onDrainComplete() { _topLevelReaderCallback.onStreamError(Messages.toStreamException(RestException.forError(500, "Serious error. " + "There should never be a call to drain part data when decoding the first part in a multipart mime response."))); }
private static void validateHeaders(RestRequest request) throws RestException { boolean supported; try { supported = ContentType.getContentType(request.getHeader(RestConstants.HEADER_CONTENT_TYPE)).isPresent(); } catch (MimeTypeParseException e) { throw RestException.forError(HttpStatus.S_400_BAD_REQUEST.getCode(), "Invalid content type"); } if (!supported) { _log.warn("Unsupported content type: " + request.getHeader(RestConstants.HEADER_CONTENT_TYPE)); // TODO Add back the check for content type. // throw RestException.forError(HttpStatus.S_415_UNSUPPORTED_MEDIA_TYPE.getCode(), "Unsupported content type"); } }
private void handleRequestAndResponseException(Exception e) { if(e instanceof ExecutionException) { if(e.getCause() instanceof RestException) { RestException re = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST Exception Status: " + re.getResponse().getStatus()); } } else { throw new VoldemortException("Unknown HTTP request execution exception: " + e.getMessage(), e); } } else if(e instanceof InterruptedException) { if(logger.isDebugEnabled()) { logger.debug("Operation interrupted : " + e.getMessage(), e); } throw new VoldemortException("Operation interrupted exception: " + e.getMessage(), e); } else if(e instanceof URISyntaxException) { throw new VoldemortException("Illegal HTTP URL " + e.getMessage(), e); } else if(e instanceof UnsupportedEncodingException) { throw new VoldemortException("Illegal Encoding Type " + e.getMessage(), e); } else { throw new VoldemortException("Unknown exception: " + e.getMessage(), e); } }
/** * Factory method to obtain a new instance for a specified HTTP status code with the given cause. * * @param status the HTTP status code for the exception. * @param throwable the throwable to be used as the cause for this exception. * @return a new instance, as described above. */ public static RestException forError(int status, Throwable throwable) { return new RestException(RestStatus.responseForError(status, throwable), throwable); }
if (contentType.getBaseType().equalsIgnoreCase(RestConstants.HEADER_VALUE_MULTIPART_RELATED)) callback.onError(RestException.forError(415, "This server cannot handle requests with a content type of multipart/related")); return true; callback.onError(RestException.forError(406, "This server cannot handle requests with an accept type of multipart/related")); return true; callback.onError(RestException.forError(400, "Unable to parse content or accept types.")); return true;
public String getStoreClientConfigString(List<String> storeNames, String coordinatorUrl) { try { // Create the REST request StringBuilder URIStringBuilder = new StringBuilder().append(coordinatorUrl) .append(URL_SEPARATOR) .append(STORE_CLIENT_CONFIG_OPS) .append(URL_SEPARATOR) .append(Joiner.on(",") .join(storeNames)); RestRequestBuilder requestBuilder = new RestRequestBuilder(new URI(URIStringBuilder.toString())); String timeoutStr = Long.toString(this.config.getTimeoutConfig() .getOperationTimeout(VoldemortOpCode.GET_OP_CODE)); requestBuilder.setMethod(requestType.GET.toString()); requestBuilder.setHeader(RestMessageHeaders.X_VOLD_REQUEST_TIMEOUT_MS, timeoutStr); requestBuilder = setCommonRequestHeader(requestBuilder); RestRequest request = requestBuilder.build(); Future<RestResponse> future = client.restRequest(request); // This will block RestResponse response = future.get(); ByteString entity = response.getEntity(); return entity.asString("UTF-8"); } catch(Exception e) { if(e.getCause() instanceof RestException) { return ((RestException) e.getCause()).getResponse().getEntity().asString("UTF-8"); } handleRequestAndResponseException(e); } return null; }
/** * Factory method to obtain a new instance for the specified HTTP status code. * * @param status the HTTP status code for the exception. * @param detail the detail message to be returned with this exception. * @return a new instance, as described above. */ public static RestException forError(int status, String detail) { return new RestException(RestStatus.responseForStatus(status, detail)); }
@Override public void onStreamError(Throwable throwable) { //At this point this could be a an exception thrown due to malformed data or this could be an exception thrown //due to an invocation of a callback. For example, an exception thrown due to an invocation of a callback could occur when //handleResourceRequest(). Though this should never happen because handleResourceRequest() catches everything //and invokes the corresponding RequestExecutionCallback. if (throwable instanceof MultiPartIllegalFormatException) { //If its an illegally formed request, then we send back 400. _streamResponseCallback.onError(Messages.toStreamException(RestException.forError(400, "Illegally formed multipart payload"))); return; } //Otherwise this is an internal server error. R2 will convert this to a 500 for us. As mentioned this should never happen. _streamResponseCallback.onError(throwable); } }
RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
/** * Factory method to obtain a new instance for a specified HTTP status code with the given cause, and an option * to disable stack trace. Consider setting {@code writableStackTrace} to {@code false} to conserve computation * cost if the stacktrace does not contribute meaningful insights. * * @param status the HTTP status code for the exception. * @param message the exception message for this exception. * @param throwable the throwable to be used as the cause for this exception. * @return a new instance, as described above. * @param writableStackTrace the exception stacktrace is filled in if true; false otherwise. */ public static RestException forError(int status, String message, Throwable throwable, boolean writableStackTrace) { return new RestException(RestStatus.responseForError(status, throwable), message, throwable, writableStackTrace); }
@Override public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) { try { _restHandler.handleRequest(req, requestContext, new TransportCallbackAdapter<RestResponse>(callback)); } catch (Exception e) { callback.onResponse(TransportResponseImpl.<RestResponse>error(RestException.forError(RestStatus.INTERNAL_SERVER_ERROR, e))); } }
RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
@Override public void onSuccess(RestResponse result) { callback.onSuccess(new RestException(result, streamException.getMessage(), streamException.getCause())); } }, addContentLengthHeader);
/** * Extracts individual requests from the given REST request. * * @return a non-empty map of individual requests * @throws RestException if the payload of the RestRequest is ill-formed, contains no individual requests, or contains * more than allowable individual requests. */ private IndividualRequestMap extractIndividualRequests(RestRequest restRequest) throws RestException { validateHeaders(restRequest); DataMap data = DataMapUtils.readMap(restRequest); MultiplexedRequestContent multiplexedRequestContent = DataTemplateUtil.wrap(data, MultiplexedRequestContent.class); IndividualRequestMap individualRequests = multiplexedRequestContent.getRequests(); int totalCount = totalRequestCount(individualRequests); if (totalCount == 0) { throw RestException.forError(HttpStatus.S_400_BAD_REQUEST.getCode(), "No individual requests to process"); } if (totalCount > _maximumRequestsNumber) { throw RestException.forError(HttpStatus.S_400_BAD_REQUEST.getCode(), "The server is configured to serve up to " + _maximumRequestsNumber + " requests, but received " + totalCount); } return individualRequests; }
RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
@Override public Greeting call() throws Exception { throw new RestException(new RestResponseBuilder().setStatus(401).build()); } });
@Override public void handleStreamRequest(final StreamRequest req, final Map<String, String> wireAttrs, final RequestContext requestContext, final TransportCallback<StreamResponse> callback) { try { _streamHandler.handleRequest(req, requestContext, new TransportCallbackAdapter<StreamResponse>(callback)); } catch (Exception e) { final Exception ex = RestException.forError(RestStatus.INTERNAL_SERVER_ERROR, e); callback.onResponse(TransportResponseImpl.<StreamResponse>error(ex)); } } }