You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by fm...@apache.org on 2013/07/26 13:22:23 UTC
[18/51] [partial] initial commit
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/AtomEntityProvider.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/AtomEntityProvider.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/AtomEntityProvider.java
new file mode 100644
index 0000000..1e99e72
--- /dev/null
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/AtomEntityProvider.java
@@ -0,0 +1,394 @@
+/*******************************************************************************
+ * 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.odata2.core.ep;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.olingo.odata2.api.ODataServiceVersion;
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+import org.apache.olingo.odata2.api.commons.ODataHttpHeaders;
+import org.apache.olingo.odata2.api.edm.Edm;
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmFunctionImport;
+import org.apache.olingo.odata2.api.edm.EdmMultiplicity;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmType;
+import org.apache.olingo.odata2.api.edm.EdmTypeKind;
+import org.apache.olingo.odata2.api.ep.EntityProviderException;
+import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties;
+import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties;
+import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
+import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
+import org.apache.olingo.odata2.api.processor.ODataResponse;
+import org.apache.olingo.odata2.api.processor.ODataResponse.ODataResponseBuilder;
+import org.apache.olingo.odata2.api.servicedocument.ServiceDocument;
+import org.apache.olingo.odata2.core.commons.ContentType;
+import org.apache.olingo.odata2.core.commons.ContentType.ODataFormat;
+import org.apache.olingo.odata2.core.ep.aggregator.EntityInfoAggregator;
+import org.apache.olingo.odata2.core.ep.aggregator.EntityPropertyInfo;
+import org.apache.olingo.odata2.core.ep.consumer.AtomServiceDocumentConsumer;
+import org.apache.olingo.odata2.core.ep.consumer.XmlEntityConsumer;
+import org.apache.olingo.odata2.core.ep.producer.AtomEntryEntityProducer;
+import org.apache.olingo.odata2.core.ep.producer.AtomFeedProducer;
+import org.apache.olingo.odata2.core.ep.producer.AtomServiceDocumentProducer;
+import org.apache.olingo.odata2.core.ep.producer.XmlCollectionEntityProducer;
+import org.apache.olingo.odata2.core.ep.producer.XmlErrorDocumentProducer;
+import org.apache.olingo.odata2.core.ep.producer.XmlLinkEntityProducer;
+import org.apache.olingo.odata2.core.ep.producer.XmlLinksEntityProducer;
+import org.apache.olingo.odata2.core.ep.producer.XmlPropertyEntityProducer;
+import org.apache.olingo.odata2.core.ep.util.CircleStreamBuffer;
+import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
+
+/**
+ * @author SAP AG
+ */
+public class AtomEntityProvider implements ContentTypeBasedEntityProvider {
+
+ /** Default used charset for writer and response content header */
+ private static final String DEFAULT_CHARSET = ContentType.CHARSET_UTF_8;
+ private static final String XML_VERSION = "1.0";
+ private final ODataFormat odataFormat;
+
+ public AtomEntityProvider() throws EntityProviderException {
+ this(ODataFormat.ATOM);
+ }
+
+ public AtomEntityProvider(final ContentType contentType) throws EntityProviderException {
+ this(contentType.getODataFormat());
+ }
+
+ public AtomEntityProvider(final ODataFormat odataFormat) throws EntityProviderException {
+ switch (odataFormat) {
+ case ATOM:
+ case XML:
+ this.odataFormat = odataFormat;
+ break;
+ default:
+ throw new EntityProviderException(EntityProviderException.ILLEGAL_ARGUMENT.addContent("Got unsupported ODataFormat '" + odataFormat + "'."));
+ }
+ }
+
+ /**
+ * <p>Serializes an error message according to the OData standard.</p>
+ * <p>In case an error occurs, it is logged.
+ * An exception is not thrown because this method is used in exception handling.</p>
+ * @param status the {@link HttpStatusCodes} associated with this error
+ * @param errorCode a String that serves as a substatus to the HTTP response code
+ * @param message a human-readable message describing the error
+ * @param locale the {@link Locale} that should be used to format the error message
+ * @param innerError the inner error for this message. If it is null or an empty String no inner error tag is shown inside the response xml
+ * @return an {@link ODataResponse} containing the serialized error message
+ */
+ @Override
+ public ODataResponse writeErrorDocument(final HttpStatusCodes status, final String errorCode, final String message, final Locale locale, final String innerError) {
+ CircleStreamBuffer csb = new CircleStreamBuffer();
+
+ try {
+ OutputStream outStream = csb.getOutputStream();
+ XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream, DEFAULT_CHARSET);
+
+ XmlErrorDocumentProducer producer = new XmlErrorDocumentProducer();
+ producer.writeErrorDocument(writer, errorCode, message, locale, innerError);
+
+ writer.flush();
+ csb.closeWrite();
+
+ ODataResponseBuilder response = ODataResponse.entity(csb.getInputStream())
+ .contentHeader(ContentType.APPLICATION_XML.toContentTypeString())
+ .header(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V10)
+ .status(status);
+ return response.build();
+ } catch (Exception e) {
+ csb.close();
+ throw new ODataRuntimeException(e);
+ }
+ }
+
+ /**
+ * Write service document based on given {@link Edm} and <code>service root</code> as
+ * content type "<code>application/atomsvc+xml; charset=utf-8</code>".
+ *
+ * @param edm the Entity Data Model
+ * @param serviceRoot the root URI of the service
+ * @return resulting {@link ODataResponse} with written service document
+ * @throws EntityProviderException
+ */
+ @Override
+ public ODataResponse writeServiceDocument(final Edm edm, final String serviceRoot) throws EntityProviderException {
+ CircleStreamBuffer csb = new CircleStreamBuffer();
+
+ try {
+ OutputStreamWriter writer = new OutputStreamWriter(csb.getOutputStream(), DEFAULT_CHARSET);
+ AtomServiceDocumentProducer as = new AtomServiceDocumentProducer(edm, serviceRoot);
+ as.writeServiceDocument(writer);
+ csb.closeWrite();
+
+ ODataResponse response = ODataResponse.entity(csb.getInputStream())
+ .contentHeader(ContentType.APPLICATION_ATOM_SVC_CS_UTF_8.toContentTypeString())
+ .build();
+
+ return response;
+ } catch (EntityProviderException e) {
+ csb.close();
+ throw e;
+ } catch (Exception e) {
+ csb.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeEntry(final EdmEntitySet entitySet, final Map<String, Object> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ CircleStreamBuffer csb = new CircleStreamBuffer();
+
+ try {
+ OutputStream outStream = csb.getOutputStream();
+ XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream, DEFAULT_CHARSET);
+ writer.writeStartDocument(DEFAULT_CHARSET, XML_VERSION);
+
+ AtomEntryEntityProducer as = new AtomEntryEntityProducer(properties);
+ EntityInfoAggregator eia = EntityInfoAggregator.create(entitySet, properties.getExpandSelectTree());
+ as.append(writer, eia, data, true, false);
+
+ writer.flush();
+ csb.closeWrite();
+
+ ODataResponseBuilder response = ODataResponse.entity(csb.getInputStream())
+ .contentHeader(getContentHeader(ContentType.APPLICATION_ATOM_XML_ENTRY))
+ .eTag(as.getETag())
+ .idLiteral(as.getLocation());
+ return response.build();
+ } catch (EntityProviderException e) {
+ csb.close();
+ throw e;
+ } catch (Exception e) {
+ csb.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeProperty(final EdmProperty edmProperty, final Object value) throws EntityProviderException {
+ EntityPropertyInfo propertyInfo = EntityInfoAggregator.create(edmProperty);
+ return writeSingleTypedElement(propertyInfo, value);
+ }
+
+ private ODataResponse writeSingleTypedElement(final EntityPropertyInfo propertyInfo, final Object value) throws EntityProviderException {
+ CircleStreamBuffer csb = new CircleStreamBuffer();
+
+ try {
+ OutputStream outStream = csb.getOutputStream();
+ XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream, DEFAULT_CHARSET);
+ writer.writeStartDocument(DEFAULT_CHARSET, XML_VERSION);
+
+ XmlPropertyEntityProducer ps = new XmlPropertyEntityProducer();
+ ps.append(writer, propertyInfo, value);
+
+ writer.flush();
+ csb.closeWrite();
+
+ return ODataResponse.entity(csb.getInputStream()).contentHeader(getContentHeader(ContentType.APPLICATION_XML)).build();
+ } catch (EntityProviderException e) {
+ csb.close();
+ throw e;
+ } catch (Exception e) {
+ csb.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeFeed(final EdmEntitySet entitySet, final List<Map<String, Object>> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ CircleStreamBuffer csb = new CircleStreamBuffer();
+
+ try {
+ OutputStream outStream = csb.getOutputStream();
+ XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream, DEFAULT_CHARSET);
+ writer.writeStartDocument(DEFAULT_CHARSET, XML_VERSION);
+
+ AtomFeedProducer atomFeedProvider = new AtomFeedProducer(properties);
+ EntityInfoAggregator eia = EntityInfoAggregator.create(entitySet, properties.getExpandSelectTree());
+ atomFeedProvider.append(writer, eia, data, false);
+
+ writer.flush();
+ csb.closeWrite();
+
+ ODataResponse response = ODataResponse.entity(csb.getInputStream()).contentHeader(getContentHeader(ContentType.APPLICATION_ATOM_XML_FEED)).build();
+ return response;
+ } catch (EntityProviderException e) {
+ csb.close();
+ throw e;
+ } catch (XMLStreamException e) {
+ csb.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ private String getContentHeader(final ContentType mediaType) {
+ if (odataFormat == ODataFormat.XML) {
+ return ContentType.APPLICATION_XML_CS_UTF_8.toContentTypeString();
+ }
+ return ContentType.create(mediaType, ContentType.PARAMETER_CHARSET, DEFAULT_CHARSET).toContentTypeString();
+ }
+
+ @Override
+ public ODataResponse writeLink(final EdmEntitySet entitySet, final Map<String, Object> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ CircleStreamBuffer csb = new CircleStreamBuffer();
+
+ try {
+ OutputStream outStream = csb.getOutputStream();
+ XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream, DEFAULT_CHARSET);
+ writer.writeStartDocument(DEFAULT_CHARSET, XML_VERSION);
+
+ XmlLinkEntityProducer entity = new XmlLinkEntityProducer(properties);
+ final EntityInfoAggregator entityInfo = EntityInfoAggregator.create(entitySet, properties.getExpandSelectTree());
+ entity.append(writer, entityInfo, data, true);
+
+ writer.flush();
+ csb.closeWrite();
+
+ return ODataResponse.entity(csb.getInputStream()).contentHeader(getContentHeader(ContentType.APPLICATION_XML)).build();
+ } catch (EntityProviderException e) {
+ csb.close();
+ throw e;
+ } catch (Exception e) {
+ csb.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+
+ }
+
+ @Override
+ public ODataResponse writeLinks(final EdmEntitySet entitySet, final List<Map<String, Object>> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ CircleStreamBuffer csb = new CircleStreamBuffer();
+
+ try {
+ OutputStream outStream = csb.getOutputStream();
+ XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream, DEFAULT_CHARSET);
+ writer.writeStartDocument(DEFAULT_CHARSET, XML_VERSION);
+
+ XmlLinksEntityProducer entity = new XmlLinksEntityProducer(properties);
+ final EntityInfoAggregator entityInfo = EntityInfoAggregator.create(entitySet, properties.getExpandSelectTree());
+ entity.append(writer, entityInfo, data);
+
+ writer.flush();
+ csb.closeWrite();
+
+ return ODataResponse.entity(csb.getInputStream()).contentHeader(getContentHeader(ContentType.APPLICATION_XML)).build();
+ } catch (EntityProviderException e) {
+ csb.close();
+ throw e;
+ } catch (Exception e) {
+ csb.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ private ODataResponse writeCollection(final EntityPropertyInfo propertyInfo, final List<?> data) throws EntityProviderException {
+ CircleStreamBuffer csb = new CircleStreamBuffer();
+
+ try {
+ OutputStream outStream = csb.getOutputStream();
+ XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream, DEFAULT_CHARSET);
+ writer.writeStartDocument(DEFAULT_CHARSET, XML_VERSION);
+
+ XmlCollectionEntityProducer.append(writer, propertyInfo, data);
+
+ writer.flush();
+ csb.closeWrite();
+
+ return ODataResponse.entity(csb.getInputStream()).contentHeader(getContentHeader(ContentType.APPLICATION_XML)).build();
+ } catch (EntityProviderException e) {
+ csb.close();
+ throw e;
+ } catch (Exception e) {
+ csb.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeFunctionImport(final EdmFunctionImport functionImport, final Object data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ try {
+ final EdmType type = functionImport.getReturnType().getType();
+ final boolean isCollection = functionImport.getReturnType().getMultiplicity() == EdmMultiplicity.MANY;
+
+ if (type.getKind() == EdmTypeKind.ENTITY) {
+ @SuppressWarnings("unchecked")
+ Map<String, Object> map = (Map<String, Object>) data;
+ return writeEntry(functionImport.getEntitySet(), map, properties);
+ }
+
+ final EntityPropertyInfo info = EntityInfoAggregator.create(functionImport);
+ if (isCollection) {
+ return writeCollection(info, (List<?>) data);
+ } else {
+ return writeSingleTypedElement(info, data);
+ }
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataFeed readFeed(final EdmEntitySet entitySet, final InputStream content, final EntityProviderReadProperties properties) throws EntityProviderException {
+ XmlEntityConsumer xec = new XmlEntityConsumer();
+ return xec.readFeed(entitySet, content, properties);
+ }
+
+ @Override
+ public ODataEntry readEntry(final EdmEntitySet entitySet, final InputStream content, final EntityProviderReadProperties properties) throws EntityProviderException {
+ XmlEntityConsumer xec = new XmlEntityConsumer();
+ return xec.readEntry(entitySet, content, properties);
+ }
+
+ @Override
+ public Map<String, Object> readProperty(final EdmProperty edmProperty, final InputStream content, final EntityProviderReadProperties properties) throws EntityProviderException {
+ XmlEntityConsumer xec = new XmlEntityConsumer();
+ return xec.readProperty(edmProperty, content, properties);
+ }
+
+ @Override
+ public String readLink(final EdmEntitySet entitySet, final InputStream content) throws EntityProviderException {
+ XmlEntityConsumer xec = new XmlEntityConsumer();
+ return xec.readLink(entitySet, content);
+ }
+
+ @Override
+ public List<String> readLinks(final EdmEntitySet entitySet, final InputStream content) throws EntityProviderException {
+ XmlEntityConsumer xec = new XmlEntityConsumer();
+ return xec.readLinks(entitySet, content);
+ }
+
+ @Override
+ public ServiceDocument readServiceDocument(final InputStream serviceDocument) throws EntityProviderException {
+ AtomServiceDocumentConsumer serviceDocConsumer = new AtomServiceDocumentConsumer();
+ return serviceDocConsumer.parseXml(serviceDocument);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/BasicEntityProvider.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/BasicEntityProvider.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/BasicEntityProvider.java
new file mode 100644
index 0000000..55b7a20
--- /dev/null
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/BasicEntityProvider.java
@@ -0,0 +1,296 @@
+/*******************************************************************************
+ * 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.odata2.core.ep;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.olingo.odata2.api.ODataServiceVersion;
+import org.apache.olingo.odata2.api.commons.HttpContentType;
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+import org.apache.olingo.odata2.api.commons.ODataHttpHeaders;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmLiteralKind;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmSimpleType;
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
+import org.apache.olingo.odata2.api.edm.provider.DataServices;
+import org.apache.olingo.odata2.api.edm.provider.EntityType;
+import org.apache.olingo.odata2.api.edm.provider.Property;
+import org.apache.olingo.odata2.api.edm.provider.Schema;
+import org.apache.olingo.odata2.api.ep.EntityProviderException;
+import org.apache.olingo.odata2.api.processor.ODataResponse;
+import org.apache.olingo.odata2.api.processor.ODataResponse.ODataResponseBuilder;
+import org.apache.olingo.odata2.core.commons.ContentType;
+import org.apache.olingo.odata2.core.ep.producer.XmlMetadataProducer;
+import org.apache.olingo.odata2.core.ep.util.CircleStreamBuffer;
+
+/**
+ * Provider for all basic (content type independent) entity provider methods.
+ *
+ * @author SAP AG
+ */
+public class BasicEntityProvider {
+
+ /** Default used charset for writer and response content header */
+ private static final String DEFAULT_CHARSET = "UTF-8";
+
+ /**
+ * Reads binary data from an input stream.
+ * @param content the content input stream
+ * @return the binary data
+ * @throws EntityProviderException
+ */
+ public byte[] readBinary(final InputStream content) throws EntityProviderException {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ byte[] value = new byte[Short.MAX_VALUE];
+ int count;
+ try {
+ while ((count = content.read(value)) > 0) {
+ buffer.write(value, 0, count);
+ }
+ content.close();
+ buffer.flush();
+ return buffer.toByteArray();
+ } catch (IOException e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ /**
+ * Reads text from an input stream.
+ * @param content the content input stream
+ * @return text as string from <code>InputStream</code>
+ * @throws EntityProviderException
+ */
+ public String readText(final InputStream content) throws EntityProviderException {
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content, Charset.forName(DEFAULT_CHARSET)));
+ StringBuilder stringBuilder = new StringBuilder();
+ try {
+ String line = null;
+ while ((line = bufferedReader.readLine()) != null) {
+ stringBuilder.append(line);
+ }
+ bufferedReader.close();
+ } catch (IOException e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ return stringBuilder.toString();
+ }
+
+ /**
+ * Reads an unformatted value of an EDM property as binary or as content type <code>text/plain</code>.
+ * @param edmProperty the EDM property
+ * @param content the content input stream
+ * @param typeMapping
+ * @return the value as the proper system data type
+ * @throws EntityProviderException
+ */
+ public Object readPropertyValue(final EdmProperty edmProperty, final InputStream content, final Class<?> typeMapping) throws EntityProviderException {
+ EdmSimpleType type;
+ try {
+ type = (EdmSimpleType) edmProperty.getType();
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+
+ if (type == EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance()) {
+ return readBinary(content);
+ } else {
+ try {
+ if (typeMapping == null) {
+ return type.valueOfString(readText(content), EdmLiteralKind.DEFAULT, edmProperty.getFacets(), type.getDefaultType());
+ } else {
+ return type.valueOfString(readText(content), EdmLiteralKind.DEFAULT, edmProperty.getFacets(), typeMapping);
+ }
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+ }
+
+ /**
+ * Write property as binary or as content type <code>text/plain</code>.
+ * @param edmProperty the EDM property
+ * @param value its value
+ * @return resulting {@link ODataResponse} with written content
+ * @throws EntityProviderException
+ */
+ public ODataResponse writePropertyValue(final EdmProperty edmProperty, final Object value) throws EntityProviderException {
+ try {
+ final EdmSimpleType type = (EdmSimpleType) edmProperty.getType();
+
+ if (type == EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance()) {
+ String contentType = HttpContentType.APPLICATION_OCTET_STREAM;
+ Object binary = value;
+ if (edmProperty.getMimeType() != null) {
+ contentType = edmProperty.getMimeType();
+ } else {
+ if (edmProperty.getMapping() != null && edmProperty.getMapping().getMimeType() != null) {
+ String mimeTypeMapping = edmProperty.getMapping().getMimeType();
+ if (value instanceof Map) {
+ final Map<?, ?> mappedData = (Map<?, ?>) value;
+ binary = mappedData.get(edmProperty.getName());
+ contentType = (String) mappedData.get(mimeTypeMapping);
+ } else {
+ throw new EntityProviderException(EntityProviderException.COMMON);
+ }
+ }
+ }
+ return writeBinary(contentType, (byte[]) binary);
+
+ } else {
+ return writeText(type.valueToString(value, EdmLiteralKind.DEFAULT, edmProperty.getFacets()));
+ }
+
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ /**
+ * Write text value as content type <code>text/plain</code>.
+ * @param value the string that is written to {@link ODataResponse}
+ * @return resulting {@link ODataResponse} with written text content
+ * @throws EntityProviderException
+ */
+ public ODataResponse writeText(final String value) throws EntityProviderException {
+ ODataResponseBuilder builder = ODataResponse.newBuilder();
+ if (value != null) {
+ ByteArrayInputStream stream;
+ try {
+ stream = new ByteArrayInputStream(value.getBytes(DEFAULT_CHARSET));
+ } catch (UnsupportedEncodingException e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ builder.entity(stream);
+ }
+ builder.contentHeader(ContentType.TEXT_PLAIN_CS_UTF_8.toContentTypeString());
+ return builder.build();
+ }
+
+ /**
+ * Write binary content with content type header set to given <code>mime type</code> parameter.
+ * @param mimeType MIME type which is written and used as content type header information
+ * @param data data is written to {@link ODataResponse}
+ * @return resulting {@link ODataResponse} with written binary content
+ * @throws EntityProviderException
+ */
+ public ODataResponse writeBinary(final String mimeType, final byte[] data) throws EntityProviderException {
+ ODataResponseBuilder builder = ODataResponse.newBuilder();
+ if (data != null) {
+ ByteArrayInputStream bais = new ByteArrayInputStream(data);
+ builder.contentHeader(mimeType);
+ builder.entity(bais);
+ } else {
+ builder.status(HttpStatusCodes.NO_CONTENT);
+ }
+ return builder.build();
+ }
+
+ /**
+ * Writes the metadata in XML format. Predefined namespaces is of type Map{@literal <}prefix,namespace{@literal >} and may be null or an empty Map.
+ * @param schemas
+ * @param predefinedNamespaces
+ * @return resulting {@link ODataResponse} with written metadata content
+ * @throws EntityProviderException
+ */
+ public ODataResponse writeMetadata(final List<Schema> schemas, final Map<String, String> predefinedNamespaces) throws EntityProviderException {
+ ODataResponseBuilder builder = ODataResponse.newBuilder();
+ String dataServiceVersion = ODataServiceVersion.V10;
+ if (schemas != null) {
+ dataServiceVersion = calculateDataServiceVersion(schemas);
+ }
+ DataServices metadata = new DataServices().setSchemas(schemas).setDataServiceVersion(dataServiceVersion);
+ OutputStreamWriter writer = null;
+ CircleStreamBuffer csb = new CircleStreamBuffer();
+ try {
+ writer = new OutputStreamWriter(csb.getOutputStream(), "UTF-8");
+ XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
+ XmlMetadataProducer.writeMetadata(metadata, xmlStreamWriter, predefinedNamespaces);
+ } catch (UnsupportedEncodingException e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ } catch (XMLStreamException e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ } catch (FactoryConfigurationError e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ builder.entity(csb.getInputStream());
+ builder.contentHeader(ContentType.APPLICATION_XML_CS_UTF_8.toContentTypeString());
+ builder.header(ODataHttpHeaders.DATASERVICEVERSION, dataServiceVersion);
+ return builder.build();
+ }
+
+ /**
+ * Calculates the necessary data service version for the metadata serialization
+ * @param schemas
+ * @return DataServiceversion as String
+ */
+ private String calculateDataServiceVersion(final List<Schema> schemas) {
+
+ String dataServiceVersion = ODataServiceVersion.V10;
+
+ if (schemas != null) {
+ for (Schema schema : schemas) {
+ List<EntityType> entityTypes = schema.getEntityTypes();
+ if (entityTypes != null) {
+ for (EntityType entityType : entityTypes) {
+ List<Property> properties = entityType.getProperties();
+ if (properties != null) {
+ for (Property property : properties) {
+ if (property.getCustomizableFeedMappings() != null) {
+ if (property.getCustomizableFeedMappings().getFcKeepInContent() != null) {
+ if (!property.getCustomizableFeedMappings().getFcKeepInContent()) {
+ dataServiceVersion = ODataServiceVersion.V20;
+ return dataServiceVersion;
+ }
+ }
+ }
+ }
+ if (entityType.getCustomizableFeedMappings() != null) {
+ if (entityType.getCustomizableFeedMappings().getFcKeepInContent() != null) {
+ if (entityType.getCustomizableFeedMappings().getFcKeepInContent()) {
+ dataServiceVersion = ODataServiceVersion.V20;
+ return dataServiceVersion;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ return dataServiceVersion;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ContentTypeBasedEntityProvider.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ContentTypeBasedEntityProvider.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ContentTypeBasedEntityProvider.java
new file mode 100644
index 0000000..9e6df27
--- /dev/null
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ContentTypeBasedEntityProvider.java
@@ -0,0 +1,73 @@
+/*******************************************************************************
+ * 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.odata2.core.ep;
+
+import java.io.InputStream;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+import org.apache.olingo.odata2.api.edm.Edm;
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmFunctionImport;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.ep.EntityProviderException;
+import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties;
+import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties;
+import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
+import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
+import org.apache.olingo.odata2.api.processor.ODataResponse;
+import org.apache.olingo.odata2.api.servicedocument.ServiceDocument;
+
+/**
+ * Interface for all none basic (content type <b>dependent</b>) provider methods.
+ *
+ * @author SAP AG
+ */
+public interface ContentTypeBasedEntityProvider {
+
+ ODataFeed readFeed(EdmEntitySet entitySet, InputStream content, EntityProviderReadProperties properties) throws EntityProviderException;
+
+ ODataEntry readEntry(EdmEntitySet entitySet, InputStream content, EntityProviderReadProperties properties) throws EntityProviderException;
+
+ Map<String, Object> readProperty(EdmProperty edmProperty, InputStream content, EntityProviderReadProperties properties) throws EntityProviderException;
+
+ String readLink(EdmEntitySet entitySet, InputStream content) throws EntityProviderException;
+
+ List<String> readLinks(EdmEntitySet entitySet, InputStream content) throws EntityProviderException;
+
+ ODataResponse writeServiceDocument(Edm edm, String serviceRoot) throws EntityProviderException;
+
+ ODataResponse writeFeed(EdmEntitySet entitySet, List<Map<String, Object>> data, EntityProviderWriteProperties properties) throws EntityProviderException;
+
+ ODataResponse writeEntry(EdmEntitySet entitySet, Map<String, Object> data, EntityProviderWriteProperties properties) throws EntityProviderException;
+
+ ODataResponse writeProperty(EdmProperty edmProperty, Object value) throws EntityProviderException;
+
+ ODataResponse writeLink(EdmEntitySet entitySet, Map<String, Object> data, EntityProviderWriteProperties properties) throws EntityProviderException;
+
+ ODataResponse writeLinks(EdmEntitySet entitySet, List<Map<String, Object>> data, EntityProviderWriteProperties properties) throws EntityProviderException;
+
+ ODataResponse writeFunctionImport(EdmFunctionImport functionImport, Object data, EntityProviderWriteProperties properties) throws EntityProviderException;
+
+ ODataResponse writeErrorDocument(HttpStatusCodes status, String errorCode, String message, Locale locale, String innerError);
+
+ ServiceDocument readServiceDocument(InputStream serviceDocument) throws EntityProviderException;
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/JsonEntityProvider.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/JsonEntityProvider.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/JsonEntityProvider.java
new file mode 100644
index 0000000..a38b7e9
--- /dev/null
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/JsonEntityProvider.java
@@ -0,0 +1,327 @@
+/*******************************************************************************
+ * 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.odata2.core.ep;
+
+import java.io.BufferedWriter;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import org.apache.olingo.odata2.api.ODataServiceVersion;
+import org.apache.olingo.odata2.api.commons.HttpContentType;
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+import org.apache.olingo.odata2.api.commons.InlineCount;
+import org.apache.olingo.odata2.api.commons.ODataHttpHeaders;
+import org.apache.olingo.odata2.api.edm.Edm;
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmFunctionImport;
+import org.apache.olingo.odata2.api.edm.EdmMultiplicity;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmTypeKind;
+import org.apache.olingo.odata2.api.ep.EntityProviderException;
+import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties;
+import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties;
+import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
+import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
+import org.apache.olingo.odata2.api.processor.ODataResponse;
+import org.apache.olingo.odata2.api.processor.ODataResponse.ODataResponseBuilder;
+import org.apache.olingo.odata2.api.servicedocument.ServiceDocument;
+import org.apache.olingo.odata2.core.ep.aggregator.EntityInfoAggregator;
+import org.apache.olingo.odata2.core.ep.aggregator.EntityPropertyInfo;
+import org.apache.olingo.odata2.core.ep.consumer.JsonEntityConsumer;
+import org.apache.olingo.odata2.core.ep.consumer.JsonServiceDocumentConsumer;
+import org.apache.olingo.odata2.core.ep.producer.JsonCollectionEntityProducer;
+import org.apache.olingo.odata2.core.ep.producer.JsonEntryEntityProducer;
+import org.apache.olingo.odata2.core.ep.producer.JsonErrorDocumentProducer;
+import org.apache.olingo.odata2.core.ep.producer.JsonFeedEntityProducer;
+import org.apache.olingo.odata2.core.ep.producer.JsonLinkEntityProducer;
+import org.apache.olingo.odata2.core.ep.producer.JsonLinksEntityProducer;
+import org.apache.olingo.odata2.core.ep.producer.JsonPropertyEntityProducer;
+import org.apache.olingo.odata2.core.ep.producer.JsonServiceDocumentProducer;
+import org.apache.olingo.odata2.core.ep.util.CircleStreamBuffer;
+import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
+
+/**
+ * @author SAP AG
+ */
+public class JsonEntityProvider implements ContentTypeBasedEntityProvider {
+
+ private static final String DEFAULT_CHARSET = "UTF-8";
+
+ /**
+ * <p>Serializes an error message according to the OData standard.</p>
+ * <p>In case an error occurs, it is logged.
+ * An exception is not thrown because this method is used in exception handling.</p>
+ * @param status the {@link HttpStatusCodes status code} associated with this error
+ * @param errorCode a String that serves as a substatus to the HTTP response code
+ * @param message a human-readable message describing the error
+ * @param locale the {@link Locale} that should be used to format the error message
+ * @param innerError the inner error for this message; if it is null or an empty String
+ * no inner error tag is shown inside the response structure
+ * @return an {@link ODataResponse} containing the serialized error message
+ */
+ @Override
+ public ODataResponse writeErrorDocument(final HttpStatusCodes status, final String errorCode, final String message, final Locale locale, final String innerError) {
+ CircleStreamBuffer buffer = new CircleStreamBuffer();
+
+ try {
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET));
+ new JsonErrorDocumentProducer().writeErrorDocument(writer, errorCode, message, locale, innerError);
+ writer.flush();
+ buffer.closeWrite();
+
+ return ODataResponse.status(status)
+ .entity(buffer.getInputStream())
+ .contentHeader(HttpContentType.APPLICATION_JSON)
+ .header(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V10)
+ .build();
+ } catch (Exception e) {
+ buffer.close();
+ throw new ODataRuntimeException(e);
+ }
+ }
+
+ /**
+ * Writes service document based on given {@link Edm} and <code>service root</code>.
+ * @param edm the Entity Data Model
+ * @param serviceRoot the root URI of the service
+ * @return resulting {@link ODataResponse} with written service document
+ * @throws EntityProviderException
+ */
+ @Override
+ public ODataResponse writeServiceDocument(final Edm edm, final String serviceRoot) throws EntityProviderException {
+ CircleStreamBuffer buffer = new CircleStreamBuffer();
+
+ try {
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET));
+ JsonServiceDocumentProducer.writeServiceDocument(writer, edm);
+ writer.flush();
+ buffer.closeWrite();
+
+ return ODataResponse.entity(buffer.getInputStream())
+ .contentHeader(HttpContentType.APPLICATION_JSON)
+ .header(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V10)
+ .build();
+ } catch (Exception e) {
+ buffer.close();
+ throw new ODataRuntimeException(e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeEntry(final EdmEntitySet entitySet, final Map<String, Object> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ final EntityInfoAggregator entityInfo = EntityInfoAggregator.create(entitySet, properties.getExpandSelectTree());
+ CircleStreamBuffer buffer = new CircleStreamBuffer();
+
+ try {
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET));
+ JsonEntryEntityProducer producer = new JsonEntryEntityProducer(properties);
+ producer.append(writer, entityInfo, data, true);
+ writer.flush();
+ buffer.closeWrite();
+
+ return ODataResponse.entity(buffer.getInputStream())
+ .contentHeader(HttpContentType.APPLICATION_JSON)
+ .eTag(producer.getETag())
+ .idLiteral(producer.getLocation())
+ .build();
+ } catch (EntityProviderException e) {
+ buffer.close();
+ throw e;
+ } catch (Exception e) {
+ buffer.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeProperty(final EdmProperty edmProperty, final Object value) throws EntityProviderException {
+ return writeSingleTypedElement(EntityInfoAggregator.create(edmProperty), value);
+ }
+
+ private ODataResponse writeSingleTypedElement(final EntityPropertyInfo propertyInfo, final Object value) throws EntityProviderException {
+ CircleStreamBuffer buffer = new CircleStreamBuffer();
+
+ try {
+ OutputStream outStream = buffer.getOutputStream();
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream, DEFAULT_CHARSET));
+ new JsonPropertyEntityProducer().append(writer, propertyInfo, value);
+ writer.flush();
+ buffer.closeWrite();
+
+ return ODataResponse.entity(buffer.getInputStream())
+ .contentHeader(HttpContentType.APPLICATION_JSON)
+ .header(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V10)
+ .build();
+ } catch (EntityProviderException e) {
+ buffer.close();
+ throw e;
+ } catch (Exception e) {
+ buffer.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeFeed(final EdmEntitySet entitySet, final List<Map<String, Object>> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ final EntityInfoAggregator entityInfo = EntityInfoAggregator.create(entitySet, properties.getExpandSelectTree());
+ CircleStreamBuffer buffer = new CircleStreamBuffer();
+
+ try {
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET));
+ new JsonFeedEntityProducer(properties).append(writer, entityInfo, data, true);
+ writer.flush();
+ buffer.closeWrite();
+
+ return ODataResponse.entity(buffer.getInputStream()).contentHeader(HttpContentType.APPLICATION_JSON).build();
+ } catch (EntityProviderException e) {
+ buffer.close();
+ throw e;
+ } catch (Exception e) {
+ buffer.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeLink(final EdmEntitySet entitySet, final Map<String, Object> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ final EntityInfoAggregator entityInfo = EntityInfoAggregator.create(entitySet, properties.getExpandSelectTree());
+ CircleStreamBuffer buffer = new CircleStreamBuffer();
+
+ try {
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET));
+ new JsonLinkEntityProducer(properties).append(writer, entityInfo, data);
+ writer.flush();
+ buffer.closeWrite();
+
+ return ODataResponse.entity(buffer.getInputStream())
+ .contentHeader(HttpContentType.APPLICATION_JSON)
+ .header(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V10)
+ .build();
+ } catch (EntityProviderException e) {
+ buffer.close();
+ throw e;
+ } catch (Exception e) {
+ buffer.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeLinks(final EdmEntitySet entitySet, final List<Map<String, Object>> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ final EntityInfoAggregator entityInfo = EntityInfoAggregator.create(entitySet, properties.getExpandSelectTree());
+ CircleStreamBuffer buffer = new CircleStreamBuffer();
+
+ try {
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET));
+ new JsonLinksEntityProducer(properties).append(writer, entityInfo, data);
+ writer.flush();
+ buffer.closeWrite();
+
+ ODataResponseBuilder response = ODataResponse.entity(buffer.getInputStream()).contentHeader(HttpContentType.APPLICATION_JSON);
+ if (properties.getInlineCountType() != InlineCount.ALLPAGES) {
+ response = response.header(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V10);
+ }
+ return response.build();
+ } catch (EntityProviderException e) {
+ buffer.close();
+ throw e;
+ } catch (Exception e) {
+ buffer.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ private ODataResponse writeCollection(final EntityPropertyInfo propertyInfo, final List<?> data) throws EntityProviderException {
+ CircleStreamBuffer buffer = new CircleStreamBuffer();
+
+ try {
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(buffer.getOutputStream(), DEFAULT_CHARSET));
+ new JsonCollectionEntityProducer().append(writer, propertyInfo, data);
+ writer.flush();
+ buffer.closeWrite();
+
+ return ODataResponse.entity(buffer.getInputStream()).contentHeader(HttpContentType.APPLICATION_JSON).build();
+ } catch (EntityProviderException e) {
+ buffer.close();
+ throw e;
+ } catch (Exception e) {
+ buffer.close();
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeFunctionImport(final EdmFunctionImport functionImport, final Object data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ try {
+ if (functionImport.getReturnType().getType().getKind() == EdmTypeKind.ENTITY) {
+ @SuppressWarnings("unchecked")
+ Map<String, Object> map = (Map<String, Object>) data;
+ return writeEntry(functionImport.getEntitySet(), map, properties);
+ }
+
+ final EntityPropertyInfo info = EntityInfoAggregator.create(functionImport);
+ if (functionImport.getReturnType().getMultiplicity() == EdmMultiplicity.MANY) {
+ return writeCollection(info, (List<?>) data);
+ } else {
+ return writeSingleTypedElement(info, data);
+ }
+ } catch (final EdmException e) {
+ throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
+ }
+ }
+
+ @Override
+ public ODataFeed readFeed(final EdmEntitySet entitySet, final InputStream content, final EntityProviderReadProperties properties) throws EntityProviderException {
+ JsonEntityConsumer jec = new JsonEntityConsumer();
+ return jec.readFeed(entitySet, content, properties);
+ }
+
+ @Override
+ public ODataEntry readEntry(final EdmEntitySet entitySet, final InputStream content, final EntityProviderReadProperties properties) throws EntityProviderException {
+ JsonEntityConsumer jec = new JsonEntityConsumer();
+ return jec.readEntry(entitySet, content, properties);
+ }
+
+ @Override
+ public Map<String, Object> readProperty(final EdmProperty edmProperty, final InputStream content, final EntityProviderReadProperties properties) throws EntityProviderException {
+ return new JsonEntityConsumer().readProperty(edmProperty, content, properties);
+ }
+
+ @Override
+ public String readLink(final EdmEntitySet entitySet, final InputStream content) throws EntityProviderException {
+ return new JsonEntityConsumer().readLink(entitySet, content);
+ }
+
+ @Override
+ public List<String> readLinks(final EdmEntitySet entitySet, final InputStream content) throws EntityProviderException {
+ return new JsonEntityConsumer().readLinks(entitySet, content);
+ }
+
+ @Override
+ public ServiceDocument readServiceDocument(final InputStream serviceDocument) throws EntityProviderException {
+ JsonServiceDocumentConsumer serviceDocConsumer = new JsonServiceDocumentConsumer();
+ return serviceDocConsumer.parseJson(serviceDocument);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ProviderFacadeImpl.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ProviderFacadeImpl.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ProviderFacadeImpl.java
new file mode 100644
index 0000000..38da4cc
--- /dev/null
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/ProviderFacadeImpl.java
@@ -0,0 +1,203 @@
+/*******************************************************************************
+ * 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.odata2.core.ep;
+
+import java.io.InputStream;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.olingo.odata2.api.batch.BatchException;
+import org.apache.olingo.odata2.api.batch.BatchPart;
+import org.apache.olingo.odata2.api.batch.BatchResponsePart;
+import org.apache.olingo.odata2.api.edm.Edm;
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmFunctionImport;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.provider.EdmProvider;
+import org.apache.olingo.odata2.api.edm.provider.Schema;
+import org.apache.olingo.odata2.api.ep.EntityProvider.EntityProviderInterface;
+import org.apache.olingo.odata2.api.ep.EntityProviderBatchProperties;
+import org.apache.olingo.odata2.api.ep.EntityProviderException;
+import org.apache.olingo.odata2.api.ep.EntityProviderReadProperties;
+import org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties;
+import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
+import org.apache.olingo.odata2.api.ep.feed.ODataFeed;
+import org.apache.olingo.odata2.api.exception.ODataNotAcceptableException;
+import org.apache.olingo.odata2.api.processor.ODataErrorContext;
+import org.apache.olingo.odata2.api.processor.ODataResponse;
+import org.apache.olingo.odata2.api.servicedocument.ServiceDocument;
+import org.apache.olingo.odata2.core.batch.BatchRequestParser;
+import org.apache.olingo.odata2.core.batch.BatchResponseWriter;
+import org.apache.olingo.odata2.core.commons.ContentType;
+import org.apache.olingo.odata2.core.edm.parser.EdmxProvider;
+import org.apache.olingo.odata2.core.edm.provider.EdmImplProv;
+import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
+
+/**
+ * @author SAP AG
+ */
+public class ProviderFacadeImpl implements EntityProviderInterface {
+
+ private static BasicEntityProvider create() throws EntityProviderException {
+ return new BasicEntityProvider();
+ }
+
+ private static ContentTypeBasedEntityProvider create(final String contentType) throws EntityProviderException {
+ return create(ContentType.create(contentType));
+ }
+
+ private static ContentTypeBasedEntityProvider create(final ContentType contentType) throws EntityProviderException {
+ try {
+ switch (contentType.getODataFormat()) {
+ case ATOM:
+ case XML:
+ return new AtomEntityProvider(contentType.getODataFormat());
+ case JSON:
+ return new JsonEntityProvider();
+ default:
+ throw new ODataNotAcceptableException(ODataNotAcceptableException.NOT_SUPPORTED_CONTENT_TYPE.addContent(contentType));
+ }
+ } catch (final ODataNotAcceptableException e) {
+ throw new EntityProviderException(EntityProviderException.COMMON, e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeErrorDocument(final ODataErrorContext context) {
+ try {
+ return create(context.getContentType()).writeErrorDocument(context.getHttpStatus(), context.getErrorCode(), context.getMessage(), context.getLocale(), context.getInnerError());
+ } catch (EntityProviderException e) {
+ throw new ODataRuntimeException(e);
+ }
+ }
+
+ @Override
+ public ODataResponse writeServiceDocument(final String contentType, final Edm edm, final String serviceRoot) throws EntityProviderException {
+ return create(contentType).writeServiceDocument(edm, serviceRoot);
+ }
+
+ @Override
+ public ODataResponse writePropertyValue(final EdmProperty edmProperty, final Object value) throws EntityProviderException {
+ return create().writePropertyValue(edmProperty, value);
+ }
+
+ @Override
+ public ODataResponse writeText(final String value) throws EntityProviderException {
+ return create().writeText(value);
+ }
+
+ @Override
+ public ODataResponse writeBinary(final String mimeType, final byte[] data) throws EntityProviderException {
+ return create().writeBinary(mimeType, data);
+ }
+
+ @Override
+ public ODataResponse writeFeed(final String contentType, final EdmEntitySet entitySet, final List<Map<String, Object>> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ return create(contentType).writeFeed(entitySet, data, properties);
+ }
+
+ @Override
+ public ODataResponse writeEntry(final String contentType, final EdmEntitySet entitySet, final Map<String, Object> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ return create(contentType).writeEntry(entitySet, data, properties);
+ }
+
+ @Override
+ public ODataResponse writeProperty(final String contentType, final EdmProperty edmProperty, final Object value) throws EntityProviderException {
+ return create(contentType).writeProperty(edmProperty, value);
+ }
+
+ @Override
+ public ODataResponse writeLink(final String contentType, final EdmEntitySet entitySet, final Map<String, Object> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ return create(contentType).writeLink(entitySet, data, properties);
+ }
+
+ @Override
+ public ODataResponse writeLinks(final String contentType, final EdmEntitySet entitySet, final List<Map<String, Object>> data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ return create(contentType).writeLinks(entitySet, data, properties);
+ }
+
+ @Override
+ public ODataResponse writeFunctionImport(final String contentType, final EdmFunctionImport functionImport, final Object data, final EntityProviderWriteProperties properties) throws EntityProviderException {
+ return create(contentType).writeFunctionImport(functionImport, data, properties);
+ }
+
+ @Override
+ public ODataFeed readFeed(final String contentType, final EdmEntitySet entitySet, final InputStream content, final EntityProviderReadProperties properties) throws EntityProviderException {
+ return create(contentType).readFeed(entitySet, content, properties);
+ }
+
+ @Override
+ public ODataEntry readEntry(final String contentType, final EdmEntitySet entitySet, final InputStream content, final EntityProviderReadProperties properties) throws EntityProviderException {
+ return create(contentType).readEntry(entitySet, content, properties);
+ }
+
+ @Override
+ public Map<String, Object> readProperty(final String contentType, final EdmProperty edmProperty, final InputStream content, final EntityProviderReadProperties properties) throws EntityProviderException {
+ return create(contentType).readProperty(edmProperty, content, properties);
+ }
+
+ @Override
+ public Object readPropertyValue(final EdmProperty edmProperty, final InputStream content, final Class<?> typeMapping) throws EntityProviderException {
+ return create().readPropertyValue(edmProperty, content, typeMapping);
+ }
+
+ @Override
+ public List<String> readLinks(final String contentType, final EdmEntitySet entitySet, final InputStream content) throws EntityProviderException {
+ return create(contentType).readLinks(entitySet, content);
+ }
+
+ @Override
+ public String readLink(final String contentType, final EdmEntitySet entitySet, final InputStream content) throws EntityProviderException {
+ return create(contentType).readLink(entitySet, content);
+ }
+
+ @Override
+ public byte[] readBinary(final InputStream content) throws EntityProviderException {
+ return create().readBinary(content);
+ }
+
+ @Override
+ public ODataResponse writeMetadata(final List<Schema> schemas, final Map<String, String> predefinedNamespaces) throws EntityProviderException {
+ return create().writeMetadata(schemas, predefinedNamespaces);
+ }
+
+ @Override
+ public Edm readMetadata(final InputStream inputStream, final boolean validate) throws EntityProviderException {
+ EdmProvider provider = new EdmxProvider().parse(inputStream, validate);
+ return new EdmImplProv(provider);
+ }
+
+ @Override
+ public ServiceDocument readServiceDocument(final InputStream serviceDocument, final String contentType) throws EntityProviderException {
+ return create(contentType).readServiceDocument(serviceDocument);
+ }
+
+ @Override
+ public List<BatchPart> parseBatchRequest(final String contentType, final InputStream content, final EntityProviderBatchProperties properties) throws BatchException {
+ List<BatchPart> batchParts = new BatchRequestParser(contentType, properties).parse(content);
+ return batchParts;
+ }
+
+ @Override
+ public ODataResponse writeBatchResponse(final List<BatchResponsePart> batchResponseParts) throws BatchException {
+ BatchResponseWriter batchWriter = new BatchResponseWriter();
+ return batchWriter.writeResponse(batchResponseParts);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/aggregator/EntityComplexPropertyInfo.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/aggregator/EntityComplexPropertyInfo.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/aggregator/EntityComplexPropertyInfo.java
new file mode 100644
index 0000000..dfd96e0
--- /dev/null
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/aggregator/EntityComplexPropertyInfo.java
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * 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.odata2.core.ep.aggregator;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.olingo.odata2.api.edm.EdmCustomizableFeedMappings;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmFacets;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmType;
+
+/**
+ * author SAP AG
+ */
+public class EntityComplexPropertyInfo extends EntityPropertyInfo {
+
+ protected List<EntityPropertyInfo> entityPropertyInfo;
+
+ EntityComplexPropertyInfo(final String name, final EdmType type, final EdmFacets facets, final EdmCustomizableFeedMappings customizableFeedMapping, final List<EntityPropertyInfo> childEntityInfos) {
+ super(name, type, facets, customizableFeedMapping, null, null);
+ entityPropertyInfo = childEntityInfos;
+ }
+
+ static EntityComplexPropertyInfo create(final EdmProperty property, final List<String> propertyNames, final Map<String, EntityPropertyInfo> childEntityInfos) throws EdmException {
+ List<EntityPropertyInfo> childEntityInfoList = new ArrayList<EntityPropertyInfo>(childEntityInfos.size());
+ for (String name : propertyNames) {
+ childEntityInfoList.add(childEntityInfos.get(name));
+ }
+
+ EntityComplexPropertyInfo info = new EntityComplexPropertyInfo(
+ property.getName(),
+ property.getType(),
+ property.getFacets(),
+ property.getCustomizableFeedMappings(),
+ childEntityInfoList);
+ return info;
+ }
+
+ @Override
+ public boolean isComplex() {
+ return true;
+ }
+
+ public List<EntityPropertyInfo> getPropertyInfos() {
+ return Collections.unmodifiableList(entityPropertyInfo);
+ }
+
+ public EntityPropertyInfo getPropertyInfo(final String name) {
+ for (EntityPropertyInfo info : entityPropertyInfo) {
+ if (info.getName().equals(name)) {
+ return info;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ for (EntityPropertyInfo info : entityPropertyInfo) {
+ if (sb.length() == 0) {
+ sb.append(super.toString()).append("=>[").append(info.toString());
+ } else {
+ sb.append(", ").append(info.toString());
+ }
+ }
+ sb.append("]");
+ return sb.toString();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/aggregator/EntityInfoAggregator.java
----------------------------------------------------------------------
diff --git a/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/aggregator/EntityInfoAggregator.java b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/aggregator/EntityInfoAggregator.java
new file mode 100644
index 0000000..7d964c2
--- /dev/null
+++ b/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/aggregator/EntityInfoAggregator.java
@@ -0,0 +1,451 @@
+/*******************************************************************************
+ * 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.odata2.core.ep.aggregator;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.olingo.odata2.api.edm.EdmComplexType;
+import org.apache.olingo.odata2.api.edm.EdmConcurrencyMode;
+import org.apache.olingo.odata2.api.edm.EdmCustomizableFeedMappings;
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmFunctionImport;
+import org.apache.olingo.odata2.api.edm.EdmNavigationProperty;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmSimpleType;
+import org.apache.olingo.odata2.api.edm.EdmStructuralType;
+import org.apache.olingo.odata2.api.edm.EdmTargetPath;
+import org.apache.olingo.odata2.api.edm.EdmType;
+import org.apache.olingo.odata2.api.edm.EdmTypeKind;
+import org.apache.olingo.odata2.api.ep.EntityProviderException;
+import org.apache.olingo.odata2.api.uri.ExpandSelectTreeNode;
+
+/**
+ * Aggregator to get easy and fast access to all for serialization and de-serialization necessary {@link EdmEntitySet} informations.
+ *
+ * @author SAP AG
+ */
+public class EntityInfoAggregator {
+
+ private static final Set<String> SYN_TARGET_PATHS = new HashSet<String>(Arrays.asList(
+ EdmTargetPath.SYNDICATION_AUTHORNAME,
+ EdmTargetPath.SYNDICATION_AUTHOREMAIL,
+ EdmTargetPath.SYNDICATION_AUTHORURI,
+ EdmTargetPath.SYNDICATION_PUBLISHED,
+ EdmTargetPath.SYNDICATION_RIGHTS,
+ EdmTargetPath.SYNDICATION_TITLE,
+ EdmTargetPath.SYNDICATION_UPDATED,
+ EdmTargetPath.SYNDICATION_CONTRIBUTORNAME,
+ EdmTargetPath.SYNDICATION_CONTRIBUTOREMAIL,
+ EdmTargetPath.SYNDICATION_CONTRIBUTORURI,
+ EdmTargetPath.SYNDICATION_SOURCE,
+ EdmTargetPath.SYNDICATION_SUMMARY));
+
+ private Map<String, EntityPropertyInfo> propertyInfo = new HashMap<String, EntityPropertyInfo>();
+ private Map<String, NavigationPropertyInfo> navigationPropertyInfos = new HashMap<String, NavigationPropertyInfo>();
+ private List<EntityPropertyInfo> keyPropertyInfos;
+
+ /*
+ * list with all property names in the order based on order in {@link EdmProperty} (normally [key, entity,
+ * navigation])
+ */
+ private List<String> etagPropertyNames = new ArrayList<String>();
+ private List<String> propertyNames;
+ private List<String> navigationPropertyNames;
+ private List<String> selectedPropertyNames;
+ private List<String> selectedNavigationPropertyNames;
+ private List<String> expandedNavigationPropertyNames;
+
+ private Map<String, EntityPropertyInfo> targetPath2EntityPropertyInfo = new HashMap<String, EntityPropertyInfo>();
+ private List<String> noneSyndicationTargetPaths = new ArrayList<String>();
+
+ private boolean isDefaultEntityContainer;
+ private String entitySetName;
+ private String entityContainerName;
+
+ private EdmEntityType entityType;
+ private EdmEntitySet entitySet;
+
+ /**
+ * Constructor is private to force creation over {@link #create(EdmEntitySet)} method.
+ */
+ private EntityInfoAggregator() {}
+
+ /**
+ * Create an {@link EntityInfoAggregator} based on given {@link EdmEntitySet}
+ *
+ * @param entitySet
+ * with which the {@link EntityInfoAggregator} is initialized.
+ * @param expandSelectTree
+ * @return created and initialized {@link EntityInfoAggregator}
+ * @throws EntityProviderException
+ * if during initialization of {@link EntityInfoAggregator} something goes wrong (e.g. exceptions during
+ * access
+ * of {@link EdmEntitySet}).
+ */
+ public static EntityInfoAggregator create(final EdmEntitySet entitySet, final ExpandSelectTreeNode expandSelectTree) throws EntityProviderException {
+ EntityInfoAggregator eia = new EntityInfoAggregator();
+ eia.initialize(entitySet, expandSelectTree);
+ return eia;
+ }
+
+ /**
+ * Create an {@link EntityInfoAggregator} based on given {@link EdmEntitySet}
+ *
+ * @param entitySet
+ * with which the {@link EntityInfoAggregator} is initialized.
+ * @return created and initialized {@link EntityInfoAggregator}
+ * @throws EntityProviderException
+ * if during initialization of {@link EntityInfoAggregator} something goes wrong (e.g. exceptions during
+ * access
+ * of {@link EdmEntitySet}).
+ */
+ public static EntityInfoAggregator create(final EdmEntitySet entitySet) throws EntityProviderException {
+ EntityInfoAggregator eia = new EntityInfoAggregator();
+ eia.initialize(entitySet, null);
+ return eia;
+ }
+
+ /**
+ * Create an {@link EntityPropertyInfo} based on given {@link EdmProperty}
+ *
+ * @param property
+ * for which the {@link EntityPropertyInfo} is created.
+ * @return created {@link EntityPropertyInfo}
+ * @throws EntityProviderException
+ * if create of {@link EntityPropertyInfo} something goes wrong (e.g. exceptions during
+ * access of {@link EdmProperty}).
+ */
+ public static EntityPropertyInfo create(final EdmProperty property) throws EntityProviderException {
+ try {
+ EntityInfoAggregator eia = new EntityInfoAggregator();
+ return eia.createEntityPropertyInfo(property);
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.COMMON, e);
+ }
+ }
+
+ /**
+ * Create an map of <code>complex type property name</code> to {@link EntityPropertyInfo} based on given {@link EdmComplexType}
+ *
+ * @param complexType
+ * for which the {@link EntityPropertyInfo} is created.
+ * @return created map of <code>complex type property name</code> to {@link EntityPropertyInfo}
+ * @throws EntityProviderException
+ * if create of {@link EntityPropertyInfo} something goes wrong (e.g. exceptions during
+ * access of {@link EntityPropertyInfo}).
+ */
+ public static Map<String, EntityPropertyInfo> create(final EdmComplexType complexType) throws EntityProviderException {
+ try {
+ EntityInfoAggregator entityInfo = new EntityInfoAggregator();
+ return entityInfo.createPropertyInfoObjects(complexType, complexType.getPropertyNames());
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.COMMON, e);
+ }
+ }
+
+ /**
+ * Create an {@link EntityPropertyInfo} based on given {@link EdmFunctionImport}
+ *
+ * @param functionImport
+ * for which the {@link EntityPropertyInfo} is created.
+ * @return created {@link EntityPropertyInfo}
+ * @throws EntityProviderException
+ * if create of {@link EntityPropertyInfo} something goes wrong (e.g. exceptions during
+ * access of {@link EdmFunctionImport}).
+ */
+ public static EntityPropertyInfo create(final EdmFunctionImport functionImport) throws EntityProviderException {
+ try {
+ EntityInfoAggregator eia = new EntityInfoAggregator();
+ return eia.createEntityPropertyInfo(functionImport, functionImport.getReturnType().getType());
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.COMMON, e);
+ }
+ }
+
+ /**
+ * @return the edm entity set which was used to build this entity info aggregator object
+ */
+ public EdmEntitySet getEntitySet() {
+ return entitySet;
+ }
+
+ /**
+ * @return entity set name.
+ */
+ public String getEntitySetName() {
+ return entitySetName;
+ }
+
+ /**
+ * @return <code>true</code> if the entity container of {@link EdmEntitySet} is the default container,
+ * otherwise <code>false</code>.
+ */
+ public boolean isDefaultEntityContainer() {
+ return isDefaultEntityContainer;
+ }
+
+ public EntityPropertyInfo getTargetPathInfo(final String targetPath) {
+ return targetPath2EntityPropertyInfo.get(targetPath);
+ }
+
+ public EdmEntityType getEntityType() {
+ return entityType;
+ }
+
+ public String getEntityContainerName() {
+ return entityContainerName;
+ }
+
+ /**
+ * @return unmodifiable set of all found target path names.
+ */
+ public Collection<String> getTargetPathNames() {
+ return Collections.unmodifiableCollection(targetPath2EntityPropertyInfo.keySet());
+ }
+
+ /**
+ * @return unmodifiable set of found <code>none syndication target path names</code> (all target path names which are
+ * not pre-defined).
+ */
+ public List<String> getNoneSyndicationTargetPathNames() {
+ return Collections.unmodifiableList(noneSyndicationTargetPaths);
+ }
+
+ /**
+ * @return unmodifiable set of all found navigation property names.
+ */
+ public List<String> getNavigationPropertyNames() throws EntityProviderException {
+ return Collections.unmodifiableList(navigationPropertyNames);
+ }
+
+ /**
+ * @return unmodifiable set of all property names.
+ */
+ public List<String> getPropertyNames() throws EntityProviderException {
+ return Collections.unmodifiableList(propertyNames);
+ }
+
+ /**
+ * @return unmodifiable set of selected property names.
+ */
+ public List<String> getSelectedPropertyNames() throws EntityProviderException {
+ return Collections.unmodifiableList(selectedPropertyNames);
+ }
+
+ /**
+ * @return unmodifiable set of selected property names.
+ */
+ public List<String> getSelectedNavigationPropertyNames() throws EntityProviderException {
+ return Collections.unmodifiableList(selectedNavigationPropertyNames);
+ }
+
+ public Collection<EntityPropertyInfo> getPropertyInfos() {
+ return Collections.unmodifiableCollection(propertyInfo.values());
+ }
+
+ public EntityPropertyInfo getPropertyInfo(final String name) {
+ return propertyInfo.get(name);
+ }
+
+ public Collection<EntityPropertyInfo> getETagPropertyInfos() {
+ List<EntityPropertyInfo> keyProperties = new ArrayList<EntityPropertyInfo>();
+ for (String etagPropertyName : etagPropertyNames) {
+ EntityPropertyInfo e = propertyInfo.get(etagPropertyName);
+ keyProperties.add(e);
+ }
+ return keyProperties;
+ }
+
+ /**
+ * @return list of all key property infos
+ * @throws EntityProviderException
+ */
+ public List<EntityPropertyInfo> getKeyPropertyInfos() throws EntityProviderException {
+
+ if (keyPropertyInfos == null) {
+ try {
+ keyPropertyInfos = new ArrayList<EntityPropertyInfo>();
+ for (String keyPropertyName : entityType.getKeyPropertyNames()) {
+ keyPropertyInfos.add(propertyInfo.get(keyPropertyName));
+ }
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.COMMON, e);
+ }
+ }
+ return keyPropertyInfos;
+ }
+
+ public NavigationPropertyInfo getNavigationPropertyInfo(final String name) {
+ return navigationPropertyInfos.get(name);
+ }
+
+ private void initialize(final EdmEntitySet entitySet, final ExpandSelectTreeNode expandSelectTree) throws EntityProviderException {
+ try {
+ this.entitySet = entitySet;
+ entityType = entitySet.getEntityType();
+ entitySetName = entitySet.getName();
+ isDefaultEntityContainer = entitySet.getEntityContainer().isDefaultEntityContainer();
+ entityContainerName = entitySet.getEntityContainer().getName();
+
+ propertyNames = entityType.getPropertyNames();
+ navigationPropertyNames = entityType.getNavigationPropertyNames();
+
+ propertyInfo = createPropertyInfoObjects(entityType, propertyNames);
+ navigationPropertyInfos = createNavigationInfoObjects(entityType, navigationPropertyNames);
+
+ selectedPropertyNames = propertyNames;
+ selectedNavigationPropertyNames = navigationPropertyNames;
+ expandedNavigationPropertyNames = new ArrayList<String>();
+
+ if (expandSelectTree != null && !expandSelectTree.isAll()) {
+ selectedPropertyNames = new ArrayList<String>();
+ selectedNavigationPropertyNames = new ArrayList<String>();
+ for (EdmProperty property : expandSelectTree.getProperties()) {
+ selectedPropertyNames.add(property.getName());
+ }
+ for (String property : expandSelectTree.getLinks().keySet()) {
+ selectedNavigationPropertyNames.add(property);
+ if (expandSelectTree.getLinks().get(property) != null) {
+ expandedNavigationPropertyNames.add(property);
+ }
+ }
+ } else if (expandSelectTree != null) {
+ for (String property : expandSelectTree.getLinks().keySet()) {
+ if (expandSelectTree.getLinks().get(property) != null) {
+ expandedNavigationPropertyNames.add(property);
+ }
+ }
+ }
+
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.COMMON, e);
+ }
+ }
+
+ private Map<String, EntityPropertyInfo> createPropertyInfoObjects(final EdmStructuralType type, final List<String> propertyNames) throws EntityProviderException {
+ try {
+ Map<String, EntityPropertyInfo> infos = new HashMap<String, EntityPropertyInfo>();
+
+ for (String propertyName : propertyNames) {
+ EdmProperty property = (EdmProperty) type.getProperty(propertyName);
+
+ checkETagRelevant(property);
+
+ EntityPropertyInfo info = createEntityPropertyInfo(property);
+ infos.put(info.getName(), info);
+ checkTargetPathInfo(property, info);
+ }
+
+ return infos;
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.COMMON, e);
+ }
+ }
+
+ private Map<String, NavigationPropertyInfo> createNavigationInfoObjects(final EdmStructuralType type, final List<String> propertyNames) throws EntityProviderException {
+ try {
+ Map<String, NavigationPropertyInfo> infos = new HashMap<String, NavigationPropertyInfo>();
+
+ for (String propertyName : propertyNames) {
+ EdmNavigationProperty navProperty = (EdmNavigationProperty) type.getProperty(propertyName);
+ NavigationPropertyInfo info = NavigationPropertyInfo.create(navProperty);
+ infos.put(propertyName, info);
+ }
+
+ return infos;
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.COMMON, e);
+ }
+ }
+
+ private EntityPropertyInfo createEntityPropertyInfo(final EdmProperty property) throws EdmException, EntityProviderException {
+ EdmType type = property.getType();
+ if (type instanceof EdmSimpleType) {
+ return EntityPropertyInfo.create(property);
+ } else if (type instanceof EdmComplexType) {
+ EdmComplexType complex = (EdmComplexType) type;
+ Map<String, EntityPropertyInfo> recursiveInfos = createPropertyInfoObjects(complex, complex.getPropertyNames());
+ return EntityComplexPropertyInfo.create(property, complex.getPropertyNames(), recursiveInfos);
+ } else {
+ throw new EntityProviderException(EntityProviderException.UNSUPPORTED_PROPERTY_TYPE);
+ }
+ }
+
+ private EntityPropertyInfo createEntityPropertyInfo(final EdmFunctionImport functionImport, final EdmType type) throws EdmException, EntityProviderException {
+ EntityPropertyInfo epi;
+
+ if (type.getKind() == EdmTypeKind.COMPLEX) {
+ EdmComplexType complex = (EdmComplexType) type;
+ Map<String, EntityPropertyInfo> eia = EntityInfoAggregator.create(complex);
+
+ List<EntityPropertyInfo> childEntityInfoList = new ArrayList<EntityPropertyInfo>();
+ for (String propertyName : complex.getPropertyNames()) {
+ childEntityInfoList.add(eia.get(propertyName));
+ }
+ epi = new EntityComplexPropertyInfo(functionImport.getName(), type, null, null, childEntityInfoList);
+
+ } else if (type.getKind() == EdmTypeKind.SIMPLE) {
+
+ epi = new EntityPropertyInfo(functionImport.getName(), type, null, null, null, null);
+ } else {
+ throw new EntityProviderException(EntityProviderException.UNSUPPORTED_PROPERTY_TYPE.addContent(type.getKind()));
+ }
+
+ return epi;
+ }
+
+ private void checkETagRelevant(final EdmProperty edmProperty) throws EntityProviderException {
+ try {
+ if (edmProperty.getFacets() != null && edmProperty.getFacets().getConcurrencyMode() == EdmConcurrencyMode.Fixed) {
+ etagPropertyNames.add(edmProperty.getName());
+ }
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.COMMON, e);
+ }
+ }
+
+ private void checkTargetPathInfo(final EdmProperty property, final EntityPropertyInfo propertyInfo) throws EntityProviderException {
+ try {
+ EdmCustomizableFeedMappings customizableFeedMappings = property.getCustomizableFeedMappings();
+ if (customizableFeedMappings != null) {
+ String targetPath = customizableFeedMappings.getFcTargetPath();
+ targetPath2EntityPropertyInfo.put(targetPath, propertyInfo);
+ if (!SYN_TARGET_PATHS.contains(targetPath)) {
+ noneSyndicationTargetPaths.add(targetPath);
+ }
+ }
+ } catch (EdmException e) {
+ throw new EntityProviderException(EntityProviderException.COMMON, e);
+ }
+ }
+
+ public List<String> getExpandedNavigationPropertyNames() {
+ return expandedNavigationPropertyNames;
+ }
+}