org.apache.olingo.odata2.api.exception.ODataNotFoundException Java Examples

The following examples show how to use org.apache.olingo.odata2.api.exception.ODataNotFoundException. 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: AnnotationsInMemoryDsTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void deleteSimpleEntity() throws Exception {
  EdmEntitySet edmEntitySet = createMockedEdmEntitySet("Buildings");
  DataStore<Building> datastore = datasource.getDataStore(Building.class);

  Building building = new Building();
  building.setName("Common Building");
  datastore.create(building);

  Map<String, Object> keys = new HashMap<String, Object>();
  keys.put("Id", "1");

  Building read = (Building) datasource.readData(edmEntitySet, keys);
  Assert.assertEquals("Common Building", read.getName());
  Assert.assertEquals("1", read.getId());

  //
  datasource.deleteData(edmEntitySet, keys);

  // validate
  try {
    Building readAfterDelete = (Building) datasource.readData(edmEntitySet, keys);
    Assert.fail("Expected " + ODataNotFoundException.class + "was not thrown for '" + readAfterDelete + "'.");
  } catch (ODataNotFoundException e) {}
}
 
Example #2
Source File: AnnotationInMemoryDs.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public BinaryData readBinaryData(final EdmEntitySet entitySet, final Object mediaLinkEntryData)
    throws ODataNotImplementedException, ODataNotFoundException, EdmException, ODataApplicationException {

  Object data = ANNOTATION_HELPER.getValueForField(mediaLinkEntryData, EdmMediaResourceContent.class);
  Object mimeType = ANNOTATION_HELPER.getValueForField(mediaLinkEntryData, EdmMediaResourceMimeType.class);

  if (data == null && mimeType == null) {
    DataStore<Object> dataStore = getDataStore(entitySet);
    Object readEntry = dataStore.read(mediaLinkEntryData);
    if (readEntry != null) {
      data = ANNOTATION_HELPER.getValueForField(readEntry, EdmMediaResourceContent.class);
      mimeType = ANNOTATION_HELPER.getValueForField(readEntry, EdmMediaResourceMimeType.class);
    }
  }

  return new BinaryData((byte[]) data, String.valueOf(mimeType));
}
 
Example #3
Source File: ScenarioDataSource.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
public BinaryData readBinaryData(final EdmEntitySet entitySet, final Object mediaLinkEntryData)
    throws ODataNotImplementedException, ODataNotFoundException, EdmException, ODataApplicationException {
  if (mediaLinkEntryData == null) {
    throw new ODataNotFoundException(null);
  }

  if (ENTITYSET_1_1.equals(entitySet.getName()) || ENTITYSET_1_4.equals(entitySet.getName())) {
    final Employee employee = (Employee) mediaLinkEntryData;
    if (employee.getImage() == null) {
      throw new ODataNotFoundException(null);
    }
    return new BinaryData(employee.getImage(), employee.getImageType());
  } else if (ENTITYSET_2_1.equals(entitySet.getName())) {
    final Photo photo = (Photo) mediaLinkEntryData;
    return new BinaryData(photo.getImage(), photo.getImageType());
  } else {
    throw new ODataNotImplementedException();
  }
}
 
Example #4
Source File: AnnotationInMemoryDs.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public Object readData(final EdmEntitySet entitySet, final Map<String, Object> keys)
    throws ODataNotFoundException, EdmException, ODataApplicationException {

  DataStore<Object> store = getDataStore(entitySet);
  if (store != null) {
    Object keyInstance = store.createInstance();
    ANNOTATION_HELPER.setKeyFields(keyInstance, keys);

    Object result = store.read(keyInstance);
    if (result != null) {
      return result;
    }
  }

  throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
}
 
