You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ra...@apache.org on 2019/09/13 06:44:01 UTC
[olingo-odata4] branch master updated: [OLINGO-1397]Handle post
requests when there is no content type and payload
This is an automated email from the ASF dual-hosted git repository.
ramyav pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/olingo-odata4.git
The following commit(s) were added to refs/heads/master by this push:
new b5be472 [OLINGO-1397]Handle post requests when there is no content type and payload
b5be472 is described below
commit b5be47209786d509c2f48f34013d0b5a5f1937a1
Author: ramya vasanth <ra...@sap.com>
AuthorDate: Fri Sep 13 12:13:46 2019 +0530
[OLINGO-1397]Handle post requests when there is no content type and payload
---
.../apache/olingo/server/core/ODataDispatcher.java | 18 ++++-
.../olingo/server/core/ODataExceptionHelper.java | 3 +-
.../olingo/server/core/ODataHandlerException.java | 4 +-
.../org/apache/olingo/server/core/ODataImpl.java | 88 +++++++++++++---------
.../server-core-exceptions-i18n.properties | 1 +
.../apache/olingo/server/core/ODataImplTest.java | 50 ++++++++++++
.../olingo/server/core/ODataHandlerImplTest.java | 30 +++++++-
7 files changed, 150 insertions(+), 44 deletions(-)
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataDispatcher.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataDispatcher.java
index 442ca6d..9d0bc67 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataDispatcher.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataDispatcher.java
@@ -18,6 +18,7 @@
*/
package org.apache.olingo.server.core;
+import java.io.IOException;
import java.util.List;
import org.apache.olingo.commons.api.edm.EdmAction;
@@ -537,11 +538,20 @@ public class ODataDispatcher {
handler.selectProcessor(MediaEntityProcessor.class)
.createMediaEntity(request, response, uriInfo, requestFormat, responseFormat);
} else {
- final ContentType requestFormat = getSupportedContentType(
+ try {
+ final ContentType requestFormat = (request.getHeader(HttpHeader.CONTENT_TYPE) == null &&
+ (request.getBody() == null || request.getBody().available() == 0)) ?
+ getSupportedContentType(
request.getHeader(HttpHeader.CONTENT_TYPE),
- RepresentationType.ENTITY, true);
- handler.selectProcessor(EntityProcessor.class)
+ RepresentationType.ENTITY, false) : getSupportedContentType(
+ request.getHeader(HttpHeader.CONTENT_TYPE),
+ RepresentationType.ENTITY, true);
+ handler.selectProcessor(EntityProcessor.class)
.createEntity(request, response, uriInfo, requestFormat, responseFormat);
+ } catch (IOException e) {
+ throw new ODataHandlerException("There is problem in the payload.",
+ ODataHandlerException.MessageKeys.INVALID_PAYLOAD);
+ }
}
} else {
throwMethodNotAllowed(method);
@@ -672,7 +682,7 @@ public class ODataDispatcher {
throw new ODataHandlerException("ContentTypeHeader parameter is null",
ODataHandlerException.MessageKeys.MISSING_CONTENT_TYPE);
}
- return null;
+ return ContentType.APPLICATION_JSON;
}
ContentType contentType;
try {
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
index 4f75590..35fff35 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java
@@ -101,7 +101,8 @@ public class ODataExceptionHelper {
|| ODataHandlerException.MessageKeys.MISSING_CONTENT_TYPE.equals(e.getMessageKey())
|| ODataHandlerException.MessageKeys.INVALID_CONTENT_TYPE.equals(e.getMessageKey())
|| ODataHandlerException.MessageKeys.UNSUPPORTED_CONTENT_TYPE.equals(e.getMessageKey())
- || ODataHandlerException.MessageKeys.INVALID_PREFER_HEADER.equals(e.getMessageKey())) {
+ || ODataHandlerException.MessageKeys.INVALID_PREFER_HEADER.equals(e.getMessageKey())
+ || ODataHandlerException.MessageKeys.INVALID_PAYLOAD.equals(e.getMessageKey())) {
serverError.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode());
} else if (ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED.equals(e.getMessageKey())) {
serverError.setStatusCode(HttpStatusCode.METHOD_NOT_ALLOWED.getStatusCode());
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerException.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerException.java
index 5596512..3fe27de 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerException.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerException.java
@@ -44,7 +44,9 @@ public class ODataHandlerException extends ODataLibraryException {
/** parameter: version */
ODATA_VERSION_NOT_SUPPORTED,
/** parameter: prefer header */
- INVALID_PREFER_HEADER;
+ INVALID_PREFER_HEADER,
+ /** invalid payload */
+ INVALID_PAYLOAD;
@Override
public String getKey() {
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataImpl.java
index db9bffc..d6605dd 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataImpl.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataImpl.java
@@ -68,7 +68,7 @@ public class ODataImpl extends OData {
public ODataSerializer createSerializer(final ContentType contentType) throws SerializerException {
ODataSerializer serializer = null;
- if (contentType.isCompatible(ContentType.APPLICATION_JSON)) {
+ if (contentType != null && contentType.isCompatible(ContentType.APPLICATION_JSON)) {
final String metadata = contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA);
if (metadata == null
|| ContentType.VALUE_ODATA_METADATA_MINIMAL.equalsIgnoreCase(metadata)
@@ -76,14 +76,16 @@ public class ODataImpl extends OData {
|| ContentType.VALUE_ODATA_METADATA_FULL.equalsIgnoreCase(metadata)) {
serializer = new ODataJsonSerializer(contentType, new Constantsv00());
}
- } else if (contentType.isCompatible(ContentType.APPLICATION_XML)
- || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) {
+ } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
+ || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
serializer = new ODataXmlSerializer();
}
if (serializer == null) {
- throw new SerializerException("Unsupported format: " + contentType.toContentTypeString(),
- SerializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
+ throw new SerializerException("Unsupported format: " +
+ ((contentType != null) ? contentType.toContentTypeString() : null),
+ SerializerException.MessageKeys.UNSUPPORTED_FORMAT,
+ ((contentType != null) ? contentType.toContentTypeString() : null));
} else {
return serializer;
}
@@ -97,7 +99,7 @@ public class ODataImpl extends OData {
if(versions!=null && versions.size()>0 && getMaxVersion(versions)>4){
constants = new Constantsv01() ;
}
- if (contentType.isCompatible(ContentType.APPLICATION_JSON)) {
+ if (contentType != null && contentType.isCompatible(ContentType.APPLICATION_JSON)) {
final String metadata = contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA);
if (metadata == null
|| ContentType.VALUE_ODATA_METADATA_MINIMAL.equalsIgnoreCase(metadata)
@@ -105,14 +107,16 @@ public class ODataImpl extends OData {
|| ContentType.VALUE_ODATA_METADATA_FULL.equalsIgnoreCase(metadata)) {
serializer = new ODataJsonSerializer(contentType, constants);
}
- } else if (contentType.isCompatible(ContentType.APPLICATION_XML)
- || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) {
+ } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
+ || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
serializer = new ODataXmlSerializer();
}
if (serializer == null) {
- throw new SerializerException("Unsupported format: " + contentType.toContentTypeString(),
- SerializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
+ throw new SerializerException("Unsupported format: " +
+ ((contentType != null) ? contentType.toContentTypeString() : null),
+ SerializerException.MessageKeys.UNSUPPORTED_FORMAT,
+ ((contentType != null) ? contentType.toContentTypeString() : null));
} else {
return serializer;
}
@@ -125,26 +129,30 @@ public class ODataImpl extends OData {
@Override
public EdmAssistedSerializer createEdmAssistedSerializer(final ContentType contentType) throws SerializerException {
- if (contentType.isCompatible(ContentType.APPLICATION_JSON)) {
+ if (contentType != null && contentType.isCompatible(ContentType.APPLICATION_JSON)) {
return new EdmAssistedJsonSerializer(contentType);
}
- throw new SerializerException("Unsupported format: " + contentType.toContentTypeString(),
- SerializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
+ throw new SerializerException("Unsupported format: " +
+ ((contentType != null) ? contentType.toContentTypeString() : null),
+ SerializerException.MessageKeys.UNSUPPORTED_FORMAT,
+ ((contentType != null) ? contentType.toContentTypeString() : null));
}
@Override
public EdmDeltaSerializer createEdmDeltaSerializer(final ContentType contentType, final List<String> versions)
throws SerializerException {
- if (contentType.isCompatible(ContentType.APPLICATION_JSON)) {
+ if (contentType != null && contentType.isCompatible(ContentType.APPLICATION_JSON)) {
if(versions!=null && !versions.isEmpty()){
return getMaxVersion(versions)>4 ? new JsonDeltaSerializerWithNavigations(contentType):
new JsonDeltaSerializer(contentType);
}
return new JsonDeltaSerializerWithNavigations(contentType);
}
- throw new SerializerException("Unsupported format: " + contentType.toContentTypeString(),
- SerializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
+ throw new SerializerException("Unsupported format: " +
+ ((contentType != null) ? contentType.toContentTypeString() : null),
+ SerializerException.MessageKeys.UNSUPPORTED_FORMAT,
+ ((contentType != null) ? contentType.toContentTypeString() : null));
}
private float getMaxVersion(List<String> versions) {
@@ -193,27 +201,31 @@ public class ODataImpl extends OData {
@Override
public ODataDeserializer createDeserializer(final ContentType contentType) throws DeserializerException {
- if (contentType.isCompatible(ContentType.JSON)) {
+ if (contentType != null && contentType.isCompatible(ContentType.JSON)) {
return new ODataJsonDeserializer(contentType);
- } else if (contentType.isCompatible(ContentType.APPLICATION_XML)
- || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) {
+ } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
+ || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
return new ODataXmlDeserializer();
} else {
- throw new DeserializerException("Unsupported format: " + contentType.toContentTypeString(),
- DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
+ throw new DeserializerException("Unsupported format: " +
+ ((contentType != null) ? contentType.toContentTypeString() : null),
+ DeserializerException.MessageKeys.UNSUPPORTED_FORMAT,
+ ((contentType != null) ? contentType.toContentTypeString() : null));
}
}
@Override
public ODataDeserializer createDeserializer(final ContentType contentType,
ServiceMetadata metadata) throws DeserializerException {
- if (contentType.isCompatible(ContentType.JSON)) {
+ if (contentType != null && contentType.isCompatible(ContentType.JSON)) {
return new ODataJsonDeserializer(contentType, metadata);
- } else if (contentType.isCompatible(ContentType.APPLICATION_XML)
- || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) {
+ } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
+ || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
return new ODataXmlDeserializer(metadata);
} else {
- throw new DeserializerException("Unsupported format: " + contentType.toContentTypeString(),
- DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
+ throw new DeserializerException("Unsupported format: " +
+ ((contentType != null) ? contentType.toContentTypeString() : null),
+ DeserializerException.MessageKeys.UNSUPPORTED_FORMAT,
+ ((contentType != null) ? contentType.toContentTypeString() : null));
}
}
@@ -246,14 +258,16 @@ public class ODataImpl extends OData {
if(versions!=null && versions.size()>0 && getMaxVersion(versions)>4){
constants = new Constantsv01() ;
}
- if (contentType.isCompatible(ContentType.JSON)) {
+ if (contentType != null && contentType.isCompatible(ContentType.JSON)) {
return new ODataJsonDeserializer(contentType, constants);
- } else if (contentType.isCompatible(ContentType.APPLICATION_XML)
- || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) {
+ } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
+ || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
return new ODataXmlDeserializer();
} else {
- throw new DeserializerException("Unsupported format: " + contentType.toContentTypeString(),
- DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
+ throw new DeserializerException("Unsupported format: " +
+ ((contentType != null) ? contentType.toContentTypeString() : null),
+ DeserializerException.MessageKeys.UNSUPPORTED_FORMAT,
+ ((contentType != null) ? contentType.toContentTypeString() : null));
}
}
@@ -265,14 +279,16 @@ public class ODataImpl extends OData {
if(versions!=null && versions.size()>0 && getMaxVersion(versions)>4){
constants = new Constantsv01() ;
}
- if (contentType.isCompatible(ContentType.JSON)) {
+ if (contentType != null && contentType.isCompatible(ContentType.JSON)) {
return new ODataJsonDeserializer(contentType, metadata, constants);
- } else if (contentType.isCompatible(ContentType.APPLICATION_XML)
- || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) {
+ } else if (contentType != null && (contentType.isCompatible(ContentType.APPLICATION_XML)
+ || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML))) {
return new ODataXmlDeserializer(metadata);
} else {
- throw new DeserializerException("Unsupported format: " + contentType.toContentTypeString(),
- DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
+ throw new DeserializerException("Unsupported format: " +
+ ((contentType != null) ? contentType.toContentTypeString() : null),
+ DeserializerException.MessageKeys.UNSUPPORTED_FORMAT,
+ ((contentType != null) ? contentType.toContentTypeString() : null));
}
}
}
diff --git a/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties b/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties
index 964cf35..a4be4ca 100644
--- a/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties
+++ b/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties
@@ -28,6 +28,7 @@ ODataHandlerException.MISSING_CONTENT_TYPE=The Content-Type HTTP header must be
ODataHandlerException.UNSUPPORTED_CONTENT_TYPE=The content type '%1$s' is not supported for this request.
ODataHandlerException.INVALID_CONTENT_TYPE=The content type '%1$s' is not valid.
ODataHandlerException.INVALID_PREFER_HEADER=The Prefer header '%1$s' is not supported for this HTTP Method.
+ODataHandlerException.INVALID_PAYLOAD=There is problem in the payload.
UriParserSyntaxException.MUST_BE_LAST_SEGMENT=The segment '%1$s' must be the last segment.
UriParserSyntaxException.UNKNOWN_SYSTEM_QUERY_OPTION=The system query option '%1$s' is not defined.
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataImplTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataImplTest.java
index df8d685..9bef198 100644
--- a/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataImplTest.java
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataImplTest.java
@@ -25,9 +25,11 @@ import java.util.List;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.server.api.OData;
+import org.apache.olingo.server.api.ServiceMetadata;
import org.apache.olingo.server.api.deserializer.DeserializerException;
import org.apache.olingo.server.api.serializer.SerializerException;
import org.junit.Test;
+import org.mockito.Mockito;
public class ODataImplTest {
@@ -58,4 +60,52 @@ public class ODataImplTest {
public void xmlDeserializer() throws DeserializerException {
assertNotNull(odata.createDeserializer(ContentType.APPLICATION_XML));
}
+
+ @Test(expected=DeserializerException.class)
+ public void deserializerWithoutContentType() throws DeserializerException {
+ odata.createDeserializer(null);
+ }
+
+ @Test(expected=DeserializerException.class)
+ public void deserializerWithoutContentTypeAndWithVersions() throws DeserializerException {
+ List<String> versions = new ArrayList<String>();
+ versions.add("4.01");
+ odata.createDeserializer(null, versions);
+ }
+
+ @Test(expected=SerializerException.class)
+ public void deltaSerializer() throws SerializerException {
+ List<String> versions = new ArrayList<String>();
+ versions.add("4.01");
+ odata.createEdmDeltaSerializer(null, versions);
+ }
+
+ @Test(expected=SerializerException.class)
+ public void edmAssitedSerializer() throws SerializerException {
+ odata.createEdmAssistedSerializer(null);
+ }
+
+ @Test(expected=DeserializerException.class)
+ public void deserializer1() throws DeserializerException {
+ List<String> versions = new ArrayList<String>();
+ versions.add("4.01");
+ odata.createDeserializer(null, null, versions);
+ }
+
+ @Test(expected=DeserializerException.class)
+ public void deserializer2() throws DeserializerException {
+ odata.createDeserializer(null, Mockito.mock(ServiceMetadata.class));
+ }
+
+ @Test(expected=SerializerException.class)
+ public void serializerWithVersions() throws SerializerException {
+ List<String> versions = new ArrayList<String>();
+ versions.add("4.01");
+ odata.createSerializer(null, versions);
+ }
+
+ @Test(expected=SerializerException.class)
+ public void serializer() throws SerializerException {
+ odata.createSerializer(null);
+ }
}
diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java
index 9d5fdd8..b9e1970 100644
--- a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java
+++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java
@@ -31,6 +31,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
+import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collections;
@@ -1091,8 +1092,7 @@ public class ODataHandlerImplTest {
EntityProcessor processor = mock(EntityProcessor.class);
final ODataResponse response = dispatch(HttpMethod.POST, "ESAllPrim", null,
HttpHeader.CONTENT_TYPE, null, processor);
- verifyZeroInteractions(processor);
- assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), response.getStatusCode());
+ assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusCode());
}
@Test
@@ -1150,6 +1150,32 @@ public class ODataHandlerImplTest {
return response;
}
+ @Test
+ public void dispatchEmptyContentWithoutContentType() {
+ final String path = "ESAllPrim";
+ final EntityCollectionProcessor processor = mock(EntityCollectionProcessor.class);
+
+ ODataRequest request = new ODataRequest();
+ request.setMethod(HttpMethod.POST);
+ request.setRawBaseUri(BASE_URI);
+ request.setRawRequestUri(BASE_URI);
+ request.setRawODataPath(path);
+ request.setBody(new ByteArrayInputStream(new byte[0]));
+
+ final OData odata = OData.newInstance();
+ final ServiceMetadata metadata = odata.createServiceMetadata(
+ new EdmTechProvider(), Collections.<EdmxReference> emptyList());
+
+ ODataHandlerImpl handler = new ODataHandlerImpl(odata, metadata, new ServerCoreDebugger(odata));
+
+ if (processor != null) {
+ handler.register(processor);
+ }
+
+ final ODataResponse response = handler.process(request);
+ assertNotNull(response);
+ }
+
private ODataResponse dispatch(final HttpMethod method, final String path, final Processor processor) {
return dispatch(method, path, null, null, null, processor);
}