Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2013 SAP AG Licensed 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 com.sap.core.odata.core.ep;
 
 import java.util.List;
 import java.util.Map;
 
 

Author(s):
SAP AG
 
 public class AtomEntityProvider implements ContentTypeBasedEntityProvider {

  
Default used charset for writer and response content header
 
   private static final String DEFAULT_CHARSET = .;
   private static final String XML_VERSION = "1.0";
   private final ODataFormat odataFormat;
 
   public AtomEntityProvider() throws EntityProviderException {
     this(.);
   }
 
   public AtomEntityProvider(final ContentType contentTypethrows EntityProviderException {
     this(contentType.getODataFormat());
   }
 
   public AtomEntityProvider(final ODataFormat odataFormatthrows EntityProviderException {
     switch (odataFormat) {
     case :
     case :
       this. = odataFormat;
       break;
     default:
       throw new EntityProviderException(..addContent("Got unsupported ODataFormat '" + odataFormat + "'."));
     }
   }

  

Serializes an error message according to the OData standard.

In case an error occurs, it is logged. An exception is not thrown because this method is used in exception handling.

Parameters:
status the com.sap.core.odata.api.commons.HttpStatusCodes associated with this error
errorCode a String that serves as a substatus to the HTTP response code
message a human-readable message describing the error
locale the java.util.Locale that should be used to format the error message
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
Returns:
an com.sap.core.odata.api.processor.ODataResponse containing the serialized error message
  public ODataResponse writeErrorDocument(final HttpStatusCodes statusfinal String errorCodefinal String messagefinal Locale localefinal String innerError) {
    OutputStream outStream = null;
    ODataRuntimeException cachedException = null;
    try {
      CircleStreamBuffer csb = new CircleStreamBuffer();
      outStream = csb.getOutputStream();
      XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream);
      XmlErrorDocumentProducer producer = new XmlErrorDocumentProducer();
      producer.writeErrorDocument(writererrorCodemessagelocaleinnerError);
      writer.flush();
      outStream.flush();
      outStream.close();
      ODataResponseBuilder response = ODataResponse.entity(csb.getInputStream())
          .status(status);
      return response.build();
    } catch (XMLStreamException e) {
      cachedException = new ODataRuntimeException(e);
      throw cachedException;
    } catch (IOException e) {
      cachedException = new ODataRuntimeException(e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (outStream != null) {
        try {
          outStream.close();
        } catch (IOException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new ODataRuntimeException(e);
          }
        }
      }
    }
  }

  
Write service document based on given com.sap.core.odata.api.edm.Edm and service root as content type "application/atomsvc+xml; charset=utf-8".

Parameters:
edm the Entity Data Model
serviceRoot the root URI of the service
Returns:
resulting com.sap.core.odata.api.processor.ODataResponse with written service document
Throws:
com.sap.core.odata.api.ep.EntityProviderException
  public ODataResponse writeServiceDocument(final Edm edmfinal String serviceRootthrows EntityProviderException {
    OutputStreamWriter writer = null;
    EntityProviderException cachedException = null;
    try {
      CircleStreamBuffer csb = new CircleStreamBuffer();
      OutputStream outputStream = csb.getOutputStream();
      writer = new OutputStreamWriter(outputStream);
      AtomServiceDocumentProducer as = new AtomServiceDocumentProducer(edmserviceRoot);
      as.writeServiceDocument(writer);
      ODataResponse response = ODataResponse.entity(csb.getInputStream())
          .build();
      return response;
    } catch (UnsupportedEncodingException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (writer != null) {
        try {
          writer.close();
        } catch (IOException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
  public ODataResponse writeEntry(final EdmEntitySet entitySetfinal Map<StringObjectdatafinal EntityProviderWriteProperties propertiesthrows EntityProviderException {
    OutputStream outStream = null;
    EntityProviderException cachedException = null;
    try {
      CircleStreamBuffer csb = new CircleStreamBuffer();
      outStream = csb.getOutputStream();
      XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream);
      AtomEntryEntityProducer as = new AtomEntryEntityProducer(properties);
      EntityInfoAggregator eia = EntityInfoAggregator.create(entitySetproperties.getExpandSelectTree());
      as.append(writereiadatatruefalse);
      writer.flush();
      outStream.flush();
      outStream.close();
      ODataResponseBuilder response = ODataResponse.entity(csb.getInputStream())
          .eTag(as.getETag())
          .idLiteral(as.getLocation());
      return response.build();
    } catch (XMLStreamException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } catch (IOException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (outStream != null) {
        try {
          outStream.close();
        } catch (IOException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
  public ODataResponse writeProperty(final EdmProperty edmPropertyfinal Object valuethrows EntityProviderException {
    EntityPropertyInfo propertyInfo = EntityInfoAggregator.create(edmProperty);
    return writeSingleTypedElement(propertyInfovalue);
  }
  private ODataResponse writeSingleTypedElement(final EntityPropertyInfo propertyInfofinal Object valuethrows EntityProviderException {
    OutputStream outStream = null;
    EntityProviderException cachedException = null;
    try {
      CircleStreamBuffer csb = new CircleStreamBuffer();
      outStream = csb.getOutputStream();
      XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream);
      ps.append(writerpropertyInfovalue);
      writer.flush();
      outStream.flush();
      outStream.close();
      return response;
    } catch (XMLStreamException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } catch (IOException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (outStream != null) {
        try {
          outStream.close();
        } catch (IOException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
  public ODataResponse writeFeed(final EdmEntitySet entitySetfinal List<Map<StringObject>> datafinal EntityProviderWriteProperties propertiesthrows EntityProviderException {
    OutputStream outStream = null;
    EntityProviderException cachedException = null;
    try {
      CircleStreamBuffer csb = new CircleStreamBuffer();
      outStream = csb.getOutputStream();
      XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream);
      AtomFeedProducer atomFeedProvider = new AtomFeedProducer(properties);
      EntityInfoAggregator eia = EntityInfoAggregator.create(entitySetproperties.getExpandSelectTree());
      atomFeedProvider.append(writereiadatafalse);
      writer.flush();
      outStream.flush();
      outStream.close();
      return response;
    } catch (XMLStreamException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } catch (IOException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (outStream != null) {
        try {
          outStream.close();
        } catch (IOException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
  private String getContentHeader(final ContentType mediaType) {
    if ( == .) {
    }
    return ContentType.create(mediaType.).toContentTypeString();
  }
  public ODataResponse writeLink(final EdmEntitySet entitySetfinal Map<StringObjectdatafinal EntityProviderWriteProperties propertiesthrows EntityProviderException {
    CircleStreamBuffer buffer = new CircleStreamBuffer();
    OutputStream outStream = buffer.getOutputStream();
    EntityProviderException cachedException = null;
    try {
      XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream);
      XmlLinkEntityProducer entity = new XmlLinkEntityProducer(properties);
      final EntityInfoAggregator entityInfo = EntityInfoAggregator.create(entitySetproperties.getExpandSelectTree());
      entity.append(writerentityInfodatatrue);
      writer.flush();
      outStream.flush();
      outStream.close();
    } catch (XMLStreamException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } catch (IOException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (outStream != null) {
        try {
          outStream.close();
        } catch (IOException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
  public ODataResponse writeLinks(final EdmEntitySet entitySetfinal List<Map<StringObject>> datafinal EntityProviderWriteProperties propertiesthrows EntityProviderException {
    CircleStreamBuffer buffer = new CircleStreamBuffer();
    OutputStream outStream = buffer.getOutputStream();
    EntityProviderException cachedException = null;
    try {
      XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream);
      XmlLinksEntityProducer entity = new XmlLinksEntityProducer(properties);
      final EntityInfoAggregator entityInfo = EntityInfoAggregator.create(entitySetproperties.getExpandSelectTree());
      entity.append(writerentityInfodata);
      writer.flush();
      outStream.flush();
      outStream.close();
    } catch (XMLStreamException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } catch (IOException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (outStream != null) {
        try {
          outStream.close();
        } catch (IOException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
  private ODataResponse writeCollection(final EntityPropertyInfo propertyInfofinal List<?> datathrows EntityProviderException {
    OutputStream outStream = null;
    EntityProviderException cachedException = null;
    try {
      CircleStreamBuffer buffer = new CircleStreamBuffer();
      outStream = buffer.getOutputStream();
      XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream);
      XmlCollectionEntityProducer.append(writerpropertyInfodata);
      writer.flush();
      outStream.flush();
      outStream.close();
    } catch (XMLStreamException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } catch (IOException e) {
      cachedException = new EntityProviderException(.e);
      throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
      if (outStream != null) {
        try {
          outStream.close();
        } catch (IOException e) {
          if (cachedException != null) {
            throw cachedException;
          } else {
            throw new EntityProviderException(.e);
          }
        }
      }
    }
  }
  public ODataResponse writeFunctionImport(final EdmFunctionImport functionImportfinal Object datafinal EntityProviderWriteProperties propertiesthrows EntityProviderException {
    try {
      final EdmType type = functionImport.getReturnType().getType();
      final boolean isCollection = functionImport.getReturnType().getMultiplicity() == .;
      if (type.getKind() == .) {
        @SuppressWarnings("unchecked")
        Map<StringObjectmap = (Map<StringObject>) data;
        return writeEntry(functionImport.getEntitySet(), mapproperties);
      }
      final EntityPropertyInfo info = EntityInfoAggregator.create(functionImport);
      if (isCollection) {
        return writeCollection(info, (List<?>) data);
      } else {
        return writeSingleTypedElement(infodata);
      }
    } catch (EdmException e) {
    }
  }
  public ODataFeed readFeed(final EdmEntitySet entitySetfinal InputStream contentfinal EntityProviderReadProperties propertiesthrows EntityProviderException {
    XmlEntityConsumer xec = new XmlEntityConsumer();
    return xec.readFeed(entitySetcontentproperties);
  }
  public ODataEntry readEntry(final EdmEntitySet entitySetfinal InputStream contentfinal EntityProviderReadProperties propertiesthrows EntityProviderException {
    XmlEntityConsumer xec = new XmlEntityConsumer();
    return xec.readEntry(entitySetcontentproperties);
  }
  public Map<StringObjectreadProperty(final EdmProperty edmPropertyfinal InputStream contentfinal EntityProviderReadProperties propertiesthrows EntityProviderException {
    XmlEntityConsumer xec = new XmlEntityConsumer();
    return xec.readProperty(edmPropertycontentproperties);
  }
  public String readLink(final EdmEntitySet entitySetfinal InputStream contentthrows EntityProviderException {
    XmlEntityConsumer xec = new XmlEntityConsumer();
    return xec.readLink(entitySetcontent);
  }
  public List<StringreadLinks(final EdmEntitySet entitySetfinal InputStream contentthrows EntityProviderException {
    XmlEntityConsumer xec = new XmlEntityConsumer();
    return xec.readLinks(entitySetcontent);
  }
  public ServiceDocument readServiceDocument(final InputStream serviceDocumentthrows EntityProviderException {
    AtomServiceDocumentConsumer serviceDocConsumer = new AtomServiceDocumentConsumer();
    return serviceDocConsumer.parseXml(serviceDocument);
  }
New to GrepCode? Check out our FAQ X