use of org.apache.olingo.odata2.api.commons.HttpStatusCodes in project camel by apache.
the class Olingo2AppAPITest method testCreateUpdateDeleteEntry.
@Test
public void testCreateUpdateDeleteEntry() throws Exception {
// create entry to update
final TestOlingo2ResponseHandler<ODataEntry> entryHandler = new TestOlingo2ResponseHandler<ODataEntry>();
olingoApp.create(edm, MANUFACTURERS, getEntityData(), entryHandler);
ODataEntry createdEntry = entryHandler.await();
LOG.info("Created Entry: {}", prettyPrint(createdEntry));
Map<String, Object> data = getEntityData();
@SuppressWarnings("unchecked") Map<String, Object> address = (Map<String, Object>) data.get(ADDRESS);
data.put("Name", "MyCarManufacturer Renamed");
address.put("Street", "Main Street");
final TestOlingo2ResponseHandler<HttpStatusCodes> statusHandler = new TestOlingo2ResponseHandler<HttpStatusCodes>();
olingoApp.update(edm, TEST_CREATE_MANUFACTURER, data, statusHandler);
statusHandler.await();
statusHandler.reset();
data.put("Name", "MyCarManufacturer Patched");
olingoApp.patch(edm, TEST_CREATE_MANUFACTURER, data, statusHandler);
statusHandler.await();
entryHandler.reset();
olingoApp.read(edm, TEST_CREATE_MANUFACTURER, null, entryHandler);
ODataEntry updatedEntry = entryHandler.await();
LOG.info("Updated Entry successfully: {}", prettyPrint(updatedEntry));
statusHandler.reset();
olingoApp.delete(TEST_CREATE_MANUFACTURER, statusHandler);
HttpStatusCodes statusCode = statusHandler.await();
LOG.info("Deletion of Entry was successful: {}: {}", statusCode.getStatusCode(), statusCode.getInfo());
try {
LOG.info("Verify Delete Entry");
entryHandler.reset();
olingoApp.read(edm, TEST_CREATE_MANUFACTURER, null, entryHandler);
entryHandler.await();
fail("Entry not deleted!");
} catch (Exception e) {
LOG.info("Deleted entry not found: {}", e.getMessage());
}
}
use of org.apache.olingo.odata2.api.commons.HttpStatusCodes in project camel by apache.
the class AbstractFutureCallback method checkStatus.
public static HttpStatusCodes checkStatus(HttpResponse response) throws ODataApplicationException {
final StatusLine statusLine = response.getStatusLine();
HttpStatusCodes httpStatusCode = HttpStatusCodes.fromStatusCode(statusLine.getStatusCode());
if (400 <= httpStatusCode.getStatusCode() && httpStatusCode.getStatusCode() <= 599) {
if (response.getEntity() != null) {
try {
final ContentType responseContentType = ContentType.parse(response.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
final String mimeType = responseContentType.getMimeType();
if (ODATA_MIME_TYPE.matcher(mimeType).matches()) {
final ODataErrorContext errorContext = EntityProvider.readErrorDocument(response.getEntity().getContent(), responseContentType.toString());
throw new ODataApplicationException(errorContext.getMessage(), errorContext.getLocale(), httpStatusCode, errorContext.getErrorCode(), errorContext.getException());
}
} catch (EntityProviderException e) {
throw new ODataApplicationException(e.getMessage(), response.getLocale(), httpStatusCode, e);
} catch (IOException e) {
throw new ODataApplicationException(e.getMessage(), response.getLocale(), httpStatusCode, e);
}
}
throw new ODataApplicationException(statusLine.getReasonPhrase(), response.getLocale(), httpStatusCode);
}
return httpStatusCode;
}
use of org.apache.olingo.odata2.api.commons.HttpStatusCodes in project camel by apache.
the class Olingo2AppImpl method writeContent.
private <T> void writeContent(final Edm edm, HttpEntityEnclosingRequestBase httpEntityRequest, final UriInfoWithType uriInfo, final Object content, final Olingo2ResponseHandler<T> responseHandler) {
try {
// process resource by UriType
final ODataResponse response = writeContent(edm, uriInfo, content);
// copy all response headers
for (String header : response.getHeaderNames()) {
httpEntityRequest.setHeader(header, response.getHeader(header));
}
// get (http) entity which is for default Olingo2 implementation an InputStream
if (response.getEntity() instanceof InputStream) {
httpEntityRequest.setEntity(new InputStreamEntity((InputStream) response.getEntity()));
/*
// avoid sending it without a header field set
if (!httpEntityRequest.containsHeader(HttpHeaders.CONTENT_TYPE)) {
httpEntityRequest.addHeader(HttpHeaders.CONTENT_TYPE, getContentType());
}
*/
}
// execute HTTP request
final Header requestContentTypeHeader = httpEntityRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE);
final ContentType requestContentType = requestContentTypeHeader != null ? ContentType.parse(requestContentTypeHeader.getValue()) : contentType;
execute(httpEntityRequest, requestContentType, new AbstractFutureCallback<T>(responseHandler) {
@SuppressWarnings("unchecked")
@Override
public void onCompleted(HttpResponse result) throws IOException, EntityProviderException, BatchException, ODataApplicationException {
// if a entity is created (via POST request) the response body contains the new created entity
HttpStatusCodes statusCode = HttpStatusCodes.fromStatusCode(result.getStatusLine().getStatusCode());
// look for no content, or no response body!!!
final boolean noEntity = result.getEntity() == null || result.getEntity().getContentLength() == 0;
if (statusCode == HttpStatusCodes.NO_CONTENT || noEntity) {
responseHandler.onResponse((T) HttpStatusCodes.fromStatusCode(result.getStatusLine().getStatusCode()));
} else {
switch(uriInfo.getUriType()) {
case URI9:
// $batch
final List<BatchSingleResponse> singleResponses = EntityProvider.parseBatchResponse(result.getEntity().getContent(), result.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());
// parse batch response bodies
final List<Olingo2BatchResponse> responses = new ArrayList<Olingo2BatchResponse>();
Map<String, String> contentIdLocationMap = new HashMap<String, String>();
final List<Olingo2BatchRequest> batchRequests = (List<Olingo2BatchRequest>) content;
final Iterator<Olingo2BatchRequest> iterator = batchRequests.iterator();
for (BatchSingleResponse response : singleResponses) {
final Olingo2BatchRequest request = iterator.next();
if (request instanceof Olingo2BatchChangeRequest && ((Olingo2BatchChangeRequest) request).getContentId() != null) {
contentIdLocationMap.put("$" + ((Olingo2BatchChangeRequest) request).getContentId(), response.getHeader(HttpHeaders.LOCATION));
}
try {
responses.add(parseResponse(edm, contentIdLocationMap, request, response));
} catch (Exception e) {
// report any parsing errors as error response
responses.add(new Olingo2BatchResponse(Integer.parseInt(response.getStatusCode()), response.getStatusInfo(), response.getContentId(), response.getHeaders(), new ODataApplicationException("Error parsing response for " + request + ": " + e.getMessage(), Locale.ENGLISH, e)));
}
}
responseHandler.onResponse((T) responses);
break;
case URI4:
case URI5:
// simple property
// get the response content as Object for $value or Map<String, Object> otherwise
final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath();
final EdmProperty simpleProperty = simplePropertyPath.get(simplePropertyPath.size() - 1);
if (uriInfo.isValue()) {
responseHandler.onResponse((T) EntityProvider.readPropertyValue(simpleProperty, result.getEntity().getContent()));
} else {
responseHandler.onResponse((T) EntityProvider.readProperty(getContentType(), simpleProperty, result.getEntity().getContent(), EntityProviderReadProperties.init().build()));
}
break;
case URI3:
// complex property
// get the response content as Map<String, Object>
final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath();
final EdmProperty complexProperty = complexPropertyPath.get(complexPropertyPath.size() - 1);
responseHandler.onResponse((T) EntityProvider.readProperty(getContentType(), complexProperty, result.getEntity().getContent(), EntityProviderReadProperties.init().build()));
break;
case URI7A:
// $links with 0..1 cardinality property
// get the response content as String
final EdmEntitySet targetLinkEntitySet = uriInfo.getTargetEntitySet();
responseHandler.onResponse((T) EntityProvider.readLink(getContentType(), targetLinkEntitySet, result.getEntity().getContent()));
break;
case URI7B:
// $links with * cardinality property
// get the response content as java.util.List<String>
final EdmEntitySet targetLinksEntitySet = uriInfo.getTargetEntitySet();
responseHandler.onResponse((T) EntityProvider.readLinks(getContentType(), targetLinksEntitySet, result.getEntity().getContent()));
break;
case URI1:
case URI2:
case URI6A:
case URI6B:
// Entity
// get the response content as an ODataEntry object
responseHandler.onResponse((T) EntityProvider.readEntry(response.getContentHeader(), uriInfo.getTargetEntitySet(), result.getEntity().getContent(), EntityProviderReadProperties.init().build()));
break;
default:
throw new ODataApplicationException("Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH);
}
}
}
});
} catch (ODataException e) {
responseHandler.onException(e);
} catch (URISyntaxException e) {
responseHandler.onException(e);
} catch (UnsupportedEncodingException e) {
responseHandler.onException(e);
} catch (IOException e) {
responseHandler.onException(e);
}
}
use of org.apache.olingo.odata2.api.commons.HttpStatusCodes in project camel by apache.
the class Olingo2AppAPITest method testReadUpdateProperties.
@Test
public void testReadUpdateProperties() throws Exception {
// test simple property Manufacturer.Founded
final TestOlingo2ResponseHandler<Map<String, Object>> propertyHandler = new TestOlingo2ResponseHandler<Map<String, Object>>();
olingoApp.read(edm, TEST_MANUFACTURER_FOUNDED_PROPERTY, null, propertyHandler);
Calendar founded = (Calendar) propertyHandler.await().get(FOUNDED_PROPERTY);
LOG.info("Founded property {}", founded.toString());
final TestOlingo2ResponseHandler<Calendar> valueHandler = new TestOlingo2ResponseHandler<Calendar>();
olingoApp.read(edm, TEST_MANUFACTURER_FOUNDED_VALUE, null, valueHandler);
founded = valueHandler.await();
LOG.info("Founded property {}", founded.toString());
final TestOlingo2ResponseHandler<HttpStatusCodes> statusHandler = new TestOlingo2ResponseHandler<HttpStatusCodes>();
final HashMap<String, Object> properties = new HashMap<String, Object>();
properties.put(FOUNDED_PROPERTY, new Date());
// olingoApp.update(edm, TEST_MANUFACTURER_FOUNDED_PROPERTY, properties, statusHandler);
// requires a plain Date for XML
olingoApp.update(edm, TEST_MANUFACTURER_FOUNDED_PROPERTY, new Date(), statusHandler);
LOG.info("Founded property updated with status {}", statusHandler.await().getStatusCode());
statusHandler.reset();
olingoApp.update(edm, TEST_MANUFACTURER_FOUNDED_VALUE, new Date(), statusHandler);
LOG.info("Founded property updated with status {}", statusHandler.await().getStatusCode());
// test complex property Manufacturer.Address
propertyHandler.reset();
olingoApp.read(edm, TEST_MANUFACTURER_ADDRESS_PROPERTY, null, propertyHandler);
final Map<String, Object> address = propertyHandler.await();
LOG.info("Address property {}", prettyPrint(address, 0));
statusHandler.reset();
address.clear();
// Olingo2 sample server MERGE/PATCH behaves like PUT!!!
// address.put("Street", "Main Street");
address.put("Street", "Star Street 137");
address.put("City", "Stuttgart");
address.put("ZipCode", "70173");
address.put("Country", "Germany");
// olingoApp.patch(edm, TEST_MANUFACTURER_ADDRESS_PROPERTY, address, statusHandler);
olingoApp.merge(edm, TEST_MANUFACTURER_ADDRESS_PROPERTY, address, statusHandler);
LOG.info("Address property updated with status {}", statusHandler.await().getStatusCode());
}
use of org.apache.olingo.odata2.api.commons.HttpStatusCodes in project camel by apache.
the class Olingo2ComponentTest method testCreateUpdateDelete.
@Test
public void testCreateUpdateDelete() throws Exception {
final Map<String, Object> data = getEntityData();
Map<String, Object> address;
final ODataEntry manufacturer = requestBody("direct://CREATE", data);
assertNotNull("Created Manufacturer", manufacturer);
final Map<String, Object> properties = manufacturer.getProperties();
assertEquals("Created Manufacturer Id", "123", properties.get(ID_PROPERTY));
LOG.info("Created Manufacturer: {}", properties);
// update
data.put("Name", "MyCarManufacturer Renamed");
address = (Map<String, Object>) data.get("Address");
address.put("Street", "Main Street");
HttpStatusCodes status = requestBody("direct://UPDATE", data);
assertNotNull("Update status", status);
assertEquals("Update status", HttpStatusCodes.NO_CONTENT.getStatusCode(), status.getStatusCode());
LOG.info("Update status: {}", status);
// delete
status = requestBody("direct://DELETE", null);
assertNotNull("Delete status", status);
assertEquals("Delete status", HttpStatusCodes.NO_CONTENT.getStatusCode(), status.getStatusCode());
LOG.info("Delete status: {}", status);
}
Aggregations