Java Code Examples for org.apache.olingo.odata2.api.edm.EdmEntitySet#getEntityType()

The following examples show how to use org.apache.olingo.odata2.api.edm.EdmEntitySet#getEntityType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EdmMock.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private static EdmNavigationProperty createNavigationProperty(final String name, final EdmMultiplicity multiplicity,
    final EdmEntitySet entitySet, final EdmEntitySet targetEntitySet) throws EdmException {
  EdmType navigationType = mock(EdmType.class);
  when(navigationType.getKind()).thenReturn(EdmTypeKind.ENTITY);

  EdmNavigationProperty navigationProperty = mock(EdmNavigationProperty.class);
  when(navigationProperty.getName()).thenReturn(name);
  EdmType type = targetEntitySet.getEntityType();
  when(navigationProperty.getType()).thenReturn(type);
  when(navigationProperty.getMultiplicity()).thenReturn(multiplicity);

  when(entitySet.getEntityType().getProperty(name)).thenReturn(navigationProperty);
  when(entitySet.getRelatedEntitySet(navigationProperty)).thenReturn(targetEntitySet);

  return navigationProperty;
}
 
Example 2
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private <T> ODataResponse writeEntry(final EdmEntitySet entitySet, final ExpandSelectTreeNode expandSelectTree,
    final T data, final String contentType) throws ODataException, EntityProviderException {
  final EdmEntityType entityType = entitySet.getEntityType();
  final Map<String, Object> values = getStructuralTypeValueMap(data, entityType);

  ODataContext context = getContext();
  EntityProviderWriteProperties writeProperties = EntityProviderWriteProperties
      .serviceRoot(context.getPathInfo().getServiceRoot())
      .expandSelectTree(expandSelectTree)
      .callbacks(getCallbacks(data, entityType))
      .build();

  final int timingHandle = context.startRuntimeMeasurement("EntityProvider", "writeEntry");

  final ODataResponse response = EntityProvider.writeEntry(contentType, entitySet, values, writeProperties);

  context.stopRuntimeMeasurement(timingHandle);

  return response;
}
 
Example 3
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse updateEntity(final PutMergePatchUriInfo uriInfo, final InputStream content,
    final String requestContentType, final boolean merge, final String contentType) throws ODataException {
  Object data = retrieveData(
      uriInfo.getStartEntitySet(),
      uriInfo.getKeyPredicates(),
      uriInfo.getFunctionImport(),
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      uriInfo.getNavigationSegments());

  if (!appliesFilter(data, uriInfo.getFilter())) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  final EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
  final EdmEntityType entityType = entitySet.getEntityType();
  final EntityProviderReadProperties properties = EntityProviderReadProperties.init()
      .mergeSemantic(merge)
      .addTypeMappings(getStructuralTypeTypeMap(data, entityType))
      .build();
  final ODataEntry entryValues = parseEntry(entitySet, content, requestContentType, properties);

  setStructuralTypeValuesFromMap(data, entityType, entryValues.getProperties(), merge);

  return ODataResponse.newBuilder().eTag(constructETag(entitySet, data)).build();
}
 
Example 4
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private <T> ODataResponse writeEntry(final EdmEntitySet entitySet, final ExpandSelectTreeNode expandSelectTree,
    final T data, final String contentType) throws ODataException, EntityProviderException {
  final EdmEntityType entityType = entitySet.getEntityType();
  final Map<String, Object> values = getStructuralTypeValueMap(data, entityType);

  ODataContext context = getContext();
  EntityProviderWriteProperties writeProperties = EntityProviderWriteProperties
      .serviceRoot(context.getPathInfo().getServiceRoot())
      .expandSelectTree(expandSelectTree)
      .callbacks(getCallbacks(data, entityType))
      .build();

  final int timingHandle = context.startRuntimeMeasurement("EntityProvider", "writeEntry");

  final ODataResponse response = EntityProvider.writeEntry(contentType, entitySet, values, writeProperties);

  context.stopRuntimeMeasurement(timingHandle);

  return response;
}
 
