Advertisement
Guest User

U1Test

a guest
Feb 2nd, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Boolean isSyncingModeOn = false;
  2. Boolean isPingModeOn = false;
  3. // Oczywiście zamiast UbuntuOneTest każdy wstawia własną klasę
  4. final String packageName = UbuntuOneTest.class.getPackage().getName();
  5. final String version = "0.1";
  6. final HttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
  7. final String login = "mail@gmail.com";
  8. final String password = "moje_hasło";
  9. final String rootNode = "~/Ubuntu One";
  10. String token = "140 dziwnych znaczków";
  11. // Jeżeli nie mamy zapisanego tokena:
  12. //String token = null;
  13.  
  14. BasicAuthorizer basicAuthorizer = new BasicAuthorizer(login, password);
  15. U1AuthAPI authAPI = new U1AuthAPI(packageName, version, httpClient, basicAuthorizer);
  16. OAuthAuthorizer oAuthAuthorizer;
  17.  
  18. if (token == null) {
  19.     AuthenticateResponse authenticateResponse = authAPI.authenticate("Ubuntu One @ Moja Aplikacja");
  20.     oAuthAuthorizer = new OAuthAuthorizer(authenticateResponse);
  21.     token = authenticateResponse.getSerialized();
  22. } else {
  23.     oAuthAuthorizer = OAuthAuthorizer.getWithTokens(token, new PlainTextMessageSigner());
  24. }
  25.  
  26. authAPI.setAuthorizer(oAuthAuthorizer);
  27.  
  28. if (isSyncingModeOn) {
  29.     OAuthAuthorizer.syncTimeWithSSO(httpClient);
  30.     OAuthAuthorizer.syncTimeWithU1(httpClient);
  31. }
  32. if (isPingModeOn) {
  33.     authAPI.pingUbuntuOne(login);
  34. }
  35.  
  36. final U1FileAPI fileAPI = new U1FileAPI(packageName, version,httpClient, oAuthAuthorizer);
  37.    
  38. // Tworzy katalog /test/ w obrębie domyślnego woluminu
  39. fileAPI.makeDirectory(
  40.     rootNode+"/test/",
  41.     new U1NodeListener() {
  42.         @Override
  43.         public void onSuccess(U1Node u1Node) {
  44.             System.out.println("Stworzyłem katalog "+u1Node.getName());
  45.         }
  46.     }
  47. );
  48.  
  49. // Uploaduje tekstowy plik log1 do uprzednio utworzonego katalogu
  50. fileAPI.uploadFile(
  51.     "/home/frank/log1",       // ścieżka na dysku
  52.     "text:plain",               // zawartość pliku
  53.     rootNode+"/test/log1",  // ścieżka docelowa
  54.     true,                       // czy serwer może zduplikować plik,
  55.                                 // jeżeli posiada już identyczny
  56.     false,                  // czy od razu upublicznić
  57.     new U1UploadListener() {
  58.         @Override
  59.         public void onSuccess(U1Node u1Node) {
  60.             System.out.println("Zuploadowałem plik: "+u1Node.getName());
  61.         }
  62.     },
  63.     null                     // cancelTrigger
  64. );
  65.  
  66. // Pobiera utworzony plik, zapisuje go jako [new]log1
  67. fileAPI.downloadFile(
  68.     rootNode+"/test/log1",
  69.     "/home/frank/[new]log1",
  70.     new U1DownloadListener() {
  71.         @Override
  72.         public void onSuccess() {
  73.             System.out.println("Pobieranie zakończone");
  74.         }
  75.     },
  76.     null
  77. );
  78.  
  79. // Usuwa plik
  80. fileAPI.deleteNode(
  81.     rootNode+"/test/log1",
  82.     new U1NodeListener() {
  83.         @Override
  84.         public void onSuccess(U1Node u1Node) {
  85.             System.out.println(u1Node.getName()+" usunięty");
  86.         }
  87.     }
  88. );
  89.  
  90. // Wypisuje nazwy i linki do publicznych plików (nie węzłów)
  91. // z domyślnego katalogu domyślnego woluminu
  92. fileAPI.listDirectory(
  93.     rootNode,
  94.     new U1NodeListener() {
  95.         @Override
  96.         public void onSuccess(U1Node u1Node) {
  97.             if (u1Node.getKind() == U1NodeKind.FILE) {
  98.                 U1File file = (U1File)u1Node;
  99.                 if (file.getIsPublic())
  100.                     System.out.println(file.getName()+": "+file.getPublicUrl()));
  101.             }
  102.         }
  103.     }
  104. );
  105.  
  106. // Wypisuje nazwy i adresy wszystkich publicznych plików
  107. // z _całej struktury_ konta U1
  108. fileAPI.getPublicFiles(new U1NodeListener() {
  109.     @Override
  110.     public void onSuccess(U1Node u1Node) {
  111.         U1File file = (U1File)u1Node;
  112.         System.out.println(String.format("%s: %s", file.getName(), file.getPublicUrl()));
  113.     }
  114. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement