/** * Returns a name of this {@link MediaType} only consisting of the type and the sub type. */ public String nameWithoutParameters() { return type() + '/' + subtype(); }
/** * Returns the number of wildcards of this {@link MediaType}. */ public int numWildcards() { int numWildcards = 0; if (WILDCARD.equals(type())) { numWildcards++; } if (WILDCARD.equals(subtype())) { numWildcards++; } return numWildcards; }
private static void addMediaTypes(StringJoiner builder, String prefix, List<MediaType> mediaTypes) { if (!mediaTypes.isEmpty()) { final StringBuilder buf = new StringBuilder(); buf.append(prefix).append(':'); for (MediaType t : mediaTypes) { buf.append(t.type()); buf.append('/'); buf.append(t.subtype()); buf.append(','); } buf.setLength(buf.length() - 1); builder.add(buf.toString()); } } }
private static String generateLoggerName(String prefix, Set<HttpMethod> supportedMethods, List<MediaType> consumeTypes, List<MediaType> produceTypes) { final StringJoiner name = new StringJoiner("."); name.add(prefix); name.add(loggerNameJoiner.join(supportedMethods.stream().sorted().iterator())); // The following three cases should be different to each other. // Each name would be produced as follows: // // consumeTypes: text/plain, text/html -> consumes.text_plain.text_html // consumeTypes: text/plain, produceTypes: text/html -> consumes.text_plain.produces.text_html // produceTypes: text/plain, text/html -> produces.text_plain.text_html if (!consumeTypes.isEmpty()) { name.add("consumes"); consumeTypes.forEach(e -> name.add(e.type() + '_' + e.subtype())); } if (!produceTypes.isEmpty()) { name.add("produces"); produceTypes.forEach(e -> name.add(e.type() + '_' + e.subtype())); } return name.toString(); }
/** * 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(); } }
if (mediaType.is(MediaType.JSON) || mediaType.subtype().endsWith("+json")) { final Charset charset = mediaType.charset().orElse(StandardCharsets.UTF_8);
private static Function<Service<HttpRequest, HttpResponse>, HttpEncodingService> contentEncodingDecorator() { return delegate -> new HttpEncodingService(delegate, contentType -> { if ("application".equals(contentType.type())) { final String subtype = contentType.subtype(); switch (subtype) { case "json": case "xml": case "x-thrift": return true; default: return subtype.endsWith("+json") || subtype.endsWith("+xml") || subtype.startsWith("vnd.apache.thrift."); } } return false; }, 1024); // Do not encode if content-length < 1024. }
private static Function<Service<HttpRequest, HttpResponse>, HttpEncodingService> contentEncodingDecorator() { return delegate -> new HttpEncodingService(delegate, contentType -> { if ("application".equals(contentType.type())) { final String subtype = contentType.subtype(); switch (subtype) { case "json": case "xml": case "x-thrift": return true; default: return subtype.endsWith("+json") || subtype.endsWith("+xml") || subtype.startsWith("vnd.apache.thrift."); } } return false; }, 1024); // Do not encode if content-length < 1024. }
private static Function<Service<HttpRequest, HttpResponse>, HttpEncodingService> contentEncodingDecorator() { return delegate -> new HttpEncodingService(delegate, contentType -> { if ("application".equals(contentType.type())) { final String subtype = contentType.subtype(); switch (subtype) { case "json": case "xml": case "x-thrift": return true; default: return subtype.endsWith("+json") || subtype.endsWith("+xml") || subtype.startsWith("vnd.apache.thrift."); } } return false; }, 1024); // Do not encode if content-length < 1024. }