You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2014/06/07 16:33:22 UTC
svn commit: r1601110 - in /httpcomponents/httpcore/trunk/httpcore/src:
main/java/org/apache/http/entity/ContentType.java
test/java/org/apache/http/entity/TestContentType.java
Author: olegk
Date: Sat Jun 7 14:33:22 2014
New Revision: 1601110
URL: http://svn.apache.org/r1601110
Log:
HTTPCORE-382: lenient methods to obtain ContentType from HttpEntity
Modified:
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java
Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java?rev=1601110&r1=1601109&r2=1601110&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java Sat Jun 7 14:33:22 2014
@@ -104,11 +104,11 @@ public final class ContentType implement
ContentType(
final String mimeType,
- final NameValuePair[] params) throws UnsupportedCharsetException {
+ final Charset charset,
+ final NameValuePair[] params) {
this.mimeType = mimeType;
+ this.charset = charset;
this.params = params;
- final String s = getParameter("charset");
- this.charset = !TextUtils.isBlank(s) ? Charset.forName(s) : null;
}
public String getMimeType() {
@@ -204,10 +204,27 @@ public final class ContentType implement
return create(mimeType, !TextUtils.isBlank(charset) ? Charset.forName(charset) : null);
}
- private static ContentType create(final HeaderElement helem) {
+ private static ContentType create(final HeaderElement helem, final boolean strict) {
final String mimeType = helem.getName();
final NameValuePair[] params = helem.getParameters();
- return new ContentType(mimeType, params != null && params.length > 0 ? params : null);
+
+ Charset charset = null;
+ for (final NameValuePair param: params) {
+ if (param.getName().equalsIgnoreCase("charset")) {
+ final String s = param.getValue();
+ if (!TextUtils.isBlank(s)) {
+ try {
+ charset = Charset.forName(s);
+ } catch (UnsupportedCharsetException ex) {
+ if (strict) {
+ throw ex;
+ }
+ }
+ }
+ break;
+ }
+ }
+ return new ContentType(mimeType, charset, params != null && params.length > 0 ? params : null);
}
/**
@@ -228,7 +245,7 @@ public final class ContentType implement
final ParserCursor cursor = new ParserCursor(0, s.length());
final HeaderElement[] elements = BasicHeaderValueParser.INSTANCE.parseElements(buf, cursor);
if (elements.length > 0) {
- return create(elements[0]);
+ return create(elements[0], true);
} else {
throw new ParseException("Invalid content type: " + s);
}
@@ -255,7 +272,35 @@ public final class ContentType implement
if (header != null) {
final HeaderElement[] elements = header.getElements();
if (elements.length > 0) {
- return create(elements[0]);
+ return create(elements[0], true);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Extracts <code>Content-Type</code> value from {@link HttpEntity}. Returns <code>null</code>
+ * if not specified or incorrect (could not be parsed)..
+ *
+ * @param entity HTTP entity
+ * @return content type
+ *
+ * @since 4.4
+ *
+ */
+ public static ContentType getLenient(final HttpEntity entity) {
+ if (entity == null) {
+ return null;
+ }
+ final Header header = entity.getContentType();
+ if (header != null) {
+ try {
+ final HeaderElement[] elements = header.getElements();
+ if (elements.length > 0) {
+ return create(elements[0], false);
+ }
+ } catch (ParseException ex) {
+ return null;
}
}
return null;
@@ -279,6 +324,21 @@ public final class ContentType implement
}
/**
+ * Extracts <code>Content-Type</code> value from {@link HttpEntity} or returns the default value
+ * {@link #DEFAULT_TEXT} if not explicitly specified or incorrect (could not be parsed).
+ *
+ * @param entity HTTP entity
+ * @return content type
+ *
+ * @since 4.4
+ */
+ public static ContentType getLenientOrDefault(
+ final HttpEntity entity) throws ParseException, UnsupportedCharsetException {
+ final ContentType contentType = get(entity);
+ return contentType != null ? contentType : DEFAULT_TEXT;
+ }
+
+ /**
* Creates a new instance with this MIME type and the given Charset.
*
* @param charset charset
Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java?rev=1601110&r1=1601109&r2=1601110&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java Sat Jun 7 14:33:22 2014
@@ -28,6 +28,7 @@
package org.apache.http.entity;
import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
import org.apache.http.Header;
import org.apache.http.ParseException;
@@ -185,4 +186,33 @@ public class TestContentType {
Assert.assertNull(contentType.getCharset());
}
+ @Test(expected = UnsupportedCharsetException.class)
+ public void testExtractInvalidCharset() throws Exception {
+ final BasicHttpEntity httpentity = new BasicHttpEntity();
+ httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset = stuff"));
+ ContentType.get(httpentity);
+ }
+
+ @Test
+ public void testExtracLenienttNullInput() throws Exception {
+ Assert.assertNull(ContentType.getLenient(null));
+ }
+
+ @Test
+ public void testExtractLenientNullContentType() throws Exception {
+ final BasicHttpEntity httpentity = new BasicHttpEntity();
+ httpentity.setContentType((Header) null);
+ Assert.assertNull(ContentType.getLenient(httpentity));
+ }
+
+ @Test
+ public void testLenientExtractInvalidCharset() throws Exception {
+ final BasicHttpEntity httpentity = new BasicHttpEntity();
+ httpentity.setContentType(new BasicHeader("Content-Type", "text/plain; charset = stuff"));
+ final ContentType contentType = ContentType.getLenient(httpentity);
+ Assert.assertNotNull(contentType);
+ Assert.assertEquals("text/plain", contentType.getMimeType());
+ Assert.assertEquals(null, contentType.getCharset());
+ }
+
}