use of com.dropbox.core.DbxException in project drill by apache.
the class DropboxFileSystem method open.
@Override
public FSDataInputStream open(Path path, int bufferSize) throws IOException {
FSDataInputStream fsDataInputStream;
String filename = getFileName(path);
client = getClient();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
client.files().download(filename).download(out);
fsDataInputStream = new FSDataInputStream(new SeekableByteArrayInputStream(out.toByteArray()));
} catch (DbxException e) {
throw new IOException(e.getMessage());
}
return fsDataInputStream;
}
use of com.dropbox.core.DbxException in project dropbox-sdk-java by dropbox.
the class Main method longpoll.
/**
* Will perform longpoll request for changes in the user's Dropbox
* account and display those changes. This is more efficient that
* periodic polling the endpoint.
*/
public static void longpoll(DbxAuthInfo auth, String path) throws IOException {
long longpollTimeoutSecs = TimeUnit.MINUTES.toSeconds(2);
// need 2 Dropbox clients for making calls:
//
// (1) One for longpoll requests, with its read timeout set longer than our polling timeout
// (2) One for all other requests, with its read timeout set to the default, shorter timeout
//
StandardHttpRequestor.Config config = StandardHttpRequestor.Config.DEFAULT_INSTANCE;
StandardHttpRequestor.Config longpollConfig = config.copy().withReadTimeout(5, TimeUnit.MINUTES).build();
DbxClientV2 dbxClient = createClient(auth, config);
DbxClientV2 dbxLongpollClient = createClient(auth, longpollConfig);
try {
// We only care about file changes, not existing files, so grab latest cursor for this
// path and then longpoll for changes.
String cursor = getLatestCursor(dbxClient, path);
System.out.println("Longpolling for changes... press CTRL-C to exit.");
while (true) {
// will block for longpollTimeoutSecs or until a change is made in the folder
ListFolderLongpollResult result = dbxLongpollClient.files().listFolderLongpoll(cursor, longpollTimeoutSecs);
// we have changes, list them
if (result.getChanges()) {
cursor = printChanges(dbxClient, cursor);
}
// we were asked to back off from our polling, wait the requested amount of seconds
// before issuing another longpoll request.
Long backoff = result.getBackoff();
if (backoff != null) {
try {
System.out.printf("backing off for %d secs...\n", backoff.longValue());
Thread.sleep(TimeUnit.SECONDS.toMillis(backoff));
} catch (InterruptedException ex) {
System.exit(0);
}
}
}
} catch (DbxApiException ex) {
// if a user message is available, try using that instead
String message = ex.getUserMessage() != null ? ex.getUserMessage().getText() : ex.getMessage();
System.err.println("Error making API call: " + message);
System.exit(1);
} catch (NetworkIOException ex) {
System.err.println("Error making API call: " + ex.getMessage());
if (ex.getCause() instanceof SocketTimeoutException) {
System.err.println("Consider increasing socket read timeout or decreasing longpoll timeout.");
}
System.exit(1);
} catch (DbxException ex) {
System.err.println("Error making API call: " + ex.getMessage());
System.exit(1);
}
}
use of com.dropbox.core.DbxException 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.INFO);
Config cfg = parseArgs(args);
if (cfg == null) {
System.exit(1);
return;
}
// Read app info file (contains app key and app secret)
DbxAppInfo appInfo;
try {
appInfo = DbxAppInfo.Reader.readFromFile(cfg.appInfoFile);
} catch (JsonReader.FileLoadException ex) {
System.err.println("Error reading <app-info-file>: " + ex.getMessage());
System.exit(1);
return;
}
DbxOAuth1AccessToken oauth1AccessToken = new DbxOAuth1AccessToken(cfg.accessTokenKey, cfg.accessTokenSecret);
// Get an OAuth 2 access token.
DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
DbxOAuth1Upgrader upgrader = new DbxOAuth1Upgrader(requestConfig, appInfo);
String oauth2AccessToken;
try {
oauth2AccessToken = upgrader.createOAuth2AccessToken(oauth1AccessToken);
} catch (DbxException ex) {
System.err.println("Error getting OAuth 2 access token: " + ex.getMessage());
System.exit(1);
return;
}
System.out.println("OAuth 2 access token obtained.");
DbxAuthInfo authInfo = new DbxAuthInfo(oauth2AccessToken, appInfo.getHost());
DbxAuthInfo.Writer.writeToStream(authInfo, System.out);
System.out.println();
// Disable the OAuth 1 access token.
if (cfg.disable) {
try {
upgrader.disableOAuth1AccessToken(oauth1AccessToken);
} catch (DbxException ex) {
System.err.println("Error disabling OAuth 1 access token: " + ex.getMessage());
System.exit(1);
return;
}
System.out.println("OAuth 1 access token disabled.");
}
}
use of com.dropbox.core.DbxException 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.DbxException 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);
// Instantiate factory and set shared factory
DbxRequestUtil.sharedCallbackFactory = new DbxExampleGlobalCallbackFactory();
try {
// Get files and folder metadata from Dropbox root directory
client.files().listFolder("/does/not/exist/folder/");
} catch (ListFolderErrorException ex) {
System.err.println("STANDARD ROUTE ERROR HANDLER: " + ex.errorValue + "\n");
} catch (DbxException ex) {
System.err.println("STANDARD NETWORK ERROR HANDLER: " + ex + "\n");
}
try {
// Get files and folder metadata from Dropbox root directory
client.auth().tokenRevoke();
client.files().listFolder("/does/not/exist");
} catch (ListFolderErrorException ex) {
System.err.println("STANDARD ROUTE ERROR HANDLER2: " + ex.errorValue + "\n");
} catch (DbxException ex) {
System.err.println("STANDARD NETWORK ERROR HANDLER2: " + ex + "\n");
}
}
Aggregations