From f187ad35131c35547b70f499adb92f33fe273400 Mon Sep 17 00:00:00 2001 From: Michael Bolz Date: Thu, 6 Aug 2015 15:09:06 +0200 Subject: [PATCH] [OLINGO-659] Minor code clean up --- .../deserializer/batch/BatchBodyPart.java | 2 +- .../deserializer/batch/BatchParserCommon.java | 13 ++++-------- .../core/deserializer/batch/Header.java | 20 +++++++------------ .../core/deserializer/batch/HeaderField.java | 8 +------- .../server/core/prefer/PreferParser.java | 6 +++--- .../serializer/AsyncResponseSerializer.java | 4 ++-- .../serializer/BatchResponseSerializer.java | 18 ++++++++++++----- .../core/deserializer/batch/HeaderTest.java | 1 - 8 files changed, 31 insertions(+), 41 deletions(-) diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchBodyPart.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchBodyPart.java index f77f886cc..cf7257a88 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchBodyPart.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchBodyPart.java @@ -87,7 +87,7 @@ public class BatchBodyPart implements BatchPart { private List> splitChangeSet(List remainingMessage) throws BatchDeserializerException { final HeaderField contentTypeField = headers.getHeaderField(HttpHeader.CONTENT_TYPE); - final String changeSetBoundary = BatchParserCommon.getBoundary(contentTypeField.getValueNotNull(), + final String changeSetBoundary = BatchParserCommon.getBoundary(contentTypeField.getValue(), contentTypeField.getLineNumber()); validateChangeSetBoundary(changeSetBoundary, headers); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchParserCommon.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchParserCommon.java index 7126f1496..ce8eb359f 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchParserCommon.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchParserCommon.java @@ -46,9 +46,9 @@ public class BatchParserCommon { public static String getBoundary(final String contentType, final int line) throws BatchDeserializerException { final ContentType type = getContentType(contentType, ContentType.MULTIPART_MIXED, line); final Map parameters = type.getParameters(); - for (final String parameterName : parameters.keySet()) { - if (BOUNDARY.equalsIgnoreCase(parameterName)) { - final String boundary = parameters.get(parameterName).trim(); + for (final Map.Entry entries : parameters.entrySet()) { + if (BOUNDARY.equalsIgnoreCase(entries.getKey())) { + final String boundary = entries.getValue().trim(); if (boundary.matches(PATTERN_BOUNDARY)) { return trimQuotes(boundary); } else { @@ -63,7 +63,7 @@ public class BatchParserCommon { public static ContentType getContentType(final String contentType, final ContentType expected, final int line) throws BatchDeserializerException { - ContentType type = null; + ContentType type; try { type = ContentType.create(contentType); } catch (final IllegalArgumentException e) { @@ -83,11 +83,6 @@ public class BatchParserCommon { } } - public static String removeEndingSlash(final String content) { - String newContent = content.trim(); - return newContent.endsWith("/") ? newContent.substring(0, newContent.length() - 1) : newContent; - } - private static String trimQuotes(final String boundary) { if (boundary != null && boundary.length() >= 2 && boundary.startsWith("\"") && boundary.endsWith("\"")) { return boundary.substring(1, boundary.length() - 1); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/Header.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/Header.java index 67a97570f..e808ddb0b 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/Header.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/Header.java @@ -73,12 +73,6 @@ public class Header implements Iterable { return (headerField == null) ? null : headerField.getValue(); } - public String getHeaderNotNull(final String name) { - final HeaderField headerField = getHeaderField(name); - - return (headerField == null) ? "" : headerField.getValueNotNull(); - } - public List getHeaders(final String name) { final HeaderField headerField = getHeaderField(name); @@ -96,9 +90,9 @@ public class Header implements Iterable { public Map toSingleMap() { final Map singleMap = new HashMap(); - for (final String key : headers.keySet()) { - HeaderField field = headers.get(key); - singleMap.put(field.getFieldName(), getHeader(key)); + for (final Map.Entry entries : headers.entrySet()) { + HeaderField field = entries.getValue(); + singleMap.put(field.getFieldName(), getHeader(entries.getKey())); } return singleMap; @@ -107,8 +101,8 @@ public class Header implements Iterable { public Map> toMultiMap() { final Map> singleMap = new HashMap>(); - for (final String key : headers.keySet()) { - HeaderField field = headers.get(key); + for (final Map.Entry entries : headers.entrySet()) { + HeaderField field = entries.getValue(); singleMap.put(field.getFieldName(), field.getValues()); } @@ -130,8 +124,8 @@ public class Header implements Iterable { public Header clone() { final Header newInstance = new Header(lineNumber); - for (final String key : headers.keySet()) { - newInstance.headers.put(key, headers.get(key).clone()); + for (final Map.Entry entries : headers.entrySet()) { + newInstance.headers.put(entries.getKey(), entries.getValue().clone()); } return newInstance; diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/HeaderField.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/HeaderField.java index 870f276ca..fc0e156a2 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/HeaderField.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/HeaderField.java @@ -59,15 +59,9 @@ public class HeaderField implements Cloneable { return result.toString(); } - public String getValueNotNull() { - final String value = getValue(); - - return (value == null) ? "" : value; - } - @Override public HeaderField clone() { - List newValues = new ArrayList(); + List newValues = new ArrayList(values.size()); newValues.addAll(values); return new HeaderField(fieldName, newValues, lineNumber); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/prefer/PreferParser.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/prefer/PreferParser.java index a3a2a423b..f963703c9 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/prefer/PreferParser.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/prefer/PreferParser.java @@ -102,9 +102,9 @@ public class PreferParser { } if (matcher.hitEnd()) { // Here we also have to keep already existing preferences. - for (final String key : partResult.keySet()) { - if (!result.containsKey(key)) { - result.put(key, partResult.get(key)); + for (final Map.Entry entry : partResult.entrySet()) { + if (!result.containsKey(entry.getKey())) { + result.put(entry.getKey(), entry.getValue()); } } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/AsyncResponseSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/AsyncResponseSerializer.java index d5e8588ae..9c3060e5d 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/AsyncResponseSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/AsyncResponseSerializer.java @@ -61,8 +61,8 @@ public class AsyncResponseSerializer { final ByteArrayOutputStream buffer) throws IOException { final Map header = response.getHeaders(); - for (final String key: header.keySet()) { - appendHeader(key, header.get(key), buffer); + for (final Map.Entry entry: header.entrySet()) { + appendHeader(entry.getKey(), entry.getValue(), buffer); } } 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 c907b8f15..46c967269 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 @@ -105,7 +105,7 @@ public class BatchResponseSerializer { private void appendChangeSetHeader(final BodyBuilder builder, final String changeSetBoundary) { appendHeader(HttpHeader.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED + "; boundary=" - + changeSetBoundary, builder); + + changeSetBoundary, builder); } private void appendHeader(final String name, final String value, final BodyBuilder builder) { @@ -121,18 +121,26 @@ public class BatchResponseSerializer { .append(SP) .append(response.getStatusCode()) .append(SP) - .append(HttpStatusCode.fromStatusCode(response.getStatusCode()).toString()) + .append(getStatusCodeInfo(response)) .append(CRLF); } + private String getStatusCodeInfo(ODataResponse response) { + HttpStatusCode status = HttpStatusCode.fromStatusCode(response.getStatusCode()); + if(status == null) { + throw new ODataRuntimeException("Invalid status code in response '" + response.getStatusCode() + "'"); + } + return status.getInfo(); + } + private void appendResponseHeader(final ODataResponse response, final int contentLength, final BodyBuilder builder) { final Map header = response.getHeaders(); - for (final String key : header.keySet()) { + for (final Map.Entry entry : header.entrySet()) { // Requests do never has a content id header - if (!key.equalsIgnoreCase(HttpHeader.CONTENT_ID)) { - appendHeader(key, header.get(key), builder); + if (!entry.getKey().equalsIgnoreCase(HttpHeader.CONTENT_ID)) { + appendHeader(entry.getKey(), entry.getValue(), builder); } } diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/HeaderTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/HeaderTest.java index c18d22793..ad3b2db5c 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/HeaderTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/HeaderTest.java @@ -47,7 +47,6 @@ public class HeaderTest { assertNull(header.getHeader(HttpHeader.CONTENT_TYPE)); assertEquals(0, header.getHeaders(HttpHeader.CONTENT_TYPE).size()); - assertEquals("", header.getHeaderNotNull(HttpHeader.CONTENT_TYPE)); } @Test