From f91568e1abe6e4b606d541d6c3a85218d6ab739e Mon Sep 17 00:00:00 2001 From: Archana Rai Date: Tue, 26 Mar 2019 16:56:08 +0530 Subject: [PATCH] [OLINGO-1315]OData v4.0:Client: API to return HTTP Request details --- .../olingo/fit/tecsvc/client/BasicITCase.java | 88 ++++++++++++++++++- .../retrieve/ODataDeltaRequestImpl.java | 6 +- .../retrieve/ODataMediaRequestImpl.java | 8 +- .../retrieve/ODataPropertyRequestImpl.java | 6 +- .../response/AbstractODataResponse.java | 21 ++++- 5 files changed, 109 insertions(+), 20 deletions(-) diff --git a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java index 8e303ba56..e04f668b6 100644 --- a/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/tecsvc/client/BasicITCase.java @@ -31,6 +31,7 @@ import static org.junit.Assume.assumeTrue; import java.io.IOException; import java.io.InputStream; +import java.io.StringWriter; import java.math.BigDecimal; import java.net.URI; import java.net.URISyntaxException; @@ -41,6 +42,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.apache.commons.io.IOUtils; import org.apache.http.client.methods.HttpUriRequest; import org.apache.olingo.client.api.EdmEnabledODataClient; import org.apache.olingo.client.api.ODataClient; @@ -303,8 +305,8 @@ public class BasicITCase extends AbstractParamTecSvcITCase { getEntitySetRequest(uriBuilder.build()); final ODataRetrieveResponse res = req.execute(); + assertNotNull(res.getRawResponse()); final ClientEntitySet feed = res.getBody(); - assertNotNull(feed); assertEquals(10, feed.getEntities().size()); @@ -364,7 +366,7 @@ public class BasicITCase extends AbstractParamTecSvcITCase { saveCookieHeader(response); assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode()); assertContentType(response.getContentType()); - + assertNotNull(response.getRawResponse()); final ClientEntity entity = response.getBody(); assertNotNull(entity); final ClientProperty property = entity.getProperty("CollPropertyInt16"); @@ -377,6 +379,30 @@ public class BasicITCase extends AbstractParamTecSvcITCase { assertShortOrInt(30112, iterator.next().asPrimitive().toValue()); } + @Test + public void readEntityProperty() throws Exception { + ODataPropertyRequest request = getClient().getRetrieveRequestFactory() + .getPropertyRequest(getClient().newURIBuilder(SERVICE_URI) + .appendEntitySetSegment("ESCollAllPrim").appendKeySegment(1) + .appendPropertySegment("CollPropertyInt16").build()); + assertNotNull(request); + setCookieHeader(request); + + final ODataRetrieveResponse response = request.execute(); + saveCookieHeader(response); + assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode()); + assertContentType(response.getContentType()); + assertNotNull(response.getRawResponse()); + final ClientProperty property = response.getBody(); + assertNotNull(property); + assertNotNull(property.getCollectionValue()); + assertEquals(3, property.getCollectionValue().size()); + Iterator iterator = property.getCollectionValue().iterator(); + assertShortOrInt(1000, iterator.next().asPrimitive().toValue()); + assertShortOrInt(2000, iterator.next().asPrimitive().toValue()); + assertShortOrInt(30112, iterator.next().asPrimitive().toValue()); + } + @Test public void deleteEntity() throws Exception { final URI uri = getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).appendKeySegment(32767) @@ -431,6 +457,64 @@ public class BasicITCase extends AbstractParamTecSvcITCase { assertEquals(isJson() ? "PT6S" : BigDecimal.valueOf(6), property4.getPrimitiveValue().toValue()); } + + @Test + public void readUpdatepdateEntity() throws Exception { + + EdmMetadataRequest request = getClient().getRetrieveRequestFactory().getMetadataRequest(SERVICE_URI); + assertNotNull(request); + setCookieHeader(request); + + ODataRetrieveResponse response = request.execute(); + saveCookieHeader(response); + assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode()); + + Edm edm = response.getBody(); + assertNotNull(edm); + + final EdmEntityContainer container = edm.getEntityContainer( + new FullQualifiedName("olingo.odata.test1", "Container")); + assertNotNull(container); + + final EdmEntitySet esAllPrim = container.getEntitySet("ESAllPrim"); + assertNotNull(esAllPrim); + assertEquals("olingo.odata.test1", esAllPrim.getEntityType().getNamespace()); + + assertEquals(2, edm.getSchemas().size()); + assertEquals(SERVICE_NAMESPACE, edm.getSchema(SERVICE_NAMESPACE).getNamespace()); + assertEquals("Namespace1_Alias", edm.getSchema(SERVICE_NAMESPACE).getAlias()); + assertEquals("Org.OData.Core.V1", edm.getSchema("Org.OData.Core.V1").getNamespace()); + assertEquals("Core", edm.getSchema("Org.OData.Core.V1").getAlias()); + + ClientEntity newEntity = getFactory().newEntity(ET_ALL_PRIM); + newEntity.getProperties().add(getFactory().newPrimitiveProperty(PROPERTY_INT64, + getFactory().newPrimitiveValueBuilder().buildInt64((long) 42))); + + final URI uri = getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).appendKeySegment(32767) + .build(); + final ODataEntityUpdateRequest request2 = getClient().getCUDRequestFactory().getEntityUpdateRequest( + uri, UpdateType.REPLACE, newEntity); + HttpUriRequest req = request2.getHttpRequest(); + final ODataEntityUpdateResponse response2 = request2.execute(); + assertNotNull(req); + assertEquals(HttpStatusCode.OK.getStatusCode(), response2.getStatusCode()); + + // Check that the updated properties have changed and that other properties have their default values. + + StringWriter writer = new StringWriter(); + InputStream stream = response2.getRawResponse(); + IOUtils.copy(stream, writer); + assertNotNull(writer.toString()); + final ClientEntity entity = response2.getBody(); + assertNotNull(entity); + final ClientProperty property1 = entity.getProperty(PROPERTY_INT64); + assertNotNull(property1); + assertShortOrInt(42, property1.getPrimitiveValue().toValue()); + final ClientProperty property2 = entity.getProperty(PROPERTY_DECIMAL); + assertNotNull(property2); + assertNull(property2.getPrimitiveValue().toValue()); + } + @Test public void updateEntity() throws Exception { ClientEntity newEntity = getFactory().newEntity(ET_ALL_PRIM); diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataDeltaRequestImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataDeltaRequestImpl.java index 42d665a5d..7485ad4a4 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataDeltaRequestImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataDeltaRequestImpl.java @@ -18,7 +18,6 @@ */ package org.apache.olingo.client.core.communication.request.retrieve; -import java.io.IOException; import java.net.URI; import org.apache.http.HttpResponse; @@ -28,7 +27,6 @@ import org.apache.olingo.client.api.communication.request.retrieve.ODataDeltaReq import org.apache.olingo.client.api.communication.response.ODataRetrieveResponse; import org.apache.olingo.client.api.data.ResWrap; import org.apache.olingo.client.api.domain.ClientDelta; -import org.apache.olingo.client.api.http.HttpClientException; import org.apache.olingo.client.api.serialization.ODataDeserializerException; import org.apache.olingo.commons.api.data.Delta; import org.apache.olingo.commons.api.format.ContentType; @@ -66,11 +64,9 @@ public class ODataDeltaRequestImpl extends AbstractODataRetrieveRequest resource = odataClient.getDeserializer(ContentType.parse(getContentType())). - toDelta(res.getEntity().getContent()); + toDelta(getRawResponse()); delta = odataClient.getBinder().getODataDelta(resource); - } catch (IOException e) { - throw new HttpClientException(e); } catch (final ODataDeserializerException e) { throw new IllegalArgumentException(e); } finally { diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataMediaRequestImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataMediaRequestImpl.java index 3f32f1b2c..1b534dbd7 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataMediaRequestImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataMediaRequestImpl.java @@ -18,7 +18,6 @@ */ package org.apache.olingo.client.core.communication.request.retrieve; -import java.io.IOException; import java.io.InputStream; import java.net.URI; @@ -27,7 +26,6 @@ import org.apache.http.client.HttpClient; import org.apache.olingo.client.api.ODataClient; import org.apache.olingo.client.api.communication.request.retrieve.ODataMediaRequest; import org.apache.olingo.client.api.communication.response.ODataRetrieveResponse; -import org.apache.olingo.client.api.http.HttpClientException; import org.apache.olingo.commons.api.format.ContentType; /** @@ -82,11 +80,7 @@ public class ODataMediaRequestImpl extends AbstractODataRetrieveRequest if (property == null) { try { final ResWrap resource = odataClient.getDeserializer(ContentType.parse(getContentType())) - .toProperty(res.getEntity().getContent()); + .toProperty(getRawResponse()); property = (T) odataClient.getBinder().getODataProperty(resource); - } catch (IOException e) { - throw new HttpClientException(e); } catch (final ODataDeserializerException e) { throw new IllegalArgumentException(e); } finally { diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/AbstractODataResponse.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/AbstractODataResponse.java index 5052e9157..8be5d359b 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/AbstractODataResponse.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/AbstractODataResponse.java @@ -101,6 +101,8 @@ public abstract class AbstractODataResponse implements ODataResponse { * Batch info (if to be batched). */ protected ODataBatchController batchInfo = null; + + private byte[] inputContent = null; public AbstractODataResponse( final ODataClient odataClient, final HttpClient httpclient, final HttpResponse res) { @@ -149,6 +151,7 @@ public abstract class AbstractODataResponse implements ODataResponse { public final ODataResponse initFromHttpResponse(final HttpResponse res) { try { this.payload = res.getEntity() == null ? null : res.getEntity().getContent(); + this.inputContent = null; } catch (final IllegalStateException e) { HttpClientUtils.closeQuietly(res); LOG.error("Error retrieving payload", e); @@ -252,6 +255,9 @@ public abstract class AbstractODataResponse implements ODataResponse { @Override public InputStream getRawResponse() { + + + InputStream inputStream = null; if (HttpStatus.SC_NO_CONTENT == getStatusCode()) { throw new NoContentException(); } @@ -279,8 +285,21 @@ public abstract class AbstractODataResponse implements ODataResponse { LOG.error("Error streaming payload response", e); throw new IllegalStateException(e); } + } else if (payload != null) { + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + try { + org.apache.commons.io.IOUtils.copy(payload, byteArrayOutputStream); + if(inputContent == null){ + inputContent = byteArrayOutputStream.toByteArray(); + } + inputStream = new ByteArrayInputStream(inputContent); + return inputStream; + } catch (IOException e) { + HttpClientUtils.closeQuietly(res); + LOG.error("Error retrieving payload", e); + throw new ODataRuntimeException(e); + } } - return payload; } }