Example 5
Source File: AtomEntrySerializerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void serializeWithCustomSrcAndTypeAttributeOnRoom() throws Exception {
  AtomSerializerDeserializer ser = createAtomEntityProvider();
  Entity localRoomData = new Entity();
  for (Entry<String, Object> data : roomData.getProperties().entrySet()) {
    localRoomData.addProperty(data.getKey(), data.getValue());
  }
  String mediaResourceSourceKey = "~src";
  localRoomData.addProperty(mediaResourceSourceKey, "http://localhost:8080/images/image1");
  String mediaResourceMimeTypeKey = "~type";
  localRoomData.addProperty(mediaResourceMimeTypeKey, "image/jpeg");
  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  EdmEntityType roomType = roomsSet.getEntityType();
  EdmMapping mapping = mock(EdmMapping.class);
  when(roomType.getMapping()).thenReturn(mapping);
  when(mapping.getMediaResourceSourceKey()).thenReturn(mediaResourceSourceKey);
  when(mapping.getMediaResourceMimeTypeKey()).thenReturn(mediaResourceMimeTypeKey);
  localRoomData.setWriteProperties(DEFAULT_PROPERTIES);
  ODataResponse response = ser.writeEntry(roomsSet, localRoomData);
  String xmlString = verifyResponse(response);

  assertXpathNotExists(
      "/a:entry/a:link[@href=\"Rooms('1')/$value\" and" +
          " @rel=\"edit-media\" and @type=\"image/jpeg\"]", xmlString);
  assertXpathNotExists("/a:entry/a:content[@type=\"image/jpeg\"]", xmlString);
  assertXpathNotExists("/a:entry/a:content[@src=\"http://localhost:8080/images/image1\"]", xmlString);
}
 
Example 6
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private <T> String constructETag(final EdmEntitySet entitySet, final T data) throws ODataException {
  final EdmEntityType entityType = entitySet.getEntityType();
  String eTag = null;
  for (final String propertyName : entityType.getPropertyNames()) {
    final EdmProperty property = (EdmProperty) entityType.getProperty(propertyName);
    if (property.getFacets() != null && property.getFacets().getConcurrencyMode() == EdmConcurrencyMode.Fixed) {
      final EdmSimpleType type = (EdmSimpleType) property.getType();
      final String component = type.valueToString(valueAccess.getPropertyValue(data, property),
          EdmLiteralKind.DEFAULT, property.getFacets());
      eTag = eTag == null ? component : eTag + Edm.DELIMITER + component;
    }
  }
  return eTag == null ? null : "W/\"" + eTag + "\"";
}
 
Example 7
Source File: JsonEntryEntitySerializerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void serializeWithCustomSrcAndTypeAttributeOnRoom() throws Exception {
  Entity roomData = new Entity();
  roomData.addProperty("Id", "1");
  roomData.addProperty("Name", "Neu Schwanstein");
  roomData.addProperty("Seats", new Integer(20));
  roomData.addProperty("Version", new Integer(3));

  String mediaResourceSourceKey = "~src";
  roomData.addProperty(mediaResourceSourceKey, "http://localhost:8080/images/image1");
  String mediaResourceMimeTypeKey = "~type";
  roomData.addProperty(mediaResourceMimeTypeKey, "image/jpeg");
  roomData.setWriteProperties(EntitySerializerProperties.serviceRoot(URI.create(BASE_URI)).
      includeMetadata(true).build());

  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  EdmEntityType roomType = roomsSet.getEntityType();
  EdmMapping mapping = mock(EdmMapping.class);
  when(roomType.getMapping()).thenReturn(mapping);
  when(mapping.getMediaResourceSourceKey()).thenReturn(mediaResourceSourceKey);
  when(mapping.getMediaResourceMimeTypeKey()).thenReturn(mediaResourceMimeTypeKey);

  ODataResponse response = new JsonSerializerDeserializer().writeEntry(roomsSet, roomData);
  String jsonString = verifyResponse(response);
  Gson gson = new Gson();
  LinkedTreeMap<String, Object> jsonMap = gson.fromJson(jsonString, LinkedTreeMap.class);
  jsonMap = (LinkedTreeMap<String, Object>) jsonMap.get("__metadata");

  assertNull(jsonMap.get("media_src"));
  assertNull(jsonMap.get("content_type"));
  assertNull(jsonMap.get("edit_media"));
}
 
Example 8
Source File: JsonEntryEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void serializeWithCustomSrcAndTypeAttributeOnRoom() throws Exception {
  Map<String, Object> roomData = new HashMap<String, Object>();
  roomData.put("Id", "1");
  roomData.put("Name", "Neu Schwanstein");
  roomData.put("Seats", new Integer(20));
  roomData.put("Version", new Integer(3));

  String mediaResourceSourceKey = "~src";
  roomData.put(mediaResourceSourceKey, "http://localhost:8080/images/image1");
  String mediaResourceMimeTypeKey = "~type";
  roomData.put(mediaResourceMimeTypeKey, "image/jpeg");

  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  EdmEntityType roomType = roomsSet.getEntityType();
  EdmMapping mapping = mock(EdmMapping.class);
  when(roomType.getMapping()).thenReturn(mapping);
  when(mapping.getMediaResourceSourceKey()).thenReturn(mediaResourceSourceKey);
  when(mapping.getMediaResourceMimeTypeKey()).thenReturn(mediaResourceMimeTypeKey);

  ODataResponse response = new JsonEntityProvider().writeEntry(roomsSet, roomData, DEFAULT_PROPERTIES);
  String jsonString = verifyResponse(response);
  Gson gson = new Gson();
  LinkedTreeMap<String, Object> jsonMap = gson.fromJson(jsonString, LinkedTreeMap.class);
  jsonMap = (LinkedTreeMap<String, Object>) jsonMap.get("d");
  jsonMap = (LinkedTreeMap<String, Object>) jsonMap.get("__metadata");

  assertNull(jsonMap.get("media_src"));
  assertNull(jsonMap.get("content_type"));
  assertNull(jsonMap.get("edit_media"));
}
 
Example 9
Source File: JsonEntryEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void serializeWithCustomSrcAttributeOnRoom() throws Exception {
  Map<String, Object> roomData = new HashMap<String, Object>();
  roomData.put("Id", "1");
  roomData.put("Name", "Neu Schwanstein");
  roomData.put("Seats", new Integer(20));
  roomData.put("Version", new Integer(3));

  String mediaResourceSourceKey = "~src";
  roomData.put(mediaResourceSourceKey, "http://localhost:8080/images/image1");

  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  EdmEntityType roomType = roomsSet.getEntityType();
  EdmMapping mapping = mock(EdmMapping.class);
  when(roomType.getMapping()).thenReturn(mapping);
  when(mapping.getMediaResourceSourceKey()).thenReturn(mediaResourceSourceKey);

  ODataResponse response = new JsonEntityProvider().writeEntry(roomsSet, roomData, DEFAULT_PROPERTIES);
  String jsonString = verifyResponse(response);
  Gson gson = new Gson();
  LinkedTreeMap<String, Object> jsonMap = gson.fromJson(jsonString, LinkedTreeMap.class);
  jsonMap = (LinkedTreeMap<String, Object>) jsonMap.get("d");
  jsonMap = (LinkedTreeMap<String, Object>) jsonMap.get("__metadata");

  assertNull(jsonMap.get("media_src"));
  assertNull(jsonMap.get("content_type"));
  assertNull(jsonMap.get("edit_media"));
}
 
