/** * 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))); }
/** * Creates a new HTTP response of the specified {@link HttpStatus}. * * @param mediaType the {@link MediaType} of the response content * @param content the content of the response */ static AggregatedHttpMessage of(HttpStatus status, MediaType mediaType, String content) { requireNonNull(status, "status"); requireNonNull(mediaType, "mediaType"); requireNonNull(content, "content"); return of(status, mediaType, content.getBytes(mediaType.charset().orElse(StandardCharsets.UTF_8))); }
/** * Creates a new HTTP request and closes the stream. * * @param method the HTTP method of the request * @param path the path of the request * @param mediaType the {@link MediaType} of the request content * @param content the content of the request */ static HttpRequest of(HttpMethod method, String path, MediaType mediaType, String content) { requireNonNull(content, "content"); requireNonNull(mediaType, "mediaType"); return of(method, path, mediaType, content.getBytes(mediaType.charset().orElse(StandardCharsets.UTF_8))); }
/** * Creates a new HTTP response of the specified {@link HttpStatus} 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 mediaType the {@link MediaType} of the response content * @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(HttpStatus status, MediaType mediaType, String format, Object... args) { return of(status, mediaType, String.format(Locale.ENGLISH, format, args).getBytes( mediaType.charset().orElse(StandardCharsets.UTF_8))); }
/** * Writes the 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 * * @deprecated Use {@link HttpResponse#of(HttpStatus, MediaType, String)}. */ @Deprecated default void respond(HttpStatus status, MediaType mediaType, String content) { requireNonNull(status, "status"); requireNonNull(mediaType, "mediaType"); requireNonNull(content, "content"); respond(status, mediaType, content.getBytes(mediaType.charset().orElse(StandardCharsets.UTF_8))); }
/** * Creates a new HTTP request. * * @param method the HTTP method of the request * @param path the path of the request * @param mediaType the {@link MediaType} of the request content * @param content the content of the request */ static AggregatedHttpMessage of(HttpMethod method, String path, MediaType mediaType, String content) { requireNonNull(method, "method"); requireNonNull(path, "path"); requireNonNull(content, "content"); requireNonNull(mediaType, "mediaType"); return of(method, path, mediaType, content.getBytes(mediaType.charset().orElse(StandardCharsets.UTF_8))); }
/** * Creates a new HTTP response of the specified {@link HttpStatus}. The content of the response is * formatted by {@link String#format(Locale, String, Object...)} with * {@linkplain Locale#ENGLISH English locale}. * * @param mediaType the {@link MediaType} of the response content * @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 AggregatedHttpMessage of(HttpStatus status, MediaType mediaType, String format, Object... args) { requireNonNull(status, "status"); requireNonNull(mediaType, "mediaType"); requireNonNull(format, "format"); requireNonNull(args, "args"); return of(status, mediaType, String.format(Locale.ENGLISH, format, args).getBytes( mediaType.charset().orElse(StandardCharsets.UTF_8))); }
/** * Creates a new HTTP request and closes the stream. The content of the request is formatted by * {@link String#format(Locale, String, Object...)} with {@linkplain Locale#ENGLISH English locale}. * * @param method the HTTP method of the request * @param path the path of the request * @param mediaType the {@link MediaType} of the request content * @param format {@linkplain Formatter the format string} of the request content * @param args the arguments referenced by the format specifiers in the format string */ static HttpRequest of(HttpMethod method, String path, MediaType mediaType, String format, Object... args) { requireNonNull(method, "method"); requireNonNull(path, "path"); return of(method, path, mediaType, String.format(Locale.ENGLISH, format, args).getBytes( mediaType.charset().orElse(StandardCharsets.UTF_8))); }
/** * Writes the HTTP response of the specified {@link HttpStatus} 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 mediaType the {@link MediaType} of the response content * @param format {@linkplain Formatter the format string} of the response content * @param args the arguments referenced by the format specifiers in the format string * * @deprecated Use {@link HttpResponse#of(HttpStatus, MediaType, String, Object...)}. */ @Deprecated default void respond(HttpStatus status, MediaType mediaType, String format, Object... args) { requireNonNull(status, "status"); requireNonNull(mediaType, "mediaType"); requireNonNull(format, "format"); requireNonNull(args, "args"); respond(status, mediaType, String.format(Locale.ENGLISH, format, args).getBytes( mediaType.charset().orElse(StandardCharsets.UTF_8))); }
/** * Creates a new HTTP request. The content of the request is formatted by * {@link String#format(Locale, String, Object...)} with {@linkplain Locale#ENGLISH English locale}. * * @param method the HTTP method of the request * @param path the path of the request * @param mediaType the {@link MediaType} of the request content * @param format {@linkplain Formatter the format string} of the request content * @param args the arguments referenced by the format specifiers in the format string */ static AggregatedHttpMessage of(HttpMethod method, String path, MediaType mediaType, String format, Object... args) { requireNonNull(method, "method"); requireNonNull(path, "path"); requireNonNull(mediaType, "mediaType"); requireNonNull(format, "format"); requireNonNull(args, "args"); return of(method, path, mediaType, String.format(Locale.ENGLISH, format, args).getBytes( mediaType.charset().orElse(StandardCharsets.UTF_8))); }
/** * Converts the specified {@link AggregatedHttpMessage} to an object of {@code expectedResultType}. */ @Override @Nullable public Object convertRequest(ServiceRequestContext ctx, AggregatedHttpMessage request, Class<?> expectedResultType) throws Exception { final MediaType contentType = request.headers().contentType(); if (contentType != null && (contentType.is(MediaType.JSON) || contentType.subtype().endsWith("+json"))) { final ObjectReader reader = readers.computeIfAbsent(expectedResultType, mapper::readerFor); if (reader != null) { final String content = request.content().toString( contentType.charset().orElse(StandardCharsets.UTF_8)); try { return reader.readValue(content); } catch (JsonProcessingException e) { throw new IllegalArgumentException("failed to parse a JSON document: " + e, e); } } } return RequestConverterFunction.fallthrough(); } }
contentType.charset().orElse(StandardCharsets.US_ASCII)); if (!body.isEmpty()) { final Map<String, List<String>> p =
/** * Converts the specified {@link AggregatedHttpMessage} to a {@link String}. */ @Override public Object convertRequest(ServiceRequestContext ctx, AggregatedHttpMessage request, Class<?> expectedResultType) throws Exception { if (expectedResultType == String.class || expectedResultType == CharSequence.class) { final MediaType contentType = request.headers().contentType(); if (contentType != null && contentType.is(MediaType.ANY_TEXT_TYPE)) { // See https://tools.ietf.org/html/rfc2616#section-3.7.1 return request.content().toString( contentType.charset().orElse(StandardCharsets.ISO_8859_1)); } } return RequestConverterFunction.fallthrough(); } }
/** * Creates a {@link SamlParameters} instance with the specified {@link AggregatedHttpMessage}. */ SamlParameters(AggregatedHttpMessage msg) { requireNonNull(msg, "msg"); final MediaType contentType = msg.headers().contentType(); final QueryStringDecoder decoder; if (contentType != null && contentType.belongsTo(MediaType.FORM_DATA)) { final String query = msg.content().toString( contentType.charset().orElse(StandardCharsets.UTF_8)); decoder = new QueryStringDecoder(query, false); } else { final String path = msg.path(); assert path != null : "path"; decoder = new QueryStringDecoder(path, true); } parameters = decoder.parameters(); }
@Override public HttpResponse convertResponse(ServiceRequestContext ctx, HttpHeaders headers, @Nullable Object result, HttpHeaders trailingHeaders) throws Exception { final MediaType mediaType = headers.contentType(); if (mediaType != null) { // @Produces("text/plain") or @ProducesText is specified. if (mediaType.is(MediaType.ANY_TEXT_TYPE)) { // Use 'utf-8' charset by default. final Charset charset = mediaType.charset().orElse(StandardCharsets.UTF_8); // To avoid sending an unfinished text to the client, always aggregate the published strings. if (result instanceof Publisher) { return aggregateFrom((Publisher<?>) result, headers, trailingHeaders, o -> toHttpData(o, charset)); } if (result instanceof Stream) { return aggregateFrom((Stream<?>) result, headers, trailingHeaders, o -> toHttpData(o, charset), ctx.blockingTaskExecutor()); } return HttpResponse.of(headers, toHttpData(result, charset), trailingHeaders); } } else if (result instanceof CharSequence) { return HttpResponse.of(toMutableHeaders(headers).contentType(MediaType.PLAIN_TEXT_UTF_8), HttpData.ofUtf8(((CharSequence) result).toString()), trailingHeaders); } return ResponseConverterFunction.fallthrough(); }
final Charset charset = mediaType.charset().orElse(StandardCharsets.UTF_8);
/** * Creates a {@link SamlParameters} instance with the specified {@link AggregatedHttpMessage}. */ SamlParameters(AggregatedHttpMessage msg) { requireNonNull(msg, "msg"); final MediaType contentType = msg.headers().contentType(); final QueryStringDecoder decoder; if (contentType != null && contentType.belongsTo(MediaType.FORM_DATA)) { final String query = msg.content().toString( contentType.charset().orElse(StandardCharsets.UTF_8)); decoder = new QueryStringDecoder(query, false); } else { final String path = msg.path(); assert path != null : "path"; decoder = new QueryStringDecoder(path, true); } parameters = decoder.parameters(); }