/** * Decodes this data into a {@link String} using UTF-8 encoding. * * @return the decoded {@link String} */ default String toStringUtf8() { return toString(StandardCharsets.UTF_8); }
/** * Decodes this data into a {@link String} using US-ASCII encoding. * * @return the decoded {@link String} */ default String toStringAscii() { return toString(StandardCharsets.US_ASCII); }
/** * 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(); } }
final String body = message.content().toString( contentType.charset().orElse(StandardCharsets.US_ASCII)); if (!body.isEmpty()) {
/** * 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(); }
private static void invoke0(THttpService service, HttpData content, CompletableFuture<HttpData> promise) throws Exception { final HttpRequestWriter req = HttpRequest.streaming(HttpHeaders.of(HttpMethod.POST, "/")); req.write(content); req.close(); final ServiceRequestContext ctx = ServiceRequestContextBuilder.of(req) .eventLoop(eventLoop.get()) .serverConfigurator(builder -> { builder.blockingTaskExecutor(ImmediateEventExecutor.INSTANCE); builder.verboseResponses(true); }) .build(); final HttpResponse res = service.serve(ctx, req); res.aggregate().handle(voidFunction((aReq, cause) -> { if (cause == null) { if (aReq.headers().status().code() == 200) { promise.complete(aReq.content()); } else { promise.completeExceptionally(new AssertionError( aReq.headers().status() + ", " + aReq.content().toString(StandardCharsets.UTF_8))); } } else { promise.completeExceptionally(cause); } })).exceptionally(CompletionActions::log); }
/** * 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(); }