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); } }
RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus());
@Override public R2ResponseStatus handleResponse(Request<RestRequest> request, RestResponse response) { R2ResponseStatus status = new R2ResponseStatus(StatusType.OK); int statusCode = response.getStatus(); status.setStatusCode(statusCode); HttpUtils.updateStatusType(status, statusCode, errorCodeWhitelist); if (status.getType() == StatusType.OK) { status.setContent(response.getEntity()); status.setContentType(response.getHeader(CONTENT_TYPE_HEADER)); } else { log.info("Receive an unsuccessful response with status code: " + statusCode); Map<String, String> metadata = Maps.newHashMap(); metadata.put(HttpConstants.STATUS_CODE, String.valueOf(statusCode)); metadata.put(HttpConstants.REQUEST, request.toString()); if (status.getType() != StatusType.CONTINUE) { FailureEventBuilder failureEvent = new FailureEventBuilder(R2_FAILED_REQUEST_EVENT); failureEvent.addAdditionalMetadata(metadata); failureEvent.submit(metricsContext); } else { GobblinTrackingEvent event = new GobblinTrackingEvent(0L, R2_RESPONSE_EVENT_NAMESPACE, R2_FAILED_REQUEST_EVENT, metadata); metricsContext.submitEvent(event); } } return status; } }
RestException exception = (RestException) e.getCause(); if(logger.isDebugEnabled()) { logger.debug("REST EXCEPTION STATUS : " + exception.getResponse().getStatus()); int httpErrorStatus = exception.getResponse().getStatus(); if(httpErrorStatus == BAD_REQUEST.getCode()) { throw new VoldemortException("Bad request: " + e.getMessage(), e);
public RestLiResponseException(RestResponse rawResponse, Response<?> decodedResponse, ErrorResponse errorResponse) { super(rawResponse); _status = rawResponse.getStatus(); _errorResponse = errorResponse; _decodedResponse = decodedResponse; }
public RestLiResponseException(RestResponse rawResponse, Response<?> decodedResponse, ErrorResponse errorResponse, Throwable cause) { super(rawResponse, cause); _status = rawResponse.getStatus(); _errorResponse = errorResponse; _decodedResponse = decodedResponse; }
@Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append(getClass().getName()).append(": Response status "); // This is the HTTP status code of the response builder.append(getResponse().getStatus()); // The Rest.li error details may contain an error message from the server, an error code // from the server if (hasServiceErrorMessage()) { builder.append(", serviceErrorMessage: ").append(getServiceErrorMessage()); } if (hasServiceErrorCode()) { builder.append(", serviceErrorCode: ").append(getServiceErrorCode()); } // TODO: decide whether to include serviceErrorDetails and serverStackTrace. return builder.toString(); // E.g.: // RestLiResponseException: Response status 400, serviceErrorMessage: Illegal content type "application/xml", serviceErrorCode: 999 }
protected void encode(ChannelHandlerContext ctx, RestResponse response, List<Object> out) throws Exception { final ByteString entity = response.getEntity(); ByteBuf content = Unpooled.wrappedBuffer(entity.asByteBuffer()); HttpResponse nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.getStatus()), content); for (Map.Entry<String, String> e : response.getHeaders().entrySet()) { nettyResponse.headers().set(e.getKey(), e.getValue()); } nettyResponse.headers().set(HttpConstants.RESPONSE_COOKIE_HEADER_NAME, response.getCookies()); nettyResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, entity.length()); out.add(nettyResponse); } }
public Response<T> decodeResponse(RestResponse restResponse) throws RestLiDecodingException { return createResponse(restResponse.getHeaders(), restResponse.getStatus(), restResponse.getEntity(), restResponse.getCookies()); }
private boolean replayResponse(RestRequest req, RequestContext requestContext, NextFilter<RestRequest, RestResponse> nextFilter) { final RestResponse res = _db.replay(req); if (res != null) { _log.debug("Using cached response for request: " + req.getURI()); // We create an empty map instead of Collections.emptyMap, because upstream filters may // try to modify the map. final Map<String, String> wireAttrs = new HashMap<String, String>(); // For symmetry with CaptureFilter - if the REST response is "not OK" then we treat it as an // exception. if (!RestStatus.isOK(res.getStatus())) { nextFilter.onError(new RestException(res), requestContext, wireAttrs); } else { nextFilter.onResponse(res, requestContext, wireAttrs); } return true; } return false; } }
private void writeResLine(OutputStream out, Response res) throws IOException { write(out, HTTP_1_1); write(out, SP); int statusCode; if (res instanceof RestResponse) { statusCode = ((RestResponse) res).getStatus(); write(out, Integer.toString(statusCode)); } else { statusCode = 200; write(out, STATUS_200); } write(out, SP); write(out, toReasonPhrase(statusCode)); write(out, CRLF); }
RestException exception = new RestException(individualRestResponse, "Received error " + individualRestResponse.getStatus()); callback.onError(exception);
private static StreamResponse createStreamResponseWithAttachment(RestResponse structuredFirstPart, RestLiResponseAttachments attachments) { //Construct the StreamResponse and invoke the callback. The RestResponse entity should be the first part. //There may potentially be attachments included in the response. Note that unlike the client side request builders, //here it is possible to have a non-null attachment list with 0 attachments due to the way the builder in //RestLiResponseAttachments works. Therefore we have to make sure its a non zero size as well. final ByteStringWriter firstPartWriter = new ByteStringWriter(structuredFirstPart.getEntity()); final MultiPartMIMEWriter multiPartMIMEWriter = AttachmentUtils.createMultiPartMIMEWriter(firstPartWriter, structuredFirstPart.getHeader(RestConstants.HEADER_CONTENT_TYPE), attachments.getMultiPartMimeWriterBuilder()); //Ensure that any headers or cookies from the RestResponse make into the outgoing StreamResponse. The exception //of course being the Content-Type header which will be overridden by MultiPartMIMEStreamResponseFactory. return MultiPartMIMEStreamResponseFactory.generateMultiPartMIMEStreamResponse(AttachmentUtils.RESTLI_MULTIPART_SUBTYPE, multiPartMIMEWriter, Collections.emptyMap(), structuredFirstPart.getHeaders(), structuredFirstPart.getStatus(), structuredFirstPart.getCookies()); }
/** * Returns true if the given throwable indicates a server-side error. */ private boolean isServerError(Throwable throwable) { if (throwable instanceof RestException) { RestException restException = (RestException) throwable; if (restException.getResponse() != null) { return matchErrorStatus(restException.getResponse().getStatus()); } } else if (throwable instanceof StreamException) { StreamException streamException = (StreamException) throwable; if (streamException.getResponse() != null) { return matchErrorStatus(streamException.getResponse().getStatus()); } } // default to false return false; }
@Override public void onResponse(TransportResponse<RestResponse> response) { if (response.hasError()) { response = TransportResponseImpl.error(new RemoteInvocationException("Failed to get response from server for URI " + uri, response.getError()), response.getWireAttributes()); } else if (!RestStatus.isOK(response.getResponse().getStatus())) { response = TransportResponseImpl.error(new RestException(response.getResponse(), "Received error " + response.getResponse() .getStatus() + " from server for URI " + uri), response.getWireAttributes()); } callback.onResponse(response); } };
private static IndividualResponse toIndividualResponse(String id, RestResponse restResponse) throws MimeTypeParseException, IOException { IndividualResponse individualResponse = new IndividualResponse(); individualResponse.setStatus(restResponse.getStatus()); individualResponse.setHeaders(new StringMap(restResponse.getHeaders())); ByteString entity = restResponse.getEntity(); if (!entity.isEmpty()) { // TODO Avoid converting bytes to datamap here. Individual response should have only the bytes. individualResponse.setBody(new IndividualBody(DataMapConverter.bytesToDataMap(restResponse.getHeaders(), entity))); } return individualResponse; }