Java Code Examples for org.apache.olingo.odata2.api.ep.EntityProvider#writeEntry()

The following examples show how to use org.apache.olingo.odata2.api.ep.EntityProvider#writeEntry() . 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: CMODataAbapClient.java    From devops-cm-client with Apache License 2.0 7 votes vote down vote up
public void updateTransport(Transport transport) throws IOException, URISyntaxException, ODataException, UnexpectedHttpResponseException
{
  Edm edm = getEntityDataModel();
  try (CloseableHttpClient client = clientFactory.createClient()) {
    
    HttpPut put = requestBuilder.updateTransport(transport.getTransportID());
    put.setHeader("x-csrf-token", getCSRFToken());
    EdmEntityContainer entityContainer = edm.getDefaultEntityContainer();
    EdmEntitySet entitySet = entityContainer.getEntitySet(TransportRequestBuilder.getEntityKey());
    URI rootUri = new URI(endpoint.toASCIIString() + "/");
    EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(rootUri).build();
    ODataResponse response = null;
    try {
        response = EntityProvider.writeEntry(put.getHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue(), entitySet,transport.getValueMap(), properties);
        put.setEntity(EntityBuilder.create().setStream(response.getEntityAsStream()).build());
        try (CloseableHttpResponse httpResponse = client.execute(put)) {
            hasStatusOrFail(httpResponse, SC_OK, HttpStatus.SC_NO_CONTENT);
        }
    }  finally {
        if(response != null) response.close();
    }
  }
}
 
Example 2
Source File: CMODataAbapClient.java    From devops-cm-client with Apache License 2.0 6 votes vote down vote up
public Transport createTransport(Map<String, Object> transport) throws IOException, URISyntaxException, ODataException, UnexpectedHttpResponseException
{
  try (CloseableHttpClient client = clientFactory.createClient()) {
    HttpPost post = requestBuilder.createTransport();
    post.setHeader("x-csrf-token", getCSRFToken());
    EdmEntityContainer entityContainer = getEntityDataModel().getDefaultEntityContainer();
    EdmEntitySet entitySet = entityContainer.getEntitySet(TransportRequestBuilder.getEntityKey());
    URI rootUri = new URI(endpoint.toASCIIString() + "/");
    EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(rootUri).build();
    ODataResponse marshaller = null;

    try {
      marshaller = EntityProvider.writeEntry(post.getHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue(), entitySet, transport, properties);
      post.setEntity(EntityBuilder.create().setStream(marshaller.getEntityAsStream()).build());

      try(CloseableHttpResponse httpResponse = client.execute(post)) {
        hasStatusOrFail(httpResponse, SC_CREATED);
        return getTransport(httpResponse);
      }
    } finally {
        if(marshaller != null) marshaller.close();
    }
  }
}
 
Example 3
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 4
Source File: ExpandSelectProducerWithBuilderTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void selectOnlyProperties() throws Exception {
  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  List<String> selectedProperties = new ArrayList<String>(roomData.keySet());

  ExpandSelectTreeNode expandSelectTree =
      ExpandSelectTreeNode.entitySet(roomsSet).selectedProperties(selectedProperties).build();

  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).expandSelectTree(expandSelectTree).build();
  ODataResponse entry = EntityProvider.writeEntry("application/xml", roomsSet, roomData, properties);

  String xml = StringHelper.inputStreamToString((InputStream) entry.getEntity());
  assertXpathExists("/a:entry/a:content/m:properties", xml);
  assertXpathNotExists("/a:entry/a:link[@type]", xml);
}
 
Example 5
Source File: ExpandSelectProducerWithBuilderTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void selectOnlyLinks() throws Exception {
  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  List<String> selectedNavigationProperties = new ArrayList<String>();
  selectedNavigationProperties.add("nr_Building");
  selectedNavigationProperties.add("nr_Employees");

  ExpandSelectTreeNode expandSelectTree =
      ExpandSelectTreeNode.entitySet(roomsSet).selectedLinks(selectedNavigationProperties).build();

  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).expandSelectTree(expandSelectTree).build();
  ODataResponse entry = EntityProvider.writeEntry("application/xml", roomsSet, roomData, properties);

  String xml = StringHelper.inputStreamToString((InputStream) entry.getEntity());
  assertXpathNotExists("/a:entry/a:content/m:properties", xml);
  assertXpathExists("/a:entry/a:link[@type]", xml);
}
 
