Repository: olingo-odata4 Updated Branches: refs/heads/olingo263 726fbe52e -> 585ddaead
[OLINGO-272]fix NonRepeatableRequestException by using repeatable entity Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/d7658017 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/d7658017 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/d7658017 Branch: refs/heads/olingo263 Commit: d76580170b9be6aad9d0763c372a512cfe196660 Parents: 6e72b1f Author: challenh <[email protected]> Authored: Mon May 5 15:45:49 2014 +0800 Committer: challenh <[email protected]> Committed: Mon May 5 15:45:49 2014 +0800 ---------------------------------------------------------------------- .../communication/request/AbstractRequest.java | 16 +++++---- .../http/ProxyWrapperHttpClientFactory.java | 4 +++ .../apache/olingo/client/core/uri/URIUtils.java | 36 ++++++++++++++++---- .../commons/core/data/AbstractODataError.java | 19 +++++++++++ 4 files changed, 63 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7658017/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AbstractRequest.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AbstractRequest.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AbstractRequest.java index 5779e08..31b65b3 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AbstractRequest.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/AbstractRequest.java @@ -16,6 +16,8 @@ package org.apache.olingo.client.core.communication.request; import java.io.IOException; + +import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpUriRequest; @@ -68,15 +70,13 @@ public abstract class AbstractRequest { protected void checkResponse( final CommonODataClient<?> odataClient, final HttpResponse response, final String accept) { - if (response.getStatusLine().getStatusCode() >= 500) { - throw new ODataServerErrorException(response.getStatusLine()); - } else if (response.getStatusLine().getStatusCode() >= 400) { + if (response.getStatusLine().getStatusCode() >= 400) { try { final HttpEntity httpEntity = response.getEntity(); if (httpEntity == null) { throw new ODataClientErrorException(response.getStatusLine()); } else { - final boolean isXML = accept.contains("json"); + final boolean isXML = !accept.contains("json"); ODataError error; try { error = odataClient.getReader().readError(httpEntity.getContent(), isXML); @@ -87,8 +87,12 @@ public abstract class AbstractRequest { response.getStatusLine().getReasonPhrase(), isXML); } - - throw new ODataClientErrorException(response.getStatusLine(), error); + + if (response.getStatusLine().getStatusCode() >= 500) { + throw new ODataServerErrorException(response.getStatusLine()); + } else { + throw new ODataClientErrorException(response.getStatusLine(), error); + } } } catch (IOException e) { throw new HttpClientException( http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7658017/lib/client-core/src/main/java/org/apache/olingo/client/core/http/ProxyWrapperHttpClientFactory.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/http/ProxyWrapperHttpClientFactory.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/http/ProxyWrapperHttpClientFactory.java index ab57901..f76b083 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/http/ProxyWrapperHttpClientFactory.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/http/ProxyWrapperHttpClientFactory.java @@ -62,6 +62,10 @@ public class ProxyWrapperHttpClientFactory implements HttpClientFactory { this.proxyPassword = proxyPassword; this.wrapped = wrapped; } + + public DefaultHttpClientFactory getWrappedHttpClientFactory(){ + return this.wrapped; + } @Override public HttpClient createHttpClient(final HttpMethod method, final URI uri) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7658017/lib/client-core/src/main/java/org/apache/olingo/client/core/uri/URIUtils.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/uri/URIUtils.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/uri/URIUtils.java index 1cce282..a4cfce3 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/uri/URIUtils.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/uri/URIUtils.java @@ -18,7 +18,6 @@ */ package org.apache.olingo.client.core.uri; -import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; @@ -32,13 +31,20 @@ import java.util.Iterator; import java.util.Map; import java.util.UUID; import java.util.regex.Pattern; + import javax.xml.datatype.Duration; + import org.apache.commons.codec.binary.Hex; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.http.entity.AbstractHttpEntity; +import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.InputStreamEntity; import org.apache.olingo.client.api.CommonODataClient; +import org.apache.olingo.client.api.http.HttpClientFactory; +import org.apache.olingo.client.core.http.BasicAuthHttpClientFactory; +import org.apache.olingo.client.core.http.ProxyWrapperHttpClientFactory; import org.apache.olingo.commons.api.Constants; import org.apache.olingo.commons.api.edm.EdmEntityContainer; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; @@ -383,9 +389,26 @@ public final class URIUtils { return value; } - public static InputStreamEntity buildInputStreamEntity(final CommonODataClient<?> client, final InputStream input) { - InputStreamEntity entity; - if (client.getConfiguration().isUseChuncked()) { + private static boolean shouldUseRepeatableHttpBodyEntry(final CommonODataClient<?> client) + { + // returns true for authentication request in case of http401 which needs retry so requires being repeatable. + HttpClientFactory httpclientFactory = client.getConfiguration().getHttpClientFactory(); + if(httpclientFactory instanceof BasicAuthHttpClientFactory){ + return true; + } else if (httpclientFactory instanceof ProxyWrapperHttpClientFactory){ + ProxyWrapperHttpClientFactory tmp = (ProxyWrapperHttpClientFactory)httpclientFactory; + if(tmp.getWrappedHttpClientFactory() instanceof BasicAuthHttpClientFactory){ + return true; + } + } + + return false; + } + + public static AbstractHttpEntity buildInputStreamEntity(final CommonODataClient<?> client, final InputStream input) { + AbstractHttpEntity entity = null; + boolean repeatableRequired= shouldUseRepeatableHttpBodyEntry(client); + if (!repeatableRequired) { entity = new InputStreamEntity(input, -1); } else { byte[] bytes = new byte[0]; @@ -395,10 +418,11 @@ public final class URIUtils { LOG.error("While reading input for not chunked encoding", e); } - entity = new InputStreamEntity(new ByteArrayInputStream(bytes), bytes.length); + entity = new ByteArrayEntity(bytes); } + + // both entities can be sent in chunked way or not entity.setChunked(client.getConfiguration().isUseChuncked()); - return entity; } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/d7658017/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractODataError.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractODataError.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractODataError.java index e5324d8..931a38b 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractODataError.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/data/AbstractODataError.java @@ -20,6 +20,25 @@ package org.apache.olingo.commons.core.data; import org.apache.olingo.commons.api.domain.ODataError; +// TODO p2 supports V4: +// { +// "error": { +// "code": "501", +// "message": "Unsupported functionality", +// "target": "query", +// "details": [ +// { +// "code": "301", +// "target": "$search" +// "message": "$search query option not supported", +// } +// ] +// "innererror": { +// "trace": [...], +// "context": {...} +// } +// } +// } public abstract class AbstractODataError implements ODataError { private String code;
