Permalink
Browse files

Simple code refactorings

  • Loading branch information...
1 parent 34de8fe commit 18c825525a7b8782f92d383e5d1eaf303c6bf4eb Renaud Paquay committed with lodejard Jan 14, 2012
@@ -556,19 +556,61 @@ public BatchResult batch(BatchOperations operations, TableServiceOptions options
WebResource.Builder builder = webResource.getRequestBuilder();
builder = addTableRequestHeaders(builder);
- MimeMultipart entity = createMimeMultipart(operations);
+ MimeMultipart entity = createBatchRequestBody(operations);
builder = builder.type(entity.getContentType());
ClientResponse response = builder.post(ClientResponse.class, entity);
ThrowIfError(response);
BatchResult result = new BatchResult();
- result.setEntries(parseMimeMultipart(response, operations));
+ result.setEntries(parseBatchResponse(response, operations));
return result;
}
- private List<Entry> parseMimeMultipart(ClientResponse response, BatchOperations operations) {
+ private MimeMultipart createBatchRequestBody(BatchOperations operations) {
+ List<DataSource> bodyPartContents = new ArrayList<DataSource>();
+ int contentId = 1;
+ for (Operation operation : operations.getOperations()) {
+
+ DataSource bodyPartContent = null;
+ // INSERT
+ if (operation instanceof InsertEntityOperation) {
+ InsertEntityOperation op = (InsertEntityOperation) operation;
+
+ //
+ // Stream content into byte[] so that we have the length
+ //
+ InputStream stream = atomReaderWriter.generateEntityEntry(op.getEntity());
+ byte[] bytes = inputStreamToByteArray(stream);
+
+ //
+ // Create body of MIME part as the HTTP request
+ //
+ InternetHeaders headers = new InternetHeaders();
+ headers.addHeader("Content-ID", Integer.toString(contentId++));
+ headers.addHeader("Content-Type", "application/atom+xml;type=entry");
+ headers.addHeader("Content-Length", Integer.toString(bytes.length));
+
+ //TODO: Review code to make sure encoding is correct
+ ByteArrayOutputStream httpRequest = new ByteArrayOutputStream();
+ httpReaderWriter.appendMethod(httpRequest, "POST", channel.resource(url).path(op.getTable()).getURI());
+ httpReaderWriter.appendHeaders(httpRequest, headers);
+ httpReaderWriter.appendEntity(httpRequest, new ByteArrayInputStream(bytes));
+
+ bodyPartContent = new InputStreamDataSource(new ByteArrayInputStream(httpRequest.toByteArray()),
+ "application/http");
+ }
+
+ if (bodyPartContent != null) {
+ bodyPartContents.add(bodyPartContent);
+ }
+ }
+
+ return mimeReaderWriter.getMimeMultipart(bodyPartContents);
+ }
+
+ private List<Entry> parseBatchResponse(ClientResponse response, BatchOperations operations) {
List<DataSource> parts = mimeReaderWriter.parseParts(response.getEntityInputStream(), response.getHeaders()
.getFirst("Content-Type"));
@@ -627,48 +669,6 @@ else if (operation instanceof DeleteEntityOperation) {
return result;
}
- private MimeMultipart createMimeMultipart(BatchOperations operations) {
- List<DataSource> bodyPartContents = new ArrayList<DataSource>();
- int contentId = 1;
- for (Operation operation : operations.getOperations()) {
-
- DataSource bodyPartContent = null;
- // INSERT
- if (operation instanceof InsertEntityOperation) {
- InsertEntityOperation op = (InsertEntityOperation) operation;
-
- //
- // Stream content into byte[] so that we have the length
- //
- InputStream stream = atomReaderWriter.generateEntityEntry(op.getEntity());
- byte[] bytes = inputStreamToByteArray(stream);
-
- //
- // Create body of MIME part as the HTTP request
- //
- InternetHeaders headers = new InternetHeaders();
- headers.addHeader("Content-ID", Integer.toString(contentId++));
- headers.addHeader("Content-Type", "application/atom+xml;type=entry");
- headers.addHeader("Content-Length", Integer.toString(bytes.length));
-
- //TODO: Review code to make sure encoding is correct
- ByteArrayOutputStream httpRequest = new ByteArrayOutputStream();
- httpReaderWriter.appendMethod(httpRequest, "POST", channel.resource(url).path(op.getTable()).getURI());
- httpReaderWriter.appendHeaders(httpRequest, headers);
- httpReaderWriter.appendEntity(httpRequest, new ByteArrayInputStream(bytes));
-
- bodyPartContent = new InputStreamDataSource(new ByteArrayInputStream(httpRequest.toByteArray()),
- "application/http");
- }
-
- if (bodyPartContent != null) {
- bodyPartContents.add(bodyPartContent);
- }
- }
-
- return mimeReaderWriter.getMimeMultipart(bodyPartContents);
- }
-
private byte[] inputStreamToByteArray(InputStream inputStream) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
@@ -691,5 +691,4 @@ private MimeMultipart createMimeMultipart(BatchOperations operations) {
throw new RuntimeException(e);
}
}
-
}
@@ -40,14 +40,15 @@ public BatchOperations addInsertOrMergeEntity(String table, Entity entity) {
}
public BatchOperations addDeleteEntity(String table, String partitionKey, String rowKey) {
- this.operations.add(new DeleteEntityOperation().setTable(table).setPartitionKey(partitionKey).setRowKey(rowKey));
+ this.operations
+ .add(new DeleteEntityOperation().setTable(table).setPartitionKey(partitionKey).setRowKey(rowKey));
return this;
}
- public abstract class Operation {
+ public static abstract class Operation {
}
- public class InsertEntityOperation extends Operation {
+ public static class InsertEntityOperation extends Operation {
private String table;
private Entity entity;
@@ -70,7 +71,7 @@ public InsertEntityOperation setEntity(Entity entity) {
}
}
- public class UpdateEntityOperation extends Operation {
+ public static class UpdateEntityOperation extends Operation {
private String table;
private Entity entity;
@@ -93,7 +94,7 @@ public UpdateEntityOperation setEntity(Entity entity) {
}
}
- public class MergeEntityOperation extends Operation {
+ public static class MergeEntityOperation extends Operation {
private String table;
private Entity entity;
@@ -116,7 +117,7 @@ public MergeEntityOperation setEntity(Entity entity) {
}
}
- public class InsertOrReplaceEntityOperation extends Operation {
+ public static class InsertOrReplaceEntityOperation extends Operation {
private String table;
private Entity entity;
@@ -139,7 +140,7 @@ public InsertOrReplaceEntityOperation setEntity(Entity entity) {
}
}
- public class InsertOrMergeEntityOperation extends Operation {
+ public static class InsertOrMergeEntityOperation extends Operation {
private String table;
private Entity entity;
@@ -162,7 +163,7 @@ public InsertOrMergeEntityOperation setEntity(Entity entity) {
}
}
- public class DeleteEntityOperation extends Operation {
+ public static class DeleteEntityOperation extends Operation {
private String table;
private String partitionKey;
private String rowKey;

0 comments on commit 18c8255

Please sign in to comment.