private Entry uploadLastChunk( byte[] bytes, int position, int length, String fileType, HttpRequestFactory requestFactory, long size, String location) { try { HttpResponse response = putRequest(bytes, position, length, fileType, requestFactory, size, location); if (!response.isSuccessStatusCode()) { String message = "upload finished, but there was an error during upload, " + response.getStatusCode() + ": " + response.getStatusMessage(); logger.error(message); throw new CoreException(message); } return response.parseAs(Entry.class); } catch (IOException e) { String message = "unable to parse response " + e.getMessage(); logger.error(message); throw new CoreException(message); } }
private String uploadChunk( byte[] bytes, int position, int length, String fileType, HttpRequestFactory requestFactory, long size, String location) { String newLocation = location; try { putRequest(bytes, position, length, fileType, requestFactory, size, location); } catch (HttpResponseException e) { if (e.getStatusCode() == 308) { newLocation = getNextLocation(newLocation, e); } else { String message = "unable to upload a chunk, response: " + e.getMessage(); logger.debug(message); throw new CoreException(message, e); } } return newLocation; }
private HttpResponse putRequest( byte[] bytes, int position, int length, String fileType, HttpRequestFactory requestFactory, long size, String location) throws HttpResponseException { try { InputStreamContent inputStreamContent = new InputStreamContent(fileType, new ByteArrayInputStream(bytes)); HttpRequest chunkRequest = requestFactory.buildPutRequest(new DocsUrl(location), inputStreamContent); chunkRequest.getHeaders().setContentRange(range(position, length, size)); chunkRequest.getHeaders().setContentLength((long) length); return requestExecutor.executeNonSensitive(chunkRequest); } catch (HttpResponseException e) { throw e; } catch (Exception e) { String message = "unable to upload chunk number " + position + ", " + e.getMessage(); logger.error(message); throw new CoreException(message, e); } }
public class Feed { private static Logger logger = Logger.getLogger(Feed.class); private static RequestExecutor requestExecutor = new RequestExecutor(); @Key("openSearch:totalResults") public int totalResults; @Key("link") public List<Link> links; @Key("entry") public List<DocumentListEntry> docs = new ArrayList<DocumentListEntry>(); public Link getLink(LinkRel linkRel) { return Link.find(links, linkRel); } public static Feed first100Entries(HttpRequestFactory requestFactory, DocsUrl url) throws IOException { HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse response = requestExecutor.executeNonSensitive(request); return response.parseAs(Feed.class); } public static Iterable<DocumentListEntry> allEntries( final HttpRequestFactory requestFactory, final DocsUrl url) throws IOException { return new Iterable<DocumentListEntry>() { @Override public Iterator<DocumentListEntry> iterator() { return new DocumentIterator(requestFactory, url); } }; } public static Entry executeGet( HttpRequestFactory requestFactory, DocsUrl url, Class<Entry> feedClass) throws IOException { HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse response = requestExecutor.executeNonSensitive(request); return response.parseAs(feedClass); } public static InputStream executeGetContent(HttpRequestFactory requestFactory, DocsUrl url) throws IOException { HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse response = requestExecutor.executeNonSensitive(request); return response.getContent(); } public static DocumentListEntry executePost( HttpRequestFactory requestFactory, DocsUrl url, AtomContent content) throws IOException { HttpRequest request = requestFactory.buildPostRequest(url, content); HttpResponse response = requestExecutor.executeSensitive(request); return response.parseAs(DocumentListEntry.class); } public static void executeDelete( HttpRequestFactory requestFactory, DocsUrl url, String etag, boolean matchAll) throws IOException { HttpRequest request = requestFactory.buildDeleteRequest(url); if (matchAll) request.getHeaders().setIfMatch("*"); else request.getHeaders().setIfMatch(etag); HttpResponse response = requestExecutor.executeNonSensitive(request); response.ignore(); } }
public class ResumableUploader { private static Logger logger = Logger.getLogger(ResumableUploader.class); public final int CHUNK_SIZE = 262144; private RequestExecutor requestExecutor; public ResumableUploader() { this.requestExecutor = new RequestExecutor(); } public RequestExecutor getRequestExecutor() { return requestExecutor; } private String uploadChunk( byte[] bytes, int position, int length, String fileType, HttpRequestFactory requestFactory, long size, String location) { String newLocation = location; try { putRequest(bytes, position, length, fileType, requestFactory, size, location); } catch (HttpResponseException e) { if (e.getStatusCode() == 308) { newLocation = getNextLocation(newLocation, e); } else { String message = "unable to upload a chunk, response: " + e.getMessage(); logger.debug(message); throw new CoreException(message, e); } } return newLocation; } private String getNextLocation(String newLocation, HttpResponseException e) { // try { String tempLocation = e.getHeaders().getLocation(); if (tempLocation != null) { newLocation = tempLocation; } // e.getResponse().ignore(); /*} catch (IOException e1) { String message = "unable to ignore response, " + e1.getMessage(); logger.error(message); throw new CoreException(message, e1); }*/ return newLocation; } private Entry uploadLastChunk( byte[] bytes, int position, int length, String fileType, HttpRequestFactory requestFactory, long size, String location) { try { HttpResponse response = putRequest(bytes, position, length, fileType, requestFactory, size, location); if (!response.isSuccessStatusCode()) { String message = "upload finished, but there was an error during upload, " + response.getStatusCode() + ": " + response.getStatusMessage(); logger.error(message); throw new CoreException(message); } return response.parseAs(Entry.class); } catch (IOException e) { String message = "unable to parse response " + e.getMessage(); logger.error(message); throw new CoreException(message); } } private HttpResponse putRequest( byte[] bytes, int position, int length, String fileType, HttpRequestFactory requestFactory, long size, String location) throws HttpResponseException { try { InputStreamContent inputStreamContent = new InputStreamContent(fileType, new ByteArrayInputStream(bytes)); HttpRequest chunkRequest = requestFactory.buildPutRequest(new DocsUrl(location), inputStreamContent); chunkRequest.getHeaders().setContentRange(range(position, length, size)); chunkRequest.getHeaders().setContentLength((long) length); return requestExecutor.executeNonSensitive(chunkRequest); } catch (HttpResponseException e) { throw e; } catch (Exception e) { String message = "unable to upload chunk number " + position + ", " + e.getMessage(); logger.error(message); throw new CoreException(message, e); } } private String initUpload( HttpRequestFactory requestFactory, DocsUrl url, long size, String fileType, AtomContent fileContent) throws IOException { HttpRequest request = requestFactory.buildPostRequest(url, fileContent); request.getHeaders().set("X-Upload-Content-Type", fileType); request.getHeaders().set("X-Upload-Content-Length", size); HttpResponse response = requestExecutor.executeNonSensitive(request); if (!response.isSuccessStatusCode()) { throw new CoreException( "unable to initiate upload, " + response.getStatusCode() + ": " + response.getStatusMessage()); } response.ignore(); return response.getHeaders().getLocation(); } public String range(long startByte, long length, long size) { long endByte = startByte; if (length > 0) endByte = startByte + (length - 1); return "bytes " + startByte + "-" + endByte + "/" + size; } public Entry createFileContent( final HttpRequestFactory requestFactory, final DocsUrl url, final InputStream is, final long size, final String fileType, AtomContent fileContent) throws IOException { String location = initUpload(requestFactory, url, size, fileType, fileContent); ChunkUploader<String, Entry> uploader = new ChunkUploader<String, Entry>(size, location) { @Override protected String onChunk(byte[] bytes, int position, int length, String location) { return uploadChunk(bytes, position, length, fileType, requestFactory, size, location); } @Override protected Entry onLastChunk(byte[] bytes, int position, int length, String location) { return uploadLastChunk( bytes, position, length, fileType, requestFactory, size, location); } }; return uploader.upload(is, CHUNK_SIZE); } }