      1 // The Antville Project
      2 // http://code.google.com/p/antville
      3 //
      4 // Copyright 2007-2011 by Tobi Schäfer.
      5 //
      6 // Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer,
      7 // Matthias & Michael Platzer, Christoph Lincke.
      8 //
      9 // Licensed under the Apache License, Version 2.0 (the ``License'');
     10 // you may not use this file except in compliance with the License.
     11 // You may obtain a copy of the License at
     12 //
     13 //    http://www.apache.org/licenses/LICENSE-2.0
     14 //
     15 // Unless required by applicable law or agreed to in writing, software
     16 // distributed under the License is distributed on an ``AS IS'' BASIS,
     17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     18 // See the License for the specific language governing permissions and
     19 // limitations under the License.
     20 //
     21 // $Revision$
     22 // $Author$
     23 // $Date$
     24 // $URL$
     25
     26 /**
     27  * @fileoverview Defines the DropboxClient prototype similar to node-dropbox.
     28  * @see https://github.com/evnm/dropbox-node
     29  */
     30
     31 app.addRepository("/Users/tobi/.m2/repository/oauth/signpost/signpost-core/1.2/signpost-core-1.2.jar");
     32
     33 var DropboxClient = function(id, key) {
     34    var API_URI = "https://api.dropbox.com/0";
     35    var CONTENT_API_URI = "https://api-content.dropbox.com/0";
     36
     37    var consumer;
     38
     39    var signpost = Packages.oauth.signpost;
     40
     41    var provider = new signpost.basic.DefaultOAuthProvider(
     42          API_URI + "/oauth/request_token",
     43          API_URI + "/oauth/access_token",
     44          API_URI + "/oauth/authorize");
     45
     46    var get = function(url) {
     47       res.debug(url);
     48       var result;
     49       var url = java.net.URL(url);
     50       var request = url.openConnection();
     51       consumer.sign(request);
     52       var status = request.getResponseCode();
     53       if (status === 200) {
     54          var buffer;
     55          var contentLength = request.contentLength;
     56          if (request.getContentType().startsWith("text/") || contentLength === -1) {
     57             buffer = [];
     58             var streamReader = new java.io.InputStreamReader(request.getInputStream());
     59             var bufferedReader = new java.io.BufferedReader(streamReader);
     60             var line;
     61             while ((line = bufferedReader.readLine()) !== null) {
     62                buffer.push(line);
     63             }
     64             streamReader.close();
     65             result = buffer.join("");
     66          } else {
     67             buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, contentLength);
     68             var inputStream = new java.io.BufferedInputStream(request.getInputStream());
     69             var bytesRead = 0, offset = 0;
     70             while (offset < contentLength) {
     71                bytesRead = inputStream.read(buffer, offset, buffer.length - offset);
     72                if (bytesRead === -1) {
     73                   break;
     74                }
     75                offset += bytesRead;
     76             }
     77             inputStream.close();
     78             result = buffer;
     79          }
     80          request.disconnect();
     81          return result;
     82       }
     83       return null;
     84    }
     85
     86    var post = function() {
     87
     88    }
     89
     90    this.connect = function(callback) {
     91       if (!consumer) {
     92          consumer = new signpost.basic.DefaultOAuthConsumer(id, key);
     93          var url = provider.retrieveRequestToken(consumer, root.href(req.action));
     94          return callback(url);
     95       } else if (req.data.oauth_token) {
     96          provider.retrieveAccessToken(consumer, req.data.oauth_token);
     97          consumer.setTokenWithSecret(consumer.getToken(), consumer.getTokenSecret());
     98          return callback();
     99       }/* else if (req.data.token && req.data.secret) {
    100          consumer = new signpost.basic.DefaultOAuthConsumer(id, key);
    101          consumer.setTokenWithSecret(req.data.token, req.data.secret);
    102          res.redirect(callbackUrl);
    103       }*/
    104       return;
    105    }
    106
    107    this.getMetadata = function(remotePath, options, callback) {
    108       if (typeof options === "function") callback = options, options = {};
    109       remotePath = escapePath(remotePath);
    110       try {
    111          var data = get(API_URI + "/metadata/dropbox" + remotePath + stringifyParams(options));
    112          return callback(null, JSON.parse(data));
    113       } catch (ex) {
    114          return callback(ex);
    115       }
    116    }
    117
    118    this.getFile = function(remotePath, options, callback) {
    119       if (typeof options === "function") callback = options, options = {};
    120       remotePath = escapePath(remotePath);
    121       try {
    122          var data = get(CONTENT_API_URI + "/files/dropbox" + remotePath + stringifyParams(options));
    123          return callback(null, data);
    124       } catch (ex) {
    125          return callback(ex);
    126       }
    127    }
    128
    129    this.test = function() {
    130       var data = get(CONTENT_API_URI + "/files/dropbox/PlainText/Hello.txt");
    131       res.debug(data);
    132       res.debug(stringifyParams({a: 1, b: 2}));
    133       return;
    134    }
    135
    136    function stringifyParams(param) {
    137       var buffer = [];
    138       for (let key in param) {
    139          buffer.push(encodeURIComponent(key) + "=" + encodeURIComponent(param[key]));
    140       }
    141       return buffer.length > 0 ? "?" + buffer.join("&") : "";
    142    }
    143
    144    function escapePath(p) {
    145       return encodeURIComponent(p).replace(/%2f/gi, '/');
    146    }
    147
    148    return this;
    149 }
    150