diff --git a/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/batch/ODataBatchResponseItem.java b/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/batch/ODataBatchResponseItem.java index 982bd8e4b..388250492 100644 --- a/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/batch/ODataBatchResponseItem.java +++ b/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/batch/ODataBatchResponseItem.java @@ -49,6 +49,13 @@ public interface ODataBatchResponseItem extends Iterator { */ boolean isChangeset(); + /** + * Checks if the current item is a braking item like as error item or asynchronous response part. + * + * @return 'TRUE' if breaking; 'FALSE' otherwise. + */ + boolean isBreakingitem(); + /** * Closes the current batch responses item including all wrapped OData responses. */ diff --git a/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/batch/v4/ODataBatchRequest.java b/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/batch/v4/ODataBatchRequest.java index 9498d9943..817546552 100644 --- a/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/batch/v4/ODataBatchRequest.java +++ b/lib/client-api/src/main/java/org/apache/olingo/client/api/communication/request/batch/v4/ODataBatchRequest.java @@ -27,4 +27,6 @@ import org.apache.olingo.client.api.communication.response.ODataBatchResponse; */ public interface ODataBatchRequest extends CommonODataBatchRequest, ODataStreamedRequest { + + ODataBatchRequest continueOnError(); } diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/AbstractBatchRequestFactory.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/AbstractBatchRequestFactory.java index 1be8f5c58..631dbed8e 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/AbstractBatchRequestFactory.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/AbstractBatchRequestFactory.java @@ -22,9 +22,9 @@ public abstract class AbstractBatchRequestFactory implements CommonBatchRequestF private static final long serialVersionUID = -3875283254713404483L; - protected final CommonODataClient client; + protected final CommonODataClient client; - protected AbstractBatchRequestFactory(final CommonODataClient client) { + protected AbstractBatchRequestFactory(final CommonODataClient client) { this.client = client; } } diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/AbstractODataBatchResponseItem.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/AbstractODataBatchResponseItem.java index 79b8f0704..a21e0aa61 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/AbstractODataBatchResponseItem.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/AbstractODataBatchResponseItem.java @@ -67,6 +67,13 @@ public abstract class AbstractODataBatchResponseItem implements ODataBatchRespon */ protected boolean closed = false; + /** + * Last cached OData response. + */ + protected ODataResponse current; + + protected boolean breakingitem = false; + /** * Constructor. * @@ -74,6 +81,7 @@ public abstract class AbstractODataBatchResponseItem implements ODataBatchRespon */ public AbstractODataBatchResponseItem(boolean isChangeset) { this.changeset = isChangeset; + this.current = null; } /** @@ -135,7 +143,15 @@ public abstract class AbstractODataBatchResponseItem implements ODataBatchRespon expectedItemsIterator = responses.values().iterator(); } - return expectedItemsIterator.hasNext(); + return !breakingitem && expectedItemsIterator.hasNext(); + } + + /** + * {@inheritDoc } + */ + @Override + public boolean isBreakingitem() { + return breakingitem; } /** diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/ODataChangesetResponseItem.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/ODataChangesetResponseItem.java index 63e7cadb8..53eeb16d2 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/ODataChangesetResponseItem.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/ODataChangesetResponseItem.java @@ -31,11 +31,6 @@ import org.apache.olingo.client.core.communication.response.batch.ODataBatchErro */ public class ODataChangesetResponseItem extends AbstractODataBatchResponseItem { - /** - * Last cached OData response. - */ - private ODataResponse current = null; - private boolean unexpected = false; /** @@ -62,7 +57,12 @@ public class ODataChangesetResponseItem extends AbstractODataBatchResponseItem { throw new IllegalStateException("Invalid request - the item has been closed"); } + if (!hasNext()) { + throw new NoSuchElementException("No item found"); + } + if (unexpected) { + breakingitem = true; return nextUnexpected(); } else { return nextExpected(); @@ -70,12 +70,8 @@ public class ODataChangesetResponseItem extends AbstractODataBatchResponseItem { } private ODataResponse nextExpected() { - if (hasNext()) { - // consume item for condition above (like a counter ...) - expectedItemsIterator.next(); - } else { - throw new NoSuchElementException("No item found"); - } + // consume item for condition above (used like a counter ...) + expectedItemsIterator.next(); final Map> nextItemHeaders = ODataBatchUtilities.nextItemHeaders(batchLineIterator, boundary); @@ -109,10 +105,8 @@ public class ODataChangesetResponseItem extends AbstractODataBatchResponseItem { current.initFromBatch(responseLine, headers, batchLineIterator, boundary); if (current.getStatusCode() >= 400) { - // found error .... consume expeted items - while (expectedItemsIterator.hasNext()) { - expectedItemsIterator.next(); - } + // found error .... + breakingitem = true; } return current; @@ -127,7 +121,8 @@ public class ODataChangesetResponseItem extends AbstractODataBatchResponseItem { final Map> headers = ODataBatchUtilities.readHeaders(batchLineIterator); LOG.debug("Retrieved item headers {}", headers); - return new ODataBatchErrorResponse(responseLine, headers, batchLineIterator, boundary); + current = new ODataBatchErrorResponse(responseLine, headers, batchLineIterator, boundary); + return current; } throw new IllegalStateException("Expected item not found"); diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/ODataRetrieveResponseItem.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/ODataRetrieveResponseItem.java index 2a45bb23e..8919ffebf 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/ODataRetrieveResponseItem.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/ODataRetrieveResponseItem.java @@ -48,25 +48,28 @@ public class ODataRetrieveResponseItem extends AbstractODataBatchResponseItem { throw new IllegalStateException("Invalid request - the item has been closed"); } + if (!hasNext()) { + throw new NoSuchElementException("No item found"); + } + final Map.Entry responseLine = ODataBatchUtilities.readResponseLine(batchLineIterator); LOG.debug("Retrieved item response {}", responseLine); final Map> headers = ODataBatchUtilities.readHeaders(batchLineIterator); LOG.debug("Retrieved item headers {}", headers); - final ODataResponse res; - if (responseLine.getKey() >= 400) { // generate error response - res = new ODataBatchErrorResponse(responseLine, headers, batchLineIterator, boundary); + current = new ODataBatchErrorResponse(responseLine, headers, batchLineIterator, boundary); + breakingitem = true; } else { if (!hasNext()) { throw new NoSuchElementException("No item found"); } - res = expectedItemsIterator.next().initFromBatch(responseLine, headers, batchLineIterator, boundary); + current = expectedItemsIterator.next().initFromBatch(responseLine, headers, batchLineIterator, boundary); } - return res; + return current; } /** diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v3/BatchRequestFactoryImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v3/BatchRequestFactoryImpl.java index 204974355..d79653271 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v3/BatchRequestFactoryImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v3/BatchRequestFactoryImpl.java @@ -34,6 +34,7 @@ public class BatchRequestFactoryImpl extends AbstractBatchRequestFactory @Override public ODataBatchRequest getBatchRequest(final String serviceRoot) { - return new ODataBatchRequestImpl(client, client.getURIBuilder(serviceRoot).appendBatchSegment().build()); + return new ODataBatchRequestImpl( + (ODataClient) client, client.getURIBuilder(serviceRoot).appendBatchSegment().build()); } } diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v3/ODataBatchRequestImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v3/ODataBatchRequestImpl.java index dd3f365ec..c4b0bdb64 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v3/ODataBatchRequestImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v3/ODataBatchRequestImpl.java @@ -24,12 +24,12 @@ import java.util.Iterator; import java.util.concurrent.TimeUnit; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; -import org.apache.olingo.client.api.CommonODataClient; import org.apache.olingo.client.api.communication.request.ODataStreamedRequest; import org.apache.olingo.client.api.communication.request.batch.ODataBatchResponseItem; import org.apache.olingo.client.api.communication.request.batch.v3.BatchStreamManager; import org.apache.olingo.client.api.communication.request.batch.v3.ODataBatchRequest; import org.apache.olingo.client.api.communication.response.ODataBatchResponse; +import org.apache.olingo.client.api.v3.ODataClient; import org.apache.olingo.client.core.communication.request.batch.AbstractBatchStreamManager; import org.apache.olingo.client.core.communication.request.batch.AbstractODataBatchRequest; import org.apache.olingo.client.core.communication.request.batch.v3.ODataBatchRequestImpl.BatchStreamManagerImpl; @@ -44,7 +44,7 @@ public class ODataBatchRequestImpl extends AbstractODataBatchRequest implements ODataBatchRequest, ODataStreamedRequest { - public ODataBatchRequestImpl(final CommonODataClient odataClient, final URI uri) { + public ODataBatchRequestImpl(final ODataClient odataClient, final URI uri) { super(odataClient, uri); } diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v4/BatchRequestFactoryImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v4/BatchRequestFactoryImpl.java index a08f7f155..bb448122a 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v4/BatchRequestFactoryImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v4/BatchRequestFactoryImpl.java @@ -34,6 +34,7 @@ public class BatchRequestFactoryImpl extends AbstractBatchRequestFactory @Override public ODataBatchRequest getBatchRequest(final String serviceRoot) { - return new ODataBatchRequestImpl(client, client.getURIBuilder(serviceRoot).appendBatchSegment().build()); + return new ODataBatchRequestImpl( + (ODataClient) client, client.getURIBuilder(serviceRoot).appendBatchSegment().build()); } } diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v4/ODataBatchRequestImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v4/ODataBatchRequestImpl.java index 525183814..648cb3532 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v4/ODataBatchRequestImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/batch/v4/ODataBatchRequestImpl.java @@ -24,12 +24,14 @@ import java.util.Iterator; import java.util.concurrent.TimeUnit; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; -import org.apache.olingo.client.api.CommonODataClient; +import org.apache.olingo.client.api.communication.header.HeaderName; +import org.apache.olingo.client.api.communication.header.ODataPreferences; import org.apache.olingo.client.api.communication.request.batch.ODataBatchResponseItem; import org.apache.olingo.client.api.communication.request.batch.v4.BatchStreamManager; import org.apache.olingo.client.api.communication.request.batch.v4.ODataBatchRequest; import org.apache.olingo.client.api.communication.request.batch.v4.ODataOutsideUpdate; import org.apache.olingo.client.api.communication.response.ODataBatchResponse; +import org.apache.olingo.client.api.v4.ODataClient; import org.apache.olingo.client.core.communication.request.batch.AbstractBatchStreamManager; import org.apache.olingo.client.core.communication.request.batch.AbstractODataBatchRequest; import org.apache.olingo.client.core.communication.response.AbstractODataResponse; @@ -43,7 +45,9 @@ public class ODataBatchRequestImpl extends AbstractODataBatchRequest implements ODataBatchRequest { - public ODataBatchRequestImpl(final CommonODataClient odataClient, final URI uri) { + private boolean continueOnError = false; + + public ODataBatchRequestImpl(final ODataClient odataClient, final URI uri) { super(odataClient, uri); setAccept(ContentType.MULTIPART_MIXED); } @@ -74,6 +78,13 @@ public class ODataBatchRequestImpl return this; } + @Override + public ODataBatchRequest continueOnError() { + addCustomHeader(HeaderName.prefer, new ODataPreferences(odataClient.getServiceVersion()).continueOnError()); + continueOnError = true; + return this; + } + /** * Batch request payload management. */ @@ -126,7 +137,7 @@ public class ODataBatchRequestImpl */ @Override public Iterator getBody() { - return new ODataBatchResponseManager(this, expectedResItems); + return new ODataBatchResponseManager(this, expectedResItems, continueOnError); } } } \ No newline at end of file diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/streamed/AbstractODataStreamedRequest.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/streamed/AbstractODataStreamedRequest.java index 056579fdb..0a78d834e 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/streamed/AbstractODataStreamedRequest.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/streamed/AbstractODataStreamedRequest.java @@ -68,7 +68,7 @@ public abstract class AbstractODataStreamedRequest odataClient, final HttpMethod method, final URI uri) { super(odataClient, ODataMediaFormat.class, method, uri); diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/batch/ODataBatchResponseManager.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/batch/ODataBatchResponseManager.java index b91dd8ab9..9db555ed1 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/batch/ODataBatchResponseManager.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/response/batch/ODataBatchResponseManager.java @@ -66,13 +66,26 @@ public class ODataBatchResponseManager implements Iterator expectedItems) { + public ODataBatchResponseManager( + final ODataBatchResponse res, + final List expectedItems) { + this(res, expectedItems, false); + } + + public ODataBatchResponseManager( + final ODataBatchResponse res, + final List expectedItems, + final boolean continueOnError) { + this.continueOnError = continueOnError; + try { this.expectedItemsIterator = expectedItems.iterator(); this.batchLineIterator = new ODataBatchLineIteratorImpl( @@ -93,7 +106,7 @@ public class ODataBatchResponseManager implements Iterator iter = response.getBody(); // retrieve the first item (ODataRetrieve) - ODataBatchResponseItem item = response.getBody().next(); + ODataBatchResponseItem item = iter.next(); ODataChangesetResponseItem retitem = (ODataChangesetResponseItem) item; ODataResponse res = retitem.next(); @@ -163,6 +162,9 @@ public class BatchTestITCase extends AbstractTestITCase { assertEquals("Not Found", res.getStatusMessage()); assertEquals(Integer.valueOf(3), Integer.valueOf( res.getHeader(ODataBatchConstants.CHANGESET_CONTENT_ID_NAME).iterator().next())); + + assertFalse(retitem.hasNext()); + assertFalse(iter.hasNext()); } @Test @@ -179,10 +181,7 @@ public class BatchTestITCase extends AbstractTestITCase { // create your request final ODataBatchRequest request = client.getBatchRequestFactory().getBatchRequest(testStaticServiceRootURL); request.setAccept(ACCEPT); - - if (continueOnError) { - request.addCustomHeader(HeaderName.prefer, new ODataPreferences(client.getServiceVersion()).continueOnError()); - } + request.continueOnError(); final BatchStreamManager streamManager = request.execute();