Example #5
Source File: ScenarioDataSource.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
public void
    writeBinaryData(final EdmEntitySet entitySet, final Object mediaLinkEntryData, final BinaryData binaryData)
        throws ODataNotImplementedException, ODataNotFoundException, EdmException, ODataApplicationException {
  if (mediaLinkEntryData == null) {
    throw new ODataNotFoundException(null);
  }

  if (ENTITYSET_1_1.equals(entitySet.getName()) || ENTITYSET_1_4.equals(entitySet.getName())) {
    final Employee employee = (Employee) mediaLinkEntryData;
    employee.setImage(binaryData.getData());
    employee.setImageType(binaryData.getMimeType());
  //Storing the binary data to be used for comparison in the tests
    Util.getInstance().setBinaryContent(employee.getImage());
  } else if (ENTITYSET_2_1.equals(entitySet.getName())) {
    final Photo photo = (Photo) mediaLinkEntryData;
    photo.setImage(binaryData.getData());
    photo.setImageType(binaryData.getMimeType());
  } else {
    throw new ODataNotImplementedException();
  }
}
 
Example #6
Source File: ListsProcessor.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 Object data = retrieveData(
      uriInfo.getStartEntitySet(),
      uriInfo.getKeyPredicates(),
      uriInfo.getFunctionImport(),
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      uriInfo.getNavigationSegments());

  final EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
  if (!appliesFilter(entitySet, data, uriInfo.getFilter())) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  final ExpandSelectTreeNode expandSelectTreeNode =
      UriParser.createExpandSelectTree(uriInfo.getSelect(), uriInfo.getExpand());

  return ODataResponse.fromResponse(writeEntry(entitySet, expandSelectTreeNode, data, contentType))
      .build();
}
 
Example #7
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());

  final EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
  if (!appliesFilter(entitySet, data, uriInfo.getFilter())) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  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 #8
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse readEntitySimplePropertyValue(final GetSimplePropertyUriInfo uriInfo, final String contentType)
    throws ODataException {
  Object data = retrieveData(
      uriInfo.getStartEntitySet(),
      uriInfo.getKeyPredicates(),
      uriInfo.getFunctionImport(),
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      uriInfo.getNavigationSegments());

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

  final List<EdmProperty> propertyPath = uriInfo.getPropertyPath();
  final EdmProperty property = propertyPath.get(propertyPath.size() - 1);
  final Object value = property.getMapping() == null || property.getMapping().getMediaResourceMimeTypeKey() == null ?
      getPropertyValue(data, propertyPath) : getSimpleTypeValueMap(data, propertyPath);

  return ODataResponse.fromResponse(EntityProvider.writePropertyValue(property, value)).eTag(
      constructETag(uriInfo.getTargetEntitySet(), data)).build();
}
 
Example #9
Source File: AnnotationInMemoryDs.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public Object readRelatedData(final EdmEntitySet sourceEntitySet, final Object sourceData,
    final EdmEntitySet targetEntitySet,
    final Map<String, Object> targetKeys)
    throws ODataNotImplementedException, ODataNotFoundException, EdmException, ODataApplicationException {

  DataStore<?> sourceStore = dataStores.get(sourceEntitySet.getName());
  DataStore<?> targetStore = dataStores.get(targetEntitySet.getName());

  AnnotatedNavInfo navInfo = ANNOTATION_HELPER.getCommonNavigationInfo(
      sourceStore.getDataTypeClass(), targetStore.getDataTypeClass());
  final Field sourceField;
  if(navInfo.isBiDirectional()) {
    sourceField = navInfo.getToField();
  } else {
    sourceField = navInfo.getFromField();
  }
  if (sourceField == null) {
    throw new AnnotationRuntimeException("Missing source field for related data (sourceStore='" + sourceStore
        + "', targetStore='" + targetStore + "').");
  }

  List<Object> resultData = readResultData(targetStore, sourceData, sourceField);
  return extractResultData(targetStore, targetKeys, navInfo, resultData);
}
 
Example #10
Source File: AnnotationInMemoryDs.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public void writeBinaryData(final EdmEntitySet entitySet, final Object mediaEntityInstance,
    final BinaryData binaryData)
    throws ODataNotImplementedException, ODataNotFoundException, EdmException, ODataApplicationException {

  try {
    DataStore<Object> dataStore = getDataStore(entitySet);
    Object readEntry = dataStore.read(mediaEntityInstance);
    if (readEntry == null) {
      throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
    } else {
      ANNOTATION_HELPER.setValueForAnnotatedField(
          mediaEntityInstance, EdmMediaResourceContent.class, binaryData.getData());
      ANNOTATION_HELPER.setValueForAnnotatedField(
          mediaEntityInstance, EdmMediaResourceMimeType.class, binaryData.getMimeType());
    }
  } catch (ODataAnnotationException e) {
    throw new AnnotationRuntimeException("Invalid media resource annotation at entity set '" + entitySet.getName()
        + "' with message '" + e.getMessage() + "'.", e);
  }
}
 
