You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2013/09/06 15:14:33 UTC
git commit: Changed content type handling for malformed syntax
Updated Branches:
refs/heads/master 9640c7643 -> 6f5e2b430
Changed content type handling for malformed syntax
Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/commit/6f5e2b43
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/6f5e2b43
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/6f5e2b43
Branch: refs/heads/master
Commit: 6f5e2b4301ffaba40123c02dc61e112bd156cacd
Parents: 9640c76
Author: Michael Bolz <mi...@apache.org>
Authored: Fri Sep 6 15:12:19 2013 +0200
Committer: Michael Bolz <mi...@apache.org>
Committed: Fri Sep 6 15:12:19 2013 +0200
----------------------------------------------------------------------
.../olingo/odata2/core/ContentNegotiator.java | 4 +-
.../olingo/odata2/core/ODataRequestHandler.java | 2 +-
.../olingo/odata2/core/commons/ContentType.java | 49 ++++++++++++++++++--
.../odata2/core/ep/ProviderFacadeImpl.java | 2 +-
.../olingo/odata2/core/rest/RestUtil.java | 7 +--
.../odata2/core/commons/ContentTypeTest.java | 44 ++++++++++++------
6 files changed, 84 insertions(+), 24 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/6f5e2b43/odata-core/src/main/java/org/apache/olingo/odata2/core/ContentNegotiator.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/ContentNegotiator.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/ContentNegotiator.java
index 02bbd23..fbb676d 100644
--- a/odata-core/src/main/java/org/apache/olingo/odata2/core/ContentNegotiator.java
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/ContentNegotiator.java
@@ -56,7 +56,7 @@ public class ContentNegotiator {
if (uriInfo.getFormat() == null) {
contentType = doContentNegotiationForAcceptHeader(acceptHeaderContentTypes, ContentType.create(supportedContentTypes));
} else {
- contentType = doContentNegotiationForFormat(uriInfo, ContentType.create(supportedContentTypes));
+ contentType = doContentNegotiationForFormat(uriInfo, ContentType.createCustom(supportedContentTypes));
}
if (contentType.getODataFormat() == ODataFormat.CUSTOM) {
@@ -106,7 +106,7 @@ public class ContentNegotiator {
return ContentType.APPLICATION_JSON;
}
- return ContentType.create(format);
+ return ContentType.createCustom(format);
}
private ContentType doContentNegotiationForAcceptHeader(final List<String> acceptHeaderContentTypes, final List<ContentType> supportedContentTypes) throws ODataException {
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/6f5e2b43/odata-core/src/main/java/org/apache/olingo/odata2/core/ODataRequestHandler.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/ODataRequestHandler.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/ODataRequestHandler.java
index b61c6c2..38e59a9 100644
--- a/odata-core/src/main/java/org/apache/olingo/odata2/core/ODataRequestHandler.java
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/ODataRequestHandler.java
@@ -422,7 +422,7 @@ public class ODataRequestHandler {
}
private List<ContentType> getSupportedContentTypes(final Class<? extends ODataProcessor> processorFeature) throws ODataException {
- return ContentType.create(service.getSupportedContentTypes(processorFeature));
+ return ContentType.createCustom(service.getSupportedContentTypes(processorFeature));
}
private static String getDebugValue(final ODataContext context, final Map<String, String> queryParameters) {
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/6f5e2b43/odata-core/src/main/java/org/apache/olingo/odata2/core/commons/ContentType.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/commons/ContentType.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/commons/ContentType.java
index 9890607..58579f5 100644
--- a/odata-core/src/main/java/org/apache/olingo/odata2/core/commons/ContentType.java
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/commons/ContentType.java
@@ -123,6 +123,15 @@ public class ContentType {
private Map<String, String> parameters;
private ODataFormat odataFormat;
+ private ContentType(final String type) {
+ if (type == null) {
+ throw new IllegalArgumentException("Type parameter MUST NOT be null.");
+ }
+ this.odataFormat = ODataFormat.CUSTOM;
+ this.type = validateType(type);
+ this.subtype = null;
+ }
+
private ContentType(final String type, final String subtype) {
this(type, subtype, ODataFormat.CUSTOM, null);
}
@@ -239,15 +248,31 @@ public class ContentType {
if (types.contains(TYPE_SUBTYPE_SEPARATOR)) {
String[] tokens = types.split(TYPE_SUBTYPE_SEPARATOR);
if (tokens.length == 2) {
- return create(tokens[0], tokens[1], parametersMap);
+ if(tokens[0] == null || tokens[0].isEmpty()) {
+ throw new IllegalArgumentException("No type found in format '" + format + "'.");
+ } else if(tokens[1] == null || tokens[1].isEmpty()) {
+ throw new IllegalArgumentException("No subtype found in format '" + format + "'.");
+ } else {
+ return create(tokens[0], tokens[1], parametersMap);
+ }
} else {
throw new IllegalArgumentException("Too many '" + TYPE_SUBTYPE_SEPARATOR + "' in format '" + format + "'.");
}
+ } else if(MEDIA_TYPE_WILDCARD.equals(types)) {
+ return ContentType.WILDCARD;
} else {
- return create(types, MEDIA_TYPE_WILDCARD, parametersMap);
+ throw new IllegalArgumentException("No separator '" + TYPE_SUBTYPE_SEPARATOR + "' was found in format '" + format + "'.");
}
}
+ public static ContentType createCustom(final String format) {
+ ContentType parsedContentType = parse(format);
+ if(parsedContentType == null) {
+ return new ContentType(format);
+ }
+ return parsedContentType;
+ }
+
/**
* Create a list of {@link ContentType} based on given input strings (<code>contentTypes</code>).
*
@@ -272,6 +297,14 @@ public class ContentType {
return contentTypes;
}
+ public static List<ContentType> createCustom(final List<String> contentTypeStrings) {
+ List<ContentType> contentTypes = new ArrayList<ContentType>(contentTypeStrings.size());
+ for (String contentTypeString : contentTypeStrings) {
+ contentTypes.add(createCustom(contentTypeString));
+ }
+ return contentTypes;
+ }
+
/**
* Parses the given input string (<code>format</code>) and returns created
* {@link ContentType} if input was valid or return <code>NULL</code> if
@@ -529,7 +562,9 @@ public class ContentType {
return false;
}
} else if (!subtype.equals(other.subtype)) {
- if (!subtype.equals(MEDIA_TYPE_WILDCARD) && !other.subtype.equals(MEDIA_TYPE_WILDCARD)) {
+ if(other.subtype == null) {
+ return false;
+ } else if (!subtype.equals(MEDIA_TYPE_WILDCARD) && !other.subtype.equals(MEDIA_TYPE_WILDCARD)) {
return false;
}
}
@@ -578,7 +613,13 @@ public class ContentType {
*/
public String toContentTypeString() {
StringBuilder sb = new StringBuilder();
- sb.append(type).append(TYPE_SUBTYPE_SEPARATOR).append(subtype);
+
+ if(odataFormat == ODataFormat.CUSTOM && subtype == null) {
+ sb.append(type);
+ } else {
+ sb.append(type).append(TYPE_SUBTYPE_SEPARATOR).append(subtype);
+ }
+
for (String key : parameters.keySet()) {
if (isParameterAllowed(key)) {
String value = parameters.get(key);
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/6f5e2b43/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ProviderFacadeImpl.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ProviderFacadeImpl.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ProviderFacadeImpl.java
index ee1a102..24466f8 100644
--- a/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ProviderFacadeImpl.java
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ProviderFacadeImpl.java
@@ -63,7 +63,7 @@ public class ProviderFacadeImpl implements EntityProviderInterface {
}
private static ContentTypeBasedEntityProvider create(final String contentType) throws EntityProviderException {
- return create(ContentType.create(contentType));
+ return create(ContentType.createCustom(contentType));
}
private static ContentTypeBasedEntityProvider create(final ContentType contentType) throws EntityProviderException {
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/6f5e2b43/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/RestUtil.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/RestUtil.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/RestUtil.java
index e636031..f134859 100644
--- a/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/RestUtil.java
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/RestUtil.java
@@ -43,6 +43,7 @@ import org.apache.olingo.odata2.api.commons.HttpHeaders;
import org.apache.olingo.odata2.api.exception.ODataBadRequestException;
import org.apache.olingo.odata2.api.exception.ODataException;
import org.apache.olingo.odata2.api.exception.ODataNotFoundException;
+import org.apache.olingo.odata2.api.exception.ODataUnsupportedMediaTypeException;
import org.apache.olingo.odata2.api.processor.ODataResponse;
import org.apache.olingo.odata2.api.uri.PathSegment;
import org.apache.olingo.odata2.core.ODataPathSegmentImpl;
@@ -68,7 +69,7 @@ public class RestUtil {
try {
odataResponse.close();
} catch (IOException inner) {
- // if close throw an exception we ignore these and re-theow our exception
+ // if close throw an exception we ignore these and re-throw our exception
throw e;
}
}
@@ -76,7 +77,7 @@ public class RestUtil {
}
}
- public static ContentType extractRequestContentType(final SubLocatorParameter param) throws ODataBadRequestException {
+ public static ContentType extractRequestContentType(final SubLocatorParameter param) throws ODataUnsupportedMediaTypeException {
final String contentType = param.getHttpHeaders().getHeaderString(HttpHeaders.CONTENT_TYPE);
if (contentType == null || contentType.isEmpty()) {
// RFC 2616, 7.2.1:
@@ -88,7 +89,7 @@ public class RestUtil {
} else if (ContentType.isParseable(contentType)) {
return ContentType.create(contentType);
} else {
- throw new ODataBadRequestException(ODataBadRequestException.INVALID_HEADER.addContent(HttpHeaders.CONTENT_TYPE, contentType));
+ throw new ODataUnsupportedMediaTypeException(ODataUnsupportedMediaTypeException.NOT_SUPPORTED_CONTENT_TYPE.addContent(HttpHeaders.CONTENT_TYPE, contentType));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/6f5e2b43/odata-core/src/test/java/org/apache/olingo/odata2/core/commons/ContentTypeTest.java
----------------------------------------------------------------------
diff --git a/odata-core/src/test/java/org/apache/olingo/odata2/core/commons/ContentTypeTest.java b/odata-core/src/test/java/org/apache/olingo/odata2/core/commons/ContentTypeTest.java
index 934d0ca..3b1af77 100644
--- a/odata-core/src/test/java/org/apache/olingo/odata2/core/commons/ContentTypeTest.java
+++ b/odata-core/src/test/java/org/apache/olingo/odata2/core/commons/ContentTypeTest.java
@@ -79,6 +79,9 @@ public class ContentTypeTest extends BaseTest {
assertFalse(ContentType.isParseable("app/app/moreapp"));
//assertFalse(ContentType.isParseable("application/atom+xml; charset = UTF-8"));
assertFalse(ContentType.isParseable(null));
+ assertFalse(ContentType.isParseable(""));
+ assertFalse(ContentType.isParseable("hugo"));
+ assertFalse(ContentType.isParseable("hugo/"));
}
@Test
@@ -93,6 +96,9 @@ public class ContentTypeTest extends BaseTest {
assertNull(ContentType.parse("app/app/moreapp"));
//assertFalse(ContentType.isParseable("application/atom+xml; charset = UTF-8"));
assertNull(ContentType.parse(null));
+ assertNull(ContentType.parse("hugo"));
+ assertNull(ContentType.parse("hugo"));
+ assertNull(ContentType.parse("hugo/"));
}
@Test
@@ -422,13 +428,29 @@ public class ContentTypeTest extends BaseTest {
assertEquals("type/subtype;key1=value1;key2=value2", mt.toString());
}
- @Test
- public void testFormatParserValidInputType() {
- ContentType t = ContentType.create("aaa");
+ @Test(expected=IllegalArgumentException.class)
+ public void testFormatParserInValidInputOnlyType() {
+ ContentType.create("aaa");
+ }
- assertEquals("aaa", t.getType());
- assertEquals("*", t.getSubtype());
- assertEquals(0, t.getParameters().size());
+ @Test(expected=IllegalArgumentException.class)
+ public void testFormatParserInValidInputOnlyTypeWithSepartor() {
+ ContentType.create("aaa/");
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testFormatParserInValidInputOnlySubTypeWithSepartor() {
+ ContentType.create("/aaa");
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testFormatParserInValidInputOnlySepartor() {
+ ContentType.create("/");
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testFormatParserInValidInputEmpty() {
+ ContentType.create("");
}
@Test
@@ -464,13 +486,9 @@ public class ContentTypeTest extends BaseTest {
assertEquals(2, t.getParameters().size());
}
- @Test
- public void testFormatParserValidInputTypeNullPara() {
- ContentType t = ContentType.create("aaa;x=y;a");
-
- assertEquals("aaa", t.getType());
- assertEquals("*", t.getSubtype());
- assertEquals(2, t.getParameters().size());
+ @Test(expected=IllegalArgumentException.class)
+ public void testFormatParserInValidInputTypeNullPara() {
+ ContentType.create("aaa;x=y;a");
}
@Test(expected = IllegalArgumentException.class)