예제 #1
0
 @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
 private static boolean isTrue(final SearchTerm term, final Property property) {
   if (property.isNull()) {
     return false;
   } else if (property.isPrimitive()) {
     if (property.isCollection()) {
       for (final Object primitive : property.asCollection()) {
         final String propertyString = asString(primitive);
         if (propertyString != null && propertyString.contains(term.getSearchTerm())) {
           return true;
         }
       }
       return false;
     } else {
       final String propertyString = asString(property.asPrimitive());
       return propertyString != null && propertyString.contains(term.getSearchTerm());
     }
   } else if (property.isComplex()) {
     if (property.isCollection()) {
       for (final Object member : property.asCollection()) {
         if (isTrue(term, (Property) member)) {
           return true;
         }
       }
       return false;
     } else {
       for (final Property innerProperty : property.asComplex().getValue()) {
         if (isTrue(term, innerProperty)) {
           return true;
         }
       }
       return false;
     }
   } else {
     return false;
   }
 }
  @Override
  public Property getProperty(
      final ODataProperty property, final Class<? extends Entry> reference, final boolean setType) {

    final Property propertyResource = ResourceFactory.newProperty(reference);
    propertyResource.setName(property.getName());
    propertyResource.setValue(getValue(property.getValue(), reference, setType));

    if (setType) {
      if (property.hasPrimitiveValue()) {
        propertyResource.setType(property.getPrimitiveValue().getType().toString());
      } else if (property.hasComplexValue()) {
        propertyResource.setType(property.getComplexValue().getType());
      } else if (property.hasCollectionValue()) {
        propertyResource.setType(property.getCollectionValue().getType());
      }
    }

    return propertyResource;
  }
예제 #4
0
  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 ODataValue getODataValue(final Property resource) {
    ODataValue value = null;

    if (resource.getValue().isSimple()) {
      value =
          client
              .getPrimitiveValueBuilder()
              .setText(resource.getValue().asSimple().get())
              .setType(
                  resource.getType() == null
                      ? null
                      : EdmPrimitiveTypeKind.valueOfFQN(
                          client.getServiceVersion(), resource.getType()))
              .build();
    } else if (resource.getValue().isGeospatial()) {
      value =
          client
              .getGeospatialValueBuilder()
              .setValue(resource.getValue().asGeospatial().get())
              .setType(
                  resource.getType() == null
                          || EdmPrimitiveTypeKind.Geography.getFullQualifiedName()
                              .toString()
                              .equals(resource.getType())
                          || EdmPrimitiveTypeKind.Geometry.getFullQualifiedName()
                              .toString()
                              .equals(resource.getType())
                      ? null
                      : EdmPrimitiveTypeKind.valueOfFQN(
                          client.getServiceVersion(), resource.getType()))
              .build();
    } else if (resource.getValue().isComplex()) {
      value = new ODataComplexValue(resource.getType());

      for (Property property : resource.getValue().asComplex().get()) {
        value.asComplex().add(getODataProperty(property));
      }
    } else if (resource.getValue().isCollection()) {
      value = new ODataCollectionValue(resource.getType());

      for (Value _value : resource.getValue().asCollection().get()) {
        final JSONPropertyImpl fake = new JSONPropertyImpl();
        fake.setValue(_value);
        value.asCollection().add(getODataValue(fake));
      }
    }

    return value;
  }
 @Override
 public ODataProperty getODataProperty(final Property property) {
   return new ODataProperty(property.getName(), getODataValue(property));
 }