Example #11
Source File: ScenarioDataSource.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private Map<Location, Integer> getLocations() throws ODataNotFoundException {
  Map<Location, Integer> locations = new LinkedHashMap<Location, Integer>();
  for (Employee employee : dataContainer.getEmployees()) {
    if (employee.getLocation() != null && employee.getLocation().getCity() != null) {
      boolean found = false;
      for (final Location location : locations.keySet()) {
        if (employee.getLocation().getCity().getPostalCode() == location.getCity().getPostalCode()
            && employee.getLocation().getCity().getCityName() == location.getCity().getCityName()
            && employee.getLocation().getCountry() == location.getCountry()) {
          found = true;
          locations.put(location, locations.get(location) + 1);
        }
      }
      if (!found) {
        locations.put(employee.getLocation(), 1);
      }
    }
  }
  if (locations.isEmpty()) {
    throw new ODataNotFoundException(null);
  } else {
    return locations;
  }
}
 
Example #12
Source File: ScenarioDataSource.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
public List<?> readData(final EdmEntitySet entitySet) throws ODataNotImplementedException, ODataNotFoundException,
    EdmException {
  if (ENTITYSET_1_1.equals(entitySet.getName())) {
    return Arrays.asList(dataContainer.getEmployees().toArray());
  } else if (ENTITYSET_1_2.equals(entitySet.getName())) {
    return Arrays.asList(dataContainer.getTeams().toArray());
  } else if (ENTITYSET_1_3.equals(entitySet.getName())) {
    return Arrays.asList(dataContainer.getRooms().toArray());
  } else if (ENTITYSET_1_4.equals(entitySet.getName())) {
    return Arrays.asList(dataContainer.getManagers().toArray());
  } else if (ENTITYSET_1_5.equals(entitySet.getName())) {
    return Arrays.asList(dataContainer.getBuildings().toArray());
  } else if (ENTITYSET_2_1.equals(entitySet.getName())) {
    return Arrays.asList(dataContainer.getPhotos().toArray());
  } else {
    throw new ODataNotImplementedException();
  }
}
 
Example #13
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse updateEntityMedia(final PutMergePatchUriInfo uriInfo, final InputStream content,
    final String requestContentType, final String contentType) throws ODataException {
  final Object data = retrieveData(
      uriInfo.getStartEntitySet(),
      uriInfo.getKeyPredicates(),
      uriInfo.getFunctionImport(),
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      uriInfo.getNavigationSegments());

  final EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
  if (!appliesFilter(entitySet, data, uriInfo.getFilter())) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  ODataContext context = getContext();
  final int timingHandle = context.startRuntimeMeasurement("EntityProvider", "readBinary");

  final byte[] value = EntityProvider.readBinary(content);

  context.stopRuntimeMeasurement(timingHandle);

  dataSource.writeBinaryData(entitySet, data, new BinaryData(value, requestContentType));

  return ODataResponse.newBuilder().eTag(constructETag(entitySet, data)).build();
}
 
Example #14
Source File: ListsProcessor.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 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 ExpandSelectTreeNode expandSelectTreeNode =
      UriParser.createExpandSelectTree(uriInfo.getSelect(), uriInfo.getExpand());
  ODataResponse odr =
      ODataResponse.fromResponse(writeEntry(uriInfo.getTargetEntitySet(), expandSelectTreeNode, data, contentType))
          .build();

  return odr;
}
 
