@Override protected boolean matchesSafely(Response<T> item) { return !item.payload().isPresent(); }
@Override protected boolean matchesSafely(Response<T> item) { return item.payload() .map(payloadMatcher::matches) .orElse(false); }
@Override protected void describeMismatchSafely(Response<T> item, Description mismatchDescription) { mismatchDescription.appendText("it contained the payload: ").appendValue(item.payload().get()); } };
@Override protected void describeMismatchSafely(Response<T> item, Description mismatchDescription) { final Optional<T> payload = item.payload(); if (!payload.isPresent()) { mismatchDescription.appendText("there is no payload"); } else { mismatchDescription.appendText("payload "); payloadMatcher.describeMismatch(payload.get(), mismatchDescription); } } };
private static <T> Response<ByteString> serialize(ObjectWriter objectWriter, Response<T> response){ if(!response.payload().isPresent()){ // Same as in com.spotify.apollo.route.Middlewares.serializePayload() // no payload, so this cast is safe to do //noinspection unchecked return (Response<ByteString>) response; } else { return response.withPayload(serialize(objectWriter, response.payload().get())); } }
@Override public void response(Response<ByteString> response) { requestRateCounter.ifPresent(consumer -> consumer.accept(response)); responseSizeHistogram .ifPresent(histogram -> response.payload() .ifPresent(payload -> histogram.update(payload.size()))); sentReplies.mark(); timerContext.ifPresent(timer -> { final long duration = timer.stop(); final long durationMs = TimeUnit.NANOSECONDS.toMillis(duration); durationThresholdTracker.ifPresent(counter -> { counter.markDurationThresholds(durationMs); }); }); StatusType.Family family = response.status().family(); if (family != INFORMATIONAL && family != SUCCESSFUL) { sentErrors.mark(); } if (family == CLIENT_ERROR) { sentErrors4xx.mark(); } if (family == SERVER_ERROR) { sentErrors5xx.mark(); } }
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()); }
private void sendReply(Response<ByteString> response) { if (!replied.compareAndSet(false, true)) { LOGGER.warn("Already replied to ongoing request {} - spurious response {}", request, response); } else { final HttpServletResponse httpResponse = (HttpServletResponse) asyncContext.getResponse(); final StatusType status = response.status(); httpResponse.setStatus(status.code(), status.reasonPhrase()); response.headerEntries().forEach(entry -> httpResponse.addHeader(entry.getKey(), entry.getValue())); response.payload().ifPresent(payload -> { try { payload.write(httpResponse.getOutputStream()); } catch (IOException e) { LOGGER.warn("Failed to write response", e); } }); asyncContext.complete(); logger.accept(this, Optional.of(response)); } }
/** * 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 <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 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; }
@Override protected boolean matchesSafely(Response<T> item) { return !item.payload().isPresent(); }
@Override protected boolean matchesSafely(Response<T> item) { return item.payload() .map(payloadMatcher::matches) .orElse(false); }
ResponseException(Response<?> response, Throwable cause) { super(cause); Preconditions.checkArgument(!response.payload().isPresent()); this.response = response; }
@Override protected void describeMismatchSafely(Response<T> item, Description mismatchDescription) { mismatchDescription.appendText("it contained the payload: ").appendValue(item.payload().get()); } };
@Override protected void describeMismatchSafely(Response<T> item, Description mismatchDescription) { final Optional<T> payload = item.payload(); if (!payload.isPresent()) { mismatchDescription.appendText("there is no payload"); } else { mismatchDescription.appendText("payload "); payloadMatcher.describeMismatch(payload.get(), mismatchDescription); } } };
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())); } }); }
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; }; } }