/** * */ package sd.rest.dropbox; import java.net.MalformedURLException; import java.rmi.NotBoundException; import java.rmi.RemoteException; import java.text.ParseException; import java.util.Iterator; import java.util.Scanner; import java.util.TreeSet; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.scribe.builder.ServiceBuilder; import org.scribe.builder.api.DropBoxApi; import org.scribe.model.OAuthRequest; import org.scribe.model.Response; import org.scribe.model.Token; import org.scribe.model.Verb; import org.scribe.model.Verifier; import org.scribe.oauth.OAuthService; import sd.files.EJSONObjectType; import sd.files.FileInfo; import sd.files.exceptions.FileNotFoundException; import sd.files.exceptions.NoSuchPathException; import sd.rmi.server.Server; /** * Classe que implementa um proxy sobre o servico da Dropbox * @author fmmarques */ public class DropboxProxy extends Server { private static final String API_KEY = "edy327k7yo0uoze"; private static final String API_SECRET = "nwyuk7lmqu056jh"; private static final String SCOPE = "app folder"; private static final String AUTHORIZE_URL = "https://www.dropbox.com/1/oauth/authorize?oauth_token="; private static final String CONTEXT = "sandbox"; private static final long serialVersionUID = 1147109062589751462L; private OAuthService service; private Token accessToken, requestToken; private JSONParser parser; private void getAuthorization() { this.service = new ServiceBuilder().provider(DropBoxApi.class).apiKey(API_KEY) .apiSecret(API_SECRET).scope(SCOPE).build(); Scanner in = new Scanner( System.in ); requestToken = service.getRequestToken(); Verifier verifier = new Verifier(""); try { Process browser = Runtime.getRuntime().exec( AUTHORIZE_URL + requestToken.getToken() ); browser.waitFor(); } catch( Exception e ) { System.out.println("Autorizacao sobre " + AUTHORIZE_URL + requestToken.getToken() + " e depois pressione [enter]." ); in.nextLine(); } verifier = new Verifier( requestToken.getSecret() ); this.accessToken = service.getAccessToken( requestToken, verifier ); } public DropboxProxy(String local_name, String path) throws RemoteException, NullPointerException, MalformedURLException { super(local_name, path); getAuthorization(); this.parser = new JSONParser(); // TODO Verificar se e necessario alguma alteracao adicional sobre o sistema. } public DropboxProxy(String name, String path, String remote_server) throws RemoteException, MalformedURLException, NotBoundException { super(name, path, remote_server); getAuthorization(); this.parser = new JSONParser(); // TODO Verificar se e necessario alguma alteracao adicional sobre o sistema. } @Override public FileInfo do_getAttr( String filename ) throws RemoteException { FileInfo retval = null; Response response = null; OAuthRequest request = new OAuthRequest( Verb.GET, "https://api.dropbox.com/1/metadata/"+ CONTEXT +'/'+ this.current_path + filename ); this.service.signRequest( this.accessToken, request ); response = request.send(); if ( 404 == response.getCode() ) { System.out.println( "[ erro@DropboxProxy.do_getAttr ] : ("+ response.getCode()+") "+ response.getBody() ); throw new FileNotFoundException( response.getBody() ); } else if ( 200 != response.getCode() ) { System.out.println( "[ erro@DropboxProxy.do_getAttr ] : ("+ response.getCode()+") "+ response.getBody() ); throw new RemoteException( response.getCode() + response.getBody() ); } try { retval = new FileInfo( (JSONObject)parser.parse( response.getBody() ), EJSONObjectType.DROPBOX_FORMAT, getName() ); } catch (ParseException e) { System.out.println("[ erro@DropboxProxy.do_getAttr ] : Falhou o parsing da resposta."); e.printStackTrace(); } catch (org.json.simple.parser.ParseException e) { System.out.println("[ erro@DropboxProxy.do_getAttr ] : Falhou o parsing da resposta."); e.printStackTrace(); } return retval; } @Override public TreeSet do_ls( String path ) throws NoSuchPathException, RemoteException { OAuthRequest request = new OAuthRequest( Verb.GET, "https://api.dropbox.com/1/metadata/"+CONTEXT+"/"+this.current_path+path+"&list=true"); this.service.signRequest( this.accessToken, request ); Response response; TreeSet retval = new TreeSet(); response = request.send(); if ( 200 != response.getCode() ) { System.out.println( "[ erro@DropboxProxy.do_getAttr ] : ("+ response.getCode()+") "+ response.getBody() ); if ( 404 == response.getCode() ) throw new NoSuchPathException( "O caminho " + path + " não foi encontrado em " + this.name ); else throw new RemoteException( response.getCode() + response.getBody() ); } Iterator it; JSONArray files; try { files = (JSONArray) ((JSONObject)this.parser.parse( response.getBody() )).get("contents"); } catch (org.json.simple.parser.ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); throw new RemoteException( e1.getLocalizedMessage() ); } it = files.iterator(); while( it.hasNext() ) { FileInfo file; try { file = new FileInfo( (JSONObject)it.next(), EJSONObjectType.DROPBOX_FORMAT, this.name ); retval.add( file ); } catch (ParseException e) { // TODO Melhorar esta excepção - mas isto é util caso o meu formato esteja errado e.printStackTrace(); } } return retval; } @Override public boolean do_mkdir( String dir_name ) throws NoSuchPathException, RemoteException { OAuthRequest request = new OAuthRequest( Verb.POST, "https://api.dropbox.com/1/fileops/create_folder"); Response response; String path_of_new_dir = current_path; if ( -1 != dir_name.lastIndexOf('/') ) path_of_new_dir += dir_name.substring(0, dir_name.lastIndexOf('/') ); request.addHeader("root", CONTEXT); request.addHeader("path", path_of_new_dir ); request.addHeader("locale", "en-US"); this.service.signRequest( this.accessToken, request ); response = request.send(); if ( 200 != response.getCode() ) { System.out.println( "[ erro@DropboxProxy.do_getAttr ] : ("+ response.getCode()+") "+ response.getBody() ); if ( 404 == response.getCode() ) throw new NoSuchPathException( "O caminho " + path_of_new_dir + " não foi encontrado em " + this.name ); else throw new RemoteException( response.getCode() + response.getBody() ); } return true; } }