Example 10
Source File: JsonEntryEntitySerializerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void serializeWithCustomSrcAttributeOnRoom() throws Exception {
  Entity roomData = new Entity();
  roomData.addProperty("Id", "1");
  roomData.addProperty("Name", "Neu Schwanstein");
  roomData.addProperty("Seats", new Integer(20));
  roomData.addProperty("Version", new Integer(3));

  String mediaResourceSourceKey = "~src";
  roomData.addProperty(mediaResourceSourceKey, "http://localhost:8080/images/image1");
  roomData.setWriteProperties(EntitySerializerProperties.serviceRoot(URI.create(BASE_URI)).
      includeMetadata(true).build());

  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  EdmEntityType roomType = roomsSet.getEntityType();
  EdmMapping mapping = mock(EdmMapping.class);
  when(roomType.getMapping()).thenReturn(mapping);
  when(mapping.getMediaResourceSourceKey()).thenReturn(mediaResourceSourceKey);

  ODataResponse response = new JsonSerializerDeserializer().writeEntry(roomsSet, roomData);
  String jsonString = verifyResponse(response);
  Gson gson = new Gson();
  LinkedTreeMap<String, Object> jsonMap = gson.fromJson(jsonString, LinkedTreeMap.class);
  jsonMap = (LinkedTreeMap<String, Object>) jsonMap.get("__metadata");

  assertNull(jsonMap.get("media_src"));
  assertNull(jsonMap.get("content_type"));
  assertNull(jsonMap.get("edit_media"));
}
 
Example 11
Source File: UriParserImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void addNavigationSegment(final String keyPredicateName, final EdmNavigationProperty navigationProperty)
    throws UriSyntaxException, EdmException {
  final EdmEntitySet targetEntitySet = uriResult.getTargetEntitySet().getRelatedEntitySet(navigationProperty);
  final EdmEntityType targetEntityType = targetEntitySet.getEntityType();
  uriResult.setTargetEntitySet(targetEntitySet);
  uriResult.setTargetType(targetEntityType);

  NavigationSegmentImpl navigationSegment = new NavigationSegmentImpl();
  navigationSegment.setEntitySet(targetEntitySet);
  navigationSegment.setNavigationProperty(navigationProperty);
  if (keyPredicateName != null) {
    navigationSegment.setKeyPredicates(parseKey(keyPredicateName, targetEntityType));
  }
  uriResult.addNavigationSegment(navigationSegment);
}
 
Example 12
Source File: UriParserImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private void handleEntitySet(final EdmEntitySet entitySet, final String keyPredicate) throws UriSyntaxException,
    UriNotMatchingException, EdmException {
  final EdmEntityType entityType = entitySet.getEntityType();

  uriResult.setTargetType(entityType);
  uriResult.setTargetEntitySet(entitySet);

  if (keyPredicate == null) {
    if (pathSegments.isEmpty()) {
      uriResult.setUriType(UriType.URI1);
    } else {
      currentPathSegment = pathSegments.remove(0);
      checkCount();
      if (uriResult.isCount()) {
        uriResult.setUriType(UriType.URI15);
      } else {
        throw new UriSyntaxException(UriSyntaxException.ENTITYSETINSTEADOFENTITY.addContent(entitySet.getName()));
      }
    }
  } else {
    uriResult.setKeyPredicates(parseKey(keyPredicate, entityType));
    if (pathSegments.isEmpty()) {
      uriResult.setUriType(UriType.URI2);
    } else {
      handleNavigationPathOptions();
    }
  }
}
 
Example 13
Source File: ODataJPAResponseBuilderDefault.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataResponse build(final GetEntityLinkUriInfo resultsView, final Object jpaEntity,
    final String contentType) throws ODataNotFoundException,
    ODataJPARuntimeException {

  if (jpaEntity == null) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }
  EdmEntityType edmEntityType = null;
  ODataResponse odataResponse = null;

  try {

    EdmEntitySet entitySet = resultsView.getTargetEntitySet();
    edmEntityType = entitySet.getEntityType();
    Map<String, Object> edmPropertyValueMap = null;

    JPAEntityParser jpaResultParser = new JPAEntityParser();
    edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, edmEntityType.getKeyProperties());

    EntityProviderWriteProperties entryProperties =
        EntityProviderWriteProperties.serviceRoot(oDataJPAContext.getODataContext().getPathInfo().getServiceRoot())
            .build();

    ODataResponse response = EntityProvider.writeLink(contentType, entitySet, edmPropertyValueMap, entryProperties);

    odataResponse = ODataResponse.fromResponse(response).build();

  } catch (ODataException e) {
    throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);

  }

  return odataResponse;
}
 
