[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>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>

View File

@ -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,24 +706,25 @@ 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,

View File

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

View File

@ -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;
}
}