diff --git a/lib/server-core-ext/pom.xml b/lib/server-core-ext/pom.xml index cc0f3b18c..616ac1657 100644 --- a/lib/server-core-ext/pom.xml +++ b/lib/server-core-ext/pom.xml @@ -80,6 +80,7 @@ commons-io commons-io + test org.apache.httpcomponents 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 da177bb13..a14d21a88 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 keyPredicates, UriInfo uriInfo, diff --git a/lib/server-core/pom.xml b/lib/server-core/pom.xml index cb0ab3119..2763fd169 100644 --- a/lib/server-core/pom.xml +++ b/lib/server-core/pom.xml @@ -58,6 +58,7 @@ commons-io commons-io + test 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 36c1d05e4..d290fc385 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; + } }