Example 14
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private <T> String constructETag(final EdmEntitySet entitySet, final T data) throws ODataException {
  final EdmEntityType entityType = entitySet.getEntityType();
  String eTag = null;
  for (final String propertyName : entityType.getPropertyNames()) {
    final EdmProperty property = (EdmProperty) entityType.getProperty(propertyName);
    if (property.getFacets() != null && property.getFacets().getConcurrencyMode() == EdmConcurrencyMode.Fixed) {
      final EdmSimpleType type = (EdmSimpleType) property.getType();
      final String component = type.valueToString(valueAccess.getPropertyValue(data, property),
          EdmLiteralKind.DEFAULT, property.getFacets());
      eTag = eTag == null ? component : eTag + Edm.DELIMITER + component;
    }
  }
  return eTag == null ? null : "W/\"" + eTag + "\"";
}
 
Example 15
Source File: Processor.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public ODataResponse createEntityLink(PostUriInfo uri_info, InputStream content,
      String request_content_type, String content_type) throws ODataException
{
   // Target of the link to create
   //EdmEntitySet link_target_es = uri_info.getTargetEntitySet();

   // Gets the entityset containing the navigation link to `link_target_es`
   EdmEntitySet  target_es = getLinkFromES(new AdaptableUriInfo(uri_info));
   EdmEntityType target_et = target_es.getEntityType();

   // Check abilities and permissions
   if (!target_es.getName().equals(Model.USER.getName()))
   {
      throw new ODataException("EntitySet " + target_et.getName() + " cannot create links");
   }

   fr.gael.dhus.database.object.User current_user = Security.getCurrentUser();

   AbstractEntitySet es = Model.getEntitySet(target_es.getName());
   if (!es.isAuthorized(current_user))
   {
      throw new NotAllowedException();
   }

   // Gets the affected entity
   String key = uri_info.getKeyPredicates().get(0).getLiteral();
   User user = new User(key);

   // Reads and parses the link
   String link = EntityProvider.readLink(content_type, target_es, content);
   link = link.trim(); // Olingo does not trim... resulting in a parse exception
   try
   {
      link = (new URI(link)).getPath();
      if (link == null || link.isEmpty())
      {
         throw new ExpectedException("Invalid link, path is empty");
      }
      // Gets the OData resource path
      Matcher matcher = RESOURCE_PATH_EXTRACTOR.matcher(link);
      if (matcher.find())
      {
         link = matcher.group(1);
      }
      else
      {
         throw new ExpectedException("Invalid link, path is malformed");
      }
   }
   catch (URISyntaxException e)
   {
      throw new ExpectedException(e.getMessage());
   }

   // Use Olingo's UriParser
   UriParser urip = RuntimeDelegate.getUriParser(getContext().getService().getEntityDataModel());
   List<PathSegment> path_segments = new ArrayList<>();
   StringTokenizer st = new StringTokenizer(link, "/");
   while (st.hasMoreTokens())
   {
      path_segments.add(UriParser.createPathSegment(st.nextToken(), null));
   }
   @SuppressWarnings("unchecked")
   UriInfo uilink = urip.parse(path_segments, Collections.EMPTY_MAP);

   // Creates link
   user.createLink(uilink);

   // Empty answer with HTTP code 204: no content
   return ODataResponse.newBuilder().build();
}
 
