public ODataResponse readEntity(GetEntityUriInfo uriInfo, String contentType)
      throws ODataException {
    LOG.debug("Reading Entity: " + uriInfo);

    if (uriInfo.getNavigationSegments().size() == 0) {
      EdmEntitySet entitySet = uriInfo.getStartEntitySet();

      if (ENTITY_SET_NAME_CARS.equals(entitySet.getName())) {
        int id = getKeyValue(uriInfo.getKeyPredicates().get(0));
        Map<String, Object> data = dataStore.getCar(id);

        if (data != null) {
          URI serviceRoot = getContext().getPathInfo().getServiceRoot();
          ODataEntityProviderPropertiesBuilder propertiesBuilder =
              EntityProviderWriteProperties.serviceRoot(serviceRoot);
          expandManufacturer(uriInfo, serviceRoot, propertiesBuilder);
          return EntityProvider.writeEntry(contentType, entitySet, data, propertiesBuilder.build());
        }
      } else if (ENTITY_SET_NAME_MANUFACTURERS.equals(entitySet.getName())) {
        int id = getKeyValue(uriInfo.getKeyPredicates().get(0));
        Map<String, Object> data = dataStore.getManufacturer(id);

        if (data != null) {
          URI serviceRoot = getContext().getPathInfo().getServiceRoot();
          ODataEntityProviderPropertiesBuilder propertiesBuilder =
              EntityProviderWriteProperties.serviceRoot(serviceRoot);
          expandCars(uriInfo, serviceRoot, propertiesBuilder);
          return EntityProvider.writeEntry(contentType, entitySet, data, propertiesBuilder.build());
        }
      }

      throw new ODataNotFoundException(ODataNotFoundException.ENTITY);

    } else if (uriInfo.getNavigationSegments().size() == 1) {
      // navigation first level, simplified example for illustration
      // purposes only
      EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
      if (ENTITY_SET_NAME_MANUFACTURERS.equals(entitySet.getName())) {
        int carKey = getKeyValue(uriInfo.getKeyPredicates().get(0));
        return EntityProvider.writeEntry(
            contentType,
            uriInfo.getTargetEntitySet(),
            dataStore.getManufacturer(carKey),
            EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot())
                .build());
      }

      throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
    }

    throw new ODataNotImplementedException();
  }
  public ODataResponse readEntitySet(GetEntitySetUriInfo uriInfo, String contentType)
      throws ODataException {
    LOG.debug("Reading EdmEntitySet: " + uriInfo);

    EdmEntitySet entitySet;

    if (uriInfo.getNavigationSegments().size() == 0) {
      entitySet = uriInfo.getStartEntitySet();

      if (ENTITY_SET_NAME_CARS.equals(entitySet.getName())) {
        return EntityProvider.writeFeed(
            contentType,
            entitySet,
            dataStore.getCars(),
            EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot())
                .build());
      } else if (ENTITY_SET_NAME_MANUFACTURERS.equals(entitySet.getName())) {
        return EntityProvider.writeFeed(
            contentType,
            entitySet,
            dataStore.getManufacturers(),
            EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot())
                .build());
      }

      throw new ODataNotFoundException(ODataNotFoundException.ENTITY);

    } else if (uriInfo.getNavigationSegments().size() == 1) {
      // navigation first level, simplified example for illustration
      // purposes only
      entitySet = uriInfo.getTargetEntitySet();

      if (ENTITY_SET_NAME_CARS.equals(entitySet.getName())) {
        int manufacturerKey = getKeyValue(uriInfo.getKeyPredicates().get(0));

        List<Map<String, Object>> cars = new ArrayList<Map<String, Object>>();
        cars.add(dataStore.getCar(manufacturerKey));

        return EntityProvider.writeFeed(
            contentType,
            entitySet,
            cars,
            EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot())
                .build());
      }

      throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
    }

    throw new ODataNotImplementedException();
  }
 @Override
 public WriteEntryCallbackResult retrieveEntryResult(final WriteEntryCallbackContext context) {
   WriteEntryCallbackResult result = new WriteEntryCallbackResult();
   try {
     if ("Employees".equals(context.getSourceEntitySet().getName())) {
       if ("ne_Room".equals(context.getNavigationProperty().getName())) {
         HashMap<String, ODataCallback> callbacks = new HashMap<String, ODataCallback>();
         for (String navPropName :
             context
                 .getSourceEntitySet()
                 .getRelatedEntitySet(context.getNavigationProperty())
                 .getEntityType()
                 .getNavigationPropertyNames()) {
           callbacks.put(navPropName, this);
         }
         EntityProviderWriteProperties inlineProperties =
             EntityProviderWriteProperties.serviceRoot(baseUri)
                 .callbacks(callbacks)
                 .expandSelectTree(context.getCurrentExpandSelectTreeNode())
                 .build();
         result.setEntryData(dataProvider.getRoomData());
         result.setInlineProperties(inlineProperties);
       } else if ("ne_Team".equals(context.getNavigationProperty().getName())) {
         result.setEntryData(null);
       }
     }
   } catch (EdmException e) {
     throw new ODataRuntimeException("EdmException:", e);
   }
   return result;
 }
  @Override
  public WriteFeedCallbackResult retrieveFeedResult(final WriteFeedCallbackContext context) {
    WriteFeedCallbackResult result = new WriteFeedCallbackResult();
    try {
      if ("Rooms".equals(context.getSourceEntitySet().getName())) {
        if ("nr_Employees".equals(context.getNavigationProperty().getName())) {
          HashMap<String, ODataCallback> callbacks = new HashMap<String, ODataCallback>();
          for (String navPropName :
              context
                  .getSourceEntitySet()
                  .getRelatedEntitySet(context.getNavigationProperty())
                  .getEntityType()
                  .getNavigationPropertyNames()) {
            callbacks.put(navPropName, this);
          }
          EntityProviderWriteProperties inlineProperties =
              EntityProviderWriteProperties.serviceRoot(baseUri)
                  .callbacks(callbacks)
                  .expandSelectTree(context.getCurrentExpandSelectTreeNode())
                  .selfLink(context.getSelfLink())
                  .validatingFacets(context.getCurrentWriteProperties().isValidatingFacets())
                  .build();

          result.setFeedData(dataProvider.getEmployeesData());
          result.setInlineProperties(inlineProperties);
        }
      } else if ("Buildings".equals(context.getSourceEntitySet().getName())) {
        EntityProviderWriteProperties inlineProperties =
            EntityProviderWriteProperties.serviceRoot(baseUri)
                .expandSelectTree(context.getCurrentExpandSelectTreeNode())
                .validatingFacets(context.getCurrentWriteProperties().isValidatingFacets())
                .selfLink(context.getSelfLink())
                .build();
        List<Map<String, Object>> emptyData = new ArrayList<Map<String, Object>>();
        result.setFeedData(emptyData);
        result.setInlineProperties(inlineProperties);
      }
    } catch (EdmException e) {
      throw new ODataRuntimeException("EdmException", e);
    }
    return result;
  }
  @Override
  public ODataResponse createEntity(
      PostUriInfo uriInfo, InputStream content, String requestContentType, String contentType)
      throws ODataException {
    LOG.debug("Creating Entity: " + uriInfo);
    // No support for creating and linking a new entry
    if (uriInfo.getNavigationSegments().size() > 0) {
      throw new ODataNotImplementedException();
    }

    // No support for media resources
    if (uriInfo.getStartEntitySet().getEntityType().hasStream()) {
      throw new ODataNotImplementedException();
    }

    EntityProviderReadProperties properties =
        EntityProviderReadProperties.init().mergeSemantic(false).build();

    ODataEntry entry =
        EntityProvider.readEntry(
            requestContentType, uriInfo.getStartEntitySet(), content, properties);
    // if something goes wrong in deserialization this is managed via the
    // ExceptionMapper
    // no need for an application to do exception handling here an convert
    // the exceptions in HTTP exceptions

    Map<String, Object> data = entry.getProperties();
    // now one can use the data to create the entry in the backend ...
    // retrieve the key value after creation, if the key is generated by the
    // server

    // update the data accordingly
    data.put("Id", Integer.valueOf(887788675));

    // serialize the entry, Location header is set by OData Library
    return EntityProvider.writeEntry(
        contentType,
        uriInfo.getStartEntitySet(),
        entry.getProperties(),
        EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot())
            .build());
  }
