mirror of
https://github.com/apache/olingo-odata4.git
synced 2025-02-06 01:59:12 +00:00
[OLINGO-1397]Handle post requests when there is no content type and payload
This commit is contained in:
parent
7049f4ebf0
commit
b5be472097
@ -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 {
|
||||
|
@ -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());
|
||||
|
@ -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() {
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user