[OLINGO-1066] Consolidation of streaming support in ODataWritableContent
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/9de1d310 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/9de1d310 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/9de1d310 Branch: refs/heads/OLINGO-1066_StreamSupport Commit: 9de1d310b1c402cceb307fb527b807e5054230c8 Parents: 685b19e Author: mibo <[email protected]> Authored: Sun Feb 19 01:00:41 2017 +0100 Committer: mibo <[email protected]> Committed: Sun Feb 19 01:00:41 2017 +0100 ---------------------------------------------------------------------- .../server/core/CollectionWritableContent.java | 39 ---- .../server/core/ComplexStreamContent.java | 86 --------- .../core/ComplexStreamContentForJson.java | 52 ----- .../server/core/ComplexStreamContentForXml.java | 52 ----- .../server/core/ODataWritableContent.java | 188 ++++++++++++++++--- .../server/core/PrimitiveStreamContent.java | 96 ---------- .../core/PrimitiveStreamContentForJson.java | 56 ------ .../core/PrimitiveStreamContentForXml.java | 56 ------ .../serializer/json/ODataJsonSerializer.java | 13 +- .../core/serializer/xml/ODataXmlSerializer.java | 14 +- .../java/myservice/mynamespace/util/Util.java | 2 +- .../java/myservice/mynamespace/util/Util.java | 2 +- .../java/myservice/mynamespace/util/Util.java | 2 +- 13 files changed, 181 insertions(+), 477 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/lib/server-core/src/main/java/org/apache/olingo/server/core/CollectionWritableContent.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/CollectionWritableContent.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/CollectionWritableContent.java deleted file mode 100644 index 27bb061..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/CollectionWritableContent.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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; - -import java.io.OutputStream; -import java.nio.channels.Channels; -import java.nio.channels.WritableByteChannel; - -import org.apache.olingo.server.api.ODataContent; - -public abstract class CollectionWritableContent implements ODataContent { - - protected abstract void writeCollection(OutputStream out); - - public void write(OutputStream stream) { - write(Channels.newChannel(stream)); - } - - @Override - public void write(WritableByteChannel writeChannel) { - writeCollection(Channels.newOutputStream(writeChannel)); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContent.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContent.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContent.java deleted file mode 100644 index 9496420..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContent.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * 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; - -import java.io.OutputStream; -import java.nio.channels.Channels; - -import org.apache.olingo.commons.api.data.ComplexIterator; -import org.apache.olingo.commons.api.edm.EdmComplexType; -import org.apache.olingo.server.api.ODataContentWriteErrorCallback; -import org.apache.olingo.server.api.ServiceMetadata; -import org.apache.olingo.server.api.serializer.ComplexSerializerOptions; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.apache.olingo.server.api.serializer.SerializerStreamResult; -import org.apache.olingo.server.core.serializer.SerializerStreamResultImpl; -import org.apache.olingo.server.core.serializer.json.ODataJsonSerializer; -import org.apache.olingo.server.core.serializer.xml.ODataXmlSerializer; - -public abstract class ComplexStreamContent extends CollectionWritableContent { - protected ComplexIterator iterator; - protected ServiceMetadata metadata; - protected EdmComplexType complexType; - protected ComplexSerializerOptions options; - - protected ComplexStreamContent(ComplexIterator iterator, ServiceMetadata metadata, EdmComplexType - complexType, ComplexSerializerOptions options) { - this.iterator = iterator; - this.complexType = complexType; - this.metadata = metadata; - this.options = options; - } - - protected abstract void writeComplex(OutputStream outputStream) throws SerializerException; - - @Override - protected void writeCollection(OutputStream out) { - try { - writeComplex(out); - } catch (SerializerException e) { - final ODataContentWriteErrorCallback errorCallback = options.getODataContentWriteErrorCallback(); - if (errorCallback != null) { - final WriteErrorContext errorContext = new WriteErrorContext(e); - errorCallback.handleError(errorContext, Channels.newChannel(out)); - } - } - } - - public static SerializerStreamResult ComplexWritableForJson(ComplexIterator iterator, EdmComplexType edmComplexType, - ODataJsonSerializer jsonSerializer, ServiceMetadata serviceMetadata, - ComplexSerializerOptions options) { - return SerializerStreamResultImpl.with() - .content(new ComplexStreamContentForJson(iterator, - edmComplexType, - jsonSerializer, - serviceMetadata, - options)).build(); - } - - public static SerializerStreamResult ComplexWritableForXml(ComplexIterator iterator, EdmComplexType - edmComplexType, - ODataXmlSerializer xmlSerializer, ServiceMetadata serviceMetadata, - ComplexSerializerOptions options) { - return SerializerStreamResultImpl.with() - .content(new ComplexStreamContentForXml(iterator, - edmComplexType, - xmlSerializer, - serviceMetadata, - options)).build(); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContentForJson.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContentForJson.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContentForJson.java deleted file mode 100644 index 1a6a262..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContentForJson.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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; - -import java.io.IOException; -import java.io.OutputStream; - -import org.apache.olingo.commons.api.data.ComplexIterator; -import org.apache.olingo.commons.api.edm.EdmComplexType; -import org.apache.olingo.commons.api.ex.ODataRuntimeException; -import org.apache.olingo.server.api.ServiceMetadata; -import org.apache.olingo.server.api.serializer.ComplexSerializerOptions; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.apache.olingo.server.core.serializer.json.ODataJsonSerializer; - -class ComplexStreamContentForJson extends ComplexStreamContent { - private final ODataJsonSerializer jsonSerializer; - - protected ComplexStreamContentForJson(ComplexIterator iterator, EdmComplexType edmComplexType, - ODataJsonSerializer jsonSerializer, ServiceMetadata serviceMetadata, - ComplexSerializerOptions options) { - super(iterator, serviceMetadata, edmComplexType, options); - - this.jsonSerializer = jsonSerializer; - } - - @Override - protected void writeComplex(OutputStream outputStream) throws SerializerException { - try { - jsonSerializer.complexCollectionIntoStream(metadata, complexType, iterator, options, outputStream); - outputStream.flush(); - } catch (final IOException e) { - throw new ODataRuntimeException("Failed complex serialization", e); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContentForXml.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContentForXml.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContentForXml.java deleted file mode 100644 index a6aec37..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ComplexStreamContentForXml.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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; - -import java.io.IOException; -import java.io.OutputStream; - -import org.apache.olingo.commons.api.data.ComplexIterator; -import org.apache.olingo.commons.api.edm.EdmComplexType; -import org.apache.olingo.commons.api.ex.ODataRuntimeException; -import org.apache.olingo.server.api.ServiceMetadata; -import org.apache.olingo.server.api.serializer.ComplexSerializerOptions; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.apache.olingo.server.core.serializer.xml.ODataXmlSerializer; - -class ComplexStreamContentForXml extends ComplexStreamContent { - private final ODataXmlSerializer xmlSerializer; - - protected ComplexStreamContentForXml(ComplexIterator iterator, EdmComplexType edmComplexType, - ODataXmlSerializer xmlSerializer, ServiceMetadata serviceMetadata, - ComplexSerializerOptions options) { - super(iterator, serviceMetadata, edmComplexType, options); - - this.xmlSerializer = xmlSerializer; - } - - @Override - protected void writeComplex(OutputStream outputStream) throws SerializerException { - try { - xmlSerializer.complexCollectionIntoStream(metadata, complexType, iterator, options, outputStream); - outputStream.flush(); - } catch (final IOException e) { - throw new ODataRuntimeException("Failed complex serialization", e); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataWritableContent.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataWritableContent.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataWritableContent.java index 9986a0f..23d0a14 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataWritableContent.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataWritableContent.java @@ -22,15 +22,23 @@ import java.io.IOException; import java.io.OutputStream; import java.nio.channels.Channels; import java.nio.channels.WritableByteChannel; +import java.util.Iterator; +import org.apache.olingo.commons.api.data.ComplexIterator; import org.apache.olingo.commons.api.data.EntityIterator; +import org.apache.olingo.commons.api.data.PrimitiveIterator; +import org.apache.olingo.commons.api.edm.EdmComplexType; import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveType; +import org.apache.olingo.commons.api.edm.EdmType; import org.apache.olingo.commons.api.ex.ODataRuntimeException; import org.apache.olingo.server.api.ODataContent; import org.apache.olingo.server.api.ODataContentWriteErrorCallback; 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.ODataSerializer; +import org.apache.olingo.server.api.serializer.PrimitiveSerializerOptions; import org.apache.olingo.server.api.serializer.SerializerException; import org.apache.olingo.server.api.serializer.SerializerStreamResult; import org.apache.olingo.server.core.serializer.SerializerStreamResultImpl; @@ -48,27 +56,46 @@ import org.apache.olingo.server.core.serializer.xml.ODataXmlSerializer; public class ODataWritableContent implements ODataContent { private StreamContent streamContent; - private static abstract class StreamContent { - protected EntityIterator iterator; + public static abstract class StreamContent<T extends Iterator, + P extends EdmType, O> implements ODataContent { + protected T iterator; + protected P type; + protected O options; protected ServiceMetadata metadata; - protected EdmEntityType entityType; - protected EntityCollectionSerializerOptions options; + ODataContentWriteErrorCallback errorCallback; - public StreamContent(EntityIterator iterator, EdmEntityType entityType, ServiceMetadata metadata, - EntityCollectionSerializerOptions options) { + StreamContent(T iterator, ServiceMetadata metadata, + P edmType, O options) { this.iterator = iterator; - this.entityType = entityType; + this.type = edmType; this.metadata = metadata; this.options = options; + if(options != null) { + if(options instanceof ComplexSerializerOptions) { + this.errorCallback = ((ComplexSerializerOptions) options).getODataContentWriteErrorCallback(); + } else if(options instanceof EntityCollectionSerializerOptions) { + this.errorCallback = ((EntityCollectionSerializerOptions) options).getODataContentWriteErrorCallback(); + } else if(options instanceof PrimitiveSerializerOptions) { + this.errorCallback = ((PrimitiveSerializerOptions) options).getODataContentWriteErrorCallback(); + } + } + } + + protected abstract void writeInternal(OutputStream outputStream) throws SerializerException; + + public void write(OutputStream stream) { + write(Channels.newChannel(stream)); } - protected abstract void writeEntity(EntityIterator entity, OutputStream outputStream) throws SerializerException; + @Override + public void write(WritableByteChannel writeChannel) { + writeCollection(Channels.newOutputStream(writeChannel)); + } - public void write(OutputStream out) { + void writeCollection(OutputStream out) { try { - writeEntity(iterator, out); + writeInternal(out); } catch (SerializerException e) { - final ODataContentWriteErrorCallback errorCallback = options.getODataContentWriteErrorCallback(); if (errorCallback != null) { final WriteErrorContext errorContext = new WriteErrorContext(e); errorCallback.handleError(errorContext, Channels.newChannel(out)); @@ -77,20 +104,21 @@ public class ODataWritableContent implements ODataContent { } } - private static class StreamContentForJson extends StreamContent { + private static class StreamContentForJson + extends StreamContent<EntityIterator, EdmEntityType, EntityCollectionSerializerOptions> { private ODataJsonSerializer jsonSerializer; - public StreamContentForJson(EntityIterator iterator, EdmEntityType entityType, - ODataJsonSerializer jsonSerializer, ServiceMetadata metadata, - EntityCollectionSerializerOptions options) { - super(iterator, entityType, metadata, options); + StreamContentForJson(EntityIterator iterator, EdmEntityType entityType, + ODataJsonSerializer jsonSerializer, ServiceMetadata metadata, + EntityCollectionSerializerOptions options) { + super(iterator, metadata, entityType, options); this.jsonSerializer = jsonSerializer; } - protected void writeEntity(EntityIterator entity, OutputStream outputStream) throws SerializerException { + protected void writeInternal(OutputStream outputStream) throws SerializerException { try { - jsonSerializer.entityCollectionIntoStream(metadata, entityType, entity, options, outputStream); + jsonSerializer.entityCollectionIntoStream(metadata, type, iterator, options, outputStream); outputStream.flush(); } catch (final IOException e) { throw new ODataRuntimeException("Failed entity serialization", e); @@ -98,20 +126,72 @@ public class ODataWritableContent implements ODataContent { } } - private static class StreamContentForXml extends StreamContent { + public static class ComplexStreamContentForJson + extends StreamContent<ComplexIterator, EdmComplexType, ComplexSerializerOptions> { + private final ODataJsonSerializer jsonSerializer; + + public ComplexStreamContentForJson(ComplexIterator iterator, EdmComplexType edmComplexType, + ODataJsonSerializer jsonSerializer, ServiceMetadata serviceMetadata, + ComplexSerializerOptions options) { + super(iterator, serviceMetadata, edmComplexType, options); + + this.jsonSerializer = jsonSerializer; + } + + @Override + protected void writeInternal(OutputStream outputStream) throws SerializerException { + try { + jsonSerializer.complexCollectionIntoStream(metadata, type, iterator, options, outputStream); + outputStream.flush(); + } catch (final IOException e) { + throw new ODataRuntimeException("Failed complex serialization", e); + } + } + } + + public static class PrimitiveStreamContentForJson + extends StreamContent<PrimitiveIterator, EdmPrimitiveType, PrimitiveSerializerOptions> { + private final ODataJsonSerializer jsonSerializer; + + public PrimitiveStreamContentForJson( + PrimitiveIterator iterator, + EdmPrimitiveType primitiveType, + ODataJsonSerializer jsonSerializer, + ServiceMetadata serviceMetadata, + PrimitiveSerializerOptions options) { + + super(iterator, serviceMetadata, primitiveType, options); + + this.jsonSerializer = jsonSerializer; + } + + protected void writeInternal(OutputStream outputStream) throws SerializerException { + try { + jsonSerializer.primitiveCollectionIntoStream(metadata, type, iterator, options, outputStream); + outputStream.flush(); + } catch (final IOException e) { + throw new ODataRuntimeException("Failed complex serialization", e); + } + } + } + + + private static class StreamContentForXml + extends StreamContent<EntityIterator, EdmEntityType, EntityCollectionSerializerOptions> { private ODataXmlSerializer xmlSerializer; - public StreamContentForXml(EntityIterator iterator, EdmEntityType entityType, - ODataXmlSerializer xmlSerializer, ServiceMetadata metadata, - EntityCollectionSerializerOptions options) { - super(iterator, entityType, metadata, options); + StreamContentForXml(EntityIterator iterator, EdmEntityType entityType, + ODataXmlSerializer xmlSerializer, ServiceMetadata metadata, + EntityCollectionSerializerOptions options) { + super(iterator, metadata, entityType, options); this.xmlSerializer = xmlSerializer; } - protected void writeEntity(EntityIterator entity, OutputStream outputStream) throws SerializerException { + @Override + protected void writeInternal(OutputStream outputStream) throws SerializerException { try { - xmlSerializer.entityCollectionIntoStream(metadata, entityType, entity, options, outputStream); + xmlSerializer.entityCollectionIntoStream(metadata, type, iterator, options, outputStream); outputStream.flush(); } catch (final IOException e) { throw new ODataRuntimeException("Failed entity serialization", e); @@ -119,6 +199,58 @@ public class ODataWritableContent implements ODataContent { } } + public static class ComplexStreamContentForXml + extends StreamContent<ComplexIterator, EdmComplexType, ComplexSerializerOptions> { + private final ODataXmlSerializer xmlSerializer; + + public ComplexStreamContentForXml(ComplexIterator iterator, EdmComplexType edmComplexType, + ODataXmlSerializer xmlSerializer, ServiceMetadata serviceMetadata, + ComplexSerializerOptions options) { + super(iterator, serviceMetadata, edmComplexType, options); + + this.xmlSerializer = xmlSerializer; + } + + @Override + protected void writeInternal(OutputStream outputStream) throws SerializerException { + try { + xmlSerializer.complexCollectionIntoStream(metadata, type, iterator, options, outputStream); + outputStream.flush(); + } catch (final IOException e) { + throw new ODataRuntimeException("Failed complex serialization", e); + } + } + } + + public static class PrimitiveStreamContentForXml + extends StreamContent<PrimitiveIterator, EdmPrimitiveType, PrimitiveSerializerOptions> { + + private final ODataXmlSerializer xmlSerializer; + + public PrimitiveStreamContentForXml( + PrimitiveIterator iterator, + EdmPrimitiveType primitiveType, + ODataXmlSerializer xmlSerializer, + ServiceMetadata serviceMetadata, + PrimitiveSerializerOptions options) { + + super(iterator, serviceMetadata, primitiveType, options); + + this.xmlSerializer = xmlSerializer; + } + + @Override + protected void writeInternal(OutputStream outputStream) throws SerializerException { + try { + xmlSerializer.primitiveCollectionIntoStream(metadata, type, iterator, options, outputStream); + outputStream.flush(); + } catch (final IOException e) { + throw new ODataRuntimeException("Failed complex serialization", e); + } + } + } + + @Override public void write(WritableByteChannel writeChannel) { this.streamContent.write(Channels.newOutputStream(writeChannel)); @@ -146,9 +278,9 @@ public class ODataWritableContent implements ODataContent { private EdmEntityType entityType; private EntityCollectionSerializerOptions options; - public ODataWritableContentBuilder(EntityIterator entities, EdmEntityType entityType, - ODataSerializer serializer, - ServiceMetadata metadata, EntityCollectionSerializerOptions options) { + ODataWritableContentBuilder(EntityIterator entities, EdmEntityType entityType, + ODataSerializer serializer, + ServiceMetadata metadata, EntityCollectionSerializerOptions options) { this.entities = entities; this.entityType = entityType; this.serializer = serializer; http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContent.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContent.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContent.java deleted file mode 100644 index 1a90b92..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContent.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * 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; - -import java.io.OutputStream; -import java.nio.channels.Channels; - -import org.apache.olingo.commons.api.data.PrimitiveIterator; -import org.apache.olingo.commons.api.edm.EdmPrimitiveType; -import org.apache.olingo.server.api.ODataContentWriteErrorCallback; -import org.apache.olingo.server.api.ServiceMetadata; -import org.apache.olingo.server.api.serializer.PrimitiveSerializerOptions; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.apache.olingo.server.api.serializer.SerializerStreamResult; -import org.apache.olingo.server.core.serializer.SerializerStreamResultImpl; -import org.apache.olingo.server.core.serializer.json.ODataJsonSerializer; -import org.apache.olingo.server.core.serializer.xml.ODataXmlSerializer; - -public abstract class PrimitiveStreamContent extends CollectionWritableContent { - protected PrimitiveIterator iterator; - protected ServiceMetadata metadata; - protected EdmPrimitiveType primitiveType; - protected PrimitiveSerializerOptions options; - - protected PrimitiveStreamContent( - PrimitiveIterator iterator, - ServiceMetadata metadata, - EdmPrimitiveType primitiveType, - PrimitiveSerializerOptions options) { - this.iterator = iterator; - this.primitiveType = primitiveType; - this.metadata = metadata; - this.options = options; - } - - protected abstract void writePrimitive(OutputStream out) throws SerializerException; - - @Override - protected void writeCollection(OutputStream out) { - try { - writePrimitive(out); - } catch (SerializerException e) { - final ODataContentWriteErrorCallback errorCallback = options.getODataContentWriteErrorCallback(); - if (errorCallback != null) { - final WriteErrorContext errorContext = new WriteErrorContext(e); - errorCallback.handleError(errorContext, Channels.newChannel(out)); - } - } - } - - public static SerializerStreamResult PrimitiveStreamContentForJson( - PrimitiveIterator iterator, - EdmPrimitiveType primitiveType, - ODataJsonSerializer jsonSerializer, - ServiceMetadata serviceMetadata, - PrimitiveSerializerOptions options) { - - return SerializerStreamResultImpl.with() - .content(new PrimitiveStreamContentForJson(iterator, - primitiveType, - jsonSerializer, - serviceMetadata, - options)).build(); - } - - public static SerializerStreamResult PrimitiveStreamContentForXml( - PrimitiveIterator iterator, - EdmPrimitiveType primitiveType, - ODataXmlSerializer xmlSerializer, - ServiceMetadata serviceMetadata, - PrimitiveSerializerOptions options) { - - return SerializerStreamResultImpl.with() - .content(new PrimitiveStreamContentForXml(iterator, - primitiveType, - xmlSerializer, - serviceMetadata, - options)).build(); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContentForJson.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContentForJson.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContentForJson.java deleted file mode 100644 index 789efaf..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContentForJson.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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; - -import java.io.IOException; -import java.io.OutputStream; - -import org.apache.olingo.commons.api.data.PrimitiveIterator; -import org.apache.olingo.commons.api.edm.EdmPrimitiveType; -import org.apache.olingo.commons.api.ex.ODataRuntimeException; -import org.apache.olingo.server.api.ServiceMetadata; -import org.apache.olingo.server.api.serializer.PrimitiveSerializerOptions; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.apache.olingo.server.core.serializer.json.ODataJsonSerializer; - -class PrimitiveStreamContentForJson extends PrimitiveStreamContent { - private final ODataJsonSerializer jsonSerializer; - - protected PrimitiveStreamContentForJson( - PrimitiveIterator iterator, - EdmPrimitiveType primitiveType, - ODataJsonSerializer jsonSerializer, - ServiceMetadata serviceMetadata, - PrimitiveSerializerOptions options) { - - super(iterator, serviceMetadata, primitiveType, options); - - this.jsonSerializer = jsonSerializer; - } - - @Override - protected void writePrimitive(OutputStream outputStream) throws SerializerException { - try { - jsonSerializer.primitiveCollectionIntoStream(metadata, primitiveType, iterator, options, outputStream); - outputStream.flush(); - } catch (final IOException e) { - throw new ODataRuntimeException("Failed complex serialization", e); - } - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContentForXml.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContentForXml.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContentForXml.java deleted file mode 100644 index 675290b..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/PrimitiveStreamContentForXml.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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; - -import java.io.IOException; -import java.io.OutputStream; - -import org.apache.olingo.commons.api.data.PrimitiveIterator; -import org.apache.olingo.commons.api.edm.EdmPrimitiveType; -import org.apache.olingo.commons.api.ex.ODataRuntimeException; -import org.apache.olingo.server.api.ServiceMetadata; -import org.apache.olingo.server.api.serializer.PrimitiveSerializerOptions; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.apache.olingo.server.core.serializer.xml.ODataXmlSerializer; - -class PrimitiveStreamContentForXml extends PrimitiveStreamContent { - private final ODataXmlSerializer xmlSerializer; - - protected PrimitiveStreamContentForXml( - PrimitiveIterator iterator, - EdmPrimitiveType primitiveType, - ODataXmlSerializer xmlSerializer, - ServiceMetadata serviceMetadata, - PrimitiveSerializerOptions options) { - - super(iterator, serviceMetadata, primitiveType, options); - - this.xmlSerializer = xmlSerializer; - } - - @Override - protected void writePrimitive(OutputStream outputStream) throws SerializerException { - try { - xmlSerializer.primitiveCollectionIntoStream(metadata, primitiveType, iterator, options, outputStream); - outputStream.flush(); - } catch (final IOException e) { - throw new ODataRuntimeException("Failed complex serialization", e); - } - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/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 ffdc6e6..5ed55c5 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 @@ -79,11 +79,10 @@ 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.LevelsExpandOption; import org.apache.olingo.server.api.uri.queryoption.SelectOption; -import org.apache.olingo.server.core.ComplexStreamContent; import org.apache.olingo.server.core.ODataWritableContent; -import org.apache.olingo.server.core.PrimitiveStreamContent; import org.apache.olingo.server.core.serializer.AbstractODataSerializer; import org.apache.olingo.server.core.serializer.SerializerResultImpl; +import org.apache.olingo.server.core.serializer.SerializerStreamResultImpl; 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; @@ -662,7 +661,7 @@ public class ODataJsonSerializer extends AbstractODataSerializer { } } } else if (type.getKind() == EdmTypeKind.COMPLEX) { - // non-collection case written in writeComplex method directly. + // non-collection case written in writeInternal method directly. if (edmProperty.isCollection()) { json.writeStringField(typeName, "#Collection(" + type.getFullQualifiedName().getFullQualifiedNameAsString() + ")"); @@ -1125,7 +1124,9 @@ public class ODataJsonSerializer extends AbstractODataSerializer { PrimitiveSerializerOptions options) throws SerializerException { - return PrimitiveStreamContent.PrimitiveStreamContentForJson(iterator, type, this, metadata, options); + return SerializerStreamResultImpl.with() + .content(new ODataWritableContent.PrimitiveStreamContentForJson(iterator, + type, this, metadata, options)).build(); } public void primitiveCollectionIntoStream( @@ -1210,7 +1211,9 @@ public class ODataJsonSerializer extends AbstractODataSerializer { ComplexSerializerOptions options) throws SerializerException { - return ComplexStreamContent.ComplexWritableForJson(iterator, type, this, metadata, options); + return SerializerStreamResultImpl.with() + .content(new ODataWritableContent.ComplexStreamContentForJson(iterator, + type, this, metadata, options)).build(); } public void complexCollectionIntoStream( http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/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 b52545f..dfb7e4f 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 @@ -74,11 +74,10 @@ 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.LevelsExpandOption; import org.apache.olingo.server.api.uri.queryoption.SelectOption; -import org.apache.olingo.server.core.ComplexStreamContent; import org.apache.olingo.server.core.ODataWritableContent; -import org.apache.olingo.server.core.PrimitiveStreamContent; import org.apache.olingo.server.core.serializer.AbstractODataSerializer; import org.apache.olingo.server.core.serializer.SerializerResultImpl; +import org.apache.olingo.server.core.serializer.SerializerStreamResultImpl; 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; @@ -1181,7 +1180,9 @@ public class ODataXmlSerializer extends AbstractODataSerializer { @Override public SerializerStreamResult primitiveCollectionStreamed(ServiceMetadata metadata, EdmPrimitiveType type, PrimitiveIterator iterator, PrimitiveSerializerOptions options) throws SerializerException { - return PrimitiveStreamContent.PrimitiveStreamContentForXml(iterator, type, this, metadata, options); + return SerializerStreamResultImpl.with() + .content(new ODataWritableContent.PrimitiveStreamContentForXml(iterator, + type, this, metadata, options)).build(); } public void primitiveCollectionIntoStream(final ServiceMetadata metadata, final EdmPrimitiveType type, @@ -1276,7 +1277,12 @@ public class ODataXmlSerializer extends AbstractODataSerializer { @Override public SerializerStreamResult complexCollectionStreamed(ServiceMetadata metadata, EdmComplexType type, ComplexIterator iterator, ComplexSerializerOptions options) throws SerializerException { - return ComplexStreamContent.ComplexWritableForXml(iterator, type, this, metadata, options); + return SerializerStreamResultImpl.with() + .content(new ODataWritableContent.ComplexStreamContentForXml(iterator, + type, + this, + metadata, + options)).build(); } public void complexCollectionIntoStream(final ServiceMetadata metadata, final EdmComplexType type, http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/samples/tutorials/p0_all/src/main/java/myservice/mynamespace/util/Util.java ---------------------------------------------------------------------- diff --git a/samples/tutorials/p0_all/src/main/java/myservice/mynamespace/util/Util.java b/samples/tutorials/p0_all/src/main/java/myservice/mynamespace/util/Util.java index dfc1662..9910096 100644 --- a/samples/tutorials/p0_all/src/main/java/myservice/mynamespace/util/Util.java +++ b/samples/tutorials/p0_all/src/main/java/myservice/mynamespace/util/Util.java @@ -82,7 +82,7 @@ public class Util { String keyName = key.getName(); String keyText = key.getText(); - // note: below line doesn't consider: keyProp can be part of a complexType in V4 + // note: below line doesn't consider: keyProp can be part of a type in V4 // in such case, it would be required to access it via getKeyPropertyRef() // but since this isn't the case in our model, we ignore it in our implementation EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/util/Util.java ---------------------------------------------------------------------- diff --git a/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/util/Util.java b/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/util/Util.java index 81c0e36..5898f61 100755 --- a/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/util/Util.java +++ b/samples/tutorials/p4_navigation/src/main/java/myservice/mynamespace/util/Util.java @@ -63,7 +63,7 @@ public class Util { String keyName = key.getName(); String keyText = key.getText(); - // note: below line doesn't consider: keyProp can be part of a complexType in V4 + // note: below line doesn't consider: keyProp can be part of a type in V4 // in such case, it would be required to access it via getKeyPropertyRef() // but since this isn't the case in our model, we ignore it in our implementation EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/9de1d310/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/util/Util.java ---------------------------------------------------------------------- diff --git a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/util/Util.java b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/util/Util.java index f16360f..8ea8585 100755 --- a/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/util/Util.java +++ b/samples/tutorials/p6_queryoptions-es/src/main/java/myservice/mynamespace/util/Util.java @@ -63,7 +63,7 @@ public class Util { String keyName = key.getName(); String keyText = key.getText(); - // note: below line doesn't consider: keyProp can be part of a complexType in V4 + // note: below line doesn't consider: keyProp can be part of a type in V4 // in such case, it would be required to access it via getKeyPropertyRef() // but since this isn't the case in our model, we ignore it in our implementation EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