Example 6
Source File: ExpandSelectProducerWithBuilderTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void selectIdAndBuildingLink() throws Exception {
  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  List<String> selectedNavigationProperties = new ArrayList<String>();
  selectedNavigationProperties.add("nr_Building");

  List<String> selectedProperties = new ArrayList<String>();
  selectedProperties.add("Id");

  ExpandSelectTreeNode expandSelectTree =
      ExpandSelectTreeNode.entitySet(roomsSet).selectedProperties(selectedProperties).selectedLinks(
          selectedNavigationProperties).build();

  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).expandSelectTree(expandSelectTree).build();
  ODataResponse entry = EntityProvider.writeEntry("application/xml", roomsSet, roomData, properties);

  String xml = StringHelper.inputStreamToString((InputStream) entry.getEntity());
  assertXpathExists("/a:entry/a:content/m:properties", xml);
  assertXpathExists("/a:entry/a:link[@type]", xml);
}
 
Example 7
Source File: ExpandSelectProducerWithBuilderTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void expandBuilding() throws Exception {
  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  List<String> expandedNavigationProperties = new ArrayList<String>();
  expandedNavigationProperties.add("nr_Building");

  ExpandSelectTreeNode expandSelectTree =
      ExpandSelectTreeNode.entitySet(roomsSet).expandedLinks(expandedNavigationProperties).build();

  Map<String, ODataCallback> callbacks = new HashMap<String, ODataCallback>();
  callbacks.put("nr_Building", new LocalCallback());
  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).callbacks(callbacks).expandSelectTree(
          expandSelectTree).build();
  ODataResponse entry = EntityProvider.writeEntry("application/xml", roomsSet, roomData, properties);

  String xml = StringHelper.inputStreamToString((InputStream) entry.getEntity());
  assertXpathExists("/a:entry/a:content/m:properties", xml);
  assertXpathExists("/a:entry/a:link[@type]/m:inline", xml);
}
 
Example 8
Source File: ExpandSelectProducerWithBuilderTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void expandBuildingAndSelectIdFromRoom() throws Exception {
  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  List<String> expandedNavigationProperties = new ArrayList<String>();
  expandedNavigationProperties.add("nr_Building");

  List<String> selectedProperties = new ArrayList<String>();
  selectedProperties.add("Id");

  ExpandSelectTreeNode expandSelectTree =
      ExpandSelectTreeNode.entitySet(roomsSet).selectedProperties(selectedProperties).expandedLinks(
          expandedNavigationProperties).build();

  Map<String, ODataCallback> callbacks = new HashMap<String, ODataCallback>();
  callbacks.put("nr_Building", new LocalCallback());
  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).callbacks(callbacks).expandSelectTree(
          expandSelectTree).build();
  ODataResponse entry = EntityProvider.writeEntry("application/xml", roomsSet, roomData, properties);

  String xml = StringHelper.inputStreamToString((InputStream) entry.getEntity());
  assertXpathExists("/a:entry/a:content/m:properties/d:Id", xml);
  assertXpathNotExists("/a:entry/a:content/m:properties/d:Name", xml);
  assertXpathExists("/a:entry/a:link[@type]/m:inline", xml);
}
 
Example 9
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 10
Source File: MapProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse readEntity(final GetEntityUriInfo uriInfo, final String contentType) throws ODataException {
  final EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot()).build();

  // query
  final String mappedKeyName =
      (String) uriInfo.getTargetEntitySet().getEntityType().getKeyProperties().get(0).getMapping().getObject();
  final String keyValue = uriInfo.getKeyPredicates().get(0).getLiteral();
  final int index = indexOf(mappedKeyName, keyValue);
  if ((index < 0) || (index > records.size())) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY.addContent(keyValue));
  }
  final HashMap<String, String> record = records.get(index);

  final HashMap<String, Object> data = new HashMap<String, Object>();
  for (final String pName : uriInfo.getTargetEntitySet().getEntityType().getPropertyNames()) {
    final EdmProperty property = (EdmProperty) uriInfo.getTargetEntitySet().getEntityType().getProperty(pName);
    final String mappedPropertyName = (String) property.getMapping().getObject();
    data.put(pName, record.get(mappedPropertyName));
  }

  final ODataResponse response =
      EntityProvider.writeEntry(contentType, uriInfo.getTargetEntitySet(), data, properties);
  return response;
}
 
Example 11
Source File: InvalidDataInScenarioTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse readEntity(GetEntityUriInfo uriInfo, String contentType) throws ODataException {
  HashMap<String, Object> data = new HashMap<String, Object>();

  if ("Employees".equals(uriInfo.getTargetEntitySet().getName())) {
    if ("2".equals(uriInfo.getKeyPredicates().get(0).getLiteral())) {
      data.put("EmployeeId", "1");
      data.put("TeamId", "420");
    }

    ODataContext context = getContext();
    EntityProviderWriteProperties writeProperties =
        EntityProviderWriteProperties.serviceRoot(context.getPathInfo().getServiceRoot()).build();

    return EntityProvider.writeEntry(contentType, uriInfo.getTargetEntitySet(), data, writeProperties);
  } else {
    throw new ODataApplicationException("Wrong testcall", Locale.getDefault(), HttpStatusCodes.NOT_IMPLEMENTED);
  }
}
 
