use of org.apache.olingo.odata2.api.exception.ODataApplicationException 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.exception.ODataApplicationException in project camel by apache.
the class Olingo2AppImpl method parseResponse.
private Olingo2BatchResponse parseResponse(Edm edm, Map<String, String> contentIdLocationMap, Olingo2BatchRequest request, BatchSingleResponse response) throws EntityProviderException, ODataApplicationException {
// validate HTTP status
final int statusCode = Integer.parseInt(response.getStatusCode());
final String statusInfo = response.getStatusInfo();
final BasicHttpResponse httpResponse = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, statusInfo));
final Map<String, String> headers = response.getHeaders();
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpResponse.setHeader(entry.getKey(), entry.getValue());
}
ByteArrayInputStream content = null;
try {
if (response.getBody() != null) {
final ContentType partContentType = receiveWithCharsetParameter(ContentType.parse(headers.get(HttpHeaders.CONTENT_TYPE)), Consts.UTF_8);
final String charset = partContentType.getCharset().toString();
final String body = response.getBody();
content = body != null ? new ByteArrayInputStream(body.getBytes(charset)) : null;
httpResponse.setEntity(new StringEntity(body, charset));
}
AbstractFutureCallback.checkStatus(httpResponse);
} catch (ODataApplicationException e) {
return new Olingo2BatchResponse(statusCode, statusInfo, response.getContentId(), response.getHeaders(), e);
} catch (UnsupportedEncodingException e) {
return new Olingo2BatchResponse(statusCode, statusInfo, response.getContentId(), response.getHeaders(), e);
}
// resolve resource path and query params and parse batch part uri
final String resourcePath = request.getResourcePath();
final String resolvedResourcePath;
if (resourcePath.startsWith("$") && !(METADATA.equals(resourcePath) || BATCH.equals(resourcePath))) {
resolvedResourcePath = findLocation(resourcePath, contentIdLocationMap);
} else {
final String resourceLocation = response.getHeader(HttpHeaders.LOCATION);
resolvedResourcePath = resourceLocation != null ? resourceLocation.substring(serviceUri.length()) : resourcePath;
}
final Map<String, String> resolvedQueryParams = request instanceof Olingo2BatchQueryRequest ? ((Olingo2BatchQueryRequest) request).getQueryParams() : null;
final UriInfoWithType uriInfo = parseUri(edm, resolvedResourcePath, resolvedQueryParams);
// resolve response content
final Object resolvedContent = content != null ? readContent(uriInfo, content) : null;
return new Olingo2BatchResponse(statusCode, statusInfo, response.getContentId(), response.getHeaders(), resolvedContent);
}
use of org.apache.olingo.odata2.api.exception.ODataApplicationException in project camel by apache.
the class Olingo2AppImpl method readContent.
@SuppressWarnings("unchecked")
private <T> T readContent(UriInfoWithType uriInfo, InputStream content) throws EntityProviderException, ODataApplicationException {
T response;
switch(uriInfo.getUriType()) {
case URI0:
// service document
response = (T) EntityProvider.readServiceDocument(content, SERVICE_DOCUMENT_CONTENT_TYPE.toString());
break;
case URI8:
// $metadata
response = (T) EntityProvider.readMetadata(content, false);
break;
case URI7A:
// link
response = (T) EntityProvider.readLink(getContentType(), uriInfo.getTargetEntitySet(), content);
break;
case URI7B:
// links
response = (T) EntityProvider.readLinks(getContentType(), uriInfo.getTargetEntitySet(), content);
break;
case URI3:
// complex property
final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath();
final EdmProperty complexProperty = complexPropertyPath.get(complexPropertyPath.size() - 1);
response = (T) EntityProvider.readProperty(getContentType(), complexProperty, content, EntityProviderReadProperties.init().build());
break;
case URI4:
case URI5:
// simple property
final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath();
final EdmProperty simpleProperty = simplePropertyPath.get(simplePropertyPath.size() - 1);
if (uriInfo.isValue()) {
response = (T) EntityProvider.readPropertyValue(simpleProperty, content);
} else {
response = (T) EntityProvider.readProperty(getContentType(), simpleProperty, content, EntityProviderReadProperties.init().build());
}
break;
case URI15:
case URI16:
case URI50A:
case URI50B:
// $count
final String stringCount = new String(EntityProvider.readBinary(content), Consts.UTF_8);
response = (T) Long.valueOf(stringCount);
break;
case URI1:
case URI6B:
if (uriInfo.getCustomQueryOptions().containsKey("!deltatoken")) {
// ODataDeltaFeed
response = (T) EntityProvider.readDeltaFeed(getContentType(), uriInfo.getTargetEntitySet(), content, EntityProviderReadProperties.init().build());
} else {
// ODataFeed
response = (T) EntityProvider.readFeed(getContentType(), uriInfo.getTargetEntitySet(), content, EntityProviderReadProperties.init().build());
}
break;
case URI2:
case URI6A:
response = (T) EntityProvider.readEntry(getContentType(), uriInfo.getTargetEntitySet(), content, EntityProviderReadProperties.init().build());
break;
// Function Imports
case URI10:
case URI11:
case URI12:
case URI13:
case URI14:
response = (T) EntityProvider.readFunctionImport(getContentType(), uriInfo.getFunctionImport(), content, EntityProviderReadProperties.init().build());
break;
default:
throw new ODataApplicationException("Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH);
}
return response;
}
use of org.apache.olingo.odata2.api.exception.ODataApplicationException in project camel by apache.
the class Olingo2AppImpl method parseBatchRequest.
private ODataResponse parseBatchRequest(final Edm edm, final List<Olingo2BatchRequest> batchParts) throws IOException, EntityProviderException, ODataApplicationException, EdmException, URISyntaxException {
// create Batch request from parts
final ArrayList<BatchPart> parts = new ArrayList<BatchPart>();
final ArrayList<BatchChangeSetPart> changeSetParts = new ArrayList<BatchChangeSetPart>();
final Map<String, String> contentIdMap = new HashMap<String, String>();
for (Olingo2BatchRequest batchPart : batchParts) {
if (batchPart instanceof Olingo2BatchQueryRequest) {
// need to add change set parts collected so far??
if (!changeSetParts.isEmpty()) {
addChangeSetParts(parts, changeSetParts);
changeSetParts.clear();
contentIdMap.clear();
}
// add to request parts
final UriInfoWithType uriInfo = parseUri(edm, batchPart.getResourcePath(), null);
parts.add(createBatchQueryPart(uriInfo, (Olingo2BatchQueryRequest) batchPart));
} else {
// add to change set parts
final BatchChangeSetPart changeSetPart = createBatchChangeSetPart(edm, contentIdMap, (Olingo2BatchChangeRequest) batchPart);
changeSetParts.add(changeSetPart);
}
}
// add any remaining change set parts
if (!changeSetParts.isEmpty()) {
addChangeSetParts(parts, changeSetParts);
}
final String boundary = BOUNDARY_PREFIX + UUID.randomUUID();
InputStream batchRequest = EntityProvider.writeBatchRequest(parts, boundary);
// two blank lines are already added. No need to add extra blank lines
final String contentHeader = BATCH_CONTENT_TYPE + BOUNDARY_PARAMETER + boundary;
return ODataResponse.entity(batchRequest).contentHeader(contentHeader).build();
}
use of org.apache.olingo.odata2.api.exception.ODataApplicationException 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);
}
}
Aggregations