private static void addRange( List<MediaType> ranges, CharSequence header, int firstCharIdx, int lastCharIdx) { if (lastCharIdx >= 0) { try { ranges.add(MediaType.parse(header.subSequence(firstCharIdx, lastCharIdx + 1).toString())); } catch (IllegalArgumentException e) { // Ignore the malformed media range. } } } }
/** * 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(); } }
@Nullable static MediaType guessFromPath(String path) { requireNonNull(path, "path"); final int dotIdx = path.lastIndexOf('.'); final int slashIdx = path.lastIndexOf('/'); if (dotIdx < 0 || slashIdx > dotIdx) { // No extension return null; } final String extension = Ascii.toLowerCase(path.substring(dotIdx + 1)); final MediaType mediaType = EXTENSION_TO_MEDIA_TYPE.get(extension); if (mediaType != null) { return mediaType; } final String guessedContentType = URLConnection.guessContentTypeFromName(path); return guessedContentType != null ? MediaType.parse(guessedContentType) : null; }
@Nullable @Override public MediaType deserialize(JsonParser p, DeserializationContext ctx) throws IOException { final JsonNode tree = p.getCodec().readTree(p); if (!tree.isTextual()) { ctx.reportInputMismatch(MediaType.class, "media type must be a string."); return null; } final String textValue = tree.textValue(); try { return MediaType.parse(textValue); } catch (IllegalArgumentException unused) { // Failed to parse. ctx.reportInputMismatch(MediaType.class, "malformed media type: " + textValue); return null; } } }
for (String value : ACCEPT_SPLITTER.split(acceptHeader)) { try { final MediaType type = MediaType.parse(value); for (MediaType producibleMediaType : producibleMediaTypes) { if (producibleMediaType.belongsTo(type)) {
@Nullable @Override public MediaType contentType() { final String contentTypeString = get(HttpHeaderNames.CONTENT_TYPE); if (contentTypeString == null) { return null; } final MediaType contentType = this.contentType; if (contentType != null && Ascii.equalsIgnoreCase(contentType.toString(), contentTypeString.trim())) { return contentType; } try { this.contentType = MediaType.parse(contentTypeString); return this.contentType; } catch (IllegalArgumentException unused) { // Invalid media type return null; } }
@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(); }
@Test public void shouldReturnMetadata() { final AggregatedHttpMessage resp = rule.httpClient().get("/saml/metadata").aggregate().join(); assertThat(resp.status()).isEqualTo(HttpStatus.OK); assertThat(resp.headers().contentType()).isEqualTo(MediaType.parse("application/samlmetadata+xml")); assertThat(resp.headers().get(HttpHeaderNames.CONTENT_DISPOSITION)) .contains("attachment; filename=\"saml_metadata.xml\""); // Check ACS URLs for the service provider. final int port = rule.serverAddress().getPort(); assertThat(resp.content().toStringUtf8()) .contains("entityID=\"test-sp\"") .contains("<md:AssertionConsumerService " + "Binding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST\" " + "Location=\"http://dogma-example.linecorp.com:" + port + "/saml/acs/post") .contains("<md:AssertionConsumerService " + "Binding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect\" " + "Location=\"http://dogma-example.linecorp.com:" + port + "/saml/acs/redirect"); } }