/** * Creates a new HTTP response of OK status with the content as UTF_8 and closes the stream. * * @param content the content of the response */ static HttpResponse of(String content) { return of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, content); }
/** * Creates a new HTTP response of OK status with the content as UTF_8 and closes the stream. * The content of the response is formatted by {@link String#format(Locale, String, Object...)} with * {@linkplain Locale#ENGLISH English locale}. * * @param format {@linkplain Formatter the format string} of the response content * @param args the arguments referenced by the format specifiers in the format string */ static HttpResponse of(String format, Object... args) { return of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, format, args); }
/** * Creates a new HTTP response of the specified headers. */ static HttpResponse of(HttpHeaders headers) { return of(headers, HttpData.EMPTY_DATA); }
/** * Creates a new HTTP response of OK status with the content and closes the stream. * * @param mediaType the {@link MediaType} of the response content * @param content the content of the response */ static HttpResponse of(MediaType mediaType, String content) { return of(HttpStatus.OK, mediaType, content); }
@Override protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.of(HttpStatus.OK, MediaType.JSON_UTF_8, objectMapper.writeValueAsBytes(dropwizardRegistry)); } });
/** * Invoked when {@code req} is throttled. By default, this method responds with the * {@link HttpStatus#SERVICE_UNAVAILABLE} status. */ @Override protected HttpResponse onFailure(ServiceRequestContext ctx, HttpRequest req, @Nullable Throwable cause) throws Exception { return HttpResponse.of(HttpStatus.SERVICE_UNAVAILABLE); } }
@Override protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.of(HttpStatus.OK, MediaType.JSON_UTF_8, objectMapper.writeValueAsBytes(dropwizardRegistry)); } });
/** * Converts this message into a new complete {@link HttpResponse}. * * @deprecated Use {@link HttpResponse#of(AggregatedHttpMessage)}. */ @Deprecated default HttpResponse toHttpResponse() { return HttpResponse.of(this); } }
@Override protected HttpResponse doRead(HttpHeaders headers, long length, Executor fileReadExecutor, ByteBufAllocator alloc) { if (content instanceof ByteBufHolder) { final ByteBufHolder holder = (ByteBufHolder) content; return HttpResponse.of(headers, (HttpData) holder.retainedDuplicate()); } else { return HttpResponse.of(headers, content); } }
@Override public HttpResponse convertResponse(ServiceRequestContext ctx, HttpHeaders headers, @Nullable Object result, HttpHeaders trailingHeaders) throws Exception { if (result == null) { // Ignore the specified headers and return '204 No Content'. return HttpResponse.of(HttpStatus.NO_CONTENT); } return ResponseConverterFunction.fallthrough(); } }
/** * Creates a new HTTP response of the specified {@link HttpStatus} and closes the stream. * * @param mediaType the {@link MediaType} of the response content * @param content the content of the response * @param offset the start offset of {@code content} * @param length the length of {@code content} */ static HttpResponse of(HttpStatus status, MediaType mediaType, byte[] content, int offset, int length) { return of(status, mediaType, HttpData.of(content, offset, length)); }
/** * Returns a new {@link HttpResponseException} instance with the specified {@link AggregatedHttpMessage}. */ public static HttpResponseException of(AggregatedHttpMessage httpMessage) { return of(HttpResponse.of(requireNonNull(httpMessage, "httpMessage"))); }
@Override protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) { return HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, "ok"); } })
/** * Creates a new HTTP response of the specified {@code statusCode} and closes the stream if the * {@link HttpStatusClass} is not {@linkplain HttpStatusClass#INFORMATIONAL informational} (1xx). */ static HttpResponse of(int statusCode) { return of(HttpStatus.valueOf(statusCode)); }
/** * Creates a new HTTP response of the specified {@link HttpStatus} and closes the stream. * * @param mediaType the {@link MediaType} of the response content * @param content the content of the response */ static HttpResponse of(HttpStatus status, MediaType mediaType, String content) { return of(status, mediaType, content.getBytes(mediaType.charset().orElse(StandardCharsets.UTF_8))); }
@Override public HttpResponse convertResponse(ServiceRequestContext ctx, HttpHeaders headers, @Nullable Object result, HttpHeaders trailingHeaders) throws Exception { return HttpResponse.of(HttpStatus.OK); } }
/** * NB: For now we redirect all methods. */ @Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.of(HttpHeaders.of(httpStatus) .set(HttpHeaderNames.LOCATION, locationFunction.apply(ctx))); }
@Override public HttpResponse handleException(RequestContext ctx, HttpRequest req, Throwable cause) { if (cause instanceof IllegalArgumentException) { return HttpResponse.of("exception"); } return ExceptionHandlerFunction.fallthrough(); } }
@Override public void messageRead(ByteBufOrStream message) { // We know there is only one message in total, so don't bother with checking endOfStream // We also know that we don't support compression, so this is always a ByteBuffer. final HttpData unframedContent = new ByteBufHttpData(message.buf(), true); unframedHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, unframedContent.length()); res.complete(HttpResponse.of(unframedHeaders, unframedContent)); }
@Override public HttpResponse loginSucceeded(ServiceRequestContext ctx, AggregatedHttpMessage req, MessageContext<Response> message, @Nullable String sessionIndex, @Nullable String relayState) { return HttpResponse.of(headersWithLocation(firstNonNull(relayState, "/")) .add(HttpHeaderNames.SET_COOKIE, setCookie)); }