Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Boolean isSyncingModeOn = false;
- Boolean isPingModeOn = false;
- // Oczywiście zamiast UbuntuOneTest każdy wstawia własną klasę
- final String packageName = UbuntuOneTest.class.getPackage().getName();
- final String version = "0.1";
- final HttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
- final String login = "mail@gmail.com";
- final String password = "moje_hasło";
- final String rootNode = "~/Ubuntu One";
- String token = "140 dziwnych znaczków";
- // Jeżeli nie mamy zapisanego tokena:
- //String token = null;
- BasicAuthorizer basicAuthorizer = new BasicAuthorizer(login, password);
- U1AuthAPI authAPI = new U1AuthAPI(packageName, version, httpClient, basicAuthorizer);
- OAuthAuthorizer oAuthAuthorizer;
- if (token == null) {
- AuthenticateResponse authenticateResponse = authAPI.authenticate("Ubuntu One @ Moja Aplikacja");
- oAuthAuthorizer = new OAuthAuthorizer(authenticateResponse);
- token = authenticateResponse.getSerialized();
- } else {
- oAuthAuthorizer = OAuthAuthorizer.getWithTokens(token, new PlainTextMessageSigner());
- }
- authAPI.setAuthorizer(oAuthAuthorizer);
- if (isSyncingModeOn) {
- OAuthAuthorizer.syncTimeWithSSO(httpClient);
- OAuthAuthorizer.syncTimeWithU1(httpClient);
- }
- if (isPingModeOn) {
- authAPI.pingUbuntuOne(login);
- }
- final U1FileAPI fileAPI = new U1FileAPI(packageName, version,httpClient, oAuthAuthorizer);
- // Tworzy katalog /test/ w obrębie domyślnego woluminu
- fileAPI.makeDirectory(
- rootNode+"/test/",
- new U1NodeListener() {
- @Override
- public void onSuccess(U1Node u1Node) {
- System.out.println("Stworzyłem katalog "+u1Node.getName());
- }
- }
- );
- // Uploaduje tekstowy plik log1 do uprzednio utworzonego katalogu
- fileAPI.uploadFile(
- "/home/frank/log1", // ścieżka na dysku
- "text:plain", // zawartość pliku
- rootNode+"/test/log1", // ścieżka docelowa
- true, // czy serwer może zduplikować plik,
- // jeżeli posiada już identyczny
- false, // czy od razu upublicznić
- new U1UploadListener() {
- @Override
- public void onSuccess(U1Node u1Node) {
- System.out.println("Zuploadowałem plik: "+u1Node.getName());
- }
- },
- null // cancelTrigger
- );
- // Pobiera utworzony plik, zapisuje go jako [new]log1
- fileAPI.downloadFile(
- rootNode+"/test/log1",
- "/home/frank/[new]log1",
- new U1DownloadListener() {
- @Override
- public void onSuccess() {
- System.out.println("Pobieranie zakończone");
- }
- },
- null
- );
- // Usuwa plik
- fileAPI.deleteNode(
- rootNode+"/test/log1",
- new U1NodeListener() {
- @Override
- public void onSuccess(U1Node u1Node) {
- System.out.println(u1Node.getName()+" usunięty");
- }
- }
- );
- // Wypisuje nazwy i linki do publicznych plików (nie węzłów)
- // z domyślnego katalogu domyślnego woluminu
- fileAPI.listDirectory(
- rootNode,
- new U1NodeListener() {
- @Override
- public void onSuccess(U1Node u1Node) {
- if (u1Node.getKind() == U1NodeKind.FILE) {
- U1File file = (U1File)u1Node;
- if (file.getIsPublic())
- System.out.println(file.getName()+": "+file.getPublicUrl()));
- }
- }
- }
- );
- // Wypisuje nazwy i adresy wszystkich publicznych plików
- // z _całej struktury_ konta U1
- fileAPI.getPublicFiles(new U1NodeListener() {
- @Override
- public void onSuccess(U1Node u1Node) {
- U1File file = (U1File)u1Node;
- System.out.println(String.format("%s: %s", file.getName(), file.getPublicUrl()));
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement