@Override public String getContentType() { return (contentType != null ? contentType.toString() : null); }
/** * Reads the charset directly from the Content-Type header string. If the Content-Type header does not contain a charset, * is malformed or unparsable, or if the header is null or empty, this method returns null. * * @param contentTypeHeader the Content-Type header string; can be null or empty * @return the character set indicated in the contentTypeHeader, or null if the charset is not present or is not parsable * @throws UnsupportedCharsetException if there is a charset specified in the content-type header, but it is not supported on this platform */ public static Charset readCharsetInContentTypeHeader(String contentTypeHeader) throws UnsupportedCharsetException { if (contentTypeHeader == null || contentTypeHeader.isEmpty()) { return null; } MediaType mediaType; try { mediaType = MediaType.parse(contentTypeHeader); } catch (IllegalArgumentException e) { log.info("Unable to parse Content-Type header: {}. Content-Type header will be ignored.", contentTypeHeader, e); return null; } try { return mediaType.charset().orNull(); } catch (java.nio.charset.UnsupportedCharsetException e) { throw new UnsupportedCharsetException(e); } }
private static boolean isText(final String type) { try { MediaType mediaType = MediaType.parse(type); return mediaType.is(MediaType.ANY_TEXT_TYPE) || mediaType.subtype().endsWith("javascript") || mediaType.subtype().endsWith("json") || mediaType.subtype().endsWith("xml") || mediaType.is(MediaType.FORM_DATA) || mediaType.subtype().endsWith("form-data"); } catch (Exception e) { return false; } }
@Override public MediaType getContentType(final HttpRequest request) { return MediaType.create("application", "json").withCharset(Charset.defaultCharset()); }
private void decideMimeAndEncoding(HttpServletRequest req, HttpServletResponse resp) { //Decide mimetype String mime = req.getServletContext().getMimeType(req.getRequestURI()); MediaType mediaType = (mime == null) ? DEFAULT_MEDIA_TYPE : MediaType.parse(mime); if (defaultCharset != null && mediaType.is(MediaType.ANY_TEXT_TYPE)) { mediaType = mediaType.withCharset(defaultCharset); } resp.setContentType(mediaType.type() + '/' + mediaType.subtype()); if (mediaType.charset().isPresent()) { resp.setCharacterEncoding(mediaType.charset().get().toString()); } }
jsonParser.nextToken(); try { contentType = MediaType.parse(jsonParser.getText()); } catch (IllegalArgumentException uce) { mockServerLogger.trace("Ignoring unsupported MediaType with value \"" + jsonParser.getText() + "\""); jsonParser.nextToken(); try { charset = Charset.forName(jsonParser.getText()); } catch (UnsupportedCharsetException uce) { mockServerLogger.trace("Ignoring unsupported Charset with value \"" + jsonParser.getText() + "\"");
@Test public void should_have_charset_for_file() { Charset gbk = Charset.forName("gbk"); FileContentType contentType = new FileContentType("result.response", of(gbk)); assertThat(contentType.getContentType(), is(MediaType.create("text", "plain").withCharset(gbk))); }
public MediaType getContentType() { Optional<MediaType> optionalType = toContentType(Files.getFileExtension(filename)); Optional<Charset> targetCharset = toCharset(optionalType); MediaType type = optionalType.or(DEFAULT_CONTENT_TYPE_WITH_CHARSET); if (targetCharset.isPresent() && !type.charset().equals(targetCharset)) { return type.withCharset(targetCharset.get()); } return type; }
private ContentType getContentType(final HttpRequest request) { MediaType type = content.getContentType(request); return ContentType.create(type.type() + "/" + type.subtype(), type.charset().or(Charset.defaultCharset())); }
/** * Creates a new media type with the given type and subtype. * * @throws IllegalArgumentException if type or subtype is invalid or if a wildcard is used for the * type, but not the subtype. */ public static MediaType create(String type, String subtype) { MediaType mediaType = create(type, subtype, ImmutableListMultimap.<String, String>of()); mediaType.parsedCharset = Optional.absent(); return mediaType; }
public String getCharset() { // Content_Type cannot have multiple, commas-separated values, so this is safe. Iterator<String> header = _headers.get(HttpHeaders.CONTENT_TYPE).iterator(); if (!header.hasNext()) { return DEFAULT_CHARSET; } else { return MediaType.parse(header.next()).charset().or(Charsets.UTF_8).toString(); } }
tokenizer.consumeCharacter('/'); String subtype = tokenizer.consumeToken(TOKEN_MATCHER); ImmutableListMultimap.Builder<String, String> parameters = ImmutableListMultimap.builder(); while (tokenizer.hasMore()) { tokenizer.consumeTokenIfPresent(LINEAR_WHITE_SPACE); value = tokenizer.consumeToken(TOKEN_MATCHER); parameters.put(attribute, value); return create(type, subtype, parameters.build()); } catch (IllegalStateException e) { throw new IllegalArgumentException("Could not parse '" + input + "'", e);
Request request = preparePost() .setUri(uriBuilder.build()) .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()) .setBodyGenerator(createStaticBodyGenerator(taskUpdateRequestJson)) .build(); ListenableFuture<JsonResponse<TaskInfo>> future = httpClient.executeAsync(request, createFullJsonResponseHandler(taskInfoCodec)); currentRequest = future; currentRequestStartNanos = System.nanoTime();
public String getContentType() { // Content_Type cannot have multiple, commas-separated values, so this is safe. Iterator<String> header = _headers.get(HttpHeaders.CONTENT_TYPE).iterator(); if (!header.hasNext()) { return DEFAULT_CONTENT_TYPE; } else { return MediaType.parse(header.next()).withoutParameters().toString(); } } }
public static List<Value> mimeTypes() { return ImmutableList.of( new StringValue(MediaType.ANY_TYPE.withoutParameters().toString()), new StringValue(MediaType.ANY_TYPE.withoutParameters().toString()), new StringValue(MediaType.ANY_TEXT_TYPE.withoutParameters().toString()), new StringValue(MediaType.ANY_IMAGE_TYPE.withoutParameters().toString()), new StringValue(MediaType.ANY_AUDIO_TYPE.withoutParameters().toString()), new StringValue(MediaType.ANY_VIDEO_TYPE.withoutParameters().toString()), new StringValue(MediaType.ANY_APPLICATION_TYPE.withoutParameters().toString()), new StringValue(MediaType.CACHE_MANIFEST_UTF_8.withoutParameters().toString()), new StringValue(MediaType.CSS_UTF_8.withoutParameters().toString()), new StringValue(MediaType.CSV_UTF_8.withoutParameters().toString()), new StringValue(MediaType.HTML_UTF_8.withoutParameters().toString()), new StringValue(MediaType.I_CALENDAR_UTF_8.withoutParameters().toString()), new StringValue(MediaType.PLAIN_TEXT_UTF_8.withoutParameters().toString()), new StringValue(MediaType.TEXT_JAVASCRIPT_UTF_8.withoutParameters().toString()), new StringValue(MediaType.TSV_UTF_8.withoutParameters().toString()), new StringValue(MediaType.VCARD_UTF_8.withoutParameters().toString()), new StringValue(MediaType.WML_UTF_8.withoutParameters().toString()), new StringValue(MediaType.XML_UTF_8.withoutParameters().toString()), new StringValue(MediaType.BMP.withoutParameters().toString()), new StringValue(MediaType.CRW.withoutParameters().toString()), new StringValue(MediaType.GIF.withoutParameters().toString()), new StringValue(MediaType.ICO.withoutParameters().toString()), new StringValue(MediaType.JPEG.withoutParameters().toString()), new StringValue(MediaType.PNG.withoutParameters().toString()), new StringValue(MediaType.PSD.withoutParameters().toString()), new StringValue(MediaType.SVG_UTF_8.withoutParameters().toString()), new StringValue(MediaType.TIFF.withoutParameters().toString()), new StringValue(MediaType.WEBP.withoutParameters().toString()),
public void testEquals() { new EqualsTester() .addEqualityGroup( MediaType.create("text", "plain"), MediaType.create("TEXT", "PLAIN"), MediaType.parse("text/plain"), MediaType.parse("TEXT/PLAIN"), MediaType.create("text", "plain").withParameter("a", "1").withoutParameters()) .addEqualityGroup( MediaType.create("text", "plain").withCharset(UTF_8), MediaType.create("text", "plain").withParameter("CHARSET", "UTF-8"), MediaType.create("text", "plain") .withParameters(ImmutableMultimap.of("charset", "utf-8")), MediaType.parse("text/plain;charset=utf-8"), MediaType.parse("text/plain; charset=utf-8"), MediaType.parse("text/plain; charset=utf-8"), MediaType.parse("text/plain; \tcharset=utf-8"), MediaType.parse("text/plain; \r\n\tcharset=utf-8"), MediaType.parse("text/plain; CHARSET=utf-8"), MediaType.parse("text/plain; charset=\"utf-8\""), MediaType.parse("text/plain; charset=\"\\u\\tf-\\8\""), MediaType.parse("text/plain; charset=UTF-8"), MediaType.parse("text/plain ; charset=utf-8")) .addEqualityGroup(MediaType.parse("text/plain; charset=utf-8; charset=utf-8")) .addEqualityGroup( MediaType.create("text", "plain").withParameter("a", "value"), MediaType.create("text", "plain").withParameter("A", "value")) .addEqualityGroup( MediaType.create("text", "plain").withParameter("a", "VALUE"), MediaType.create("text", "plain").withParameter("A", "VALUE"))