Example 12
Source File: ExpandSelectProducerWithBuilderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void customExpandBuildingAndSelectIdFromCustomNode() throws Exception {
  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");

  List<String> selectedPropertiesAtCustomProperties = new ArrayList<String>();
  selectedPropertiesAtCustomProperties.add("Id");
  EdmEntitySet buildingsSet =
      roomsSet.getRelatedEntitySet((EdmNavigationProperty) roomsSet.getEntityType().getProperty("nr_Building"));
  ExpandSelectTreeNode customNode =
      ExpandSelectTreeNode.entitySet(buildingsSet).selectedProperties(selectedPropertiesAtCustomProperties).build();

  ExpandSelectTreeNode expandSelectTree =
      ExpandSelectTreeNode.entitySet(roomsSet).customExpandedLink("nr_Building", customNode).build();

  Map<String, ODataCallback> callbacks = new HashMap<String, ODataCallback>();
  callbacks.put("nr_Building", new LocalCallback());
  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).callbacks(callbacks).expandSelectTree(
          expandSelectTree).build();
  ODataResponse entry = EntityProvider.writeEntry("application/xml", roomsSet, roomData, properties);

  String xml = StringHelper.inputStreamToString((InputStream) entry.getEntity());
  assertXpathExists("/a:entry/a:content/m:properties", xml);
  assertXpathExists("/a:entry/a:link[@type]/m:inline", xml);
  assertXpathExists("/a:entry/a:link[@type]/m:inline/a:entry/a:content/m:properties/d:Id", xml);
  assertXpathNotExists("/a:entry/a:link[@type]/m:inline/a:entry/a:content/m:properties/d:Name", xml);
}
 
Example 13
Source File: ProducerConsumerIntegrationTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> execute(final Map<String, Object> localRoomData, final EdmEntitySet roomSet,
    final String contentType)
    throws EntityProviderException {
  ODataResponse response = EntityProvider.writeEntry(contentType, roomSet, localRoomData, DEFAULT_WRITE_PROPERTIES);
  InputStream content = (InputStream) response.getEntity();

  ODataEntry entry = EntityProvider.readEntry(contentType, roomSet, content, DEFAULT_READ_PROPERTIES);
  Map<String, Object> properties = entry.getProperties();
  return properties;
}
 
Example 14
Source File: Processor.java    From DataHubSystem with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public ODataResponse readEntity(GetEntityUriInfo uri_info, String content_type)
      throws ODataException
{
   EdmEntitySet targetES = uri_info.getTargetEntitySet();
   AbstractEntitySet target = Model.getEntitySet(targetES.getName());

   // Validity and security checks.
   if (!target.isAuthorized(Security.getCurrentUser()))
   {
      throw new NotAllowedException();
   }

   // Contained target workaround (non OData2: non standard!)
   int last_id = getContext().getPathInfo().getODataSegments().size() - 1;
   String navlinkname = getContext().getPathInfo().getODataSegments().get(last_id).getPath();
   // remove the key
   last_id = navlinkname.indexOf('(');
   if (last_id != -1)
   {
      navlinkname = navlinkname.substring(0, last_id);
   }
   if (!navlinkname.equals(targetES.getName()))
   {
      targetES = new ContainedEntitySetDecorator(navlinkname, targetES);
   }

   // Navigate to target Entity
   KeyPredicate startKP = uri_info.getKeyPredicates().get(0);
   AbstractEntity entity = Navigator.<AbstractEntity>navigate(
         uri_info.getStartEntitySet(), startKP, uri_info.getNavigationSegments(), null);
   Map<String, Object> data = entity.toEntityResponse(makeLink().toString());

   // $expand & $select
   ExpandSelectTreeNode expand_select_tree =
         UriParser.createExpandSelectTree(uri_info.getSelect(), uri_info.getExpand());

   EntityProviderWriteProperties prop = EntityProviderWriteProperties
         .serviceRoot(makeLink())
         .expandSelectTree(expand_select_tree)
         .callbacks(makeCallbacks(
               entity.getExpandableNavLinkNames(),
               new Expander(makeLink(false), entity)))
         .build();

   return EntityProvider.writeEntry(content_type, targetES, data, prop);
}