/** * 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(); }
@VisibleForTesting static int compareMediaType(MediaType m1, MediaType m2) { // The order should be "q=1.0, q=0.5". // To ensure descending order, we pass the q values of m2 and m1 respectively. final int qCompare = Float.compare(m2.qualityFactor(), m1.qualityFactor()); if (qCompare != 0) { return qCompare; } // The order should be "application/*, */*". final int wildcardCompare = Integer.compare(m1.numWildcards(), m2.numWildcards()); if (wildcardCompare != 0) { return wildcardCompare; } // Finally, sort by lexicographic order. ex, application/*, image/* return m1.type().compareTo(m2.type()); }
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. }