use of com.dropbox.core.v2.DbxClientV2 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);
}
}
}
use of com.dropbox.core.v2.DbxClientV2 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();
}
use of com.dropbox.core.v2.DbxClientV2 in project dropbox-sdk-java by dropbox.
the class Main method main.
public static void main(String[] args) throws IOException {
// Only display important log messages.
Logger.getLogger("").setLevel(Level.WARNING);
if (args.length != 1) {
System.out.println("");
System.out.println("Usage: COMMAND <auth-file>");
System.out.println("");
System.out.println(" <auth-file>: An \"auth file\" that contains the information necessary to make");
System.out.println(" an authorized Dropbox API request. Generate this file using the \"authorize\"");
System.out.println(" example program.");
System.out.println("");
System.exit(1);
return;
}
DbxClientV2 client = createClient(args[0]);
runTest(client, Main::testBasicSerialization, "testBasicSerialization");
runTest(client, Main::testEnumeratedSubtypeSerialization, "testEnumeratedSubtypeSerialization");
runTest(client, Main::testErrorSerialization, "testErrorSerialization");
}
use of com.dropbox.core.v2.DbxClientV2 in project dropbox-sdk-java by dropbox.
the class Main method createClient.
private static DbxClientV2 createClient(String authFile) {
DbxAuthInfo authInfo;
try {
authInfo = DbxAuthInfo.Reader.readFromFile(authFile);
} catch (JsonReader.FileLoadException ex) {
System.err.println("Error loading <auth-file>: " + ex.getMessage());
System.exit(1);
return null;
}
DbxRequestConfig requestConfig = new DbxRequestConfig("examples-proguard");
return new DbxClientV2(requestConfig, authInfo.getAccessToken(), authInfo.getHost());
}
use of com.dropbox.core.v2.DbxClientV2 in project dropbox-sdk-java by dropbox.
the class Main method main.
public static void main(String[] args) throws DbxException, IOException {
// Create Dropbox client
DbxRequestConfig config = new DbxRequestConfig("dropbox/java-tutorial");
DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN);
// Get current account info
FullAccount account = client.users().getCurrentAccount();
System.out.println(account.getName().getDisplayName());
// Get files and folder metadata from Dropbox root directory
ListFolderResult result = client.files().listFolder("");
while (true) {
for (Metadata metadata : result.getEntries()) {
System.out.println(metadata.getPathLower());
}
if (!result.getHasMore()) {
break;
}
result = client.files().listFolderContinue(result.getCursor());
}
// Upload "test.txt" to Dropbox
try (InputStream in = new FileInputStream("test.txt")) {
FileMetadata metadata = client.files().uploadBuilder("/test.txt").uploadAndFinish(in);
}
DbxDownloader<FileMetadata> downloader = client.files().download("/test.txt");
try {
FileOutputStream out = new FileOutputStream("test.txt");
downloader.download(out);
out.close();
} catch (DbxException ex) {
System.out.println(ex.getMessage());
}
}
Aggregations