/** * 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); }
/** * 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); }
/** * Parses textual representation of {@code Content-Type} value ignoring invalid charsets. * * @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 parseLenient(final CharSequence s) throws UnsupportedCharsetException { return parse(s, false); }
/** * Parses textual representation of {@code Content-Type} value ignoring invalid charsets. * * @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 parseLenient(final CharSequence s) throws UnsupportedCharsetException { return parse(s, false); }
/** * If it is legal to use cached content in response response to the {@link HttpRequest} then * generate an {@link HttpResponse} based on {@link HttpCacheEntry}. * @param request {@link HttpRequest} to generate the response for * @param entry {@link HttpCacheEntry} to transform into an {@link HttpResponse} * @return {@link SimpleHttpResponse} constructed response */ SimpleHttpResponse generateResponse(final HttpRequest request, final HttpCacheEntry entry) throws ResourceIOException { final Date now = new Date(); final SimpleHttpResponse response = new SimpleHttpResponse(entry.getStatus()); response.setVersion(HttpVersion.DEFAULT); response.setHeaders(entry.getHeaders()); if (responseShouldContainEntity(request, entry)) { final Resource resource = entry.getResource(); final Header h = entry.getFirstHeader(HttpHeaders.CONTENT_TYPE); final ContentType contentType = h != null ? ContentType.parse(h.getValue()) : null; final byte[] content = resource.get(); addMissingContentLengthHeader(response, content); response.setBodyBytes(content, contentType); } final long age = this.validityStrategy.getCurrentAgeSecs(entry, now); if (age > 0) { if (age >= Integer.MAX_VALUE) { response.setHeader(HeaderConstants.AGE, "2147483648"); } else { response.setHeader(HeaderConstants.AGE, "" + ((int) age)); } } return response; }
/** * Extracts {@code Content-Type} value from {@link HttpEntity} exactly as * specified by the {@code Content-Type} header of the entity. Returns {@code null} * if not specified. * * @param entity HTTP entity * @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 getContentType(final HttpEntity entity) throws UnsupportedCharsetException { if (entity == null) { return null; } final String contentType = entity.getContentType(); if (contentType != null) { return ContentType.parse(contentType); } return null; }
@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; streamStart(contentType); } catch (final UnsupportedCharsetException ex) { throw new UnsupportedEncodingException(ex.getMessage()); } }
@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; streamStart(contentType); } catch (final UnsupportedCharsetException ex) { throw new UnsupportedEncodingException(ex.getMessage()); } }
@Override public void completed(final E entity) { final ContentType contentType; try { contentType = ContentType.parse(entityDetails.getContentType()); final T result = buildResult(response, entity, contentType); resultRef.compareAndSet(null, result); if (resultCallback != null) { resultCallback.completed(result); } } catch (final UnsupportedCharsetException ex) { exceptionRef.compareAndSet(null, ex); if (resultCallback != null) { resultCallback.failed(ex); } } }
@Override public void completed(final E entity) { final ContentType contentType; try { contentType = ContentType.parse(entityDetails.getContentType()); final T result = buildResult(response, entity, contentType); resultRef.compareAndSet(null, result); if (resultCallback != null) { resultCallback.completed(result); } } catch (final UnsupportedCharsetException ex) { exceptionRef.compareAndSet(null, ex); if (resultCallback != null) { resultCallback.failed(ex); } } }
@Test public void testParseEmptyValue() throws Exception { Assert.assertEquals(null, ContentType.parse(null)); Assert.assertEquals(null, ContentType.parse("")); Assert.assertEquals(null, ContentType.parse(" ")); Assert.assertEquals(null, ContentType.parse(";")); Assert.assertEquals(null, ContentType.parse("=")); }
@Override public void completed(final E entity) { final ContentType contentType; try { contentType = ContentType.parse(entityDetails.getContentType()); result = buildResult(request, entity, contentType); resultCallback.completed(result); } catch (final UnsupportedCharsetException ex) { resultCallback.failed(ex); } }
@Override public void completed(final E entity) { final ContentType contentType; try { contentType = ContentType.parse(entityDetails.getContentType()); result = buildResult(request, entity, contentType); resultCallback.completed(result); } catch (final UnsupportedCharsetException ex) { resultCallback.failed(ex); } }
@Override public final void consumePromise( final HttpRequest promise, final HttpResponse response, final EntityDetails entityDetails, final HttpContext context) throws HttpException, IOException { if (entityDetails != null) { final ContentType contentType; try { contentType = ContentType.parse(entityDetails.getContentType()); } catch (final UnsupportedCharsetException ex) { throw new UnsupportedEncodingException(ex.getMessage()); } start(promise, response, contentType != null ? contentType : ContentType.DEFAULT_BINARY); } else { start(promise, response, null); completed(); } }
@Override public final void consumeResponse( final HttpResponse response, final EntityDetails entityDetails, final HttpContext context, final FutureCallback<T> resultCallback) throws HttpException, IOException { this.resultCallback = resultCallback; if (entityDetails != null) { try { final ContentType contentType = ContentType.parse(entityDetails.getContentType()); start(response, contentType != null ? contentType : ContentType.DEFAULT_BINARY); } catch (final UnsupportedCharsetException ex) { throw new UnsupportedEncodingException(ex.getMessage()); } } else { start(response, null); completed(); } }
@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()); } }
@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()); }
@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 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")); }