diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BufferedReaderIncludingLineEndings.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReader.java similarity index 65% rename from lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BufferedReaderIncludingLineEndings.java rename to lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReader.java index a36b86bdf..c4cee5afe 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BufferedReaderIncludingLineEndings.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReader.java @@ -27,22 +27,27 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; -public class BufferedReaderIncludingLineEndings { +public class BatchLineReader { private static final byte CR = '\r'; private static final byte LF = '\n'; private static final int EOF = -1; private static final int BUFFER_SIZE = 8192; - public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); + private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); + private static final Charset CS_ISO_8859_1 = Charset.forName("iso-8859-1"); + private Charset currentCharset = DEFAULT_CHARSET; + private String currentBoundary = null; +// private boolean readBody = false; + private ReadState readState = new ReadState(); private InputStream reader; private byte[] buffer; private int offset = 0; private int limit = 0; - public BufferedReaderIncludingLineEndings(final InputStream reader) { + public BatchLineReader(final InputStream reader) { this(reader, BUFFER_SIZE); } - public BufferedReaderIncludingLineEndings(final InputStream reader, final int bufferSize) { + public BatchLineReader(final InputStream reader, final int bufferSize) { if (bufferSize <= 0) { throw new IllegalArgumentException("Buffer size must be greater than zero."); } @@ -51,7 +56,41 @@ public class BufferedReaderIncludingLineEndings { buffer = new byte[bufferSize]; } - public int read(final byte[] byteBuffer, final int bufferOffset, final int length) throws IOException { + public void close() throws IOException { + reader.close(); + } + + public List toList() throws IOException { + final List result = new ArrayList(); + String currentLine = readLine(); + if(currentLine != null) { + currentBoundary = currentLine.trim(); + result.add(currentLine); + + while ((currentLine = readLine()) != null) { + result.add(currentLine); + } + } + return result; + } + + public List toLineList() throws IOException { + final List result = new ArrayList(); + String currentLine = readLine(); + if(currentLine != null) { + currentBoundary = currentLine.trim(); + int counter = 1; + result.add(new Line(currentLine, counter++)); + + while ((currentLine = readLine()) != null) { + result.add(new Line(currentLine, counter++)); + } + } + + return result; + } + + int read(final byte[] byteBuffer, final int bufferOffset, final int length) throws IOException { if ((bufferOffset + length) > byteBuffer.length) { throw new IndexOutOfBoundsException("Buffer is too small"); } @@ -98,51 +137,69 @@ public class BufferedReaderIncludingLineEndings { return bytesRead; } - public List toList() throws IOException { - final List result = new ArrayList(); - String currentLine; - - while ((currentLine = readLine()) != null) { - result.add(currentLine); - } - - return result; - } - - private Charset currentCharset = DEFAULT_CHARSET; - private void updateCurrentCharset(String currentLine) { + // TODO: mibo: Improve this method if(currentLine != null) { - if(currentLine.startsWith("Content-Type:") && currentLine.contains(ContentType.PARAMETER_CHARSET)) { - currentLine = currentLine.substring(13, currentLine.length()-2).trim(); + if(currentLine.startsWith("Content-Type:")) { +// if(currentLine.contains(ContentType.PARAMETER_CHARSET)) { + currentLine = currentLine.substring(13, currentLine.length() - 2).trim(); ContentType t = ContentType.parse(currentLine); - if(t != null) { + if (t != null) { String charsetString = t.getParameter(ContentType.PARAMETER_CHARSET); - currentCharset = Charset.forName(charsetString); + if (charsetString != null) { + currentCharset = Charset.forName(charsetString); + } else { + currentCharset = DEFAULT_CHARSET; + } + // boundary + String boundary = t.getParameter("boundary"); + if (boundary != null) { + currentBoundary = "--" + boundary; + } } - } else if(isEndBoundary(currentLine)) { - currentCharset = Charset.forName("us-ascii"); + } else if("\r\n".equals(currentLine)) { + readState.foundLinebreak(); + } else if(isBoundary(currentLine)) { + readState.foundBoundary(); +// if(readState.isReadBody()) { +// currentCharset = CS_ISO_8859_1; +// } } } } - private boolean isEndBoundary(String currentLine) { + private class ReadState { + private int state = 0; + + public void foundLinebreak() { + state++; + } + public void foundBoundary() { + state = 0; + } + public boolean isReadBody() { + return state >= 2; + } + public boolean isReadHeader() { + return state < 2; + } + + @Override + public String toString() { + return String.valueOf(state); + } + } + + private boolean isBoundary(String currentLine) { + if((currentBoundary + "\r\n").equals(currentLine)) { + return true; + } else if((currentBoundary + "--\r\n").equals(currentLine)) { + return true; + } return false; } - public List toLineList() throws IOException { - final List result = new ArrayList(); - String currentLine; - int counter = 1; - - while ((currentLine = readLine()) != null) { - result.add(new Line(currentLine, counter++)); - } - - return result; - } - - public String readLine() throws IOException { + String readLine() throws IOException { if (limit == EOF) { return null; } @@ -191,49 +248,17 @@ public class BufferedReaderIncludingLineEndings { if(buffer.position() == 0) { return null; } else { - String currentLine = new String(buffer.array(), 0, buffer.position(), getCurrentCharset()); + String currentLine; + if(readState.isReadBody()) { + currentLine = new String(buffer.array(), 0, buffer.position(), getCurrentCharset()); + } else { + currentLine = new String(buffer.array(), 0, buffer.position(), CS_ISO_8859_1); + } updateCurrentCharset(currentLine); return currentLine; } } - public void close() throws IOException { - reader.close(); - } - - public long skip(final long n) throws IOException { - if (n == 0) { - return 0; - } else if (n < 0) { - throw new IllegalArgumentException("skip value is negative"); - } else { - long charactersToSkip = n; - long charactersSkiped = 0; - - while (charactersToSkip != 0) { - // Is buffer refill required? - if (limit == offset) { - fillBuffer(); - - if (isEOF()) { - charactersToSkip = 0; - } - } - - // Check if more characters are available - if (!isEOF()) { - int skipChars = (int) Math.min(limit - offset, charactersToSkip); - - charactersSkiped += skipChars; - charactersToSkip -= skipChars; - offset += skipChars; - } - } - - return charactersSkiped; - } - } - private boolean isEOF() { return limit == EOF; } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchParser.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchParser.java index 731437ab9..3aeb1b564 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchParser.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchParser.java @@ -73,7 +73,7 @@ public class BatchParser { private List> splitBodyParts(final InputStream in, final String boundary) throws IOException, BatchDeserializerException { - final BufferedReaderIncludingLineEndings reader = new BufferedReaderIncludingLineEndings(in); + final BatchLineReader reader = new BatchLineReader(in); final List message = reader.toLineList(); reader.close(); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java index e277b595b..28d7c73f3 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java @@ -29,7 +29,6 @@ import java.util.Map; import java.util.UUID; import org.apache.olingo.commons.api.ODataRuntimeException; -import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.commons.api.http.HttpContentType; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpStatusCode; @@ -165,6 +164,7 @@ public class BatchResponseSerializer { private class BodyBuilder { private final Charset CHARSET_UTF_8 = Charset.forName("utf-8"); + private final Charset CHARSET_ISO_8859_1 = Charset.forName("iso-8859-1"); private ByteBuffer buffer = ByteBuffer.allocate(8192); private boolean isClosed = false; @@ -177,7 +177,9 @@ public class BatchResponseSerializer { } public BodyBuilder append(String string) { - byte [] b = string.getBytes(CHARSET_UTF_8); + // TODO: mibo: check used charset +// byte [] b = string.getBytes(CHARSET_UTF_8); + byte [] b = string.getBytes(CHARSET_ISO_8859_1); put(b); return this; } @@ -212,22 +214,10 @@ public class BatchResponseSerializer { } private class Body { - private final Charset CHARSET_DEFAULT = Charset.forName("utf-8"); private final byte[] content; - private Charset charset = CHARSET_DEFAULT; public Body(ODataResponse response) { this.content = getBody(response); - String contentType = response.getHeaders().get(HttpHeader.CONTENT_TYPE); - if(contentType != null) { - ContentType ct = ContentType.create(contentType); - if(ct != null) { - String usedCharset = ct.getParameter(ContentType.PARAMETER_CHARSET); - if(usedCharset != null) { - this.charset = Charset.forName(usedCharset); - } - } - } } public int getLength() { diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/batchhandler/MockedBatchHandlerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/batchhandler/MockedBatchHandlerTest.java index 3e4e610d2..7d13d8a1f 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/batchhandler/MockedBatchHandlerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/batchhandler/MockedBatchHandlerTest.java @@ -53,7 +53,7 @@ import org.apache.olingo.server.api.processor.BatchProcessor; import org.apache.olingo.server.api.serializer.BatchSerializerException; import org.apache.olingo.server.core.ODataHandler; import org.apache.olingo.server.core.deserializer.batch.BatchParserCommon; -import org.apache.olingo.server.core.deserializer.batch.BufferedReaderIncludingLineEndings; +import org.apache.olingo.server.core.deserializer.batch.BatchLineReader; import org.junit.Before; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; @@ -148,8 +148,8 @@ public class MockedBatchHandlerTest { batchHandler.process(request, response, true); - BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(response.getContent()); + BatchLineReader reader = + new BatchLineReader(response.getContent()); final List responseContent = reader.toList(); reader.close(); @@ -219,8 +219,8 @@ public class MockedBatchHandlerTest { batchHandler.process(request, response, true); - BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(response.getContent()); + BatchLineReader reader = + new BatchLineReader(response.getContent()); final List responseContent = reader.toList(); int line = 0; @@ -298,8 +298,8 @@ public class MockedBatchHandlerTest { batchHandler.process(request, response, true); - BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(response.getContent()); + BatchLineReader reader = + new BatchLineReader(response.getContent()); final List responseContent = reader.toList(); reader.close(); @@ -416,8 +416,8 @@ public class MockedBatchHandlerTest { batchHandler.process(request, response, true); - BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(response.getContent()); + BatchLineReader reader = + new BatchLineReader(response.getContent()); final List responseContent = reader.toList(); reader.close(); diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BufferedReaderIncludingLineEndingsTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReaderTest.java similarity index 65% rename from lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BufferedReaderIncludingLineEndingsTest.java rename to lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReaderTest.java index d0d2fa88f..3d2507ca5 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BufferedReaderIncludingLineEndingsTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReaderTest.java @@ -28,7 +28,7 @@ import java.util.List; import org.junit.Test; -public class BufferedReaderIncludingLineEndingsTest { +public class BatchLineReaderTest { private static final String TEXT_COMBINED = "Test\r" + "Test2\r\n" + @@ -42,14 +42,12 @@ public class BufferedReaderIncludingLineEndingsTest { "Test7\n" + "\n"; - private static final String TEXT_SMALL = "Test\r" + - "123"; private static final String TEXT_EMPTY = ""; @Test public void testSimpleText() throws Exception { final String TEXT = "Test"; - BufferedReaderIncludingLineEndings reader = create(TEXT); + BatchLineReader reader = create(TEXT); assertEquals(TEXT, reader.readLine()); assertNull(reader.readLine()); @@ -60,7 +58,7 @@ public class BufferedReaderIncludingLineEndingsTest { @Test public void testNoText() throws Exception { final String TEXT = ""; - BufferedReaderIncludingLineEndings reader = create(TEXT); + BatchLineReader reader = create(TEXT); assertNull(reader.readLine()); assertNull(reader.readLine()); @@ -69,8 +67,8 @@ public class BufferedReaderIncludingLineEndingsTest { @Test public void testNoBytes() throws Exception { - BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(new ByteArrayInputStream(new byte[0])); + BatchLineReader reader = + new BatchLineReader(new ByteArrayInputStream(new byte[0])); assertNull(reader.readLine()); assertNull(reader.readLine()); @@ -82,7 +80,7 @@ public class BufferedReaderIncludingLineEndingsTest { final String TEXT = "Test\r\n" + "Test2"; - BufferedReaderIncludingLineEndings reader = create(TEXT); + BatchLineReader reader = create(TEXT); assertEquals("Test\r\n", reader.readLine()); assertEquals("Test2", reader.readLine()); @@ -96,7 +94,7 @@ public class BufferedReaderIncludingLineEndingsTest { final String TEXT = "Test\n" + "Test2"; - BufferedReaderIncludingLineEndings reader = create(TEXT); + BatchLineReader reader = create(TEXT); assertEquals("Test\n", reader.readLine()); assertEquals("Test2", reader.readLine()); @@ -110,7 +108,7 @@ public class BufferedReaderIncludingLineEndingsTest { final String TEXT = "Test\r" + "Test2"; - BufferedReaderIncludingLineEndings reader = create(TEXT); + BatchLineReader reader = create(TEXT); assertEquals("Test\r", reader.readLine()); assertEquals("Test2", reader.readLine()); @@ -121,7 +119,7 @@ public class BufferedReaderIncludingLineEndingsTest { @Test public void testCombined() throws Exception { - BufferedReaderIncludingLineEndings reader = create(TEXT_COMBINED); + BatchLineReader reader = create(TEXT_COMBINED); assertEquals("Test\r", reader.readLine()); assertEquals("Test2\r\n", reader.readLine()); @@ -141,7 +139,7 @@ public class BufferedReaderIncludingLineEndingsTest { @Test public void testCombinedBufferSizeTwo() throws Exception { - BufferedReaderIncludingLineEndings reader = create(TEXT_COMBINED, 2); + BatchLineReader reader = create(TEXT_COMBINED, 2); assertEquals("Test\r", reader.readLine()); assertEquals("Test2\r\n", reader.readLine()); @@ -173,7 +171,7 @@ public class BufferedReaderIncludingLineEndingsTest { "Test7\n" + "\r\n"; - BufferedReaderIncludingLineEndings reader = create(TEXT, 1); + BatchLineReader reader = create(TEXT, 1); assertEquals("Test\r", reader.readLine()); assertEquals("Test2\r\n", reader.readLine()); @@ -197,92 +195,29 @@ public class BufferedReaderIncludingLineEndingsTest { final String TEXT = "Test\r" + "\r"; - BufferedReaderIncludingLineEndings reader = create(TEXT, 1); + BatchLineReader reader = create(TEXT, 1); assertEquals("Test\r", reader.readLine()); assertEquals("\r", reader.readLine()); reader.close(); } - @Test - public void testSkipSimple() throws Exception { - BufferedReaderIncludingLineEndings reader = create(TEXT_SMALL); - - assertEquals(5, reader.skip(5)); // Test\r - assertEquals("123", reader.readLine()); - assertNull(reader.readLine()); - assertNull(reader.readLine()); - reader.close(); - } - - @Test - public void testSkipBufferOne() throws Exception { - BufferedReaderIncludingLineEndings reader = create(TEXT_SMALL, 1); - - assertEquals(5, reader.skip(5)); // Test\r - assertEquals("123", reader.readLine()); - assertNull(reader.readLine()); - assertNull(reader.readLine()); - reader.close(); - } - - @Test - public void testReadThanSkip() throws Exception { - final String TEXT = "Test\r" + - "\r" + - "123"; - - BufferedReaderIncludingLineEndings reader = create(TEXT); - - assertEquals("Test\r", reader.readLine()); - assertEquals(1, reader.skip(1)); // Test\r - assertEquals("123", reader.readLine()); - assertNull(reader.readLine()); - assertNull(reader.readLine()); - reader.close(); - } - @Test public void testReadMoreBufferCapacityThanCharacterAvailable() throws Exception { final String TEXT = "Foo"; byte[] buffer = new byte[20]; - BufferedReaderIncludingLineEndings reader = create(TEXT); + BatchLineReader reader = create(TEXT); assertEquals(3, reader.read(buffer, 0, 20)); assertEquals(-1, reader.read(buffer, 0, 20)); reader.close(); - BufferedReaderIncludingLineEndings readerBufferOne = create(TEXT, 1); + BatchLineReader readerBufferOne = create(TEXT, 1); assertEquals(3, readerBufferOne.read(buffer, 0, 20)); assertEquals(-1, readerBufferOne.read(buffer, 0, 20)); readerBufferOne.close(); } - @Test - public void testSkipZero() throws Exception { - final String TEXT = "Test\r" + - "123\r\n"; - - BufferedReaderIncludingLineEndings reader = create(TEXT); - - assertEquals(0, reader.skip(0)); // Test\r - assertEquals("Test\r", reader.readLine()); - assertEquals("123\r\n", reader.readLine()); - assertNull(reader.readLine()); - assertNull(reader.readLine()); - reader.close(); - } - -// @Test -// public void testSkipToMuch() throws Exception { -// BufferedReaderIncludingLineEndings reader = create(TEXT_SMALL); -// -// assertEquals(8, reader.skip(10)); // Test\r -// assertEquals(null, reader.readLine()); -// reader.close(); -// } -// - @Test public void testLineEqualsAndHashCode() { Line l1 = new Line("The first line", 1); @@ -302,41 +237,19 @@ public class BufferedReaderIncludingLineEndingsTest { @Test(expected = IllegalArgumentException.class) public void testFailBufferSizeZero() throws Exception { - BufferedReaderIncludingLineEndings reader = create(TEXT_EMPTY, 0); + BatchLineReader reader = create(TEXT_EMPTY, 0); reader.close(); } @Test(expected = IllegalArgumentException.class) public void testFailBufferSizeNegative() throws Exception { - BufferedReaderIncludingLineEndings reader = create(TEXT_EMPTY, -1); + BatchLineReader reader = create(TEXT_EMPTY, -1); reader.close(); } -// @Test -// public void testMarkSupoorted() throws Exception { -// BufferedReaderIncludingLineEndings reader = create(TEXT_EMPTY); -// -// assertEquals(false, reader.markSupported()); -// reader.close(); -// } - -// @Test(expected = Exception.class) -// public void testFailMark() throws Exception { -// BufferedReaderIncludingLineEndings reader = create("123"); -// -// reader.mark(1); -// } -// -// @Test(expected = Exception.class) -// public void testFailReset() throws Exception { -// BufferedReaderIncludingLineEndings reader = create("123"); -// -// reader.reset(); -// } - @Test public void testToList() throws Exception { - BufferedReaderIncludingLineEndings reader = create(TEXT_COMBINED); + BatchLineReader reader = create(TEXT_COMBINED); List stringList = reader.toLineList(); assertEquals(11, stringList.size()); @@ -354,13 +267,13 @@ public class BufferedReaderIncludingLineEndingsTest { reader.close(); } - private BufferedReaderIncludingLineEndings create(final String inputString) throws Exception { - return new BufferedReaderIncludingLineEndings(new ByteArrayInputStream(inputString + private BatchLineReader create(final String inputString) throws Exception { + return new BatchLineReader(new ByteArrayInputStream(inputString .getBytes("UTF-8"))); } - private BufferedReaderIncludingLineEndings create(final String inputString, final int bufferSize) throws Exception { - return new BufferedReaderIncludingLineEndings(new ByteArrayInputStream(inputString + private BatchLineReader create(final String inputString, final int bufferSize) throws Exception { + return new BatchLineReader(new ByteArrayInputStream(inputString .getBytes("UTF-8")), bufferSize); } diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java index 29d0329aa..9d3ebeea6 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java @@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -34,13 +35,16 @@ import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart; -import org.apache.olingo.server.core.deserializer.batch.BufferedReaderIncludingLineEndings; +import org.apache.olingo.server.core.deserializer.batch.BatchLineReader; import org.junit.Test; public class BatchResponseSerializerTest { private static final String CRLF = "\r\n"; private static final String BOUNDARY = "batch_" + UUID.randomUUID().toString(); + private static final Charset CS_ISO_8859_1 = Charset.forName("iso-8859-1"); + private static final Charset CS_UTF_8 = Charset.forName("utf-8"); + @Test public void testBatchResponse() throws Exception { final List parts = new ArrayList(); @@ -63,8 +67,8 @@ public class BatchResponseSerializerTest { BatchResponseSerializer serializer = new BatchResponseSerializer(); final InputStream content = serializer.serialize(parts, BOUNDARY); assertNotNull(content); - final BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(content); + final BatchLineReader reader = + new BatchLineReader(content); final List body = reader.toList(); reader.close(); @@ -97,7 +101,7 @@ public class BatchResponseSerializerTest { } @Test - public void testBatchResponseUmlauteUtf8() throws Exception { + public void testBatchResponseUmlautsUtf8() throws Exception { final List parts = new ArrayList(); ODataResponse response = new ODataResponse(); response.setStatusCode(HttpStatusCode.OK.getStatusCode()); @@ -119,8 +123,8 @@ public class BatchResponseSerializerTest { BatchResponseSerializer serializer = new BatchResponseSerializer(); final InputStream content = serializer.serialize(parts, BOUNDARY); assertNotNull(content); - final BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(content); + final BatchLineReader reader = + new BatchLineReader(content); final List body = reader.toList(); reader.close(); @@ -152,6 +156,105 @@ public class BatchResponseSerializerTest { assertTrue(body.get(line++).contains("--batch_")); } + @Test + public void testBatchResponseUmlautsUtf8BodyIsoHeader() throws Exception { + final List parts = new ArrayList(); + ODataResponse response = new ODataResponse(); + response.setStatusCode(HttpStatusCode.OK.getStatusCode()); + response.setHeader(HttpHeader.CONTENT_TYPE, + ContentType.APPLICATION_JSON.toContentTypeString() + "; charset=UTF-8"); + response.setContent(IOUtils.toInputStream("Wälter Winter" + CRLF)); + + List responses = new ArrayList(1); + responses.add(response); + parts.add(new ODataResponsePart(responses, false)); + + ODataResponse changeSetResponse = new ODataResponse(); + changeSetResponse.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode()); + changeSetResponse.setHeader(HttpHeader.CONTENT_ID, "1"); + + byte[] umlauts = "äüö".getBytes(CS_ISO_8859_1); + changeSetResponse.setHeader("Custom-Header", new String(umlauts, CS_ISO_8859_1)); + responses = new ArrayList(1); + responses.add(changeSetResponse); + parts.add(new ODataResponsePart(responses, true)); + + BatchResponseSerializer serializer = new BatchResponseSerializer(); + final InputStream content = serializer.serialize(parts, BOUNDARY); + assertNotNull(content); + final BatchLineReader reader = + new BatchLineReader(content); + final List body = reader.toList(); + reader.close(); + + int line = 0; + assertEquals(25, body.size()); + assertTrue(body.get(line++).contains("--batch_")); + assertEquals("Content-Type: application/http" + CRLF, body.get(line++)); + assertEquals("Content-Transfer-Encoding: binary" + CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertEquals("HTTP/1.1 200 OK" + CRLF, body.get(line++)); + assertEquals("Content-Type: application/json; charset=UTF-8" + CRLF, body.get(line++)); + assertEquals("Content-Length: 16" + CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertEquals("Wälter Winter" + CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertTrue(body.get(line++).contains("--batch_")); + assertTrue(body.get(line++).contains("Content-Type: multipart/mixed; boundary=changeset_")); + assertEquals(CRLF, body.get(line++)); + assertTrue(body.get(line++).contains("--changeset_")); + assertEquals("Content-Type: application/http" + CRLF, body.get(line++)); + assertEquals("Content-Transfer-Encoding: binary" + CRLF, body.get(line++)); + assertEquals("Content-ID: 1" + CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertEquals("HTTP/1.1 204 No Content" + CRLF, body.get(line++)); + assertEquals("Custom-Header: äüö" + CRLF, body.get(line++)); + assertEquals("Content-Length: 0" + CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertEquals(CRLF, body.get(line++)); + assertTrue(body.get(line++).contains("--changeset_")); + assertTrue(body.get(line++).contains("--batch_")); + } + + @Test + public void testBatchResponseUmlautsUtf8BodyAndHeader() throws Exception { + final List parts = new ArrayList(); + ODataResponse response = new ODataResponse(); + response.setStatusCode(HttpStatusCode.OK.getStatusCode()); + response.setHeader(HttpHeader.CONTENT_TYPE, + ContentType.APPLICATION_JSON.toContentTypeString() + "; charset=UTF-8"); + response.setContent(IOUtils.toInputStream("Wälter Winter" + CRLF)); + + List responses = new ArrayList(1); + responses.add(response); + parts.add(new ODataResponsePart(responses, false)); + + ODataResponse changeSetResponse = new ODataResponse(); + changeSetResponse.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode()); + changeSetResponse.setHeader(HttpHeader.CONTENT_ID, "1"); + +// byte[] umlauts = "äüö".getBytes(CS_UTF_8); +// changeSetResponse.setHeader("Custom-Header", new String(umlauts, CS_UTF_8)); + changeSetResponse.setHeader("Custom-Header", "äüö"); + responses = new ArrayList(1); + responses.add(changeSetResponse); + parts.add(new ODataResponsePart(responses, true)); + + BatchResponseSerializer serializer = new BatchResponseSerializer(); + final InputStream content = serializer.serialize(parts, BOUNDARY); + assertNotNull(content); + final BatchLineReader reader = + new BatchLineReader(content); + final List body = reader.toList(); + reader.close(); + + assertEquals(25, body.size()); + // TODO: check: with latest change in BatchResponseSerializer is not possible + // to set header values with UTF-8 (only iso-8859-1) +// assertEquals("Custom-Header: äüö" + CRLF, body.get(19)); + assertEquals("Custom-Header: äüö" + CRLF, body.get(19)); + } + @Test public void testBatchResponseUmlauteIso() throws Exception { final List parts = new ArrayList(); @@ -176,8 +279,8 @@ public class BatchResponseSerializerTest { BatchResponseSerializer serializer = new BatchResponseSerializer(); final InputStream content = serializer.serialize(parts, BOUNDARY); assertNotNull(content); - final BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(content); + final BatchLineReader reader = + new BatchLineReader(content); final List body = reader.toList(); reader.close(); @@ -231,8 +334,8 @@ public class BatchResponseSerializerTest { BatchResponseSerializer serializer = new BatchResponseSerializer(); final InputStream content = serializer.serialize(parts, BOUNDARY); assertNotNull(content); - final BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(content); + final BatchLineReader reader = + new BatchLineReader(content); final List body = reader.toList(); reader.close(); @@ -279,8 +382,8 @@ public class BatchResponseSerializerTest { final InputStream content = serializer.serialize(parts, BOUNDARY); assertNotNull(content); - final BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(content); + final BatchLineReader reader = + new BatchLineReader(content); final List body = reader.toList(); reader.close(); @@ -314,8 +417,8 @@ public class BatchResponseSerializerTest { assertNotNull(content); - final BufferedReaderIncludingLineEndings reader = - new BufferedReaderIncludingLineEndings(content); + final BatchLineReader reader = + new BatchLineReader(content); final List body = reader.toList(); reader.close();