You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2015/08/14 16:20:52 UTC

olingo-odata4 git commit: [OLINGO-659] fix code style issues

Repository: olingo-odata4
Updated Branches:
  refs/heads/master 198452538 -> f9c68b8ba


[OLINGO-659] fix code style issues


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

Branch: refs/heads/master
Commit: f9c68b8ba424abc1736fbca7b58a642fdd8e2508
Parents: 1984525
Author: Christian Amend <ch...@sap.com>
Authored: Fri Aug 14 16:07:35 2015 +0200
Committer: Christian Amend <ch...@sap.com>
Committed: Fri Aug 14 16:14:19 2015 +0200

----------------------------------------------------------------------
 .../commons/core/edm/EdmProviderImpl.java       |  81 ++---
 .../core/debug/DebugResponseHelperImpl.java     |  65 ++--
 .../olingo/server/core/debug/DebugTabUri.java   |  52 +++-
 .../server/core/deserializer/batch/Header.java  |   2 +-
 .../serializer/AbstractODataSerializer.java     |  45 +++
 .../serializer/json/ODataJsonSerializer.java    | 221 ++++++++------
 .../core/serializer/xml/ODataXmlSerializer.java | 298 ++++++++++++-------
 .../olingo/server/core/uri/parser/Parser.java   |   1 +
 .../core/uri/queryoption/ExpandItemImpl.java    |   1 -
 .../server/core/debug/DebugTabRequestTest.java  |   1 -
 10 files changed, 493 insertions(+), 274 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f9c68b8b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmProviderImpl.java
----------------------------------------------------------------------
diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmProviderImpl.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmProviderImpl.java
index 3239276..39c66aa 100644
--- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmProviderImpl.java
+++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/edm/EdmProviderImpl.java
@@ -6,9 +6,9 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License. You may obtain a copy of the License at
- *
+ * 
  * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * 
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -182,29 +182,29 @@ public class EdmProviderImpl extends AbstractEdm {
       }
       final List<String> parameterNamesCopy =
           parameterNames == null ? Collections.<String> emptyList() : parameterNames;
