[OLINGO-773] Replaced IOUtils with separate method

This commit is contained in:
Michael Bolz 2015-09-15 13:55:16 +02:00
parent 9c3ca381e2
commit 48810032a1
4 changed files with 56 additions and 22 deletions

View File

@ -80,6 +80,7 @@
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>

View File

@ -23,10 +23,13 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; 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.LinkedList;
import java.util.List; 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;
import org.apache.olingo.commons.api.data.ContextURL.Suffix; import org.apache.olingo.commons.api.data.ContextURL.Suffix;
import org.apache.olingo.commons.api.data.Entity; 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.EdmNavigationPropertyBinding;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.EdmProperty; 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.format.ContentType;
import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; 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; import org.apache.olingo.server.core.uri.parser.UriParserSemanticException;
public class DataRequest extends ServiceRequest { public class DataRequest extends ServiceRequest {
public static final int DEFAULT_BUFFER_SIZE = 8192;
protected UriResourceEntitySet uriResourceEntitySet; protected UriResourceEntitySet uriResourceEntitySet;
private boolean countRequest; private boolean countRequest;
private UriResourceProperty uriResourceProperty; private UriResourceProperty uriResourceProperty;
@ -623,7 +628,7 @@ public class DataRequest extends ServiceRequest {
Property property = new Property( Property property = new Property(
edmProperty.getType().getFullQualifiedName().getFullQualifiedNameAsString(), edmProperty.getType().getFullQualifiedName().getFullQualifiedNameAsString(),
edmProperty.getName()); edmProperty.getName());
property.setValue(ValueType.PRIMITIVE, getRawValueFromClient(edmProperty)); property.setValue(ValueType.PRIMITIVE, getRawValueFromClient());
handler.updateProperty(DataRequest.this, property, false, handler.updateProperty(DataRequest.this, property, false,
getETag(), propertyResponse); getETag(), propertyResponse);
} }
@ -701,25 +706,26 @@ public class DataRequest extends ServiceRequest {
return deserializer.property(getODataRequest().getBody(), edmProperty).getProperty(); return deserializer.property(getODataRequest().getBody(), edmProperty).getProperty();
} }
private Object getRawValueFromClient( private Object getRawValueFromClient() throws DeserializerException {
EdmProperty edmProperty) throws DeserializerException { InputStream input = getODataRequest().getBody();
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] buffer = new byte[1024]; if (input != null) {
int read = 0;
do {
try { try {
read = IOUtils.read(getODataRequest().getBody(), buffer, 0, 1024); ByteBuffer inBuffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
bos.write(buffer, 0, read); ReadableByteChannel ic = Channels.newChannel(input);
if (read < 1024) { WritableByteChannel oc = Channels.newChannel(buffer);
break; while (ic.read(inBuffer) > 0) {
inBuffer.flip();
oc.write(inBuffer);
inBuffer.rewind();
} }
return buffer.toByteArray();
} catch (IOException e) { } catch (IOException e) {
throw new DeserializerException("Error reading raw value", throw new ODataRuntimeException("Error on reading content");
SerializerException.MessageKeys.IO_EXCEPTION);
} }
} while (true); }
return bos.toByteArray(); return null;
} }
static ContextURL.Builder buildEntitySetContextURL(UriHelper helper, static ContextURL.Builder buildEntitySetContextURL(UriHelper helper,
EdmBindingTarget edmEntitySet, List<UriParameter> keyPredicates, UriInfo uriInfo, EdmBindingTarget edmEntitySet, List<UriParameter> keyPredicates, UriInfo uriInfo,

View File

@ -58,6 +58,7 @@
<dependency> <dependency>
<groupId>commons-io</groupId> <groupId>commons-io</groupId>
<artifactId>commons-io</artifactId> <artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -18,11 +18,17 @@
*/ */
package org.apache.olingo.server.core.debug; package org.apache.olingo.server.core.debug;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.Writer; 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.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.commons.api.http.HttpHeader;
import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.ODataResponse;
@ -33,9 +39,9 @@ import com.fasterxml.jackson.core.JsonGenerator;
*/ */
public class DebugTabBody implements DebugTab { public class DebugTabBody implements DebugTab {
private static enum ResponseContent { private enum ResponseContent {
JSON, XML, TEXT, IMAGE JSON, XML, TEXT, IMAGE
}; }
private final ODataResponse response; private final ODataResponse response;
private final ResponseContent responseContent; private final ResponseContent responseContent;
@ -82,13 +88,13 @@ public class DebugTabBody implements DebugTab {
String contentString; String contentString;
switch (responseContent) { switch (responseContent) {
case IMAGE: case IMAGE:
contentString = Base64.encodeBase64String(IOUtils.toString(response.getContent()).getBytes("UTF-8")); contentString = Base64.encodeBase64String(streamToBytes(response.getContent()));
break; break;
case JSON: case JSON:
case XML: case XML:
case TEXT: case TEXT:
default: default:
contentString = IOUtils.toString(response.getContent(), "UTF-8"); contentString = new String(streamToBytes(response.getContent()), "UTF-8");
break; break;
} }
return contentString; return contentString;
@ -126,4 +132,24 @@ public class DebugTabBody implements DebugTab {
break; 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;
}
} }