예제 #1
0
 public FilterValidator runUriOrderBy(final String path, final String query)
     throws UriParserException, UriValidationException {
   final UriInfo uriInfo = new Parser(edm, odata).parseUri(path, query, null);
   assertTrue(
       "Filtervalidator can only be used on resourcePaths",
       uriInfo.getKind() == UriInfoKind.resource);
   orderBy = uriInfo.getOrderByOption();
   return this;
 }
예제 #2
0
  public FilterValidator runUriOrderBy(final String path, final String query)
      throws UriParserException {
    Parser parser = new Parser();
    UriInfo uriInfo = null;

    uriInfo = parser.parseUri(path, query, null, edm);

    if (uriInfo.getKind() != UriInfoKind.resource) {
      fail("Filtervalidator can only be used on resourcePaths");
    }

    setOrderBy((OrderByOptionImpl) uriInfo.getOrderByOption());
    return this;
  }
예제 #3
0
 public void handleException(
     final ODataRequest request,
     final ODataResponse response,
     final ODataServerError serverError,
     final Exception exception) {
   lastThrownException = exception;
   ErrorProcessor exceptionProcessor;
   try {
     exceptionProcessor = selectProcessor(ErrorProcessor.class);
   } catch (ODataHandlerException e) {
     // This cannot happen since there is always an ExceptionProcessor registered.
     exceptionProcessor = new DefaultProcessor();
   }
   ContentType requestedContentType;
   try {
     requestedContentType =
         ContentNegotiator.doContentNegotiation(
             uriInfo == null ? null : uriInfo.getFormatOption(),
             request,
             getCustomContentTypeSupport(),
             RepresentationType.ERROR);
   } catch (final ContentNegotiatorException e) {
     requestedContentType = ContentType.JSON;
   }
   final int measurementHandle =
       debugger.startRuntimeMeasurement("ErrorProcessor", "processError");
   exceptionProcessor.processError(request, response, serverError, requestedContentType);
   debugger.stopRuntimeMeasurement(measurementHandle);
 }
예제 #4
0
  @Override
  public void readEntity(
      final ODataRequest request,
      ODataResponse response,
      final UriInfo uriInfo,
      final ContentType requestedContentType)
      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
    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 {
      // If an entity was found we proceed by serializing it and sending it to the client.
      ODataSerializer serializer = odata.createSerializer(requestedContentType);
      final ExpandOption expand = uriInfo.getExpandOption();
      final SelectOption select = uriInfo.getSelectOption();
      InputStream serializedContent =
          serializer
              .entity(
                  edm,
                  edmEntitySet.getEntityType(),
                  entity,
                  EntitySerializerOptions.with()
                      .contextURL(
                          isODataMetadataNone(requestedContentType)
                              ? null
                              : getContextUrl(edmEntitySet, true, expand, select, null))
                      .expand(expand)
                      .select(select)
                      .build())
              .getContent();
      response.setContent(serializedContent);
      response.setStatusCode(HttpStatusCode.OK.getStatusCode());
      response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
    }
  }
예제 #5
0
  @Override
  public void readEntityCollection(
      final ODataRequest request,
      ODataResponse response,
      final UriInfo uriInfo,
      final ContentType requestedContentType)
      throws ODataApplicationException, SerializerException {
    // First we have to figure out which entity set to use
    final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());

    // Second we fetch the data for this specific entity set from the mock database and transform it
    // into an EntitySet
    // object which is understood by our serialization
    EntityCollection entitySet = dataProvider.readAll(edmEntitySet);

    // Next we create a serializer based on the requested format. This could also be a custom format
    // but we do not
    // support them in this example
    ODataSerializer serializer = odata.createSerializer(requestedContentType);

    // Now the content is serialized using the serializer.
    final ExpandOption expand = uriInfo.getExpandOption();
    final SelectOption select = uriInfo.getSelectOption();
    InputStream serializedContent =
        serializer
            .entityCollection(
                edm,
                edmEntitySet.getEntityType(),
                entitySet,
                EntityCollectionSerializerOptions.with()
                    .contextURL(
                        isODataMetadataNone(requestedContentType)
                            ? null
                            : getContextUrl(edmEntitySet, false, expand, select, null))
                    .count(uriInfo.getCountOption())
                    .expand(expand)
                    .select(select)
                    .build())
            .getContent();

    // Finally we set the response data, headers and status code
    response.setContent(serializedContent);
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
  }
예제 #6
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());
       }
     }
   }
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * com.rohitghatol.spring.odata.edm.providers.EntityProvider#getEntitySet
   * (org.apache.olingo.server.api.uri.UriInfo)
   */
  @Override
  public EntitySet getEntitySet(UriInfo uriInfo) {
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();

    UriResourceEntitySet uriResourceEntitySet =
        (UriResourceEntitySet)
            resourcePaths.get(0); // in our example, the first segment is the EntitySet

    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    EntitySet entitySet = getData(edmEntitySet);

    return entitySet;
  }
예제 #8
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());
        }
      }
    }
  }