/** * Middleware that adds the ability to set the response's Content-Type header to a defined * value. * * This middleware is type-unsafe, and it might be a better idea to set the content type directly * in your own middleware that does response serialization. */ public static Middleware<AsyncHandler<?>, AsyncHandler<Response<?>>> replyContentType( String contentType) { return inner -> inner .map(Middlewares::ensureResponse) .map(response -> response.withHeader(CONTENT_TYPE, contentType)); }
/** * Middleware that serializes the payload of the result response of the inner handler using * the supplied {@link ObjectWriter}, and sets the Content-Type header to application/json. */ public static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<ByteString>>> jsonSerializeResponse(ObjectWriter objectWriter) { return handler -> requestContext -> handler.invoke(requestContext) .thenApply(response -> serialize(objectWriter, response) .withHeader(CONTENT_TYPE, JSON)); }
private <R> Function<Response<R>, Response<ByteString>> serialize( RequestContext rc, Class<? extends R> entityClass) { return response -> { final Optional<R> entityOpt = response.payload(); if (!entityOpt.isPresent()) { //noinspection unchecked return (Response<ByteString>) response; } final EncodedResponse encoded; try { encoded = codec.write(entityOpt.get(), entityClass, rc); } catch (Throwable e) { LOG.error("error", e); return Response.forStatus( Status.INTERNAL_SERVER_ERROR .withReasonPhrase("Payload serialization failed: " + e.getMessage())); } final Response<ByteString> result = response.withPayload(encoded.data()); return contentType != null ? result.withHeader(CONTENT_TYPE, contentType) : encoded.contentType().isPresent() ? result.withHeader(CONTENT_TYPE, encoded.contentType().get()) : result; }; } }
private static <T> Response<ByteString> serializePayload( Serializer serializer, Request request, Response<T> response) { if (!response.payload().isPresent()) { // no payload, so this cast is safe to do //noinspection unchecked return (Response<ByteString>) response; } final T payloadObject = response.payload().get(); final Serializer.Payload payload = serializer.serialize(request, payloadObject); if (payload.contentType().isPresent()) { response = response.withHeader(CONTENT_TYPE, payload.contentType().get()); } return response.withPayload(payload.byteString()); }
/** * Middleware that serializes the result of the inner handler using the supplied * {@link ObjectWriter}, and sets the Content-Type header to application/json. */ public static <T> Middleware<AsyncHandler<T>, AsyncHandler<Response<ByteString>>> jsonSerialize(ObjectWriter objectWriter) { return handler -> requestContext -> handler.invoke(requestContext) .thenApply(result -> Response .forPayload(serialize(objectWriter, result)) .withHeader(CONTENT_TYPE, JSON)); }
/** * Async middleware for a Response object. * * @param templateName The template name, respective to the "resources" folder * @param <T> The Type of the parameters * @return the middlware */ public static <T> Middleware<AsyncHandler<Response<T>>, AsyncHandler<Response<ByteString>>> htmlSerializeResponse(final String templateName) { return handler -> requestContext -> handler.invoke(requestContext) .thenApply(response -> response .withPayload(serialize(templateName, response.payload().orElse(null))) .withHeader(CONTENT_TYPE, HTML)); }
private static Response<ByteString> applyHttpPayloadSemantics( Request request, Response<ByteString> response) { Response<ByteString> result = response; Optional<ByteString> payload = response.payload(); if (setContentLengthForStatus(response.status())) { int payloadSize = payload.isPresent() ? payload.get().size() : 0; result = result.withHeader("Content-Length", String.valueOf(payloadSize)); } if (!setPayloadForMethod(request.method()) || !setPayloadForStatus(response.status())) { result = result.withPayload(null); } return result; }
static Response<ByteString> transformResponse(com.squareup.okhttp.Response response) throws IOException { final StatusType status = transformStatus(response.code(), Optional.ofNullable(response.message())); Response<ByteString> apolloResponse = Response.forStatus(status); for (Map.Entry<String, List<String>> entry : response.headers().toMultimap().entrySet()) { apolloResponse = apolloResponse.withHeader( entry.getKey(), HEADER_JOINER.join(entry.getValue())); } final byte[] bytes = response.body().bytes(); if (bytes.length > 0) { apolloResponse = apolloResponse.withPayload(ByteString.of(bytes)); } return apolloResponse; }
/** * Async middleware for POJO. * * @param templateName The template name, respective to the "resources" folder * @param <T> The Type of the parameters * @return the middlware */ public static <T> Middleware<AsyncHandler<T>, AsyncHandler<Response<ByteString>>> htmlSerialize( final String templateName) { return handler -> requestContext -> handler.invoke(requestContext) .thenApply(result -> Response .forPayload(serialize(templateName, result)) .withHeader(CONTENT_TYPE, HTML)); }
ongoingRequest.reply( Response.<ByteString>forStatus(statusCode) .withHeader("Allow", Joiner.on(", ").join(methods)));
private <R> Function<Response<R>, Response<ByteString>> serialize( RequestContext rc, Class<? extends R> entityClass) { return response -> { final Optional<R> entityOpt = response.payload(); if (!entityOpt.isPresent()) { //noinspection unchecked return (Response<ByteString>) response; } final EncodedResponse encoded; try { encoded = codec.write(entityOpt.get(), entityClass, rc); } catch (Throwable e) { LOG.error("error", e); return Response.forStatus( Status.INTERNAL_SERVER_ERROR .withReasonPhrase("Payload serialization failed: " + e.getMessage())); } final Response<ByteString> result = response.withPayload(encoded.data()); return contentType != null ? result.withHeader(CONTENT_TYPE, contentType) : encoded.contentType().isPresent() ? result.withHeader(CONTENT_TYPE, encoded.contentType().get()) : result; }; } }
public static Middleware<AsyncHandler<? extends Response<?>>, AsyncHandler<Response<ByteString>>> jsonAsync() { return innerHandler -> innerHandler.map(response -> { if (!response.payload().isPresent()) { // noinspection unchecked return (Response<ByteString>) response; } final Object tPayload = response.payload().get(); try { final byte[] bytes = OBJECT_MAPPER.writeValueAsBytes(tPayload); final ByteString payload = ByteString.of(bytes); return response.withPayload(payload) .withHeader("Content-Type", "application/json"); } catch (JsonProcessingException e) { return Response.forStatus( INTERNAL_SERVER_ERROR.withReasonPhrase( "Failed to serialize response " + e.getMessage())); } }); }
response = r; return response.withHeader(X_REQUEST_ID, requestId); }); } catch (ResponseException e) { return completedFuture(e.<T>getResponse() .withHeader(X_REQUEST_ID, requestId)); } catch (Throwable t) { return completedFuture(Response.<T>forStatus(INTERNAL_SERVER_ERROR .withReasonPhrase(internalServerErrorReason(requestId, t))) .withHeader(X_REQUEST_ID, requestId));