Example 16
Source File: ODataJPAResponseBuilderDefault.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public ODataResponse build(final GetEntitySetLinksUriInfo resultsView, final List<Object> jpaEntities,
    final String contentType) throws ODataJPARuntimeException {
  EdmEntityType edmEntityType = null;
  ODataResponse odataResponse = null;

  try {

    EdmEntitySet entitySet = resultsView.getTargetEntitySet();
    edmEntityType = entitySet.getEntityType();
    List<EdmProperty> keyProperties = edmEntityType.getKeyProperties();

    List<Map<String, Object>> edmEntityList = new ArrayList<Map<String, Object>>();
    Map<String, Object> edmPropertyValueMap = null;
    JPAEntityParser jpaResultParser = new JPAEntityParser();

    for (Object jpaEntity : jpaEntities) {
      edmPropertyValueMap = jpaResultParser.parse2EdmPropertyValueMap(jpaEntity, keyProperties);
      edmEntityList.add(edmPropertyValueMap);
    }

    Integer count = null;
    if (resultsView.getInlineCount() != null) {
      if ((resultsView.getSkip() != null || resultsView.getTop() != null)) {
        // when $skip and/or $top is present with $inlinecount
        count = getInlineCountForNonFilterQueryLinks(edmEntityList, resultsView);
      } else {
        // In all other cases
        count = resultsView.getInlineCount() == InlineCount.ALLPAGES ? edmEntityList.size() : null;
      }
    }

    ODataContext context = oDataJPAContext.getODataContext();
    EntityProviderWriteProperties entryProperties =
        EntityProviderWriteProperties.serviceRoot(context.getPathInfo().getServiceRoot()).inlineCountType(
            resultsView.getInlineCount()).inlineCount(count).build();

    odataResponse = EntityProvider.writeLinks(contentType, entitySet, edmEntityList, entryProperties);

    odataResponse = ODataResponse.fromResponse(odataResponse).build();

  } catch (ODataException e) {
    throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL.addContent(e.getMessage()), e);
  }

  return odataResponse;

}
 
Example 17
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public ODataResponse readEntitySet(final GetEntitySetUriInfo uriInfo, final String contentType)
    throws ODataException {
  ArrayList<Object> data = new ArrayList<Object>();
  try {
    data.addAll((List<?>) retrieveData(
        uriInfo.getStartEntitySet(),
        uriInfo.getKeyPredicates(),
        uriInfo.getFunctionImport(),
        mapFunctionParameters(uriInfo.getFunctionImportParameters()),
        uriInfo.getNavigationSegments()));
  } catch (final ODataNotFoundException e) {
    data.clear();
  }

  final EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
  final InlineCount inlineCountType = uriInfo.getInlineCount();
  final Integer count = applySystemQueryOptions(
      entitySet,
      data,
      uriInfo.getFilter(),
      inlineCountType,
      uriInfo.getOrderBy(),
      uriInfo.getSkipToken(),
      uriInfo.getSkip(),
      uriInfo.getTop());

  ODataContext context = getContext();
  String nextLink = null;

  // Limit the number of returned entities and provide a "next" link
  // if there are further entities.
  // Almost all system query options in the current request must be carried
  // over to the URI for the "next" link, with the exception of $skiptoken
  // and $skip.
  if (data.size() > SERVER_PAGING_SIZE) {
    if (uriInfo.getOrderBy() == null
        && uriInfo.getSkipToken() == null
        && uriInfo.getSkip() == null
        && uriInfo.getTop() == null) {
      sortInDefaultOrder(entitySet, data);
    }

    nextLink = context.getPathInfo().getServiceRoot().relativize(context.getPathInfo().getRequestUri()).toString();
    nextLink = percentEncodeNextLink(nextLink);

    nextLink += (nextLink.contains("?") ? "&" : "?")
        + "$skiptoken=" + getSkipToken(entitySet, data.get(SERVER_PAGING_SIZE));

    while (data.size() > SERVER_PAGING_SIZE) {
      data.remove(SERVER_PAGING_SIZE);
    }
  }

  final EdmEntityType entityType = entitySet.getEntityType();
  List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();
  for (final Object entryData : data) {
    values.add(getStructuralTypeValueMap(entryData, entityType));
  }

  final EntityProviderWriteProperties feedProperties = EntityProviderWriteProperties
      .serviceRoot(context.getPathInfo().getServiceRoot())
      .inlineCountType(inlineCountType)
      .inlineCount(count)
      .expandSelectTree(UriParser.createExpandSelectTree(uriInfo.getSelect(), uriInfo.getExpand()))
      .callbacks(getCallbacks(data, entityType))
      .nextLink(nextLink)
      .build();

  final int timingHandle = context.startRuntimeMeasurement("EntityProvider", "writeFeed");
  final ODataResponse response = EntityProvider.writeFeed(contentType, entitySet, values, feedProperties);

  context.stopRuntimeMeasurement(timingHandle);

  return ODataResponse.fromResponse(response).build();
}
 
