use of org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart in project camel by apache.
the class Olingo2AppImpl method addChangeSetParts.
private void addChangeSetParts(ArrayList<BatchPart> parts, ArrayList<BatchChangeSetPart> changeSetParts) {
final BatchChangeSet changeSet = BatchChangeSet.newBuilder().build();
for (BatchChangeSetPart changeSetPart : changeSetParts) {
changeSet.add(changeSetPart);
}
parts.add(changeSet);
}
use of org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart 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.client.batch.BatchChangeSetPart in project camel by apache.
the class Olingo2AppImpl method createBatchChangeSetPart.
private BatchChangeSetPart createBatchChangeSetPart(Edm edm, Map<String, String> contentIdMap, Olingo2BatchChangeRequest batchRequest) throws EdmException, URISyntaxException, EntityProviderException, IOException, ODataApplicationException {
// build body string
String resourcePath = batchRequest.getResourcePath();
// is it a referenced entity?
if (resourcePath.startsWith("$")) {
resourcePath = replaceContentId(edm, resourcePath, contentIdMap);
}
final UriInfoWithType uriInfo = parseUri(edm, resourcePath, null);
// serialize data into ODataResponse object, if set in request and this is not a DELETE request
final Map<String, String> headers = new HashMap<String, String>();
byte[] body = null;
if (batchRequest.getBody() != null && !Operation.DELETE.equals(batchRequest.getOperation())) {
final ODataResponse response = writeContent(edm, uriInfo, batchRequest.getBody());
// copy response headers
for (String header : response.getHeaderNames()) {
headers.put(header, response.getHeader(header));
}
// get (http) entity which is for default Olingo2 implementation an InputStream
body = response.getEntity() instanceof InputStream ? EntityProvider.readBinary((InputStream) response.getEntity()) : null;
if (body != null) {
headers.put(HttpHeaders.CONTENT_LENGTH, String.valueOf(body.length));
}
}
// Olingo is sensitive to batch part charset case!!
headers.put(HttpHeaders.ACCEPT, getResourceContentType(uriInfo).toString().toLowerCase());
if (!headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
headers.put(HttpHeaders.CONTENT_TYPE, getContentType());
}
// add request headers
headers.putAll(batchRequest.getHeaders());
final String contentId = batchRequest.getContentId();
if (contentId != null) {
contentIdMap.put("$" + contentId, resourcePath);
}
return BatchChangeSetPart.uri(createBatchUri(batchRequest)).method(batchRequest.getOperation().getHttpMethod()).contentId(contentId).headers(headers).body(body == null ? null : new String(body, Consts.UTF_8)).build();
}
Aggregations