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/07/16 12:53:11 UTC

olingo-odata2 git commit: [OLINGO-725] Make buffer size configurable

Repository: olingo-odata2
Updated Branches:
  refs/heads/master ea04bfc91 -> 5cbc7506c


[OLINGO-725] Make buffer size configurable


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/5cbc7506
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/5cbc7506
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/5cbc7506

Branch: refs/heads/master
Commit: 5cbc7506c88a57680cfec4eacfd7ec99259c1fa6
Parents: ea04bfc
Author: Michael Bolz <mi...@sap.com>
Authored: Thu Jul 16 12:50:58 2015 +0200
Committer: Michael Bolz <mi...@sap.com>
Committed: Thu Jul 16 12:50:58 2015 +0200

----------------------------------------------------------------------
 .../odata2/core/servlet/ODataServlet.java       | 49 +++++++++++---
 .../odata2/core/servlet/ODataServletTest.java   | 68 ++++++++++++++++++++
 2 files changed, 108 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/5cbc7506/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
index f3c8a20..d2ee57f 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
@@ -50,6 +50,11 @@ public class ODataServlet extends HttpServlet {
   private static final String HTTP_METHOD_HEAD = "HEAD";
 
   /**
+   * Label used in web.xml to assign servlet init parameter for a path split (service resolution).
+   */
+  private static final String BUFFER_SIZE = "org.apache.olingo.odata2.core.servlet.buffer.size";
+
+  /**
    * 
    */
   private static final long serialVersionUID = 1L;
@@ -249,17 +254,10 @@ public class ODataServlet extends HttpServlet {
     Object entity = response.getEntity();
     if (entity != null) {
       ServletOutputStream out = resp.getOutputStream();
-      int contentLength = 0;
+      int contentLength;
 
       if (entity instanceof InputStream) {
-        int len;
-        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
-        InputStream stream = (InputStream) entity;
-        while ((len = stream.read(buffer)) != -1) {
-          contentLength += len;
-          out.write(buffer, 0, len);
-        }
-        stream.close();
+        contentLength = handleStream((InputStream) entity, out);
       } else if (entity instanceof String) {
         String body = (String) entity;
         final byte[] entityBytes = body.getBytes(DEFAULT_READ_CHARSET);
@@ -284,6 +282,39 @@ public class ODataServlet extends HttpServlet {
     }
   }
 
+  private int handleStream(InputStream stream, ServletOutputStream out) throws IOException {
+    int contentLength = 0;
+    byte[] buffer = getBuffer();
+
+    try {
+      int len;
+      while ((len = stream.read(buffer)) != -1) {
+        contentLength += len;
+        out.write(buffer, 0, len);
+      }
+    } finally {
+      stream.close();
+    }
+    return contentLength;
+  }
+
+  private byte[] getBuffer() {
+    int bufferSize = DEFAULT_BUFFER_SIZE;
+    String bufSizeInit = getInitParameter(BUFFER_SIZE);
+    if(bufSizeInit != null) {
+      try {
+        bufferSize = Integer.parseInt(bufSizeInit);
+        if(bufferSize <= 0) {
+          bufferSize = DEFAULT_BUFFER_SIZE;
+        }
+      } catch (NumberFormatException ignored) {
+        // this exception is ignored because if parameter is not parse able the default is used
+      }
+    }
+
+    return new byte[bufferSize];
+  }
+
   private void createNotImplementedResponse(final HttpServletRequest req, final MessageReference messageReference,
       final HttpServletResponse resp, ODataServiceFactory serviceFactory) throws IOException {
     // RFC 2616, 5.1.1: "An origin server SHOULD return the status code [...]

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/5cbc7506/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/servlet/ODataServletTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/servlet/ODataServletTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/servlet/ODataServletTest.java
index da985e5..d814b5a 100644
--- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/servlet/ODataServletTest.java
+++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/servlet/ODataServletTest.java
@@ -19,10 +19,15 @@
 package org.apache.olingo.odata2.core.servlet;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.nio.charset.Charset;
 import java.util.Collections;
+import java.util.HashSet;
+import java.util.Random;
 
 import javax.servlet.GenericServlet;
 import javax.servlet.ServletConfig;
@@ -137,6 +142,69 @@ public class ODataServletTest {
     Mockito.verify(respMock).setContentLength(content.getBytes("utf-8").length);
   }
 
+
+  @Test
+  public void inputStreamResponse() throws Exception {
+    testInputStreamResponse("123", "utf-8", null);
+    testInputStreamResponse("1234567890", "utf-8", "5");
+    testInputStreamResponse(testData(200000), "utf-8", null);
+    testInputStreamResponse(testData(200000), "utf-8", "8192");
+    testInputStreamResponse(testData(200000), "utf-8", "32768");
+    //
+    testInputStreamResponse("üäö", "iso-8859-1", "8192");
+    testInputStreamResponse(testData(200000), "iso-8859-1", "32768");
+    testInputStreamResponse(testData(200000), "iso-8859-1", "8192");
+    testInputStreamResponse(testData(200000), "iso-8859-1", "32768");
+    //
+    testInputStreamResponse("1234567890", "utf-8", "5");
+    testInputStreamResponse("1234567890", "utf-8", "ABD");
+    testInputStreamResponse("1234567890", "utf-8", "");
+    testInputStreamResponse("1234567890", "utf-8", "-29");
+  }
+
+
+  private void testInputStreamResponse(String content, String encoding, String bufferSize) throws Exception {
+    ODataServlet servlet = new ODataServlet();
+    Mockito.when(configMock.getInitParameter(
+        "org.apache.olingo.odata2.core.servlet.buffer.size")).thenReturn(bufferSize);
+    prepareServlet(servlet);
+
+    final Charset charset = Charset.forName(encoding);
+    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
+    final ServletOutputStream out = new ServletOutputStream() {
+      @Override
+      public void write(int i) throws IOException {
+        bout.write(i);
+      }
+    };
+    Mockito.when(respMock.getOutputStream()).thenReturn(out);
+
+    HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
+    Mockito.when(servletResponse.getOutputStream()).thenReturn(out);
+
+    ODataResponse odataResponse = Mockito.mock(ODataResponse.class);
+    Mockito.when(odataResponse.getStatus()).thenReturn(HttpStatusCodes.ACCEPTED);
+    Mockito.when(odataResponse.getHeaderNames()).thenReturn(new HashSet<String>());
+    InputStream input = new ByteArrayInputStream(content.getBytes(charset));
+    Mockito.when(odataResponse.getEntity()).thenReturn(input);
+    servlet.createResponse(servletResponse, odataResponse);
+
+    String outputContent = new String(bout.toByteArray(), charset);
+    Assert.assertEquals(content, outputContent);
+  }
+
+
+  private String testData(int amount) {
+    StringBuilder result = new StringBuilder();
+    Random r = new Random();
+    for (int i = 0; i < amount; i++) {
+      result.append((char)(r.nextInt(26) + 'a'));
+    }
+
+    return result.toString();
+  }
+
+
   @Test
   public void serviceInstance() throws Exception {
     ODataServlet servlet = new ODataServlet();