-          for (CsdlFunction function : functions) {
-            if (function.isBound()) {
-              List<CsdlParameter> providerParameters = function.getParameters();
-              if (providerParameters == null || providerParameters.size() == 0) {
-                throw new EdmException("No parameter specified for bound function: " + functionName);
-              }
-              final CsdlParameter bindingParameter = providerParameters.get(0);
-              if (bindingParameterTypeName.equals(bindingParameter.getTypeFQN())
-                  && isBindingParameterCollection.booleanValue() == bindingParameter.isCollection()) {
+      for (CsdlFunction function : functions) {
+        if (function.isBound()) {
+          List<CsdlParameter> providerParameters = function.getParameters();
+          if (providerParameters == null || providerParameters.size() == 0) {
+            throw new EdmException("No parameter specified for bound function: " + functionName);
+          }
+          final CsdlParameter bindingParameter = providerParameters.get(0);
+          if (bindingParameterTypeName.equals(bindingParameter.getTypeFQN())
+              && isBindingParameterCollection.booleanValue() == bindingParameter.isCollection()) {
 
-                if (parameterNamesCopy.size() == providerParameters.size() - 1) {
-                  final List<String> providerParameterNames = new ArrayList<String>();
-                  for (int i = 1; i < providerParameters.size(); i++) {
-                    providerParameterNames.add(providerParameters.get(i).getName());
-                  }
-                  if (parameterNamesCopy.containsAll(providerParameterNames)) {
-                    return new EdmFunctionImpl(this, functionName, function);
-                  }
-                }
+            if (parameterNamesCopy.size() == providerParameters.size() - 1) {
+              final List<String> providerParameterNames = new ArrayList<String>();
+              for (int i = 1; i < providerParameters.size(); i++) {
+                providerParameterNames.add(providerParameters.get(i).getName());
+              }
+              if (parameterNamesCopy.containsAll(providerParameterNames)) {
+                return new EdmFunctionImpl(this, functionName, function);
               }
             }
           }
-          return null;
+        }
+      }
+      return null;
     } catch (ODataException e) {
       throw new EdmException(e);
     }
@@ -291,25 +291,25 @@ public class EdmProviderImpl extends AbstractEdm {
 
       final List<String> parameterNamesCopy =
           parameterNames == null ? Collections.<String> emptyList() : parameterNames;
-          for (CsdlFunction function : functions) {
-            if (!function.isBound()) {
-              List<CsdlParameter> providerParameters = function.getParameters();
-              if (providerParameters == null) {
-                providerParameters = Collections.emptyList();
-              }
-              if (parameterNamesCopy.size() == providerParameters.size()) {
-                final List<String> functionParameterNames = new ArrayList<String>();
-                for (CsdlParameter parameter : providerParameters) {
-                  functionParameterNames.add(parameter.getName());
-                }
+      for (CsdlFunction function : functions) {
+        if (!function.isBound()) {
+          List<CsdlParameter> providerParameters = function.getParameters();
+          if (providerParameters == null) {
+            providerParameters = Collections.emptyList();
+          }
+          if (parameterNamesCopy.size() == providerParameters.size()) {
+            final List<String> functionParameterNames = new ArrayList<String>();
+            for (CsdlParameter parameter : providerParameters) {
+              functionParameterNames.add(parameter.getName());
+            }
 
-                if (parameterNamesCopy.containsAll(functionParameterNames)) {
-                  return new EdmFunctionImpl(this, functionName, function);
-                }
-              }
+            if (parameterNamesCopy.containsAll(functionParameterNames)) {
+              return new EdmFunctionImpl(this, functionName, function);
             }
           }
-          return null;
+        }
+      }
+      return null;
     } catch (ODataException e) {
       throw new EdmException(e);
     }
@@ -319,8 +319,11 @@ public class EdmProviderImpl extends AbstractEdm {
   protected Map<String, EdmSchema> createSchemas() {
     try {
       final Map<String, EdmSchema> providerSchemas = new LinkedHashMap<String, EdmSchema>();
-      for (CsdlSchema schema : provider.getSchemas()) {
-        providerSchemas.put(schema.getNamespace(), new EdmSchemaImpl(this, provider, schema));
+      List<CsdlSchema> localSchemas = provider.getSchemas();
+      if (localSchemas != null) {
+        for (CsdlSchema schema : localSchemas) {
+          providerSchemas.put(schema.getNamespace(), new EdmSchemaImpl(this, provider, schema));
+        }
       }
       return providerSchemas;
     } catch (ODataException e) {

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f9c68b8b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java
index 222bd0a..7c0d617 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java
@@ -73,7 +73,7 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
       switch (requestedFormat) {
       case DOWNLOAD:
         response.setHeader("Content-Disposition", "attachment; filename=OData-Response."
-                + new Date().toString().replace(' ', '_').replace(':', '.') + ".html");
+            + new Date().toString().replace(' ', '_').replace(':', '.') + ".html");
         // Download is the same as html except for the above header
       case HTML:
         String title = debugInfo.getRequest() == null ?
@@ -114,7 +114,7 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
     }
 
     // URI
-    if (debugInfo.getUriInfo() != null ) {
+    if (debugInfo.getUriInfo() != null) {
       parts.add(new DebugTabUri(debugInfo.getUriInfo()));
     }
 
@@ -134,29 +134,48 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
 
   private InputStream wrapInJson(final List<DebugTab> parts) throws IOException {
     CircleStreamBuffer csb = new CircleStreamBuffer();
-    JsonGenerator gen = new JsonFactory().createGenerator(csb.getOutputStream(), JsonEncoding.UTF8);
+    IOException cachedException = null;
 
-    gen.writeStartObject();
-    DebugTab requestInfo = parts.get(0);
-    gen.writeFieldName(requestInfo.getName().toLowerCase(Locale.ROOT));
-    requestInfo.appendJson(gen);
+    try {
+      JsonGenerator gen = new JsonFactory().createGenerator(csb.getOutputStream(), JsonEncoding.UTF8);
 
-    DebugTab responseInfo = parts.get(1);
-    gen.writeFieldName(responseInfo.getName().toLowerCase(Locale.ROOT));
-    responseInfo.appendJson(gen);
+      gen.writeStartObject();
+      DebugTab requestInfo = parts.get(0);
+      gen.writeFieldName(requestInfo.getName().toLowerCase(Locale.ROOT));
+      requestInfo.appendJson(gen);
 
-    gen.writeFieldName("server");
-    gen.writeStartObject();
-    gen.writeStringField("version", getVersion());
-    for (DebugTab part : parts.subList(2, parts.size())) {
-      gen.writeFieldName(part.getName().toLowerCase(Locale.ROOT));
-      part.appendJson(gen);
-    }
-    gen.writeEndObject();
+      DebugTab responseInfo = parts.get(1);
+      gen.writeFieldName(responseInfo.getName().toLowerCase(Locale.ROOT));
+      responseInfo.appendJson(gen);
 
-    gen.writeEndObject();
-    gen.close();
-    csb.closeWrite();
+      gen.writeFieldName("server");
+      gen.writeStartObject();
+      gen.writeStringField("version", getVersion());
+      for (DebugTab part : parts.subList(2, parts.size())) {
+        gen.writeFieldName(part.getName().toLowerCase(Locale.ROOT));
+        part.appendJson(gen);
+      }
+      gen.writeEndObject();
+
+      gen.writeEndObject();
+      gen.close();
+      csb.getOutputStream().close();
+
+    } catch (IOException e) {
+      throw e;
+    } finally {
+      if (csb != null && csb.getOutputStream() != null) {
+        try {
+          csb.getOutputStream().close();
+        } catch (IOException e) {
+          if (cachedException != null) {
+            throw cachedException;
+          } else {
+            throw e;
+          }
+        }
+      }
+    }
 
     return csb.getInputStream();
   }
@@ -266,10 +285,10 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
     for (final String name : entries.keySet()) {
       final String value = entries.get(name);
       writer.append("<tr><td class=\"name\">").append(name).append("</td>")
-      .append("<td class=\"value\">");
+          .append("<td class=\"value\">");
       if (value != null) {
         writer.append(escapeHtml(value));
-      }else{
+      } else {
         writer.append("null");
       }
       writer.append("</td></tr>\n");

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f9c68b8b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabUri.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabUri.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabUri.java
index b878cde..9953cee 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabUri.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabUri.java
@@ -234,6 +234,7 @@ public class DebugTabUri implements DebugTab {
           selectedProperty = selectedProperty + "/";
         }
         selectedProperty = resourcePart.toString();
+        first = false;
       }
     }
 
@@ -254,13 +255,48 @@ public class DebugTabUri implements DebugTab {
 
   private String getJsonString() throws IOException {
     CircleStreamBuffer csb = new CircleStreamBuffer();
-    JsonGenerator gen =
-        new JsonFactory().createGenerator(csb.getOutputStream(), JsonEncoding.UTF8).setPrettyPrinter(
-            new DefaultPrettyPrinter());
-    appendJson(gen);
-    gen.close();
-    csb.closeWrite();
-
-    return IOUtils.toString(csb.getInputStream());
+    IOException cachedException = null;
+    try {
+      JsonGenerator json =
+          new JsonFactory().createGenerator(csb.getOutputStream(), JsonEncoding.UTF8)
+              .setPrettyPrinter(new DefaultPrettyPrinter());
+      appendJson(json);
+      json.close();
+      csb.getOutputStream().close();
+    } catch (IOException e) {
+      throw e;
+    } finally {
+      if (csb != null && csb.getOutputStream() != null) {
+        try {
+          csb.getOutputStream().close();
+        } catch (IOException e) {
+          if (cachedException != null) {
+            throw cachedException;
+          } else {
+            throw e;
+          }
+        }
+      }
+    }
+
+    try {
+      String jsonString = IOUtils.toString(csb.getInputStream());
+      csb.getInputStream().close();
+      return jsonString;
+    } catch (IOException e) {
+      throw e;
+    } finally {
+      if (csb != null && csb.getInputStream() != null) {
+        try {
+          csb.getInputStream().close();
+        } catch (IOException e) {
+          if (cachedException != null) {
+            throw cachedException;
+          } else {
+            throw e;
+          }
+        }
+      }
+    }
   }
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f9c68b8b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/Header.java
----------------------------------------------------------------------
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 e808ddb..1391f94 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
@@ -25,7 +25,7 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
-public class Header implements Iterable<HeaderField> {
+public class Header implements Iterable<HeaderField>, Cloneable {
   private final Map<String, HeaderField> headers = new HashMap<String, HeaderField>();
   private int lineNumber;
 

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f9c68b8b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/AbstractODataSerializer.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/AbstractODataSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/AbstractODataSerializer.java
new file mode 100644
index 0000000..e34817d
--- /dev/null
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/AbstractODataSerializer.java
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.olingo.server.core.serializer;
+
+import java.io.IOException;
+
+import org.apache.olingo.server.api.serializer.ODataSerializer;
+import org.apache.olingo.server.api.serializer.SerializerException;
+import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer;
+
+public abstract class AbstractODataSerializer implements ODataSerializer {
+
+  protected void closeCircleStreamBufferOutput(CircleStreamBuffer buffer, SerializerException cachedException)
+      throws SerializerException {
+    if (buffer != null && buffer.getOutputStream() != null) {
+      try {
+        buffer.getOutputStream().close();
+      } catch (IOException e) {
+        if (cachedException != null) {
+          throw cachedException;
+        } else {
+          throw new SerializerException("An I/O exception occurred.", e,
+              SerializerException.MessageKeys.IO_EXCEPTION);
+        }
+      }
+    }
+  }
+ 
+}

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f9c68b8b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java
index 6bd8713..c7bd383 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java
@@ -6,9 +6,9 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License. You may obtain a copy of the License at
- *
+ * 
  * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * 
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -48,7 +48,6 @@ import org.apache.olingo.server.api.ServiceMetadata;
 import org.apache.olingo.server.api.serializer.ComplexSerializerOptions;
 import org.apache.olingo.server.api.serializer.EntityCollectionSerializerOptions;
 import org.apache.olingo.server.api.serializer.EntitySerializerOptions;
-import org.apache.olingo.server.api.serializer.ODataSerializer;
 import org.apache.olingo.server.api.serializer.PrimitiveSerializerOptions;
 import org.apache.olingo.server.api.serializer.ReferenceCollectionSerializerOptions;
 import org.apache.olingo.server.api.serializer.ReferenceSerializerOptions;
@@ -58,21 +57,18 @@ import org.apache.olingo.server.api.uri.UriHelper;
 import org.apache.olingo.server.api.uri.queryoption.ExpandItem;
 import org.apache.olingo.server.api.uri.queryoption.ExpandOption;
 import org.apache.olingo.server.api.uri.queryoption.SelectOption;
+import org.apache.olingo.server.core.serializer.AbstractODataSerializer;
 import org.apache.olingo.server.core.serializer.SerializerResultImpl;
 import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer;
 import org.apache.olingo.server.core.serializer.utils.ContentTypeHelper;
 import org.apache.olingo.server.core.serializer.utils.ContextURLBuilder;
 import org.apache.olingo.server.core.serializer.utils.ExpandSelectHelper;
 import org.apache.olingo.server.core.uri.UriHelperImpl;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import com.fasterxml.jackson.core.JsonFactory;
 import com.fasterxml.jackson.core.JsonGenerator;
 
-public class ODataJsonSerializer implements ODataSerializer {
-
-  private static final Logger log = LoggerFactory.getLogger(ODataJsonSerializer.class);
+public class ODataJsonSerializer extends AbstractODataSerializer {
 
   private final boolean isIEEE754Compatible;
   private final boolean isODataMetadataNone;
@@ -85,32 +81,23 @@ public class ODataJsonSerializer implements ODataSerializer {
   @Override
   public SerializerResult serviceDocument(final ServiceMetadata metadata, final String serviceRoot)
       throws SerializerException {
-    CircleStreamBuffer buffer;
-    JsonGenerator gen = null;
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
 
     try {
       buffer = new CircleStreamBuffer();
-      gen = new JsonFactory().createGenerator(buffer.getOutputStream());
-
-      new ServiceDocumentJsonSerializer(metadata, serviceRoot, isODataMetadataNone).writeServiceDocument(gen);
-
-      gen.close();
+      JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
+      new ServiceDocumentJsonSerializer(metadata, serviceRoot, isODataMetadataNone).writeServiceDocument(json);
 
+      json.close();
+      buffer.getOutputStream().close();
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
-
     } catch (final IOException e) {
-      log.error(e.getMessage(), e);
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
     } finally {
-      if (gen != null) {
-        try {
-          gen.close();
-        } catch (IOException e) {
-          throw new SerializerException("An I/O exception occurred.", e,
-              SerializerException.MessageKeys.IO_EXCEPTION);
-        }
-      }
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
@@ -122,24 +109,33 @@ public class ODataJsonSerializer implements ODataSerializer {
 
   @Override
   public SerializerResult error(final ODataServerError error) throws SerializerException {
-    CircleStreamBuffer buffer = new CircleStreamBuffer();
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
+      buffer = new CircleStreamBuffer();
       JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
       new ODataErrorSerializer().writeErrorDocument(json, error);
+
       json.close();
+      buffer.getOutputStream().close();
+      return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final IOException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
-    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
   }
 
   @Override
   public SerializerResult entityCollection(final ServiceMetadata metadata,
       final EdmEntityType entityType, final EntityCollection entitySet,
       final EntityCollectionSerializerOptions options) throws SerializerException {
-    CircleStreamBuffer buffer = new CircleStreamBuffer();
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
+      buffer = new CircleStreamBuffer();
       JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
       json.writeStartObject();
 
@@ -159,32 +155,44 @@ public class ODataJsonSerializer implements ODataSerializer {
             options.getExpand(), options.getSelect(), options.onlyReferences(), json);
       }
       writeNextLink(entitySet, json);
+
       json.close();
+      buffer.getOutputStream().close();
+      return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final IOException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
-    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
   }
 
   @Override
   public SerializerResult entity(final ServiceMetadata metadata, final EdmEntityType entityType,
       final Entity entity, final EntitySerializerOptions options) throws SerializerException {
-    final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    CircleStreamBuffer buffer = new CircleStreamBuffer();
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
+      final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
+      buffer = new CircleStreamBuffer();
       JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
       writeEntity(metadata, entityType, entity, contextURL,
           options == null ? null : options.getExpand(),
           options == null ? null : options.getSelect(),
           options == null ? false : options.onlyReferences(),
           json);
+
       json.close();
+      buffer.getOutputStream().close();
+      return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final IOException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
-    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
   }
 
   private ContextURL checkContextURL(final ContextURL contextURL) throws SerializerException {
@@ -418,12 +426,18 @@ public class ODataJsonSerializer implements ODataSerializer {
   private void writePrimitiveCollection(final EdmPrimitiveType type, final Property property,
       final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
       final Boolean isUnicode, final JsonGenerator json)
-      throws IOException, EdmPrimitiveTypeException, SerializerException {
+      throws IOException, SerializerException {
     json.writeStartArray();
     for (Object value : property.asCollection()) {
       switch (property.getValueType()) {
       case COLLECTION_PRIMITIVE:
-        writePrimitiveValue(type, value, isNullable, maxLength, precision, scale, isUnicode, json);
+        try {
+          writePrimitiveValue(type, value, isNullable, maxLength, precision, scale, isUnicode, json);
+        } catch (EdmPrimitiveTypeException e) {
+          throw new SerializerException("Wrong value for property!", e,
+              SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
+              property.getName(), property.getValue().toString());
+        }
         break;
       case COLLECTION_GEOSPATIAL:
         throw new SerializerException("Property type not yet supported!",
@@ -441,7 +455,7 @@ public class ODataJsonSerializer implements ODataSerializer {
 
   private void writeComplexCollection(final EdmComplexType type, final Property property,
       final Set<List<String>> selectedPaths, final JsonGenerator json)
-      throws IOException, EdmPrimitiveTypeException, SerializerException {
+      throws IOException, SerializerException {
     json.writeStartArray();
     for (Object value : property.asCollection()) {
       switch (property.getValueType()) {
@@ -501,7 +515,7 @@ public class ODataJsonSerializer implements ODataSerializer {
 
   protected void writeComplexValue(final EdmComplexType type, final List<Property> properties,
       final Set<List<String>> selectedPaths, final JsonGenerator json)
-      throws IOException, EdmPrimitiveTypeException, SerializerException {
+      throws IOException, SerializerException {
     json.writeStartObject();
     for (final String propertyName : type.getPropertyNames()) {
       final Property property = findProperty(propertyName, properties);
@@ -526,9 +540,11 @@ public class ODataJsonSerializer implements ODataSerializer {
   @Override
   public SerializerResult primitive(final ServiceMetadata metadata, final EdmPrimitiveType type,
       final Property property, final PrimitiveSerializerOptions options) throws SerializerException {
-    final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    CircleStreamBuffer buffer = new CircleStreamBuffer();
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
+      final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
+      buffer = new CircleStreamBuffer();
       JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
       json.writeStartObject();
       writeContextURL(contextURL, json);
@@ -545,24 +561,32 @@ public class ODataJsonSerializer implements ODataSerializer {
             options == null ? null : options.isUnicode(), json);
       }
       json.writeEndObject();
+
       json.close();
+      buffer.getOutputStream().close();
+      return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final IOException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
     } catch (final EdmPrimitiveTypeException e) {
-      throw new SerializerException("Wrong value for property!", e,
+      cachedException = new SerializerException("Wrong value for property!", e,
           SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
           property.getName(), property.getValue().toString());
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
-    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
   }
 
   @Override
   public SerializerResult complex(final ServiceMetadata metadata, final EdmComplexType type,
       final Property property, final ComplexSerializerOptions options) throws SerializerException {
-    final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    CircleStreamBuffer buffer = new CircleStreamBuffer();
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
+      final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
+      buffer = new CircleStreamBuffer();
       JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
       json.writeStartObject();
       writeContextURL(contextURL, json);
@@ -579,20 +603,27 @@ public class ODataJsonSerializer implements ODataSerializer {
             options == null ? null : options.getExpand(), json);
       }
       json.writeEndObject();
+
       json.close();
+      buffer.getOutputStream().close();
+      return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final IOException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
-    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
   }
 
   @Override
   public SerializerResult primitiveCollection(final ServiceMetadata metadata, final EdmPrimitiveType type,
       final Property property, final PrimitiveSerializerOptions options) throws SerializerException {
-    final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    CircleStreamBuffer buffer = new CircleStreamBuffer();
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
+      final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
+      buffer = new CircleStreamBuffer();
       JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
       json.writeStartObject();
       writeContextURL(contextURL, json);
@@ -605,24 +636,27 @@ public class ODataJsonSerializer implements ODataSerializer {
           options == null ? null : options.getScale(),
           options == null ? null : options.isUnicode(), json);
       json.writeEndObject();
+
       json.close();
+      buffer.getOutputStream().close();
+      return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final IOException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
-    } catch (final EdmPrimitiveTypeException e) {
-      throw new SerializerException("Wrong value for property!", e,
-          SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
-          property.getName(), property.getValue().toString());
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
-    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
   }
 
   @Override
   public SerializerResult complexCollection(final ServiceMetadata metadata, final EdmComplexType type,
       final Property property, final ComplexSerializerOptions options) throws SerializerException {
-    final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    CircleStreamBuffer buffer = new CircleStreamBuffer();
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
+      final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
+      buffer = new CircleStreamBuffer();
       JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
       json.writeStartObject();
       writeContextURL(contextURL, json);
@@ -630,26 +664,29 @@ public class ODataJsonSerializer implements ODataSerializer {
       json.writeFieldName(Constants.VALUE);
       writeComplexCollection(type, property, null, json);
       json.writeEndObject();
+
       json.close();
+      buffer.getOutputStream().close();
+      return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final IOException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
-    } catch (final EdmPrimitiveTypeException e) {
-      throw new SerializerException("Wrong value for property!", e,
-          SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
-          property.getName(), property.getValue().toString());
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
-    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
   }
 
   @Override
   public SerializerResult reference(final ServiceMetadata metadata, final EdmEntitySet edmEntitySet,
       final Entity entity, final ReferenceSerializerOptions options) throws SerializerException {
-    final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    final CircleStreamBuffer buffer = new CircleStreamBuffer();
-    final UriHelper uriHelper = new UriHelperImpl();
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
 
     try {
+      final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
+      buffer = new CircleStreamBuffer();
+      final UriHelper uriHelper = new UriHelperImpl();
       final JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
 
       json.writeStartObject();
@@ -658,22 +695,28 @@ public class ODataJsonSerializer implements ODataSerializer {
       json.writeEndObject();
 
       json.close();
-    } catch (IOException e) {
-      throw new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      buffer.getOutputStream().close();
+      return SerializerResultImpl.with().content(buffer.getInputStream()).build();
+    } catch (final IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
-
-    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
   }
 
   @Override
   public SerializerResult referenceCollection(final ServiceMetadata metadata, final EdmEntitySet edmEntitySet,
       final EntityCollection entityCollection, final ReferenceCollectionSerializerOptions options)
       throws SerializerException {
-    final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    final CircleStreamBuffer buffer = new CircleStreamBuffer();
-    final UriHelper uriHelper = new UriHelperImpl();
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
 
     try {
+      final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
+      buffer = new CircleStreamBuffer();
+      final UriHelper uriHelper = new UriHelperImpl();
       final JsonGenerator json = new JsonFactory().createGenerator(buffer.getOutputStream());
       json.writeStartObject();
 
@@ -693,12 +736,18 @@ public class ODataJsonSerializer implements ODataSerializer {
       writeNextLink(entityCollection, json);
 
       json.writeEndObject();
+
       json.close();
-    } catch (IOException e) {
-      throw new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      buffer.getOutputStream().close();
+      return SerializerResultImpl.with().content(buffer.getInputStream()).build();
+    } catch (final IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
 
-    return SerializerResultImpl.with().content(buffer.getInputStream()).build();
   }
 
   private void writeContextURL(final ContextURL contextURL, JsonGenerator json) throws IOException {

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f9c68b8b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializer.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializer.java
index b9413f7..e7d60c0 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializer.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializer.java
@@ -18,6 +18,7 @@
  */
 package org.apache.olingo.server.core.serializer.xml;
 
+import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.List;
@@ -63,12 +64,13 @@ import org.apache.olingo.server.api.serializer.SerializerResult;
 import org.apache.olingo.server.api.uri.queryoption.ExpandItem;
 import org.apache.olingo.server.api.uri.queryoption.ExpandOption;
 import org.apache.olingo.server.api.uri.queryoption.SelectOption;
+import org.apache.olingo.server.core.serializer.AbstractODataSerializer;
 import org.apache.olingo.server.core.serializer.SerializerResultImpl;
 import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer;
 import org.apache.olingo.server.core.serializer.utils.ContextURLBuilder;
 import org.apache.olingo.server.core.serializer.utils.ExpandSelectHelper;
 
-public class ODataXmlSerializer implements ODataSerializer {
+public class ODataXmlSerializer extends AbstractODataSerializer {
   private static final String DATA = "d";
   private static final String CONTEXT = "context";
   /** The default character set is UTF-8. */
@@ -83,87 +85,74 @@ public class ODataXmlSerializer implements ODataSerializer {
   @Override
   public SerializerResult serviceDocument(final ServiceMetadata metadata, final String serviceRoot)
       throws SerializerException {
-    CircleStreamBuffer buffer;
-    XMLStreamWriter xmlStreamWriter = null;
+    CircleStreamBuffer buffer = null;
     SerializerException cachedException = null;
     try {
       buffer = new CircleStreamBuffer();
-      xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(),
+      XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(),
           DEFAULT_CHARSET);
       ServiceDocumentXmlSerializer serializer = new ServiceDocumentXmlSerializer(metadata, serviceRoot);
       serializer.writeServiceDocument(xmlStreamWriter);
+
       xmlStreamWriter.flush();
       xmlStreamWriter.close();
+      buffer.getOutputStream().close();
 
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
       cachedException =
           new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
       throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
     } finally {
-      if (xmlStreamWriter != null) {
-        try {
-          xmlStreamWriter.close();
-        } catch (XMLStreamException e) {
-          if (cachedException != null) {
-            throw cachedException;
-          } else {
-            throw new SerializerException("An I/O exception occurred.", e,
-                SerializerException.MessageKeys.IO_EXCEPTION);
-          }
-        }
-      }
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
   @Override
   public SerializerResult metadataDocument(final ServiceMetadata serviceMetadata) throws SerializerException {
-    CircleStreamBuffer buffer;
-    XMLStreamWriter xmlStreamWriter = null;
+    CircleStreamBuffer buffer = null;
     SerializerException cachedException = null;
     try {
       buffer = new CircleStreamBuffer();
-      xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(),
+      XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(),
           DEFAULT_CHARSET);
       MetadataDocumentXmlSerializer serializer = new MetadataDocumentXmlSerializer(serviceMetadata);
       serializer.writeMetadataDocument(xmlStreamWriter);
+
       xmlStreamWriter.flush();
       xmlStreamWriter.close();
+      buffer.getOutputStream().close();
 
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
       cachedException =
           new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
       throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
     } finally {
-      if (xmlStreamWriter != null) {
-        try {
-          xmlStreamWriter.close();
-        } catch (XMLStreamException e) {
-          if (cachedException != null) {
-            throw cachedException;
-          } else {
-            throw new SerializerException("An I/O exception occurred.", e,
-                SerializerException.MessageKeys.IO_EXCEPTION);
-          }
-        }
-      }
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
   @Override
   public SerializerResult error(final ODataServerError error) throws SerializerException {
-    CircleStreamBuffer buffer;
-    XMLStreamWriter writer = null;
-
     if (error == null) {
       throw new SerializerException("ODataError object MUST NOT be null!",
           SerializerException.MessageKeys.NULL_INPUT);
     }
 
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
       buffer = new CircleStreamBuffer();
-      writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(),
+      XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(),
           DEFAULT_CHARSET);
       writer.writeStartDocument(ODataSerializer.DEFAULT_CHARSET, "1.0");
 
@@ -182,10 +171,19 @@ public class ODataXmlSerializer implements ODataSerializer {
 
       writer.flush();
       writer.close();
+      buffer.getOutputStream().close();
+
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
@@ -214,17 +212,18 @@ public class ODataXmlSerializer implements ODataSerializer {
       final EntityCollectionSerializerOptions options) throws SerializerException {
 
     final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    if (options.onlyReferences()) {
+    if (options != null && options.onlyReferences()) {
       ReferenceCollectionSerializerOptions rso = ReferenceCollectionSerializerOptions.with()
           .contextURL(contextURL).build();
       return entityReferenceCollection(metadata, entityType, entitySet, rso);
     }
 
-    CircleStreamBuffer buffer;
-    XMLStreamWriter writer = null;
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
       buffer = new CircleStreamBuffer();
-      writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
+      XMLStreamWriter writer =
+          XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
       writer.writeStartDocument(ODataSerializer.DEFAULT_CHARSET, "1.0");
       writer.writeStartElement(ATOM, "feed", NS_ATOM);
       writer.writeNamespace(ATOM, NS_ATOM);
@@ -235,9 +234,11 @@ public class ODataXmlSerializer implements ODataSerializer {
           ContextURLBuilder.create(contextURL).toASCIIString());
       writeMetadataETag(metadata, writer);
 
-      writer.writeStartElement(ATOM, "id", NS_ATOM);
-      writer.writeCharacters(options.getId());
-      writer.writeEndElement();
+      if (options != null) {
+        writer.writeStartElement(ATOM, "id", NS_ATOM);
+        writer.writeCharacters(options.getId());
+        writer.writeEndElement();
+      }
 
       if (options != null && options.getCount() != null && options.getCount().getValue()
           && entitySet.getCount() != null) {
@@ -247,17 +248,31 @@ public class ODataXmlSerializer implements ODataSerializer {
         writeNextLink(entitySet, writer);
       }
 
-      writeEntitySet(metadata, entityType, entitySet,
-          options.getExpand(), options.getSelect(), options.onlyReferences(), writer);
+      if (options == null) {
+        writeEntitySet(metadata, entityType, entitySet, null, null, false, writer);
+      } else {
+        writeEntitySet(metadata, entityType, entitySet,
+            options.getExpand(), options.getSelect(), options.onlyReferences(), writer);
+      }
 
       writer.writeEndElement();
       writer.writeEndDocument();
+
       writer.flush();
       writer.close();
+      buffer.getOutputStream().close();
+
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
@@ -266,29 +281,40 @@ public class ODataXmlSerializer implements ODataSerializer {
       final Entity entity, final EntitySerializerOptions options) throws SerializerException {
     final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
 
-    if (options.onlyReferences()) {
+    if (options != null && options.onlyReferences()) {
       ReferenceSerializerOptions rso = ReferenceSerializerOptions.with()
           .contextURL(contextURL).build();
       return entityReference(metadata, entityType, entity, rso);
     }
 
-    CircleStreamBuffer buffer;
-    XMLStreamWriter writer = null;
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
       buffer = new CircleStreamBuffer();
-      writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
+      XMLStreamWriter writer =
+          XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
       writer.writeStartDocument(ODataSerializer.DEFAULT_CHARSET, "1.0");
       writeEntity(metadata, entityType, entity, contextURL,
           options == null ? null : options.getExpand(),
           options == null ? null : options.getSelect(),
           options == null ? false : options.onlyReferences(), writer, true);
       writer.writeEndDocument();
+
       writer.flush();
       writer.close();
+      buffer.getOutputStream().close();
+
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
@@ -684,7 +710,7 @@ public class ODataXmlSerializer implements ODataSerializer {
 
   private void writeComplexCollection(final ServiceMetadata metadata, final EdmComplexType type,
       final Property property, final Set<List<String>> selectedPaths, final XMLStreamWriter writer)
-      throws XMLStreamException, EdmPrimitiveTypeException, SerializerException {
+      throws XMLStreamException, SerializerException {
     for (Object value : property.asCollection()) {
       writer.writeStartElement(METADATA, "element", NS_METADATA);
       if (derivedComplexType(metadata, type, property.getType()) != null) {
@@ -751,7 +777,7 @@ public class ODataXmlSerializer implements ODataSerializer {
 
   protected void writeComplexValue(final ServiceMetadata metadata, final EdmComplexType type,
       final List<Property> properties, final Set<List<String>> selectedPaths, final XMLStreamWriter writer)
-      throws XMLStreamException, EdmPrimitiveTypeException, SerializerException {
+      throws XMLStreamException, SerializerException {
     for (final String propertyName : type.getPropertyNames()) {
       final Property property = findProperty(propertyName, properties);
       if (selectedPaths == null || ExpandSelectHelper.isSelected(selectedPaths, propertyName)) {
@@ -775,12 +801,13 @@ public class ODataXmlSerializer implements ODataSerializer {
   public SerializerResult primitive(final ServiceMetadata metadata, final EdmPrimitiveType type,
       final Property property, final PrimitiveSerializerOptions options) throws SerializerException {
     final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    CircleStreamBuffer buffer;
-    XMLStreamWriter writer = null;
 
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
       buffer = new CircleStreamBuffer();
-      writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
+      XMLStreamWriter writer =
+          XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
 
       writer.writeStartDocument(ODataSerializer.DEFAULT_CHARSET, "1.0");
       writer.writeStartElement(METADATA, "value", NS_METADATA);
@@ -794,33 +821,35 @@ public class ODataXmlSerializer implements ODataSerializer {
         writer.writeAttribute(METADATA, NS_METADATA, "null", "true");
       } else {
         writePrimitive(type, property,
-            options.isNullable(), options.getMaxLength(), options.getPrecision(), options.getScale(),
-            options.isUnicode(),
+            options == null ? null : options.isNullable(),
+            options == null ? null : options.getMaxLength(),
+            options == null ? null : options.getPrecision(),
+            options == null ? null : options.getScale(),
+            options == null ? null : options.isUnicode(),
             writer);
       }
       writer.writeEndElement();
       writer.writeEndDocument();
       writer.flush();
       writer.close();
+      buffer.getOutputStream().close();
 
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
+      cachedException = new SerializerException("An I/O exception occurred.", e,
           SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
     } catch (final EdmPrimitiveTypeException e) {
-      throw new SerializerException("Wrong value for property!", e,
+      cachedException = new SerializerException("Wrong value for property!", e,
           SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
           property.getName(), property.getValue().toString());
+      throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
     } finally {
-      if (writer != null) {
-        try {
-          writer.close();
-        } catch (XMLStreamException e) {
-          throw new SerializerException("Wrong value for property!", e,
-              SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
-              property.getName(), property.getValue().toString());
-        }
-      }
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
@@ -828,12 +857,14 @@ public class ODataXmlSerializer implements ODataSerializer {
   public SerializerResult complex(final ServiceMetadata metadata, final EdmComplexType type,
       final Property property, final ComplexSerializerOptions options) throws SerializerException {
     final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    CircleStreamBuffer buffer;
-    XMLStreamWriter writer = null;
+
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
       EdmComplexType resolvedType = resolveComplexType(metadata, type, property.getType());
       buffer = new CircleStreamBuffer();
-      writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
+      XMLStreamWriter writer =
+          XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
       writer.writeStartDocument(ODataSerializer.DEFAULT_CHARSET, "1.0");
       writer.writeStartElement(METADATA, "value", NS_METADATA);
       writer.writeNamespace(METADATA, NS_METADATA);
@@ -852,10 +883,19 @@ public class ODataXmlSerializer implements ODataSerializer {
       writer.writeEndDocument();
       writer.flush();
       writer.close();
+      buffer.getOutputStream().close();
+
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
@@ -863,12 +903,13 @@ public class ODataXmlSerializer implements ODataSerializer {
   public SerializerResult primitiveCollection(final ServiceMetadata metadata, final EdmPrimitiveType type,
       final Property property, final PrimitiveSerializerOptions options) throws SerializerException {
     final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    CircleStreamBuffer buffer;
-    XMLStreamWriter writer = null;
 
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
       buffer = new CircleStreamBuffer();
-      writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
+      XMLStreamWriter writer =
+          XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
 
       writer.writeStartDocument(ODataSerializer.DEFAULT_CHARSET, "1.0");
       writer.writeStartElement(METADATA, "value", NS_METADATA);
@@ -880,32 +921,34 @@ public class ODataXmlSerializer implements ODataSerializer {
       writeMetadataETag(metadata, writer);
       writer.writeAttribute(METADATA, NS_METADATA, "type", "#Collection(" + type.getName() + ")");
       writePrimitiveCollection(type, property,
-          options.isNullable(), options.getMaxLength(), options.getPrecision(), options.getScale(),
-          options.isUnicode(),
+          options == null ? null : options.isNullable(),
+          options == null ? null : options.getMaxLength(),
+          options == null ? null : options.getPrecision(),
+          options == null ? null : options.getScale(),
+          options == null ? null : options.isUnicode(),
           writer);
       writer.writeEndElement();
       writer.writeEndDocument();
       writer.flush();
       writer.close();
+      buffer.getOutputStream().close();
 
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
+      cachedException = new SerializerException("An I/O exception occurred.", e,
           SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
     } catch (final EdmPrimitiveTypeException e) {
-      throw new SerializerException("Wrong value for property!", e,
+      cachedException = new SerializerException("Wrong value for property!", e,
           SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
           property.getName(), property.getValue().toString());
+      throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
     } finally {
-      if (writer != null) {
-        try {
-          writer.close();
-        } catch (XMLStreamException e) {
-          throw new SerializerException("Wrong value for property!", e,
-              SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
-              property.getName(), property.getValue().toString());
-        }
-      }
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
@@ -913,12 +956,13 @@ public class ODataXmlSerializer implements ODataSerializer {
   public SerializerResult complexCollection(final ServiceMetadata metadata, final EdmComplexType type,
       final Property property, final ComplexSerializerOptions options) throws SerializerException {
     final ContextURL contextURL = checkContextURL(options == null ? null : options.getContextURL());
-    CircleStreamBuffer buffer;
-    XMLStreamWriter writer = null;
 
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
       buffer = new CircleStreamBuffer();
-      writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
+      XMLStreamWriter writer =
+          XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
 
       writer.writeStartElement(METADATA, "value", NS_METADATA);
       writer.writeNamespace(METADATA, NS_METADATA);
@@ -932,14 +976,18 @@ public class ODataXmlSerializer implements ODataSerializer {
       writer.writeEndDocument();
       writer.flush();
       writer.close();
+      buffer.getOutputStream().close();
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
-    } catch (final EdmPrimitiveTypeException e) {
-      throw new SerializerException("Wrong value for property!", e,
-          SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
-          property.getName(), property.getValue().toString());
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
@@ -951,20 +999,30 @@ public class ODataXmlSerializer implements ODataSerializer {
 
   protected SerializerResult entityReference(final ServiceMetadata metadata, final EdmEntityType entityType,
       final Entity entity, ReferenceSerializerOptions options) throws SerializerException {
-    CircleStreamBuffer buffer;
-    XMLStreamWriter writer = null;
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
       buffer = new CircleStreamBuffer();
-      writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
+      XMLStreamWriter writer =
+          XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
       writer.writeStartDocument(ODataSerializer.DEFAULT_CHARSET, "1.0");
-      writeReference(metadata, entityType, entity, options.getContextURL(), writer, true);
+      writeReference(metadata, entityType, entity, options == null ? null : options.getContextURL(), writer, true);
       writer.writeEndDocument();
       writer.flush();
       writer.close();
+      buffer.getOutputStream().close();
+
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 
@@ -992,16 +1050,17 @@ public class ODataXmlSerializer implements ODataSerializer {
   protected SerializerResult entityReferenceCollection(final ServiceMetadata metadata,
       final EdmEntityType entityType, final EntityCollection entitySet,
       ReferenceCollectionSerializerOptions options) throws SerializerException {
-    CircleStreamBuffer buffer;
-    XMLStreamWriter writer = null;
+    CircleStreamBuffer buffer = null;
+    SerializerException cachedException = null;
     try {
       buffer = new CircleStreamBuffer();
-      writer = XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
+      XMLStreamWriter writer =
+          XMLOutputFactory.newInstance().createXMLStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET);
       writer.writeStartDocument(ODataSerializer.DEFAULT_CHARSET, "1.0");
       writer.writeStartElement(ATOM, "feed", NS_ATOM);
       writer.writeNamespace(ATOM, NS_ATOM);
       writer.writeNamespace(METADATA, NS_METADATA);
-      if (options.getContextURL() != null) { // top-level entity
+      if (options != null && options.getContextURL() != null) { // top-level entity
         writer.writeAttribute(METADATA, NS_METADATA, CONTEXT, ContextURLBuilder.create(options.getContextURL())
             .toASCIIString());
       }
@@ -1013,16 +1072,25 @@ public class ODataXmlSerializer implements ODataSerializer {
         writeNextLink(entitySet, writer);
       }
       for (final Entity entity : entitySet.getEntities()) {
-        writeReference(metadata, entityType, entity, options.getContextURL(), writer, false);
+        writeReference(metadata, entityType, entity, options == null ? null : options.getContextURL(), writer, false);
       }
       writer.writeEndElement();
       writer.writeEndDocument();
       writer.flush();
       writer.close();
+      buffer.getOutputStream().close();
+
       return SerializerResultImpl.with().content(buffer.getInputStream()).build();
     } catch (final XMLStreamException e) {
-      throw new SerializerException("An I/O exception occurred.", e,
-          SerializerException.MessageKeys.IO_EXCEPTION);
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } catch (IOException e) {
+      cachedException =
+          new SerializerException("An I/O exception occurred.", e, SerializerException.MessageKeys.IO_EXCEPTION);
+      throw cachedException;
+    } finally {
+      closeCircleStreamBufferOutput(buffer, cachedException);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f9c68b8b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java
index 135ba56..e0d77c3 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java
@@ -312,6 +312,7 @@ public class Parser {
 
       // create parser
       if (logLevel > 0) {
+        //TODO: Discuss if we should keep this code
         lexer = new UriLexer(new ANTLRInputStream(input));
         showTokens(input, lexer.getAllTokens());
       }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f9c68b8b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/queryoption/ExpandItemImpl.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/queryoption/ExpandItemImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/queryoption/ExpandItemImpl.java
index aa20845..66b1bb6 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/queryoption/ExpandItemImpl.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/queryoption/ExpandItemImpl.java
@@ -31,7 +31,6 @@ import org.apache.olingo.server.api.uri.queryoption.OrderByOption;
 import org.apache.olingo.server.api.uri.queryoption.SearchOption;
 import org.apache.olingo.server.api.uri.queryoption.SelectOption;
 import org.apache.olingo.server.api.uri.queryoption.SkipOption;
-import org.apache.olingo.server.api.uri.queryoption.SystemQueryOptionKind;
 import org.apache.olingo.server.api.uri.queryoption.TopOption;
 
 public class ExpandItemImpl implements ExpandItem {

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f9c68b8b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabRequestTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabRequestTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabRequestTest.java
index 906adbd..24732ba 100644
--- a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabRequestTest.java
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabRequestTest.java
@@ -112,7 +112,6 @@ public class DebugTabRequestTest extends AbstractDebugTabTest {
     oDataRequest.addHeader("HeaderName", headerValues);
 
     DebugTabRequest requestTab = new DebugTabRequest(oDataRequest);
-    System.out.println(createHtml(requestTab));
     assertEquals(expectedJson, createJson(requestTab));
     assertEquals(expectedHtml, createHtml(requestTab));
   }