Exemple #6
0
  private ODataResponse writeContent(Edm edm, UriInfoWithType uriInfo, Object content)
      throws ODataApplicationException, EdmException, EntityProviderException, URISyntaxException,
          IOException {

    String responseContentType = getContentType();
    ODataResponse response;

    switch (uriInfo.getUriType()) {
      case URI4:
      case URI5:
        // simple property
        final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath();
        final EdmProperty simpleProperty = simplePropertyPath.get(simplePropertyPath.size() - 1);
        responseContentType = simpleProperty.getMimeType();
        if (uriInfo.isValue()) {
          response = EntityProvider.writePropertyValue(simpleProperty, content);
          responseContentType = TEXT_PLAIN_WITH_CS_UTF_8.toString();
        } else {
          response = EntityProvider.writeProperty(getContentType(), simpleProperty, content);
        }
        break;

      case URI3:
        // complex property
        final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath();
        final EdmProperty complexProperty = complexPropertyPath.get(complexPropertyPath.size() - 1);
        response = EntityProvider.writeProperty(responseContentType, complexProperty, content);
        break;

      case URI7A:
        // $links with 0..1 cardinality property
        final EdmEntitySet targetLinkEntitySet = uriInfo.getTargetEntitySet();
        EntityProviderWriteProperties linkProperties =
            EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build();
        @SuppressWarnings("unchecked")
        final Map<String, Object> linkMap = (Map<String, Object>) content;
        response =
            EntityProvider.writeLink(
                responseContentType, targetLinkEntitySet, linkMap, linkProperties);
        break;

      case URI7B:
        // $links with * cardinality property
        final EdmEntitySet targetLinksEntitySet = uriInfo.getTargetEntitySet();
        EntityProviderWriteProperties linksProperties =
            EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build();
        @SuppressWarnings("unchecked")
        final List<Map<String, Object>> linksMap = (List<Map<String, Object>>) content;
        response =
            EntityProvider.writeLinks(
                responseContentType, targetLinksEntitySet, linksMap, linksProperties);
        break;

      case URI1:
      case URI2:
      case URI6A:
      case URI6B:
        // Entity
        final EdmEntitySet targetEntitySet = uriInfo.getTargetEntitySet();
        EntityProviderWriteProperties properties =
            EntityProviderWriteProperties.serviceRoot(new URI(serviceUri + SEPARATOR)).build();
        @SuppressWarnings("unchecked")
        final Map<String, Object> objectMap = (Map<String, Object>) content;
        response =
            EntityProvider.writeEntry(responseContentType, targetEntitySet, objectMap, properties);
        break;

      case URI9:
        // $batch
        @SuppressWarnings("unchecked")
        final List<Olingo2BatchRequest> batchParts = (List<Olingo2BatchRequest>) content;
        response = parseBatchRequest(edm, batchParts);
        break;

      default:
        // notify exception and return!!!
        throw new ODataApplicationException(
            "Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH);
    }

    return response.getContentHeader() != null
        ? response
        : ODataResponse.fromResponse(response).contentHeader(responseContentType).build();
  }