@Override
 public void readPrimitiveValue(
     ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType format)
     throws ODataApplicationException, SerializerException {
   // First we have to figure out which entity set the requested entity is in
   final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
   // Next we fetch the requested entity from the database
   final Entity entity;
   try {
     entity = readEntityInternal(uriInfo.asUriInfoResource(), edmEntitySet);
   } catch (DataProviderException e) {
     throw new ODataApplicationException(e.getMessage(), 500, Locale.ENGLISH);
   }
   if (entity == null) {
     // If no entity was found for the given key we throw an exception.
     throw new ODataApplicationException(
         "No entity found for this key", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
   } else {
     // Next we get the property value from the entity and pass the value to serialization
     UriResourceProperty uriProperty =
         (UriResourceProperty)
             uriInfo.getUriResourceParts().get(uriInfo.getUriResourceParts().size() - 1);
     EdmProperty edmProperty = uriProperty.getProperty();
     Property property = entity.getProperty(edmProperty.getName());
     if (property == null) {
       throw new ODataApplicationException(
           "No property found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
     } else {
       if (property.getValue() == null) {
         response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
       } else {
         String value = String.valueOf(property.getValue());
         ByteArrayInputStream serializerContent =
             new ByteArrayInputStream(value.getBytes(Charset.forName("UTF-8")));
         response.setContent(serializerContent);
         response.setStatusCode(HttpStatusCode.OK.getStatusCode());
         response.setHeader(HttpHeader.CONTENT_TYPE, ContentType.TEXT_PLAIN.toContentTypeString());
       }
     }
   }
 }
示例#2
0
  public static boolean entityMatchesAllKeys(
      EdmEntityType edmEntityType, Entity rt_entity, List<UriParameter> keyParams) {

    // loop over all keys
    for (final UriParameter key : keyParams) {
      // key
      String keyName = key.getName();
      String keyText = key.getText();

      // note: below line doesn't consider: keyProp can be part of a complexType in V4
      // in such case, it would be required to access it via getKeyPropertyRef()
      // but since this isn't the case in our model, we ignore it in our implementation
      EdmProperty edmKeyProperty = (EdmProperty) edmEntityType.getProperty(keyName);
      // Edm: we need this info for the comparison below
      Boolean isNullable = edmKeyProperty.isNullable();
      Integer maxLength = edmKeyProperty.getMaxLength();
      Integer precision = edmKeyProperty.getPrecision();
      Boolean isUnicode = edmKeyProperty.isUnicode();
      Integer scale = edmKeyProperty.getScale();
      // get the EdmType in order to compare
      EdmType edmType = edmKeyProperty.getType();
      // if(EdmType instanceof EdmPrimitiveType) // do we need this?
      EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmType;

      // Runtime data: the value of the current entity
      // don't need to check for null, this is done in FWK
      Object valueObject = rt_entity.getProperty(keyName).getValue();
      // TODO if the property is a complex type

      // now need to compare the valueObject with the keyText String
      // this is done using the type.valueToString
      String valueAsString = null;
      try {
        valueAsString =
            edmPrimitiveType.valueToString(
                valueObject, isNullable, maxLength, precision, scale, isUnicode);
      } catch (EdmPrimitiveTypeException e) {
        return false; // TODO proper Exception handling
      }

      if (valueAsString == null) {
        return false;
      }

      boolean matches = valueAsString.equals(keyText);
      // if any of the key properties is not found in the entity, we don't need to search further
      if (!matches) {
        return false;
      }
      // if the given key value is found in the current entity, continue with the next key
    }

    return true;
  }
  private void readProperty(
      ODataResponse response, UriInfo uriInfo, ContentType contentType, boolean complex)
      throws ODataApplicationException, SerializerException {
    // To read a property we have to first get the entity out of the entity set
    final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
    Entity entity;
    try {
      entity = readEntityInternal(uriInfo.asUriInfoResource(), edmEntitySet);
    } catch (DataProviderException e) {
      throw new ODataApplicationException(
          e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.ENGLISH);
    }

    if (entity == null) {
      // If no entity was found for the given key we throw an exception.
      throw new ODataApplicationException(
          "No entity found for this key", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
    } else {
      // Next we get the property value from the entity and pass the value to serialization
      UriResourceProperty uriProperty =
          (UriResourceProperty)
              uriInfo.getUriResourceParts().get(uriInfo.getUriResourceParts().size() - 1);
      EdmProperty edmProperty = uriProperty.getProperty();
      Property property = entity.getProperty(edmProperty.getName());
      if (property == null) {
        throw new ODataApplicationException(
            "No property found", HttpStatusCode.NOT_FOUND.getStatusCode(), Locale.ENGLISH);
      } else {
        if (property.getValue() == null) {
          response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
        } else {
          ODataSerializer serializer = odata.createSerializer(contentType);
          final ContextURL contextURL =
              isODataMetadataNone(contentType)
                  ? null
                  : getContextUrl(edmEntitySet, true, null, null, edmProperty.getName());
          InputStream serializerContent =
              complex
                  ? serializer
                      .complex(
                          edm,
                          (EdmComplexType) edmProperty.getType(),
                          property,
                          ComplexSerializerOptions.with().contextURL(contextURL).build())
                      .getContent()
                  : serializer
                      .primitive(
                          edm,
                          (EdmPrimitiveType) edmProperty.getType(),
                          property,
                          PrimitiveSerializerOptions.with()
                              .contextURL(contextURL)
                              .scale(edmProperty.getScale())
                              .nullable(edmProperty.isNullable())
                              .precision(edmProperty.getPrecision())
                              .maxLength(edmProperty.getMaxLength())
                              .unicode(edmProperty.isUnicode())
                              .build())
                      .getContent();
          response.setContent(serializerContent);
          response.setStatusCode(HttpStatusCode.OK.getStatusCode());
          response.setHeader(HttpHeader.CONTENT_TYPE, contentType.toContentTypeString());
        }
      }
    }
  }
  private void appendProperties(final XMLStreamWriter writer, final EdmStructuredType type)
      throws XMLStreamException {
    List<String> propertyNames = new ArrayList<String>(type.getPropertyNames());
    if (type.getBaseType() != null) {
      propertyNames.removeAll(type.getBaseType().getPropertyNames());
    }
    for (String propertyName : propertyNames) {
      EdmProperty property = type.getStructuralProperty(propertyName);
      writer.writeEmptyElement(XML_PROPERTY);
      writer.writeAttribute(XML_NAME, propertyName);
      String fqnString;
      if (property.isPrimitive()) {
        fqnString = getFullQualifiedName(property.getType(), property.isCollection());
      } else {
        fqnString = getAliasedFullQualifiedName(property.getType(), property.isCollection());
      }
      writer.writeAttribute(XML_TYPE, fqnString);

      // Facets
      if (!property.isNullable()) {
        writer.writeAttribute(XML_NULLABLE, "" + property.isNullable());
      }

      if (!property.isUnicode()) {
        writer.writeAttribute(XML_UNICODE, "" + property.isUnicode());
      }

      if (property.getDefaultValue() != null) {
        writer.writeAttribute(XML_DEFAULT_VALUE, property.getDefaultValue());
      }

      if (property.getMaxLength() != null) {
        writer.writeAttribute(XML_MAX_LENGTH, "" + property.getMaxLength());
      }

      if (property.getPrecision() != null) {
        writer.writeAttribute(XML_PRECISION, "" + property.getPrecision());
      }

      if (property.getScale() != null) {
        writer.writeAttribute(XML_SCALE, "" + property.getScale());
      }
    }
  }