From df332b3384ef2d51daf395eb541d77af9dd8b9f1 Mon Sep 17 00:00:00 2001 From: Ramesh Reddy Date: Tue, 20 Nov 2018 12:05:54 -0600 Subject: [PATCH] OLINGO-1316: allowing multipart/mixed as contentType for batch requests --- .../olingo/server/core/ServiceDispatcher.java | 23 ++++++++++------ .../server/example/TripPinServiceTest.java | 26 +++++++++++++++++++ .../server/core/ContentNegotiatorTest.java | 8 ++++++ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceDispatcher.java b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceDispatcher.java index c6c3a7900..7f06e9584 100644 --- a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceDispatcher.java +++ b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceDispatcher.java @@ -51,6 +51,7 @@ import org.apache.olingo.server.api.uri.UriResourcePrimitiveProperty; import org.apache.olingo.server.api.uri.UriResourceRef; import org.apache.olingo.server.api.uri.UriResourceSingleton; import org.apache.olingo.server.api.uri.UriResourceValue; +import org.apache.olingo.server.api.uri.queryoption.FormatOption; import org.apache.olingo.server.core.requests.ActionRequest; import org.apache.olingo.server.core.requests.BatchRequest; import org.apache.olingo.server.core.requests.DataRequest; @@ -79,11 +80,9 @@ public class ServiceDispatcher extends RequestURLHierarchyVisitor { } public void execute(ODataRequest odRequest, ODataResponse odResponse) { - ContentType contentType = ContentType.JSON; + FormatOption formatOption = null; + ODataException oDataException = null; try { - contentType = ContentNegotiator.doContentNegotiation(null, - odRequest, this.customContentSupport, RepresentationType.ERROR); - String path = odRequest.getRawODataPath(); String query = odRequest.getRawQueryPath(); if(path.indexOf("$entity") != -1) { @@ -92,16 +91,24 @@ public class ServiceDispatcher extends RequestURLHierarchyVisitor { UriInfo uriInfo = new Parser(this.metadata.getEdm(), odata) .parseUri(path, query, null, odRequest.getRawBaseUri()); - contentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - odRequest, this.customContentSupport, RepresentationType.ERROR); + formatOption = uriInfo.getFormatOption(); internalExecute(uriInfo, odRequest, odResponse); } + return; } catch(ODataLibraryException e) { - handleException(e, contentType, odRequest, odResponse); + oDataException = e; } catch(ODataApplicationException e) { - handleException(e, contentType, odRequest, odResponse); + oDataException = e; } + ContentType contentType = ContentType.JSON; + try { + contentType = ContentNegotiator.doContentNegotiation(formatOption, + odRequest, this.customContentSupport, RepresentationType.ERROR); + } catch (ContentNegotiatorException e) { + // ignore, default to JSON + } + handleException(oDataException, contentType, odRequest, odResponse); } protected void handleException(ODataException e, ContentType contentType, diff --git a/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java b/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java index bbbaa9e6e..fdc0e4a30 100644 --- a/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java +++ b/lib/server-core-ext/src/test/java/org/apache/olingo/server/example/TripPinServiceTest.java @@ -63,6 +63,7 @@ public class TripPinServiceTest { private static String baseURL; private static DefaultHttpClient http = new DefaultHttpClient(); private static final int TOMCAT_PORT = 9900; + private static final String CRLF = "\r\n"; @BeforeClass public static void beforeTest() throws Exception { @@ -808,4 +809,29 @@ public class TripPinServiceTest { HttpResponse response = httpSend(request, 412); EntityUtils.consumeQuietly(response.getEntity()); } + + @Test + public void batchAccept() throws Exception { + final String batchtUrl = baseURL + "/$batch"; + + final String content = "" + + "--batch_12345" + CRLF + + "Content-Type: application/http" + CRLF + + "Content-Transfer-Encoding: binary" + CRLF + + CRLF + + "GET Airlines('FM') HTTP/1.1" + CRLF + + CRLF + + CRLF + + "--batch_12345--"; + + HttpPost request = new HttpPost(batchtUrl); + StringEntity stringEntity = new StringEntity(content); + stringEntity.setContentType("multipart/mixed;boundary=batch_12345"); + request.setEntity(stringEntity); + // multipart/mixed should work as an Accept value + request.setHeader("Accept", "multipart/mixed"); + HttpResponse response = httpSend(request, 202); + EntityUtils.consumeQuietly(response.getEntity()); + } + } diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java index 8e91cd18e..167fa7ea5 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java @@ -55,6 +55,7 @@ public class ContentNegotiatorTest { static final private String ACCEPT_CASE_WILDCARD1 = "*/*"; static final private String ACCEPT_CASE_WILDCARD2 = "application/*"; static final private String ACCEPT_CASE_JSON_IEEE754 = ACCEPT_CASE_JSON + ";IEEE754Compatible=true"; + static final private String ACCEPT_CASE_MULTIPART_MIXED = ContentType.MULTIPART_MIXED.toContentTypeString(); //@formatter:off (Eclipse formatter) //CHECKSTYLE:OFF (Maven checkstyle) @@ -335,4 +336,11 @@ public class ContentNegotiatorTest { assertTrue(ContentNegotiator.isSupported(ContentType.create("a/b"), createCustomContentTypeSupport("a/b"), RepresentationType.BINARY)); } + + @Test + public void checBatchkSupport() throws Exception { + testContentNegotiation(new String[] { ACCEPT_CASE_MULTIPART_MIXED, null, ACCEPT_CASE_MULTIPART_MIXED, null }, + RepresentationType.BATCH); + } + }