Search in sources :

Example 6 with DbxException

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;
}
Also used : FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DbxException(com.dropbox.core.DbxException)

Example 7 with DbxException

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);
    }
}
Also used : StandardHttpRequestor(com.dropbox.core.http.StandardHttpRequestor) DbxClientV2(com.dropbox.core.v2.DbxClientV2) SocketTimeoutException(java.net.SocketTimeoutException) DbxApiException(com.dropbox.core.DbxApiException) ListFolderLongpollResult(com.dropbox.core.v2.files.ListFolderLongpollResult) DbxException(com.dropbox.core.DbxException) NetworkIOException(com.dropbox.core.NetworkIOException)

Example 8 with DbxException

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.");
    }
}
Also used : DbxOAuth1Upgrader(com.dropbox.core.DbxOAuth1Upgrader) DbxOAuth1AccessToken(com.dropbox.core.DbxOAuth1AccessToken) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAppInfo(com.dropbox.core.DbxAppInfo) JsonReader(com.dropbox.core.json.JsonReader) DbxException(com.dropbox.core.DbxException)

Example 9 with DbxException

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();
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) MultipartConfigElement(javax.servlet.MultipartConfigElement) Part(javax.servlet.http.Part) FileMetadata(com.dropbox.core.v2.files.FileMetadata) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) DbxException(com.dropbox.core.DbxException) PrintWriter(java.io.PrintWriter)

Example 10 with DbxException

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");
    }
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) ListFolderErrorException(com.dropbox.core.v2.files.ListFolderErrorException) DbxException(com.dropbox.core.DbxException)

Aggregations

DbxException (com.dropbox.core.DbxException)31 FileMetadata (com.dropbox.core.v2.files.FileMetadata)14 IOException (java.io.IOException)14 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)10 Metadata (com.dropbox.core.v2.files.Metadata)9 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)8 FolderMetadata (com.dropbox.core.v2.files.FolderMetadata)8 DbxWebAuth (com.dropbox.core.DbxWebAuth)7 File (java.io.File)6 JsonReader (com.dropbox.core.json.JsonReader)5 DeletedMetadata (com.dropbox.core.v2.files.DeletedMetadata)5 ListFolderResult (com.dropbox.core.v2.files.ListFolderResult)5 FileInputStream (java.io.FileInputStream)5 InputStream (java.io.InputStream)5 DbxAuthInfo (com.dropbox.core.DbxAuthInfo)4 BufferedReader (java.io.BufferedReader)4 FileOutputStream (java.io.FileOutputStream)4 InputStreamReader (java.io.InputStreamReader)4 Uri (android.net.Uri)3 DbxAppInfo (com.dropbox.core.DbxAppInfo)3