Example 18
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public ODataResponse createEntity(final PostUriInfo uriInfo, final InputStream content,
    final String requestContentType, final String contentType) throws ODataException {
  final EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
  final EdmEntityType entityType = entitySet.getEntityType();

  Object data = dataSource.newDataObject(entitySet);
  ExpandSelectTreeNode expandSelectTree = null;

  if (entityType.hasStream()) {
    dataSource.createData(entitySet, data);
    dataSource.writeBinaryData(entitySet, data,
        new BinaryData(EntityProvider.readBinary(content), requestContentType));

  } else {
    final EntityProviderReadProperties properties = EntityProviderReadProperties.init()
        .mergeSemantic(false)
        .addTypeMappings(getStructuralTypeTypeMap(data, entityType))
        .build();
    final ODataEntry entryValues = parseEntry(entitySet, content, requestContentType, properties);

    setStructuralTypeValuesFromMap(data, entityType, entryValues.getProperties(), false);

    dataSource.createData(entitySet, data);

    createInlinedEntities(entitySet, data, entryValues);

    expandSelectTree = entryValues.getExpandSelectTree();
  }

  // Link back to the entity the target entity set is related to, if any.
  final List<NavigationSegment> navigationSegments = uriInfo.getNavigationSegments();
  if (!navigationSegments.isEmpty()) {
    final List<NavigationSegment> previousSegments = navigationSegments.subList(0, navigationSegments.size() - 1);
    final Object sourceData = retrieveData(
        uriInfo.getStartEntitySet(),
        uriInfo.getKeyPredicates(),
        uriInfo.getFunctionImport(),
        mapFunctionParameters(uriInfo.getFunctionImportParameters()),
        previousSegments);
    final EdmEntitySet previousEntitySet = previousSegments.isEmpty() ?
        uriInfo.getStartEntitySet() : previousSegments.get(previousSegments.size() - 1).getEntitySet();
    dataSource.writeRelation(previousEntitySet, sourceData, entitySet, getStructuralTypeValueMap(data, entityType));
  }

  return ODataResponse.fromResponse(writeEntry(uriInfo.getTargetEntitySet(), expandSelectTree, data, contentType))
      .eTag(constructETag(entitySet, data)).build();
}
 
Example 19
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public ODataResponse createEntity(final PostUriInfo uriInfo, final InputStream content,
    final String requestContentType, final String contentType) throws ODataException {
  final EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
  final EdmEntityType entityType = entitySet.getEntityType();

  Object data = dataSource.newDataObject(entitySet);
  ExpandSelectTreeNode expandSelectTree = null;

  if (entityType.hasStream()) {
    dataSource.createData(entitySet, data);
    dataSource.writeBinaryData(entitySet, data,
        new BinaryData(EntityProvider.readBinary(content), requestContentType));

  } else {
    final EntityProviderReadProperties properties = EntityProviderReadProperties.init()
        .mergeSemantic(false)
        .addTypeMappings(getStructuralTypeTypeMap(data, entityType))
        .build();
    final ODataEntry entryValues = parseEntry(entitySet, content, requestContentType, properties);

    setStructuralTypeValuesFromMap(data, entityType, entryValues.getProperties(), false);

    dataSource.createData(entitySet, data);

    createInlinedEntities(entitySet, data, entryValues);

    expandSelectTree = entryValues.getExpandSelectTree();
  }

  // Link back to the entity the target entity set is related to, if any.
  final List<NavigationSegment> navigationSegments = uriInfo.getNavigationSegments();
  if (!navigationSegments.isEmpty()) {
    final List<NavigationSegment> previousSegments = navigationSegments.subList(0, navigationSegments.size() - 1);
    final Object sourceData = retrieveData(
        uriInfo.getStartEntitySet(),
        uriInfo.getKeyPredicates(),
        uriInfo.getFunctionImport(),
        mapFunctionParameters(uriInfo.getFunctionImportParameters()),
        previousSegments);
    final EdmEntitySet previousEntitySet = previousSegments.isEmpty() ?
        uriInfo.getStartEntitySet() : previousSegments.get(previousSegments.size() - 1).getEntitySet();
    dataSource.writeRelation(previousEntitySet, sourceData, entitySet, getStructuralTypeValueMap(data, entityType));
  }

  return ODataResponse.fromResponse(writeEntry(uriInfo.getTargetEntitySet(), expandSelectTree, data, contentType))
      .eTag(constructETag(entitySet, data)).build();
}
 
