public HttpResponse returnResponse() throws IOException { assertNotConsumed(); try { final HttpEntity entity = this.response.getEntity(); if (entity != null) { final ByteArrayEntity byteArrayEntity = new ByteArrayEntity( EntityUtils.toByteArray(entity)); final ContentType contentType = EntityUtils.getContentTypeOrDefault(entity); byteArrayEntity.setContentType(contentType.toString()); this.response.setEntity(byteArrayEntity); } return this.response; } finally { this.consumed = true; } }
public String asString() { Charset charset = this.type.getCharset(); if (charset == null) { charset = StandardCharsets.ISO_8859_1; } return asString(charset); }
/** * Parses textual representation of {@code Content-Type} value. * * @param s text * @return content type * {@code Content-Type} value. * @throws UnsupportedCharsetException Thrown when the named charset is not available in * this instance of the Java virtual machine */ public static ContentType parse(final CharSequence s) throws UnsupportedCharsetException { return parse(s, true); }
/** * 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); }
@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 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()); }
@Override public final void streamStart( final EntityDetails entityDetails, final FutureCallback<T> resultCallback) throws IOException, HttpException { Args.notNull(resultCallback, "Result callback"); this.resultCallback = resultCallback; try { final ContentType contentType = entityDetails != null ? ContentType.parse(entityDetails.getContentType()) : null; Charset charset = contentType != null ? contentType.getCharset() : null; if (charset == null) { charset = StandardCharsets.US_ASCII; } setCharset(charset); streamStart(contentType); } catch (final UnsupportedCharsetException ex) { throw new UnsupportedEncodingException(ex.getMessage()); } }
@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()); }
MultipartFormEntity buildEntity() { String boundaryCopy = boundary; if (boundaryCopy == null && contentType != null) { boundaryCopy = contentType.getParameter("boundary"); charsetCopy = contentType.getCharset(); contentType.withParameters(params) : ContentType.create("multipart/" + DEFAULT_SUBTYPE, params); final List<FormBodyPart> bodyPartsCopy = bodyParts != null ? new ArrayList<>(bodyParts) : Collections.<FormBodyPart>emptyList();
@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()); }
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); }
@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()); }}
charset = contentType.getCharset(); if (charset == null) { final ContentType defaultContentType = ContentType.getByMimeType(contentType.getMimeType()); charset = defaultContentType != null ? defaultContentType.getCharset() : null;
@Override public void consumeResponse( final HttpResponse response, final EntityDetails entityDetails, final HttpContext context, final FutureCallback<Void> resultCallback) throws HttpException, IOException { status = response.getCode(); resultCallbackRef.set(resultCallback); stats.setVersion(response.getVersion()); final Header serverHeader = response.getFirstHeader(HttpHeaders.SERVER); if (serverHeader != null) { stats.setServerName(serverHeader.getValue()); } if (config.getVerbosity() >= 2) { System.out.println(response.getCode()); } if (entityDetails != null) { if (config.getVerbosity() >= 6) { if (entityDetails.getContentType() != null) { final ContentType contentType = ContentType.parseLenient(entityDetails.getContentType()); charset = contentType.getCharset(); } } } else { streamEnd(null); } }
if (contentType.getCharset() == null) { contentType = contentType.withCharset(defaultCharset); contentType = ContentType.DEFAULT_TEXT.withCharset(defaultCharset);
/** * Creates a new instance of {@link ContentType} with the given parameters. * * @param mimeType MIME type. It may not be {@code null} or empty. It may not contain * characters {@code <">, <;>, <,>} reserved by the HTTP specification. * @param params parameters. * @return content type * * @since 4.4 */ public static ContentType create( final String mimeType, final NameValuePair... params) throws UnsupportedCharsetException { final String type = Args.notBlank(mimeType, "MIME type").toLowerCase(Locale.ROOT); Args.check(valid(type), "MIME type may not contain reserved characters"); return create(mimeType, params, true); }
@Override public String getMimeType() { return this.contentType.getMimeType(); }
final ContentType contentType = ContentType.parseLenient(entityDetails.getContentType()); entityConsumer.streamStart(entityDetails, new FutureCallback<byte[]>() {
/** * Creates a StringEntity with the specified content and charset. The MIME type defaults * to "text/plain". * * @param string content to be used. Not {@code null}. * @param charset character set to be used. May be {@code null}, in which case the default * is {@link StandardCharsets#ISO_8859_1} is assumed * * @throws IllegalArgumentException if the string parameter is null * * @since 4.2 */ public StringEntity(final String string, final Charset charset) { this(string, ContentType.TEXT_PLAIN.withCharset(charset)); }
@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()); }