[OLINGO-659] FIx all stream handling issues

This commit is contained in:
Christian Amend 2015-08-17 13:29:48 +02:00
parent 262cee8b7e
commit 75b5523080
4 changed files with 28 additions and 20 deletions

View File

@ -21,6 +21,7 @@ package org.apache.olingo.server.core.debug;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.util.ArrayList; import java.util.ArrayList;
@ -133,11 +134,13 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
} }
private InputStream wrapInJson(final List<DebugTab> parts) throws IOException { private InputStream wrapInJson(final List<DebugTab> parts) throws IOException {
CircleStreamBuffer csb = new CircleStreamBuffer();
IOException cachedException = null; IOException cachedException = null;
OutputStream outputStream = null;
try { try {
JsonGenerator gen = new JsonFactory().createGenerator(csb.getOutputStream(), JsonEncoding.UTF8); CircleStreamBuffer csb = new CircleStreamBuffer();
outputStream = csb.getOutputStream();
JsonGenerator gen = new JsonFactory().createGenerator(outputStream, JsonEncoding.UTF8);
gen.writeStartObject(); gen.writeStartObject();
DebugTab requestInfo = parts.get(0); DebugTab requestInfo = parts.get(0);
@ -159,14 +162,15 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
gen.writeEndObject(); gen.writeEndObject();
gen.close(); gen.close();
csb.getOutputStream().close(); outputStream.close();
return csb.getInputStream();
} catch (IOException e) { } catch (IOException e) {
throw e; throw e;
} finally { } finally {
if (csb != null && csb.getOutputStream() != null) { if (outputStream != null) {
try { try {
csb.getOutputStream().close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {
if (cachedException != null) { if (cachedException != null) {
throw cachedException; throw cachedException;
@ -176,8 +180,6 @@ public class DebugResponseHelperImpl implements DebugResponseHelper {
} }
} }
} }
return csb.getInputStream();
} }
/** /**

View File

@ -19,6 +19,8 @@
package org.apache.olingo.server.core.debug; package org.apache.olingo.server.core.debug;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer; import java.io.Writer;
import java.util.List; import java.util.List;
@ -256,19 +258,20 @@ public class DebugTabUri implements DebugTab {
private String getJsonString() throws IOException { private String getJsonString() throws IOException {
CircleStreamBuffer csb = new CircleStreamBuffer(); CircleStreamBuffer csb = new CircleStreamBuffer();
IOException cachedException = null; IOException cachedException = null;
OutputStream outputStream = csb.getOutputStream();
try { try {
JsonGenerator json = JsonGenerator json =
new JsonFactory().createGenerator(csb.getOutputStream(), JsonEncoding.UTF8) new JsonFactory().createGenerator(outputStream, JsonEncoding.UTF8)
.setPrettyPrinter(new DefaultPrettyPrinter()); .setPrettyPrinter(new DefaultPrettyPrinter());
appendJson(json); appendJson(json);
json.close(); json.close();
csb.getOutputStream().close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {
throw e; throw e;
} finally { } finally {
if (csb != null && csb.getOutputStream() != null) { if (outputStream != null) {
try { try {
csb.getOutputStream().close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {
if (cachedException != null) { if (cachedException != null) {
throw cachedException; throw cachedException;
@ -279,16 +282,17 @@ public class DebugTabUri implements DebugTab {
} }
} }
InputStream inputStream = csb.getInputStream();
try { try {
String jsonString = IOUtils.toString(csb.getInputStream()); String jsonString = IOUtils.toString(inputStream);
csb.getInputStream().close(); inputStream.close();
return jsonString; return jsonString;
} catch (IOException e) { } catch (IOException e) {
throw e; throw e;
} finally { } finally {
if (csb != null && csb.getInputStream() != null) { if (inputStream != null) {
try { try {
csb.getInputStream().close(); inputStream.close();
} catch (IOException e) { } catch (IOException e) {
if (cachedException != null) { if (cachedException != null) {
throw cachedException; throw cachedException;

View File

@ -21,6 +21,7 @@ package org.apache.olingo.server.core.serializer.json;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -319,7 +320,7 @@ public class ODataJsonSerializer extends AbstractODataSerializer {
final SelectOption select, final JsonGenerator json) final SelectOption select, final JsonGenerator json)
throws IOException, SerializerException { throws IOException, SerializerException {
final boolean all = ExpandSelectHelper.isAll(select); final boolean all = ExpandSelectHelper.isAll(select);
final Set<String> selected = all ? null : final Set<String> selected = all ? new HashSet<String>() :
ExpandSelectHelper.getSelectedPropertyNames(select.getSelectItems()); ExpandSelectHelper.getSelectedPropertyNames(select.getSelectItems());
for (final String propertyName : type.getPropertyNames()) { for (final String propertyName : type.getPropertyNames()) {
if (all || selected.contains(propertyName)) { if (all || selected.contains(propertyName)) {
@ -337,7 +338,7 @@ public class ODataJsonSerializer extends AbstractODataSerializer {
final JsonGenerator json) throws SerializerException, IOException { final JsonGenerator json) throws SerializerException, IOException {
if (ExpandSelectHelper.hasExpand(expand)) { if (ExpandSelectHelper.hasExpand(expand)) {
final boolean expandAll = ExpandSelectHelper.isExpandAll(expand); final boolean expandAll = ExpandSelectHelper.isExpandAll(expand);
final Set<String> expanded = expandAll ? null : final Set<String> expanded = expandAll ? new HashSet<String>() :
ExpandSelectHelper.getExpandedPropertyNames(expand.getExpandItems()); ExpandSelectHelper.getExpandedPropertyNames(expand.getExpandItems());
for (final String propertyName : type.getNavigationPropertyNames()) { for (final String propertyName : type.getNavigationPropertyNames()) {
if (expandAll || expanded.contains(propertyName)) { if (expandAll || expanded.contains(propertyName)) {

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -495,7 +496,7 @@ public class ODataXmlSerializer extends AbstractODataSerializer {
final List<Property> properties, final SelectOption select, final XMLStreamWriter writer) final List<Property> properties, final SelectOption select, final XMLStreamWriter writer)
throws XMLStreamException, SerializerException { throws XMLStreamException, SerializerException {
final boolean all = ExpandSelectHelper.isAll(select); final boolean all = ExpandSelectHelper.isAll(select);
final Set<String> selected = all ? null : final Set<String> selected = all ? new HashSet<String>() :
ExpandSelectHelper.getSelectedPropertyNames(select.getSelectItems()); ExpandSelectHelper.getSelectedPropertyNames(select.getSelectItems());
for (final String propertyName : type.getPropertyNames()) { for (final String propertyName : type.getPropertyNames()) {
if (all || selected.contains(propertyName)) { if (all || selected.contains(propertyName)) {
@ -513,7 +514,7 @@ public class ODataXmlSerializer extends AbstractODataSerializer {
final XMLStreamWriter writer) throws SerializerException, XMLStreamException { final XMLStreamWriter writer) throws SerializerException, XMLStreamException {
if (ExpandSelectHelper.hasExpand(expand)) { if (ExpandSelectHelper.hasExpand(expand)) {
final boolean expandAll = ExpandSelectHelper.isExpandAll(expand); final boolean expandAll = ExpandSelectHelper.isExpandAll(expand);
final Set<String> expanded = expandAll ? null : final Set<String> expanded = expandAll ? new HashSet<String>() :
ExpandSelectHelper.getExpandedPropertyNames(expand.getExpandItems()); ExpandSelectHelper.getExpandedPropertyNames(expand.getExpandItems());
for (final String propertyName : type.getNavigationPropertyNames()) { for (final String propertyName : type.getNavigationPropertyNames()) {
final EdmNavigationProperty property = type.getNavigationProperty(propertyName); final EdmNavigationProperty property = type.getNavigationProperty(propertyName);