use of com.dropbox.core.v2.users.Name 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.users.Name in project dropbox-sdk-java by dropbox.
the class DropboxBrowse method renderFolder.
private void renderFolder(HttpServletResponse response, User user, DbxClientV2 dbxClient, String path) throws IOException {
// Get the folder listing from Dropbox.
TreeMap<String, Metadata> children = new TreeMap<String, Metadata>();
ListFolderResult result;
try {
try {
result = dbxClient.files().listFolder(path);
} catch (ListFolderErrorException ex) {
if (ex.errorValue.isPath()) {
if (checkPathError(response, path, ex.errorValue.getPathValue()))
return;
}
throw ex;
}
while (true) {
for (Metadata md : result.getEntries()) {
if (md instanceof DeletedMetadata) {
children.remove(md.getPathLower());
} else {
children.put(md.getPathLower(), md);
}
}
if (!result.getHasMore())
break;
try {
result = dbxClient.files().listFolderContinue(result.getCursor());
} catch (ListFolderContinueErrorException ex) {
if (ex.errorValue.isPath()) {
if (checkPathError(response, path, ex.errorValue.getPathValue()))
return;
}
throw ex;
}
}
} catch (DbxException ex) {
common.handleDbxException(response, user, ex, "listFolder(" + jq(path) + ")");
return;
}
FormProtection fp = FormProtection.start(response);
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = new PrintWriter(IOUtil.utf8Writer(response.getOutputStream()));
out.println("<html>");
out.println("<head><title>" + escapeHtml4(path) + "- Web File Browser</title></head>");
out.println("<body>");
fp.insertAntiRedressHtml(out);
out.println("<h2>Path: " + escapeHtml4(path) + "</h2>");
// Upload form
out.println("<form action='/upload' method='post' enctype='multipart/form-data'>");
fp.insertAntiCsrfFormField(out);
out.println("<label for='file'>Upload file:</label> <input name='file' type='file'/>");
out.println("<input type='submit' value='Upload'/>");
out.println("<input name='targetFolder' type='hidden' value='" + escapeHtml4(path) + "'/>");
out.println("</form>");
// Listing of folder contents.
out.println("<ul>");
for (Metadata child : children.values()) {
String href = "/browse?path=" + DbxRequestUtil.encodeUrlParam(child.getPathLower());
out.println(" <li><a href='" + escapeHtml4(href) + "'>" + escapeHtml4(child.getName()) + "</a></li>");
}
out.println("</ul>");
out.println("</body>");
out.println("</html>");
out.flush();
}
Aggregations