Example #15
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 #16
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse readEntitySimplePropertyValue(final GetSimplePropertyUriInfo uriInfo, final String contentType)
    throws ODataException {
  Object data = retrieveData(
      uriInfo.getStartEntitySet(),
      uriInfo.getKeyPredicates(),
      uriInfo.getFunctionImport(),
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      uriInfo.getNavigationSegments());

  if (data == null) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  final List<EdmProperty> propertyPath = uriInfo.getPropertyPath();
  final EdmProperty property = propertyPath.get(propertyPath.size() - 1);
  final Object value = property.getMapping() == null || property.getMapping().getMediaResourceMimeTypeKey() == null ?
      getPropertyValue(data, propertyPath) : getSimpleTypeValueMap(data, propertyPath);

  return ODataResponse.fromResponse(EntityProvider.writePropertyValue(property, value)).eTag(
      constructETag(uriInfo.getTargetEntitySet(), data)).build();
}
 
Example #17
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse deleteEntitySimplePropertyValue(final DeleteUriInfo uriInfo, final String contentType)
    throws ODataException {
  Object data = retrieveData(
      uriInfo.getStartEntitySet(),
      uriInfo.getKeyPredicates(),
      uriInfo.getFunctionImport(),
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      uriInfo.getNavigationSegments());

  if (data == null) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  final List<EdmProperty> propertyPath = uriInfo.getPropertyPath();
  final EdmProperty property = propertyPath.get(propertyPath.size() - 1);

  data = getPropertyValue(data, propertyPath.subList(0, propertyPath.size() - 1));
  valueAccess.setPropertyValue(data, property, null);
  valueAccess.setMappingValue(data, property.getMapping(), null);

  return ODataResponse.newBuilder().build();
}
 
Example #18
Source File: HttpExceptionResponseTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void test404HttpNotFound() throws Exception {
  when(processor.readEntity(any(GetEntityUriInfo.class), any(String.class))).thenThrow(
      new ODataNotFoundException(ODataNotFoundException.ENTITY));

  final HttpResponse response = executeGetRequest("Managers('199')");
  assertEquals(HttpStatusCodes.NOT_FOUND.getStatusCode(), response.getStatusLine().getStatusCode());

  final String content = StringHelper.inputStreamToString(response.getEntity().getContent());
  Map<String, String> prefixMap = new HashMap<String, String>();
  prefixMap.put("a", Edm.NAMESPACE_M_2007_08);
  XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap));
  assertXpathExists("/a:error/a:code", content);
  assertXpathValuesEqual("\"" + MessageService.getMessage(Locale.ENGLISH, ODataNotFoundException.ENTITY).getText()
      + "\"", "/a:error/a:message", content);
}
 
Example #19
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse readEntityMedia(final GetMediaResourceUriInfo uriInfo, final String contentType)
    throws ODataException {
  final 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 BinaryData binaryData = dataSource.readBinaryData(entitySet, data);
  if (binaryData == null) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  final String mimeType = binaryData.getMimeType() == null ?
      HttpContentType.APPLICATION_OCTET_STREAM : binaryData.getMimeType();

  return ODataResponse.fromResponse(EntityProvider.writeBinary(mimeType, binaryData.getData())).eTag(
      constructETag(entitySet, data)).build();
}
 
Example #20
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse readEntityMedia(final GetMediaResourceUriInfo uriInfo, final String contentType)
    throws ODataException {
  final Object data = retrieveData(
      uriInfo.getStartEntitySet(),
      uriInfo.getKeyPredicates(),
      uriInfo.getFunctionImport(),
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      uriInfo.getNavigationSegments());

  final EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
  if (!appliesFilter(entitySet, data, uriInfo.getFilter())) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  final BinaryData binaryData = dataSource.readBinaryData(entitySet, data);
  if (binaryData == null) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  final String mimeType = binaryData.getMimeType() == null ?
      HttpContentType.APPLICATION_OCTET_STREAM : binaryData.getMimeType();

  return ODataResponse.fromResponse(EntityProvider.writeBinary(mimeType, binaryData.getData())).eTag(
      constructETag(entitySet, data)).build();
}
 
