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;
 
 
Provider for all basic (content type independent) entity provider methods.

Author(s):
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.

Parameters:
content the content input stream
Returns:
the binary data
Throws:
com.sap.core.odata.api.ep.EntityProviderException
 
   public byte[] readBinary(final InputStream contentthrows EntityProviderException {
     ByteArrayOutputStream buffer = new ByteArrayOutputStream();
     byte[] value = new byte[.];
     int count;
     try {
       while ((count = content.read(value)) > 0) {
         buffer.write(value, 0, count);
       }
       content.close();
       buffer.flush();
       return buffer.toByteArray();
     } catch (IOException e) {
     }
   }

  

Parameters:
content
Returns:
text as string from InputStream
Throws:
com.sap.core.odata.api.ep.EntityProviderException
 
   private String readText(final InputStream contentthrows EntityProviderException {
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(content, Charset.forName()));
     StringBuilder stringBuilder = new StringBuilder();
     try {
       String line = null;
       while ((line = bufferedReader.readLine()) != null) {
         stringBuilder.append(line);
       }
      bufferedReader.close();
    } catch (IOException e) {
    }
    return stringBuilder.toString();
  }

  
Reads an unformatted value of an EDM property as binary or as content type text/plain.

Parameters:
edmProperty the EDM property
content the content input stream
typeMapping
Returns:
the value as the proper system data type
Throws:
com.sap.core.odata.api.ep.EntityProviderException
  public Object readPropertyValue(final EdmProperty edmPropertyfinal InputStream contentfinal Class<?> typeMappingthrows EntityProviderException {
    EdmSimpleType type;
    try {
      type = (EdmSimpleTypeedmProperty.getType();
    } catch (EdmException e) {
    }
      return readBinary(content);
    } else {
      try {
        if (typeMapping == null) {
          return type.valueOfString(readText(content), .edmProperty.getFacets(), type.getDefaultType());
        } else {
          return type.valueOfString(readText(content), .edmProperty.getFacets(), typeMapping);
        }
      } catch (EdmException e) {
      }
    }
  }

  
Write property as binary or as content type text/plain.

Parameters:
edmProperty the EDM property
value its value
Returns:
resulting com.sap.core.odata.api.processor.ODataResponse with written content
Throws:
com.sap.core.odata.api.ep.EntityProviderException
  public ODataResponse writePropertyValue(final EdmProperty edmPropertyfinal Object valuethrows EntityProviderException {
    try {
      final EdmSimpleType type = (EdmSimpleTypeedmProperty.getType();
      if (type == ..getEdmSimpleTypeInstance()) {
        String contentType = .;
        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 = (StringmappedData.get(mimeTypeMapping);
            } else {
              throw new EntityProviderException(.);
            }
          }
        }
        return writeBinary(contentType, (byte[]) binary);
      } else {
        return writeText(type.valueToString(value.edmProperty.getFacets()));
      }
    } catch (EdmException e) {
    }
  }

  
Write text value as content type text/plain.

Parameters:
value the string that is written to com.sap.core.odata.api.processor.ODataResponse
Returns:
resulting com.sap.core.odata.api.processor.ODataResponse with written text content
Throws:
com.sap.core.odata.api.ep.EntityProviderException
  public ODataResponse writeText(final String valuethrows EntityProviderException {
    ODataResponseBuilder builder = ODataResponse.newBuilder();
    if (value != null) {
      ByteArrayInputStream stream;
      try {
        stream = new ByteArrayInputStream(value.getBytes());
      } catch (UnsupportedEncodingException e) {
      }
      builder.entity(stream);
    }
    return builder.build();
  }

  
Write binary content with content type header set to given mime type parameter.

Parameters:
mimeType MIME type which is written and used as content type header information
data data is written to com.sap.core.odata.api.processor.ODataResponse
Returns:
resulting com.sap.core.odata.api.processor.ODataResponse with written binary content
Throws:
com.sap.core.odata.api.ep.EntityProviderException
  public ODataResponse writeBinary(final String mimeTypefinal byte[] datathrows EntityProviderException {
    ODataResponseBuilder builder = ODataResponse.newBuilder();
    if (data != null) {
      ByteArrayInputStream bais = new ByteArrayInputStream(data);
      builder.entity(bais);
    }
    builder.contentHeader(mimeType);
    return builder.build();
  }

  
Writes the metadata in XML format. Predefined namespaces is of type Map<prefix,namespace> and may be null or an empty Map.

Parameters:
schemas
predefinedNamespaces
Returns:
resulting com.sap.core.odata.api.processor.ODataResponse with written metadata content
Throws:
com.sap.core.odata.api.ep.EntityProviderException
  public ODataResponse writeMetadata(final List<Schemaschemasfinal Map<StringStringpredefinedNamespacesthrows EntityProviderException {
    ODataResponseBuilder builder = ODataResponse.newBuilder();
    String dataServiceVersion = .;
    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(metadataxmlStreamWriterpredefinedNamespaces);
    } catch (UnsupportedEncodingException e) {
    } catch (XMLStreamException e) {
    } catch (FactoryConfigurationError e) {
    }
    builder.entity(csb.getInputStream());
    builder.header(.dataServiceVersion);
    return builder.build();
  }

  
Calculates the necessary data service version for the metadata serialization

Parameters:
schemas
Returns:
DataServiceversion as String
  private String calculateDataServiceVersion(final List<Schemaschemas) {
    String dataServiceVersion = .;
    if (schemas != null) {
      for (Schema schema : schemas) {
        List<EntityTypeentityTypes = schema.getEntityTypes();
        if (entityTypes != null) {
          for (EntityType entityType : entityTypes) {
            List<Propertyproperties = entityType.getProperties();
            if (properties != null) {
              for (Property property : properties) {
                if (property.getCustomizableFeedMappings() != null) {
                  if (property.getCustomizableFeedMappings().getFcKeepInContent() != null) {
                    if (!property.getCustomizableFeedMappings().getFcKeepInContent()) {
                      dataServiceVersion = .;
                      return dataServiceVersion;
                    }
                  }
                }
              }
              if (entityType.getCustomizableFeedMappings() != null) {
                if (entityType.getCustomizableFeedMappings().getFcKeepInContent() != null) {
                  if (entityType.getCustomizableFeedMappings().getFcKeepInContent()) {
                    dataServiceVersion = .;
                    return dataServiceVersion;
                  }
                }
              }
            }
          }
        }
      }
    }
    return dataServiceVersion;
  }
New to GrepCode? Check out our FAQ X