Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
******************************************************************************/
package org.apache.olingo.odata2.api.client.batch;

import java.util.Map;

import org.apache.olingo.odata2.api.rt.RuntimeDelegate;

import java.util.Map;

/**
* A BatchChangeSetPart
* <p> BatchChangeSetPart represents a change request within a Change Set
Expand All @@ -30,9 +30,7 @@ public abstract class BatchChangeSetPart {

public abstract Map<String, String> getHeaders();

public abstract Object getBody();

public abstract byte[] getBodyAsBytes();
public abstract BatchInputResource getBatchInputResource();

public abstract String getUri();

Expand Down Expand Up @@ -64,6 +62,14 @@ public static BatchChangeSetPartBuilder body(final byte[] body) {
return newBuilder().body(body);
}

/**
* @param body a change request body
* @return a new builder object
*/
public static BatchChangeSetPartBuilder body(final BatchInputResource body) {
return newBuilder().body(body);
}

/**
* @param uri should not be null
* @return a new builder object
Expand Down Expand Up @@ -111,6 +117,8 @@ private static BatchChangeSetPartBuilder newInstance() {

public abstract BatchChangeSetPartBuilder body(byte[] body);

public abstract BatchChangeSetPartBuilder body(BatchInputResource inputStream);

public abstract BatchChangeSetPartBuilder uri(String uri);

public abstract BatchChangeSetPartBuilder method(String method);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.apache.olingo.odata2.api.client.batch;

import java.io.*;

public class BatchInputResource implements Closeable {

private final InputStream inputStream;
private final int size;

public BatchInputResource(InputStream inputStream, int size) {
this.inputStream = inputStream;
this.size = size;
}

public InputStream getInputStream() {
return inputStream;
}

public int size() {
return size;
}

@Override
public void close() throws IOException {
if (inputStream != null) {
inputStream.close();
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.apache.olingo.odata2.core.batch;

import org.apache.olingo.odata2.api.client.batch.BatchChangeSetPart;
import org.apache.olingo.odata2.api.client.batch.BatchInputResource;

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -28,7 +30,7 @@
public class BatchChangeSetPartImpl extends BatchChangeSetPart {
private String method;
private Map<String, String> headers = new HashMap<String, String>();
private Object body;
private BatchInputResource batchInputResource;
private String uri;
private String cntId;
private static final String CHANGE_METHODS = "(PUT|POST|DELETE|MERGE|PATCH)";
Expand All @@ -39,21 +41,8 @@ public Map<String, String> getHeaders() {
}

@Override
public String getBody() {
return body.toString();
}

@Override
public byte[] getBodyAsBytes() {
if(body == null) {
return new byte[0];
}
Charset charset = getCharset();
if (body instanceof byte[]) {
return (byte[]) body; //NOSONAR
} else {
return body.toString().getBytes(charset);
}
public BatchInputResource getBatchInputResource() {
return batchInputResource;
}

private Charset getCharset() {
Expand All @@ -78,9 +67,10 @@ public String getContentId() {
public class BatchChangeSetRequestBuilderImpl extends BatchChangeSetPartBuilder {
private String method;
private Map<String, String> headers = new HashMap<String, String>();
private Object body;
private BatchInputResource batchInputResource;
private String uri;
private String contentId;
private String stringBody;

@Override
public BatchChangeSetPart build() {
Expand All @@ -89,7 +79,8 @@ public BatchChangeSetPart build() {
}
BatchChangeSetPartImpl.this.method = method;
BatchChangeSetPartImpl.this.headers = headers;
BatchChangeSetPartImpl.this.body = body;
BatchChangeSetPartImpl.this.batchInputResource = batchInputResource != null ?
batchInputResource : stringBodyToBatchInputResource();
BatchChangeSetPartImpl.this.uri = uri;
BatchChangeSetPartImpl.this.cntId = contentId;
return BatchChangeSetPartImpl.this;
Expand All @@ -103,13 +94,24 @@ public BatchChangeSetPartBuilder headers(final Map<String, String> headers) {

@Override
public BatchChangeSetPartBuilder body(final String body) {
this.body = body;
this.stringBody = body;
return this;
}

private BatchInputResource stringBodyToBatchInputResource() {
byte[] bytes = stringBody.getBytes(getCharset());
return new BatchInputResource(new ByteArrayInputStream(bytes), bytes.length);
}

@Override
public BatchChangeSetPartBuilder body(byte[] body) {
this.body = body;
this.batchInputResource = new BatchInputResource(new ByteArrayInputStream(body), body.length);
return this;
}

@Override
public BatchChangeSetPartBuilder body(BatchInputResource batchInputStream) {
this.batchInputResource = batchInputStream;
return this;
}

Expand Down
Loading