@Override public String getMimeType() { return this.contentType.getMimeType(); }
@Override public String getMediaType() { final String mimeType = this.contentType.getMimeType(); final int i = mimeType.indexOf('/'); if (i != -1) { return mimeType.substring(0, i); } return mimeType; }
@Override public String getSubType() { final String mimeType = this.contentType.getMimeType(); final int i = mimeType.indexOf('/'); if (i != -1) { return mimeType.substring(i + 1); } return null; }
public boolean isSameMimeType(final ContentType contentType) { return contentType != null && mimeType.equalsIgnoreCase(contentType.getMimeType()); }
/** * Creates a new instance with this MIME type and the given Charset name. * * @param charset name * @return a new instance with this MIME type and the given Charset name. * @throws UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine * @since 4.3 */ public ContentType withCharset(final String charset) { return create(this.getMimeType(), charset); }
/** * Creates a new instance with this MIME type and the given Charset. * * @param charset charset * @return a new instance with this MIME type and the given Charset. * @since 4.3 */ public ContentType withCharset(final Charset charset) { return create(this.getMimeType(), charset); }
/** * Creates a new instance with this MIME type and the given Charset name. * * @param charset name * @return a new instance with this MIME type and the given Charset name. * @throws UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine * @since 4.3 */ public ContentType withCharset(final String charset) { return create(this.getMimeType(), charset); }
/** * Creates a new instance with this MIME type and the given Charset. * * @param charset charset * @return a new instance with this MIME type and the given Charset. * @since 4.3 */ public ContentType withCharset(final Charset charset) { return create(this.getMimeType(), charset); }
charset = contentType.getCharset(); if (charset == null) { final ContentType defaultContentType = ContentType.getByMimeType(contentType.getMimeType()); charset = defaultContentType != null ? defaultContentType.getCharset() : null;
@Test public void testWithCharset() throws Exception { ContentType contentType = ContentType.create("text/plain", "US-ASCII"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString()); contentType = contentType.withCharset(Charset.forName("UTF-8")); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals("UTF-8", contentType.getCharset().name()); Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString()); }
@Test public void testLowCaseText() throws Exception { final ContentType contentType = ContentType.create("Text/Plain", "ascii"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); }
@Test public void testParseEmptyCharset() throws Exception { final ContentType contentType = ContentType.parse("text/plain; charset=\" \""); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(null, contentType.getCharset()); }
@Test public void testWithCharsetString() throws Exception { ContentType contentType = ContentType.create("text/plain", "US-ASCII"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString()); contentType = contentType.withCharset(StandardCharsets.UTF_8); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.UTF_8, contentType.getCharset()); Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString()); }
@Test public void testParse() throws Exception { final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\""); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=ascii", contentType.toString()); }
@Test public void testBasis() throws Exception { final ContentType contentType = ContentType.create("text/plain", "US-ASCII"); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString()); }
@Test public void testLenientExtractInvalidCharset() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType("text/plain; charset = stuff"); final ContentType contentType = EntityUtils.getContentTypeLenient(httpentity); Assert.assertNotNull(contentType); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(null, contentType.getCharset()); }}
@Test public void testExtract() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType("text/plain; charset = UTF-8"); final ContentType contentType = EntityUtils.getContentType(httpentity); Assert.assertNotNull(contentType); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.UTF_8, contentType.getCharset()); }}
@Test public void testExtractNoCharset() throws Exception { try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContentType("text/plain; param=yadayada"); final ContentType contentType = EntityUtils.getContentType(httpentity); Assert.assertNotNull(contentType); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertNull(contentType.getCharset()); }}
@Test public void testWithParams() throws Exception { ContentType contentType = ContentType.create("text/plain", new BasicNameValuePair("charset", "UTF-8"), new BasicNameValuePair("p", "this"), new BasicNameValuePair("p", "that")); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.UTF_8, contentType.getCharset()); Assert.assertEquals("text/plain; charset=UTF-8; p=this; p=that", contentType.toString()); contentType = contentType.withParameters( new BasicNameValuePair("charset", "ascii"), new BasicNameValuePair("p", "this and that")); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=ascii; p=\"this and that\"", contentType.toString()); contentType = ContentType.create("text/blah").withParameters( new BasicNameValuePair("p", "blah")); Assert.assertEquals("text/blah", contentType.getMimeType()); Assert.assertEquals(null, contentType.getCharset()); Assert.assertEquals("text/blah; p=blah", contentType.toString()); contentType = ContentType.create("text/blah", StandardCharsets.ISO_8859_1).withParameters( new BasicNameValuePair("p", "blah")); Assert.assertEquals("text/blah", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.ISO_8859_1, contentType.getCharset()); Assert.assertEquals("text/blah; charset=ISO-8859-1; p=blah", contentType.toString()); }
@Test public void testParseMultiparam() throws Exception { final ContentType contentType = ContentType.parse("text/plain; charset=\"ascii\"; " + "p0 ; p1 = \"blah-blah\" ; p2 = \" yada yada \" "); Assert.assertEquals("text/plain", contentType.getMimeType()); Assert.assertEquals(StandardCharsets.US_ASCII, contentType.getCharset()); Assert.assertEquals("text/plain; charset=ascii; p0; p1=blah-blah; p2=\" yada yada \"", contentType.toString()); Assert.assertEquals(null, contentType.getParameter("p0")); Assert.assertEquals("blah-blah", contentType.getParameter("p1")); Assert.assertEquals(" yada yada ", contentType.getParameter("p2")); Assert.assertEquals(null, contentType.getParameter("p3")); }