Example #21
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 #22
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse updateEntityMedia(final PutMergePatchUriInfo uriInfo, final InputStream content,
    final String requestContentType, final String contentType) throws ODataException {
  final Object data = retrieveData(
      uriInfo.getStartEntitySet(),
      uriInfo.getKeyPredicates(),
      uriInfo.getFunctionImport(),
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      uriInfo.getNavigationSegments());

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

  ODataContext context = getContext();
  final int timingHandle = context.startRuntimeMeasurement("EntityProvider", "readBinary");

  final byte[] value = EntityProvider.readBinary(content);

  context.stopRuntimeMeasurement(timingHandle);

  final EdmEntitySet entitySet = uriInfo.getTargetEntitySet();
  dataSource.writeBinaryData(entitySet, data, new BinaryData(value, requestContentType));

  return ODataResponse.newBuilder().eTag(constructETag(entitySet, data)).build();
}
 
Example #23
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse executeFunctionImportValue(final GetFunctionImportUriInfo uriInfo, final String contentType)
    throws ODataException {
  final EdmFunctionImport functionImport = uriInfo.getFunctionImport();
  final EdmSimpleType type = (EdmSimpleType) functionImport.getReturnType().getType();

  final Object data = dataSource.readData(
      functionImport,
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      null);

  if (data == null) {
    throw new ODataNotFoundException(ODataHttpException.COMMON);
  }

  ODataResponse response;
  if (type == EdmSimpleTypeKind.Binary.getEdmSimpleTypeInstance()) {
    response = EntityProvider.writeBinary(((BinaryData) data).getMimeType(), ((BinaryData) data).getData());
  } else {
    final String value = type.valueToString(data, EdmLiteralKind.DEFAULT, null);
    response = EntityProvider.writeText(value == null ? "" : value);
  }
  return ODataResponse.fromResponse(response).build();
}
 
Example #24
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse deleteEntitySimplePropertyValue(final DeleteUriInfo uriInfo, final String contentType)
    throws ODataException {
  Object data = retrieveData(
      uriInfo.getStartEntitySet(),
      uriInfo.getKeyPredicates(),
      uriInfo.getFunctionImport(),
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      uriInfo.getNavigationSegments());

  if (data == null) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  final List<EdmProperty> propertyPath = uriInfo.getPropertyPath();
  final EdmProperty property = propertyPath.get(propertyPath.size() - 1);

  data = getPropertyValue(data, propertyPath.subList(0, propertyPath.size() - 1));
  valueAccess.setPropertyValue(data, property, null);
  valueAccess.setMappingValue(data, property.getMapping(), null);

  return ODataResponse.newBuilder().build();
}
 
Example #25
Source File: ODataJPAResponseBuilderDefault.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataResponse build(final PutMergePatchUriInfo putUriInfo, final Object updatedObject)
    throws ODataJPARuntimeException, ODataNotFoundException {
  if (updatedObject == null) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }
  return ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
}
 
Example #26
Source File: BeanPropertyAccessTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test(expected = ODataNotFoundException.class)
public void getPropertyTypeInvalidPropertyName() throws ODataException {
  SimpleEntity data = new SimpleEntity();
  data.name = "A Name";
  EdmProperty property = mockProperty("invalid.Name");

  Object value = ava.getPropertyValue(data, property);
  Assert.assertNull(value);
}
 
Example #27
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataResponse readEntityComplexProperty(final GetComplexPropertyUriInfo uriInfo, final String contentType)
    throws ODataException {
  Object data = retrieveData(
      uriInfo.getStartEntitySet(),
      uriInfo.getKeyPredicates(),
      uriInfo.getFunctionImport(),
      mapFunctionParameters(uriInfo.getFunctionImportParameters()),
      uriInfo.getNavigationSegments());

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

  final List<EdmProperty> propertyPath = uriInfo.getPropertyPath();
  final EdmProperty property = propertyPath.get(propertyPath.size() - 1);
  final Object value = property.isSimple() ?
      property.getMapping() == null || property.getMapping().getMediaResourceMimeTypeKey() == null ?
          getPropertyValue(data, propertyPath) : getSimpleTypeValueMap(data, propertyPath) :
      getStructuralTypeValueMap(getPropertyValue(data, propertyPath), (EdmStructuralType) property.getType());

  ODataContext context = getContext();
  final int timingHandle = context.startRuntimeMeasurement("EntityProvider", "writeProperty");

  final ODataResponse response = EntityProvider.writeProperty(contentType, property, value);

  context.stopRuntimeMeasurement(timingHandle);

  return ODataResponse.fromResponse(response).eTag(constructETag(uriInfo.getTargetEntitySet(), data)).build();
}
 
