You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2015/09/15 13:55:26 UTC
olingo-odata4 git commit: [OLINGO-773] Replaced IOUtils with separate
method
Repository: olingo-odata4
Updated Branches:
refs/heads/OLINGO-773_ReplaceCommonsIo 9c3ca381e -> 48810032a
[OLINGO-773] Replaced IOUtils with separate method
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/48810032
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/48810032
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/48810032
Branch: refs/heads/OLINGO-773_ReplaceCommonsIo
Commit: 48810032a101a56cbad58f9314b6ea645cc0d1b0
Parents: 9c3ca38
Author: Michael Bolz <mi...@sap.com>
Authored: Tue Sep 15 13:55:16 2015 +0200
Committer: Michael Bolz <mi...@sap.com>
Committed: Tue Sep 15 13:55:16 2015 +0200
----------------------------------------------------------------------
lib/server-core-ext/pom.xml | 1 +
.../server/core/requests/DataRequest.java | 40 +++++++++++---------
lib/server-core/pom.xml | 1 +
.../olingo/server/core/debug/DebugTabBody.java | 36 +++++++++++++++---
4 files changed, 56 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/48810032/lib/server-core-ext/pom.xml
----------------------------------------------------------------------
diff --git a/lib/server-core-ext/pom.xml b/lib/server-core-ext/pom.xml
index cc0f3b1..616ac16 100644
--- a/lib/server-core-ext/pom.xml
+++ b/lib/server-core-ext/pom.xml
@@ -80,6 +80,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
+ <scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/48810032/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
----------------------------------------------------------------------
diff --git a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
index da177bb..a14d21a 100644
--- a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
+++ b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/requests/DataRequest.java
@@ -23,10 +23,13 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
import java.util.LinkedList;
import java.util.List;
-import org.apache.commons.io.IOUtils;
import org.apache.olingo.commons.api.data.ContextURL;
import org.apache.olingo.commons.api.data.ContextURL.Suffix;
import org.apache.olingo.commons.api.data.Entity;
@@ -40,6 +43,7 @@ import org.apache.olingo.commons.api.edm.EdmNavigationProperty;
import org.apache.olingo.commons.api.edm.EdmNavigationPropertyBinding;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.EdmProperty;
+import org.apache.olingo.commons.api.ex.ODataRuntimeException;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory;
@@ -82,6 +86,7 @@ import org.apache.olingo.server.core.responses.StreamResponse;
import org.apache.olingo.server.core.uri.parser.UriParserSemanticException;
public class DataRequest extends ServiceRequest {
+ public static final int DEFAULT_BUFFER_SIZE = 8192;
protected UriResourceEntitySet uriResourceEntitySet;
private boolean countRequest;
private UriResourceProperty uriResourceProperty;
@@ -623,7 +628,7 @@ public class DataRequest extends ServiceRequest {
Property property = new Property(
edmProperty.getType().getFullQualifiedName().getFullQualifiedNameAsString(),
edmProperty.getName());
- property.setValue(ValueType.PRIMITIVE, getRawValueFromClient(edmProperty));
+ property.setValue(ValueType.PRIMITIVE, getRawValueFromClient());
handler.updateProperty(DataRequest.this, property, false,
getETag(), propertyResponse);
}
@@ -701,25 +706,26 @@ public class DataRequest extends ServiceRequest {
return deserializer.property(getODataRequest().getBody(), edmProperty).getProperty();
}
- private Object getRawValueFromClient(
- EdmProperty edmProperty) throws DeserializerException {
- ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
- byte[] buffer = new byte[1024];
- int read = 0;
- do {
+ private Object getRawValueFromClient() throws DeserializerException {
+ InputStream input = getODataRequest().getBody();
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ if (input != null) {
try {
- read = IOUtils.read(getODataRequest().getBody(), buffer, 0, 1024);
- bos.write(buffer, 0, read);
- if (read < 1024) {
- break;
+ ByteBuffer inBuffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
+ ReadableByteChannel ic = Channels.newChannel(input);
+ WritableByteChannel oc = Channels.newChannel(buffer);
+ while (ic.read(inBuffer) > 0) {
+ inBuffer.flip();
+ oc.write(inBuffer);
+ inBuffer.rewind();
}
+ return buffer.toByteArray();
} catch (IOException e) {
- throw new DeserializerException("Error reading raw value",
- SerializerException.MessageKeys.IO_EXCEPTION);
+ throw new ODataRuntimeException("Error on reading content");
}
- } while (true);
- return bos.toByteArray();
- }
+ }
+ return null;
+ }
static ContextURL.Builder buildEntitySetContextURL(UriHelper helper,
EdmBindingTarget edmEntitySet, List<UriParameter> keyPredicates, UriInfo uriInfo,
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/48810032/lib/server-core/pom.xml
----------------------------------------------------------------------
diff --git a/lib/server-core/pom.xml b/lib/server-core/pom.xml
index cb0ab31..2763fd1 100644
--- a/lib/server-core/pom.xml
+++ b/lib/server-core/pom.xml
@@ -58,6 +58,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
+ <scope>test</scope>
</dependency>
<dependency>
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/48810032/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java
index 36c1d05..d290fc3 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java
@@ -18,11 +18,17 @@
*/
package org.apache.olingo.server.core.debug;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.Writer;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.io.IOUtils;
+import org.apache.olingo.commons.api.ex.ODataRuntimeException;
import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.server.api.ODataResponse;
@@ -33,9 +39,9 @@ import com.fasterxml.jackson.core.JsonGenerator;
*/
public class DebugTabBody implements DebugTab {
- private static enum ResponseContent {
+ private enum ResponseContent {
JSON, XML, TEXT, IMAGE
- };
+ }
private final ODataResponse response;
private final ResponseContent responseContent;
@@ -82,13 +88,13 @@ public class DebugTabBody implements DebugTab {
String contentString;
switch (responseContent) {
case IMAGE:
- contentString = Base64.encodeBase64String(IOUtils.toString(response.getContent()).getBytes("UTF-8"));
+ contentString = Base64.encodeBase64String(streamToBytes(response.getContent()));
break;
case JSON:
case XML:
case TEXT:
default:
- contentString = IOUtils.toString(response.getContent(), "UTF-8");
+ contentString = new String(streamToBytes(response.getContent()), "UTF-8");
break;
}
return contentString;
@@ -126,4 +132,24 @@ public class DebugTabBody implements DebugTab {
break;
}
}
+
+ private byte[] streamToBytes(InputStream input) {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ if (input != null) {
+ try {
+ ByteBuffer inBuffer = ByteBuffer.allocate(8192);
+ ReadableByteChannel ic = Channels.newChannel(input);
+ WritableByteChannel oc = Channels.newChannel(buffer);
+ while (ic.read(inBuffer) > 0) {
+ inBuffer.flip();
+ oc.write(inBuffer);
+ inBuffer.rewind();
+ }
+ return buffer.toByteArray();
+ } catch (IOException e) {
+ throw new ODataRuntimeException("Error on reading request content");
+ }
+ }
+ return null;
+ }
}