Example 20
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public ODataResponse readEntitySet(final GetEntitySetUriInfo uriInfo, final String contentType)
    throws ODataException {
  ArrayList<Object> data = new ArrayList<Object>();
  try {
    data.addAll((List<?>) retrieveData(
        uriInfo.getStartEntitySet(),
        uriInfo.getKeyPredicates(),
        uriInfo.getFunctionImport(),
        mapFunctionParameters(uriInfo.getFunctionImportParameters()),
        uriInfo.getNavigationSegments()));
  } catch (final ODataNotFoundException e) {
    data.clear();
  }

  final EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
  final InlineCount inlineCountType = uriInfo.getInlineCount();
  final Integer count = applySystemQueryOptions(
      entitySet,
      data,
      uriInfo.getFilter(),
      inlineCountType,
      uriInfo.getOrderBy(),
      uriInfo.getSkipToken(),
      uriInfo.getSkip(),
      uriInfo.getTop());

  ODataContext context = getContext();
  String nextLink = null;

  // Limit the number of returned entities and provide a "next" link
  // if there are further entities.
  // Almost all system query options in the current request must be carried
  // over to the URI for the "next" link, with the exception of $skiptoken
  // and $skip.
  if (data.size() > SERVER_PAGING_SIZE) {
    if (uriInfo.getOrderBy() == null
        && uriInfo.getSkipToken() == null
        && uriInfo.getSkip() == null
        && uriInfo.getTop() == null) {
      sortInDefaultOrder(entitySet, data);
    }

    nextLink = context.getPathInfo().getServiceRoot().relativize(context.getPathInfo().getRequestUri()).toString();
    nextLink = percentEncodeNextLink(nextLink);
    nextLink += (nextLink.contains("?") ? "&" : "?")
        + "$skiptoken=" + getSkipToken(entitySet, data.get(SERVER_PAGING_SIZE));

    while (data.size() > SERVER_PAGING_SIZE) {
      data.remove(SERVER_PAGING_SIZE);
    }
  }

  final EdmEntityType entityType = entitySet.getEntityType();
  List<Map<String, Object>> values = new ArrayList<Map<String, Object>>();
  for (final Object entryData : data) {
    values.add(getStructuralTypeValueMap(entryData, entityType));
  }

  final EntityProviderWriteProperties feedProperties = EntityProviderWriteProperties
      .serviceRoot(context.getPathInfo().getServiceRoot())
      .inlineCountType(inlineCountType)
      .inlineCount(count)
      .expandSelectTree(UriParser.createExpandSelectTree(uriInfo.getSelect(), uriInfo.getExpand()))
      .callbacks(getCallbacks(data, entityType))
      .nextLink(nextLink)
      .build();

  final int timingHandle = context.startRuntimeMeasurement("EntityProvider", "writeFeed");
  final ODataResponse response = EntityProvider.writeFeed(contentType, entitySet, values, feedProperties);

  context.stopRuntimeMeasurement(timingHandle);

  return ODataResponse.fromResponse(response).build();
}