Example #28
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataResponse updateEntityLink(final PutMergePatchUriInfo uriInfo, final InputStream content,
    final String requestContentType, final String contentType) throws ODataException {
  final List<NavigationSegment> navigationSegments = uriInfo.getNavigationSegments();
  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 entitySet = previousSegments.isEmpty() ?
      uriInfo.getStartEntitySet() : previousSegments.get(previousSegments.size() - 1).getEntitySet();
  final EdmEntitySet targetEntitySet = uriInfo.getTargetEntitySet();
  final Map<String, Object> keys = mapKey(uriInfo.getTargetKeyPredicates());

  final Object targetData = dataSource.readRelatedData(entitySet, sourceData, targetEntitySet, keys);

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

  dataSource.deleteRelation(entitySet, sourceData, targetEntitySet, keys);

  final Map<String, Object> newKeys = parseLink(targetEntitySet, content, requestContentType);

  dataSource.writeRelation(entitySet, sourceData, targetEntitySet, newKeys);

  return ODataResponse.newBuilder().build();
}
 
Example #29
Source File: RestUtil.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static PathInfoImpl splitPath(final SubLocatorParameter param) throws ODataException {
  PathInfoImpl pathInfo = new PathInfoImpl();

  List<javax.ws.rs.core.PathSegment> precedingPathSegments;
  List<javax.ws.rs.core.PathSegment> pathSegments;

  if (param.getPathSplit() == 0) {
    precedingPathSegments = Collections.emptyList();
    pathSegments = param.getPathSegments();
  } else {
    if (param.getPathSegments().size() < param.getPathSplit()) {
      throw new ODataBadRequestException(ODataBadRequestException.URLTOOSHORT);
    }

    precedingPathSegments = param.getPathSegments().subList(0, param.getPathSplit());
    final int pathSegmentCount = param.getPathSegments().size();
    pathSegments = param.getPathSegments().subList(param.getPathSplit(), pathSegmentCount);
  }

  // Percent-decode only the preceding path segments.
  // The OData path segments are decoded during URI parsing.
  pathInfo.setPrecedingPathSegment(convertPathSegmentList(precedingPathSegments));

  List<PathSegment> odataSegments = new ArrayList<PathSegment>();
  for (final javax.ws.rs.core.PathSegment segment : pathSegments) {
    if (segment.getMatrixParameters() == null || segment.getMatrixParameters().isEmpty()) {
      odataSegments.add(new ODataPathSegmentImpl(segment.getPath(), null));
    } else {
      // post condition: we do not allow matrix parameters in OData path segments
      throw new ODataNotFoundException(ODataNotFoundException.MATRIX.addContent(segment.getMatrixParameters()
          .keySet(), segment.getPath()));
    }
  }
  pathInfo.setODataPathSegment(odataSegments);

  return pathInfo;
}
 
Example #30
Source File: ListsProcessor.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public ODataResponse deleteEntityLink(final DeleteUriInfo uriInfo, final String contentType) throws ODataException {
  final List<NavigationSegment> navigationSegments = uriInfo.getNavigationSegments();
  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 entitySet = previousSegments.isEmpty() ?
      uriInfo.getStartEntitySet() : previousSegments.get(previousSegments.size() - 1).getEntitySet();
  final EdmEntitySet targetEntitySet = uriInfo.getTargetEntitySet();
  final Map<String, Object> keys = mapKey(uriInfo.getTargetKeyPredicates());

  final Object targetData = dataSource.readRelatedData(entitySet, sourceData, targetEntitySet, keys);

  // if (!appliesFilter(targetData, uriInfo.getFilter()))
  if (targetData == null) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY);
  }

  dataSource.deleteRelation(entitySet, sourceData, targetEntitySet, keys);

  return ODataResponse.newBuilder().build();
}