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 225bc34 [OLINGO-1442]Support update requests on Stream properties 225bc34 is described below commit 225bc34dd9d49c17a3c226d97131a8dce6364d94 Author: ramya vasanth <ramya.vasa...@sap.com> AuthorDate: Fri Apr 3 10:25:09 2020 +0530 [OLINGO-1442]Support update requests on Stream properties --- .../olingo/fit/tecsvc/http/BasicStreamITCase.java | 31 ++++++++++++++++++++++ .../apache/olingo/server/core/ODataDispatcher.java | 14 ++++++++-- .../TechnicalPrimitiveComplexProcessor.java | 23 +++++++++++----- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicStreamITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicStreamITCase.java index 697c970..dd1819a 100644 --- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicStreamITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/http/BasicStreamITCase.java @@ -21,6 +21,10 @@ package org.apache.olingo.fit.tecsvc.http; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedOutputStream; +import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; @@ -310,6 +314,33 @@ public class BasicStreamITCase extends AbstractBaseTestITCase { + "{\"@id\":\"ESWithStream(7)\"}]}")); } + @Test + public void putRequestOnStreamProperty() throws Exception { + URL url = new URL(SERVICE_URI + "ESWithStream(7)/PropertyStream"); + + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod(HttpMethod.PUT.name()); + connection.setRequestProperty(HttpHeader.CONTENT_TYPE, "image/jpeg"); + connection.setRequestProperty(HttpHeader.IF_MATCH, "*"); + connection.setRequestProperty(HttpHeader.ACCEPT, "application/json"); + InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("sample.png"); + byte[] bytes = IOUtils.toByteArray(in); + connection.setDoOutput(true); + BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream()); + try { + out.write(bytes, 0, bytes.length); + out.flush(); + } finally { + out.close(); + } + connection.connect(); + + assertEquals(HttpStatusCode.OK.getStatusCode(), connection.getResponseCode()); + assertEquals(ContentType.parse("image/jpeg"), + ContentType.create(connection.getHeaderField(HttpHeader.CONTENT_TYPE))); + assertEquals(bytes.length, IOUtils.toByteArray(connection.getInputStream()).length); + } + @Override protected ODataClient getClient() { return null; 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 9d0bc67..985bee6 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 @@ -83,6 +83,7 @@ public class ODataDispatcher { private final ODataHandlerImpl handler; private static final String RETURN_MINIMAL = "return=minimal"; private static final String RETURN_REPRESENTATION = "return=representation"; + private static final String EDMSTREAM = "Edm.Stream"; public ODataDispatcher(final UriInfo uriInfo, final ODataHandlerImpl handler) { this.uriInfo = uriInfo; @@ -458,8 +459,17 @@ public class ODataDispatcher { } } else if (method == HttpMethod.PUT || method == HttpMethod.PATCH) { validatePreconditions(request, false); - final ContentType requestFormat = getSupportedContentType(request.getHeader(HttpHeader.CONTENT_TYPE), - representationType, true); + ContentType requestFormat = null; + List<UriResource> uriResources = uriInfo.getUriResourceParts(); + UriResource uriResource = uriResources.get(uriResources.size() - 1); + if (uriResource instanceof UriResourcePrimitiveProperty && + ((UriResourcePrimitiveProperty)uriResource).getType() + .getFullQualifiedName().getFullQualifiedNameAsString().equalsIgnoreCase(EDMSTREAM)) { + requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); + } else { + requestFormat = getSupportedContentType(request.getHeader(HttpHeader.CONTENT_TYPE), + representationType, true); + } final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, handler.getCustomContentTypeSupport(), representationType); if (isCollection) { diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalPrimitiveComplexProcessor.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalPrimitiveComplexProcessor.java index d3983a3..2565555 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalPrimitiveComplexProcessor.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalPrimitiveComplexProcessor.java @@ -86,7 +86,7 @@ public class TechnicalPrimitiveComplexProcessor extends TechnicalProcessor PrimitiveCollectionProcessor, CountPrimitiveCollectionProcessor, ComplexProcessor, ComplexCollectionProcessor, CountComplexCollectionProcessor { - private static final Object EDMSTREAM = "Edm.Stream"; + private static final String EDMSTREAM = "Edm.Stream"; public TechnicalPrimitiveComplexProcessor(final DataProvider dataProvider, final ServiceMetadata serviceMetadata) { @@ -334,9 +334,12 @@ public class TechnicalPrimitiveComplexProcessor extends TechnicalProcessor Property property = getPropertyData(entity, path); - if (representationType == RepresentationType.VALUE) { + if (representationType == RepresentationType.VALUE || + edmProperty.getType().getFullQualifiedName() + .getFullQualifiedNameAsString().equalsIgnoreCase(EDMSTREAM)) { final FixedFormatDeserializer deserializer = odata.createFixedFormatDeserializer(); - final Object value = edmProperty.getType() == odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Binary) ? + final Object value = edmProperty.getType() == odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Binary) + || edmProperty.getType() == odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Stream) ? deserializer.binary(request.getBody()) : deserializer.primitiveValue(request.getBody(), edmProperty); dataProvider.updatePropertyValue(property, value); @@ -354,7 +357,9 @@ public class TechnicalPrimitiveComplexProcessor extends TechnicalProcessor final Return returnPreference = odata.createPreferences(request.getHeaders(HttpHeader.PREFER)).getReturn(); if (returnPreference == null || returnPreference == Return.REPRESENTATION) { response.setStatusCode(HttpStatusCode.OK.getStatusCode()); - if (representationType == RepresentationType.VALUE) { + if (representationType == RepresentationType.VALUE || + edmProperty.getType().getFullQualifiedName() + .getFullQualifiedNameAsString().equalsIgnoreCase(EDMSTREAM)) { response.setContent( serializePrimitiveValue(property, edmProperty, (EdmPrimitiveType) edmProperty.getType(), null)); } else { @@ -362,7 +367,12 @@ public class TechnicalPrimitiveComplexProcessor extends TechnicalProcessor edmProperty.getType(), null, representationType, responseFormat, null, null); response.setContent(result.getContent()); } - response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString()); + if (edmProperty.getType().getFullQualifiedName() + .getFullQualifiedNameAsString().equalsIgnoreCase(EDMSTREAM)) { + response.setHeader(HttpHeader.CONTENT_TYPE, requestFormat.toContentTypeString()); + } else { + response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString()); + } } else { response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode()); } @@ -519,7 +529,8 @@ public class TechnicalPrimitiveComplexProcessor extends TechnicalProcessor private InputStream serializePrimitiveValue(final Property property, final EdmProperty edmProperty, final EdmPrimitiveType type, final EdmReturnType returnType) throws SerializerException { final FixedFormatSerializer serializer = odata.createFixedFormatSerializer(); - return type == odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Binary) ? + return type == odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Binary) || + type == odata.createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Stream) ? serializer.binary((byte[]) property.getValue()) : serializer.primitiveValue(type, property.getValue(), PrimitiveValueSerializerOptions.with()