use of com.dropbox.core.v2.files.Metadata in project dropbox-sdk-java by dropbox.
the class DropboxBrowse method doUpload.
// -------------------------------------------------------------------------------------------
// POST /upload
// -------------------------------------------------------------------------------------------
public void doUpload(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
MultipartConfigElement multipartConfigElement = new MultipartConfigElement("/tmp");
request.setAttribute("org.eclipse.multipartConfig", multipartConfigElement);
if (!common.checkPost(request, response))
return;
User user = common.requireLoggedInUser(request, response);
if (user == null)
return;
DbxClientV2 dbxClient = requireDbxClient(request, response, user);
if (dbxClient == null)
return;
try {
// Just call getParts() to trigger the too-large exception.
request.getParts();
} catch (IllegalStateException ex) {
response.sendError(400, "Request too large");
return;
}
String targetFolder = slurpUtf8Part(request, response, "targetFolder", 1024);
if (targetFolder == null)
return;
Part filePart = request.getPart("file");
if (filePart == null) {
response.sendError(400, "Field \"file\" is missing.");
return;
}
String fileName = filePart.getName();
if (fileName == null) {
response.sendError(400, "Field \"file\" has no name.");
return;
}
// Upload file to Dropbox
String fullTargetPath = targetFolder + "/" + fileName;
FileMetadata metadata;
try {
metadata = dbxClient.files().upload(fullTargetPath).uploadAndFinish(filePart.getInputStream());
} catch (DbxException ex) {
common.handleDbxException(response, user, ex, "upload(" + jq(fullTargetPath) + ", ...)");
return;
} catch (IOException ex) {
response.sendError(400, "Error getting file data from you.");
return;
}
// Display uploaded file metadata.
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
out.println("<html>");
out.println("<head><title>File uploaded: " + escapeHtml4(metadata.getPathLower()) + "</title></head>");
out.println("<body>");
out.println("<h2>File uploaded: " + escapeHtml4(metadata.getPathLower()) + "</h2>");
out.println("<pre>");
out.print(escapeHtml4(metadata.toStringMultiline()));
out.println("</pre>");
out.println("</body>");
out.println("</html>");
out.flush();
}
use of com.dropbox.core.v2.files.Metadata in project dropbox-sdk-java by dropbox.
the class Main method chunkedUploadFile.
/**
* Uploads a file in chunks using multiple requests. This approach is preferred for larger files
* since it allows for more efficient processing of the file contents on the server side and
* also allows partial uploads to be retried (e.g. network connection problem will not cause you
* to re-upload all the bytes).
*
* @param dbxClient Dropbox user authenticated client
* @param localFIle local file to upload
* @param dropboxPath Where to upload the file to within Dropbox
*/
private static void chunkedUploadFile(DbxClientV2 dbxClient, File localFile, String dropboxPath) {
long size = localFile.length();
// below to simplify the logic.
if (size < CHUNKED_UPLOAD_CHUNK_SIZE) {
System.err.println("File too small, use upload() instead.");
System.exit(1);
return;
}
long uploaded = 0L;
DbxException thrown = null;
// Chunked uploads have 3 phases, each of which can accept uploaded bytes:
//
// (1) Start: initiate the upload and get an upload session ID
// (2) Append: upload chunks of the file to append to our session
// (3) Finish: commit the upload and close the session
//
// We track how many bytes we uploaded to determine which phase we should be in.
String sessionId = null;
for (int i = 0; i < CHUNKED_UPLOAD_MAX_ATTEMPTS; ++i) {
if (i > 0) {
System.out.printf("Retrying chunked upload (%d / %d attempts)\n", i + 1, CHUNKED_UPLOAD_MAX_ATTEMPTS);
}
try (InputStream in = new FileInputStream(localFile)) {
// if this is a retry, make sure seek to the correct offset
in.skip(uploaded);
// (1) Start
if (sessionId == null) {
sessionId = dbxClient.files().uploadSessionStart().uploadAndFinish(in, CHUNKED_UPLOAD_CHUNK_SIZE).getSessionId();
uploaded += CHUNKED_UPLOAD_CHUNK_SIZE;
printProgress(uploaded, size);
}
UploadSessionCursor cursor = new UploadSessionCursor(sessionId, uploaded);
// (2) Append
while ((size - uploaded) > CHUNKED_UPLOAD_CHUNK_SIZE) {
dbxClient.files().uploadSessionAppendV2(cursor).uploadAndFinish(in, CHUNKED_UPLOAD_CHUNK_SIZE);
uploaded += CHUNKED_UPLOAD_CHUNK_SIZE;
printProgress(uploaded, size);
cursor = new UploadSessionCursor(sessionId, uploaded);
}
// (3) Finish
long remaining = size - uploaded;
CommitInfo commitInfo = CommitInfo.newBuilder(dropboxPath).withMode(WriteMode.ADD).withClientModified(new Date(localFile.lastModified())).build();
FileMetadata metadata = dbxClient.files().uploadSessionFinish(cursor, commitInfo).uploadAndFinish(in, remaining);
System.out.println(metadata.toStringMultiline());
return;
} catch (RetryException ex) {
thrown = ex;
// RetryExceptions are never automatically retried by the client for uploads. Must
// catch this exception even if DbxRequestConfig.getMaxRetries() > 0.
sleepQuietly(ex.getBackoffMillis());
continue;
} catch (NetworkIOException ex) {
thrown = ex;
// network issue with Dropbox (maybe a timeout?) try again
continue;
} catch (UploadSessionLookupErrorException ex) {
if (ex.errorValue.isIncorrectOffset()) {
thrown = ex;
// server offset into the stream doesn't match our offset (uploaded). Seek to
// the expected offset according to the server and try again.
uploaded = ex.errorValue.getIncorrectOffsetValue().getCorrectOffset();
continue;
} else {
// Some other error occurred, give up.
System.err.println("Error uploading to Dropbox: " + ex.getMessage());
System.exit(1);
return;
}
} catch (UploadSessionFinishErrorException ex) {
if (ex.errorValue.isLookupFailed() && ex.errorValue.getLookupFailedValue().isIncorrectOffset()) {
thrown = ex;
// server offset into the stream doesn't match our offset (uploaded). Seek to
// the expected offset according to the server and try again.
uploaded = ex.errorValue.getLookupFailedValue().getIncorrectOffsetValue().getCorrectOffset();
continue;
} else {
// some other error occurred, give up.
System.err.println("Error uploading to Dropbox: " + ex.getMessage());
System.exit(1);
return;
}
} catch (DbxException ex) {
System.err.println("Error uploading to Dropbox: " + ex.getMessage());
System.exit(1);
return;
} catch (IOException ex) {
System.err.println("Error reading from file \"" + localFile + "\": " + ex.getMessage());
System.exit(1);
return;
}
}
// if we made it here, then we must have run out of attempts
System.err.println("Maxed out upload attempts to Dropbox. Most recent error: " + thrown.getMessage());
System.exit(1);
}
use of com.dropbox.core.v2.files.Metadata in project dropbox-sdk-java by dropbox.
the class Main method uploadFile.
/**
* Uploads a file in a single request. This approach is preferred for small files since it
* eliminates unnecessary round-trips to the servers.
*
* @param dbxClient Dropbox user authenticated client
* @param localFIle local file to upload
* @param dropboxPath Where to upload the file to within Dropbox
*/
private static void uploadFile(DbxClientV2 dbxClient, File localFile, String dropboxPath) {
try (InputStream in = new FileInputStream(localFile)) {
FileMetadata metadata = dbxClient.files().uploadBuilder(dropboxPath).withMode(WriteMode.ADD).withClientModified(new Date(localFile.lastModified())).uploadAndFinish(in);
System.out.println(metadata.toStringMultiline());
} catch (UploadErrorException ex) {
System.err.println("Error uploading to Dropbox: " + ex.getMessage());
System.exit(1);
} catch (DbxException ex) {
System.err.println("Error uploading to Dropbox: " + ex.getMessage());
System.exit(1);
} catch (IOException ex) {
System.err.println("Error reading from file \"" + localFile + "\": " + ex.getMessage());
System.exit(1);
}
}
use of com.dropbox.core.v2.files.Metadata in project dropbox-sdk-java by dropbox.
the class DbxClientV2Test method testRetrySuccess.
@Test
public void testRetrySuccess() throws Exception {
HttpRequestor mockRequestor = mock(HttpRequestor.class);
DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
FileMetadata expected = constructFileMetadate();
// 503 twice, then return result
HttpRequestor.Uploader mockUploader = mockUploader();
when(mockUploader.finish()).thenReturn(createEmptyResponse(503)).thenReturn(createEmptyResponse(503)).thenReturn(createSuccessResponse(serialize(expected)));
when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
Metadata actual = client.files().getMetadata(expected.getId());
// should have only been called 3 times: initial call + 2 retries
verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
assertEquals(actual.getName(), expected.getName());
assertTrue(actual instanceof FileMetadata, actual.getClass().toString());
assertEquals(((FileMetadata) actual).getId(), expected.getId());
}
use of com.dropbox.core.v2.files.Metadata in project dropbox-sdk-java by dropbox.
the class DropboxBrowse method doBrowse.
// -------------------------------------------------------------------------------------------
// GET /browse?path=...
// -------------------------------------------------------------------------------------------
// The page that lets you browse your Dropbox account. Makes Dropbox API calls to figure out
// the contents of your files and folders and display them to you.
public void doBrowse(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (!common.checkGet(request, response))
return;
User user = common.getLoggedInUser(request);
if (user == null) {
common.pageSoftError(response, "Can't do /browse. Nobody is logged in.");
return;
}
DbxClientV2 dbxClient = requireDbxClient(request, response, user);
if (dbxClient == null)
return;
String path = request.getParameter("path");
if (path == null || path.length() == 0) {
renderFolder(response, user, dbxClient, "");
} else {
String pathError = DbxPathV2.findError(path);
if (pathError != null) {
response.sendError(400, "Invalid path: " + jq(path) + ": " + pathError);
return;
}
Metadata metadata;
try {
metadata = dbxClient.files().getMetadata(path);
} catch (GetMetadataErrorException ex) {
if (ex.errorValue.isPath()) {
LookupError le = ex.errorValue.getPathValue();
if (le.isNotFound()) {
response.sendError(400, "Path doesn't exist on Dropbox: " + jq(path));
return;
}
}
common.handleException(response, ex, "getMetadata(" + jq(path) + ")");
return;
} catch (DbxException ex) {
common.handleDbxException(response, user, ex, "getMetadata(" + jq(path) + ")");
return;
}
path = DbxPathV2.getParent(path) + "/" + metadata.getName();
if (metadata instanceof FolderMetadata) {
renderFolder(response, user, dbxClient, path);
} else {
renderFile(response, path, (FileMetadata) metadata);
}
}
}
Aggregations