public Request bodyForm(final Iterable <? extends NameValuePair> formParams, final Charset charset) { final List<NameValuePair> paramList = new ArrayList<>(); for (final NameValuePair param : formParams) { paramList.add(param); } final ContentType contentType = ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset); final String s = URLEncodedUtils.format(paramList, charset); return bodyString(s, contentType); }
/** * Creates a new instance of {@link ContentType} without a charset. * * @param mimeType MIME type. It may not be {@code null} or empty. It may not contain * characters {@code <">, <;>, <,>} reserved by the HTTP specification. * @return content type */ public static ContentType create(final String mimeType) { return create(mimeType, (Charset) null); }
/** * Creates a new instance of {@link ContentType} without a charset. * * @param mimeType MIME type. It may not be {@code null} or empty. It may not contain * characters {@code <">, <;>, <,>} reserved by the HTTP specification. * @return content type */ public static ContentType create(final String mimeType) { return create(mimeType, (Charset) null); }
/** * 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); }
/** * @since 4.4 */ public MultipartEntityBuilder setMimeSubtype(final String subType) { Args.notBlank(subType, "MIME subtype"); this.contentType = ContentType.create("multipart/" + subType); return this; }
/** * 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); }
/** * Creates a new instance of {@link ContentType}. * * @param mimeType MIME type. It may not be {@code null} or empty. It may not contain * characters {@code <">, <;>, <,>} reserved by the HTTP specification. * @param charset charset. It may not contain characters {@code <">, <;>, <,>} reserved by the HTTP * specification. This parameter is optional. * @return content type * @throws UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine */ public static ContentType create( final String mimeType, final String charset) throws UnsupportedCharsetException { return create(mimeType, !TextUtils.isBlank(charset) ? Charset.forName(charset) : null); }
public Request bodyForm(final Iterable <? extends NameValuePair> formParams, final Charset charset) { final List<NameValuePair> paramList = new ArrayList<>(); for (final NameValuePair param : formParams) { paramList.add(param); } final ContentType contentType = ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset); final String s = URLEncodedUtils.format(paramList, charset); return bodyString(s, contentType); }
private static ContentType parse(final CharSequence s, final boolean strict) throws UnsupportedCharsetException { if (TextUtils.isBlank(s)) { return null; } final ParserCursor cursor = new ParserCursor(0, s.length()); final HeaderElement[] elements = BasicHeaderValueParser.INSTANCE.parseElements(s, cursor); if (elements.length > 0) { return create(elements[0], strict); } return null; }
private static ContentType parse(final CharSequence s, final boolean strict) throws UnsupportedCharsetException { if (TextUtils.isBlank(s)) { return null; } final ParserCursor cursor = new ParserCursor(0, s.length()); final HeaderElement[] elements = BasicHeaderValueParser.INSTANCE.parseElements(s, cursor); if (elements.length > 0) { return create(elements[0], strict); } return null; }
private static ContentType create(final HeaderElement helem, final boolean strict) { final String mimeType = helem.getName(); if (TextUtils.isBlank(mimeType)) { return null; } return create(helem.getName(), helem.getParameters(), strict); }
private static ContentType create(final HeaderElement helem, final boolean strict) { final String mimeType = helem.getName(); if (TextUtils.isBlank(mimeType)) { return null; } return create(helem.getName(), helem.getParameters(), strict); }
@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 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 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 testDefaultContent() throws Exception { final String s = "Message content"; StringEntity httpentity = new StringEntity(s, ContentType.create("text/csv", "ANSI_X3.4-1968")); Assert.assertEquals("text/csv; charset=US-ASCII", httpentity.getContentType()); httpentity = new StringEntity(s, StandardCharsets.US_ASCII); Assert.assertEquals("text/plain; charset=US-ASCII", httpentity.getContentType()); httpentity = new StringEntity(s); Assert.assertEquals("text/plain; charset=ISO-8859-1", httpentity.getContentType()); }
@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 testNullCharset() throws Exception { final String s = constructString(SWISS_GERMAN_HELLO); StringEntity httpentity = new StringEntity(s, ContentType.create("text/plain", (Charset) null)); Assert.assertNotNull(httpentity.getContentType()); Assert.assertEquals("text/plain", httpentity.getContentType()); Assert.assertEquals(s, EntityUtils.toString(httpentity)); httpentity = new StringEntity(s, (Charset) null); Assert.assertNotNull(httpentity.getContentType()); Assert.assertEquals("text/plain", httpentity.getContentType()); Assert.assertEquals(s, EntityUtils.toString(httpentity)); }