Skip to content

Commit

Permalink
Experimenting with streaming uploads - why doesn't it do this by defa…
Browse files Browse the repository at this point in the history
…ult :(. This particular changeset breaks a lot of stuff, need to refactor blend4j to allow conditionally setting streaming chunk size on a per route basis.
  • Loading branch information
jmchilton committed Sep 9, 2013
1 parent a77fb3a commit 22608de
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/main/java/com/github/jmchilton/blend4j/BaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;
import com.sun.jersey.multipart.file.StreamDataBodyPart;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -17,6 +21,10 @@
import org.codehaus.jackson.type.TypeReference;

public class BaseClient {
// http://stackoverflow.com/questions/10326460/upload-a-large-file-using-jersey-client
// Why does it only chunk streams and not files, seems wrong?
private static final boolean STREAMING_FILES = false;

protected final ObjectMapper mapper = new ObjectMapper();
protected final WebResource webResource;

Expand Down Expand Up @@ -94,8 +102,18 @@ protected Iterable<BodyPart> prepareUploads(final Iterable<File> files) {
final List<BodyPart> bodyParts = new ArrayList<BodyPart>();
for(final File file : files) {
final String paramName = "files_0|file_data";
final FileDataBodyPart fdbp = new FileDataBodyPart(paramName, file);
bodyParts.add(fdbp);
final BodyPart bp;
if(STREAMING_FILES) {
try {
final InputStream fileInStream = new FileInputStream(file);
bp = new StreamDataBodyPart(paramName, fileInStream, file.getName());
} catch(final FileNotFoundException e) {
throw new RuntimeException(e);
}
} else{
bp = new FileDataBodyPart(paramName, file);
}
bodyParts.add(bp);
}
return bodyParts;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class DefaultWebResourceFactoryImpl implements WebResourceFactory {
private static final boolean DEBUG = false;
public static String API_PATH = "api";
private static final Integer CHUNKED_ENCODING_SIZE = Integer.valueOf(1024);
private String url;
private String key;

Expand All @@ -29,6 +30,9 @@ public DefaultWebResourceFactoryImpl(final String url, final String key) {
*/
public WebResource get() {
final com.sun.jersey.api.client.Client client = getJerseyClient();
if(CHUNKED_ENCODING_SIZE != null) {
client.setChunkedEncodingSize(CHUNKED_ENCODING_SIZE);
}
final String apiKey = getApiKey();
WebResource resource = client.resource(getGalaxyUrl()).path(API_PATH);
if(apiKey != null) {
Expand Down

0 comments on commit 22608de

Please sign in to comment.