/** * Sets the default {@link SerializationFormat}. */ public EndpointInfoBuilder defaultFormat(SerializationFormat defaultFormat) { requireNonNull(defaultFormat, "defaultFormat"); return defaultMimeType(defaultFormat.mediaType()); }
@Nullable private SerializationFormat findSerializationFormat(MediaType contentType) { for (SerializationFormat format : allowedSerializationFormatArray) { if (format.isAccepted(contentType)) { return format; } } return null; }
private static boolean validateAcceptHeaders(HttpRequest req, SerializationFormat serializationFormat) { // If accept header is present, make sure it is sane. Currently, we do not support accept // headers with a different format than the content type header. final List<String> acceptHeaders = req.headers().getAll(HttpHeaderNames.ACCEPT); if (!acceptHeaders.isEmpty() && !serializationFormat.mediaTypes().matchHeaders(acceptHeaders).isPresent()) { return false; } return true; }
/** * Finds the {@link SerializationFormat} which is accepted by any of the specified media ranges. */ public static Optional<SerializationFormat> find(MediaType... ranges) { requireNonNull(ranges, "ranges"); if (ranges.length == 0) { return Optional.empty(); } for (SerializationFormat f : values()) { if (f.isAccepted(Arrays.asList(ranges))) { return Optional.of(f); } } return Optional.empty(); }
if (format.isAccepted(contentType)) { framedContentType = GrpcSerializationFormats.PROTO.mediaType(); } else if (contentType.is(MediaType.JSON_UTF_8)) { framedContentType = GrpcSerializationFormats.JSON.mediaType(); } else { return HttpResponse.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE,
/** * Returns the HTTPS URI for the {@link Server}. * * @throws IllegalStateException if the {@link Server} is not started or it did not open an HTTPS port */ public String httpsUri(SerializationFormat serializationFormat, String path) { requireNonNull(serializationFormat, "serializationFormat"); return serializationFormat.uriText() + '+' + httpsUri(path); }
/** * Finds the {@link SerializationFormat} which is accepted by the specified media range. * * @deprecated Use {@link #find(MediaType...)}. */ @Deprecated public static Optional<SerializationFormat> fromMediaType(@Nullable String mediaType) { if (mediaType == null) { return Optional.empty(); } try { return find(MediaType.parse(mediaType)); } catch (IllegalArgumentException e) { // Malformed media type return Optional.empty(); } }
if (format.isAccepted(contentType)) { framedContentType = GrpcSerializationFormats.PROTO.mediaType(); } else if (contentType.is(MediaType.JSON_UTF_8)) { framedContentType = GrpcSerializationFormats.JSON.mediaType(); } else { return HttpResponse.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE,
/** * Returns the HTTP or HTTPS URI for the {@link Server}. * * @throws IllegalStateException if the {@link Server} is not started or * it opened neither HTTP nor HTTPS port */ public String uri(SerializationFormat serializationFormat, String path) { requireNonNull(serializationFormat, "serializationFormat"); return serializationFormat.uriText() + '+' + uri(path); }
@Test public void findByMediaType() { // The 'protocol' parameter has to be case-insensitive. assertThat(find(parse("application/x-thrift; protocol=tbinary"))).containsSame(BINARY); assertThat(find(parse("application/x-thrift;protocol=TCompact"))).containsSame(COMPACT); assertThat(find(parse("application/x-thrift ; protocol=\"TjSoN\""))).containsSame(JSON); // An unknown parameter ('version' in this case) should not be accepted. assertThat(find(parse("application/x-thrift ; version=3;protocol=ttext"))).isEmpty(); // 'charset=utf-8' parameter should be accepted for TJSON and TTEXT. assertThat(find(parse("application/x-thrift; protocol=tjson; charset=utf-8"))).containsSame(JSON); assertThat(find(parse("application/vnd.apache.thrift.json; charset=utf-8"))).containsSame(JSON); assertThat(find(parse("application/x-thrift; protocol=ttext; charset=utf-8"))).containsSame(TEXT); assertThat(find(parse("application/vnd.apache.thrift.text; charset=utf-8"))).containsSame(TEXT); // .. but neither non-UTF-8 charsets: assertThat(find(parse("application/x-thrift; protocol=tjson; charset=us-ascii"))).isEmpty(); assertThat(find(parse("application/vnd.apache.thrift.json; charset=us-ascii"))).isEmpty(); assertThat(find(parse("application/x-thrift; protocol=ttext; charset=us-ascii"))).isEmpty(); assertThat(find(parse("application/vnd.apache.thrift.text; charset=us-ascii"))).isEmpty(); // .. nor binary/compact formats: assertThat(find(parse("application/x-thrift; protocol=tbinary; charset=utf-8"))).isEmpty(); assertThat(find(parse("application/vnd.apache.thrift.binary; charset=utf-8"))).isEmpty(); assertThat(find(parse("application/x-thrift; protocol=tcompact; charset=utf-8"))).isEmpty(); assertThat(find(parse("application/vnd.apache.thrift.compact; charset=utf-8"))).isEmpty(); }
THttpClientDelegate(Client<HttpRequest, HttpResponse> httpClient, SerializationFormat serializationFormat) { this.httpClient = httpClient; this.serializationFormat = serializationFormat; protocolFactory = ThriftProtocolFactories.get(serializationFormat); mediaType = serializationFormat.mediaType(); }
@Nullable private SerializationFormat findSerializationFormat(@Nullable MediaType contentType) { if (contentType == null) { return null; } for (SerializationFormat format : supportedSerializationFormats) { if (format.isAccepted(contentType)) { return format; } } return null; }
/** * Makes sure the specified {@link MediaType} or its compatible one is registered already. */ private static void checkMediaType(Multimap<MediaType, SerializationFormat> simplifiedMediaTypeToFormats, MediaType mediaType) { final MediaType simplifiedMediaType = mediaType.withoutParameters(); for (SerializationFormat format : simplifiedMediaTypeToFormats.get(simplifiedMediaType)) { for (MediaType registeredMediaType : format.mediaTypes()) { checkState(!registeredMediaType.is(mediaType) && !mediaType.is(registeredMediaType), "media type registered already: ", mediaType); } } }
/** * Returns the HTTP URI for the {@link Server}. * * @throws IllegalStateException if the {@link Server} is not started or it did not open an HTTP port */ public String httpUri(SerializationFormat serializationFormat, String path) { requireNonNull(serializationFormat, "serializationFormat"); return serializationFormat.uriText() + '+' + httpUri(path); }
private static void respond(SerializationFormat serializationFormat, HttpData content, CompletableFuture<HttpResponse> res) { res.complete(HttpResponse.of(HttpStatus.OK, serializationFormat.mediaType(), content)); }
/** * Returns whether any of the specified media ranges is accepted by any of the {@link #mediaTypes()} * defined by this format. */ public boolean isAccepted(MediaType first, MediaType... rest) { requireNonNull(first, "first"); requireNonNull(rest, "rest"); return isAccepted(Lists.asList(first, rest)); }
private static boolean validateAcceptHeaders(HttpRequest req, SerializationFormat serializationFormat) { // If accept header is present, make sure it is sane. Currently, we do not support accept // headers with a different format than the content type header. final List<String> acceptHeaders = req.headers().getAll(HttpHeaderNames.ACCEPT); if (!acceptHeaders.isEmpty() && !serializationFormat.mediaTypes().matchHeaders(acceptHeaders).isPresent()) { return false; } return true; }
/** * Returns the {@link Scheme} of the specified {@link SerializationFormat} and {@link SessionProtocol}. * This method returns the same {@link Scheme} instance for the same combination of * {@link SerializationFormat} and {@link SessionProtocol}. */ public static Scheme of(SerializationFormat serializationFormat, SessionProtocol sessionProtocol) { return SCHEMES.get(requireNonNull(serializationFormat, "serializationFormat").uriText() + '+' + requireNonNull(sessionProtocol, "sessionProtocol").uriText()); }
private void doSendHeaders(Metadata unusedGrpcMetadata) { checkState(!sendHeadersCalled, "sendHeaders already called"); checkState(!closeCalled, "call is closed"); final HttpHeaders headers = HttpHeaders.of(HttpStatus.OK); headers.contentType(serializationFormat.mediaType()); if (compressor == null || !messageCompression || clientAcceptEncoding == null) { compressor = Codec.Identity.NONE; } else { final List<String> acceptedEncodingsList = ACCEPT_ENCODING_SPLITTER.splitToList(clientAcceptEncoding); if (!acceptedEncodingsList.contains(compressor.getMessageEncoding())) { // resort to using no compression. compressor = Codec.Identity.NONE; } } messageFramer.setCompressor(compressor); // Always put compressor, even if it's identity. headers.add(GrpcHeaderNames.GRPC_ENCODING, compressor.getMessageEncoding()); if (!advertisedEncodingsHeader.isEmpty()) { headers.add(GrpcHeaderNames.GRPC_ACCEPT_ENCODING, advertisedEncodingsHeader); } sendHeadersCalled = true; res.write(headers); }
@Nullable private SerializationFormat findSerializationFormat(MediaType contentType) { for (SerializationFormat format : allowedSerializationFormatArray) { if (format.isAccepted(contentType)) { return format; } } return null; }