descriptionmaker.photobucket.AlbumUploader.java Source code

Java tutorial

Introduction

Here is the source code for descriptionmaker.photobucket.AlbumUploader.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package descriptionmaker.photobucket;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.ResourceBundle;

import junit.framework.TestCase;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;

import org.json.JSONException;
import org.json.JSONObject;

import com.photobucket.api.rest.RESTfulResponse;
import com.photobucket.api.core.*;

import descriptionmaker.gui.*;

/**
 *
 * @author Andrew
 */
public class AlbumUploader /*extends TestCase*/ {

    private PhotobucketAPI api = null;
    private String subdomain = null;
    //private String homeUrl = null;
    private String oauthToken = null;
    private String oauthTokenSecret = null;
    private final String API_SUBDOMAIN = "api.photobucket.com";

    private GuiState guiState = null;

    public void setUp() throws Exception {

        /*System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
        System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
        System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug");
        System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "debug"); */

        api = new PhotobucketAPI();
        api.setOauthConsumerKey(ResourceBundle.getBundle("consumer").getString("oauth.consumer"));
        api.setOauthConsumerSecret(ResourceBundle.getBundle("consumer").getString("oauth.consumer.key"));
    }

    public void tearDown() throws Exception {
        api = null;
    }

    public void loginJSON(String user, String pass, GuiState guiState) {
        Map<String, String> params = api.getParameters();
        params.put(user, pass);

        api.setSubdomain(this.API_SUBDOMAIN);
        api.setRequestPath("/login/direct/pbapi");
        api.setMethod("post");
        api.setFormat("json");

        this.guiState = guiState;

        try {
            RESTfulResponse response = api.execute();
            System.out.println(response.getResponseString());
            JSONObject json = new JSONObject(response.getResponseString());
            System.out.println("Status " + json.getString("status"));
            JSONObject content = json.getJSONObject("content");
            this.oauthToken = content.getString("oauth_token");
            this.oauthTokenSecret = content.getString("oauth_token_secret");
            System.out.println("Username " + content.getString("username"));
            System.out.println("Subdomain " + content.getString("subdomain"));
            System.out.println("Home URL " + content.getString("homeurl"));
            System.out.println("OAuth Token " + this.oauthToken);
            System.out.println("OAuth Token Secret " + this.oauthTokenSecret);
            guiState.setState("login_success");
        } catch (IOException e) {
            e.printStackTrace();
            guiState.setState("login_fail");
        } catch (URISyntaxException e) {
            e.printStackTrace();
            guiState.setState("login_fail");
        } catch (OAuthMessageSignerException e) {
            e.printStackTrace();
            guiState.setState("login_fail");
        } catch (OAuthExpectationFailedException e) {
            e.printStackTrace();
            guiState.setState("login_fail");
        } catch (JSONException e) {
            e.printStackTrace();
            guiState.setState("login_fail");
        } catch (Exception e) {
            e.printStackTrace();
            guiState.setState("login_fail");
        }
    }

    public GuiState getState() {
        return guiState;
    }

    public void readAlbum() {
        if ((this.oauthToken == null) || (this.oauthTokenSecret == null)) {
            this.login();
        }
        Map<String, String> params = api.getParameters();
        params.put("recurse", "false");

        api.setSubdomain(this.subdomain);
        api.setRequestPath("/album/pbapi");
        api.setMethod("get");
        api.setOauthToken(this.oauthToken);
        api.setOauthTokenSecret(this.oauthTokenSecret);

        try {
            RESTfulResponse response = api.execute();
            System.out.println(response.getResponseString());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void createAlbum() {
        if ((this.oauthToken == null) || (this.oauthTokenSecret == null)) {
            this.login();
        }
        Map<String, String> params = api.getParameters();
        params.put("name", "album-foo");

        api.setSubdomain(this.subdomain);
        api.setRequestPath("/album/pbapi");
        api.setMethod("post");
        api.setOauthToken(this.oauthToken);
        api.setOauthTokenSecret(this.oauthTokenSecret);

        try {
            RESTfulResponse response = api.execute();
            System.out.println(response.getResponseString());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void renameAlbum() {
        if ((this.oauthToken == null) || (this.oauthTokenSecret == null)) {
            this.login();
        }
        Map<String, String> params = api.getParameters();
        params.put("name", "album-bar");

        String album = URLEncoder.encode("pbapi/album-foo");

        api = new PhotobucketAPI();
        api.setOauthConsumerKey(ResourceBundle.getBundle("consumer").getString("oauth.consumer"));
        api.setOauthConsumerSecret(ResourceBundle.getBundle("consumer").getString("oauth.consumer.key"));
        api.setSubdomain(subdomain);
        api.setRequestPath("/album/" + album);
        api.setMethod("put");
        api.setOauthToken(this.oauthToken);
        api.setOauthTokenSecret(this.oauthTokenSecret);

        try {
            RESTfulResponse response = api.execute();
            System.out.println(response.getResponseString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void uploadAlbum() {

        try {
            if ((this.oauthToken == null) || (this.oauthTokenSecret == null)) {
                this.login();
            }
            File imgFile = new File(ResourceBundle.getBundle("consumer").getString("file.path"));
            String album = URLEncoder.encode("pbapi/api-test");
            Map<String, String> params = api.getParameters();
            params.put("title", "HighRes");
            params.put("type", "image");

            api.setSubdomain(this.subdomain);
            api.setRequestPath("/album/" + album + "/upload");
            api.setMethod("post");
            api.setOauthToken(this.oauthToken);
            api.setOauthTokenSecret(this.oauthTokenSecret);
            api.setUploadFile(imgFile, "Coors.jpg");

            RESTfulResponse response = api.execute();
            System.out.println(response.getResponseString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void uploadAlbumUTF() {

        try {
            if ((this.oauthToken == null) || (this.oauthTokenSecret == null)) {
                this.login();
            }
            File imgFile = new File(ResourceBundle.getBundle("consumer").getString("file.path"));
            String album = URLEncoder.encode("pbapi/api-test");
            Map<String, String> params = api.getParameters();
            params.put("title", "HighRes");
            params.put("type", "image");

            api.setSubdomain(this.subdomain);
            api.setRequestPath("/album/" + album + "/upload");
            api.setMethod("post");
            api.setOauthToken(this.oauthToken);
            api.setOauthTokenSecret(this.oauthTokenSecret);
            api.setUploadFile(imgFile, "Coors.jpg");

            RESTfulResponse response = api.execute();
            System.out.println(response.getResponseString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void uploadInputStreamAlbum() {

        try {
            if ((this.oauthToken == null) || (this.oauthTokenSecret == null)) {
                this.login();
            }
            File imgFile = new File(ResourceBundle.getBundle("consumer").getString("file.path"));
            String album = URLEncoder.encode("pbapi/api-test");
            Map<String, String> params = api.getParameters();
            params.put("title", "HighRes");
            params.put("type", "image");

            api.setSubdomain(this.subdomain);
            api.setRequestPath("/album/" + album + "/upload");
            api.setMethod("post");
            api.setOauthToken(this.oauthToken);
            api.setOauthTokenSecret(this.oauthTokenSecret);
            api.setUploadStream(new FileInputStream(imgFile), (int) imgFile.length(), "Coors1.jpg");

            RESTfulResponse response = api.execute();
            System.out.println(response.getResponseString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void uploadWithStatus() {

        try {
            if ((this.oauthToken == null) || (this.oauthTokenSecret == null)) {
                this.login();
            }
            File imgFile = new File(ResourceBundle.getBundle("consumer").getString("file.path"));
            String album = URLEncoder.encode("pbapi/api-test");
            Map<String, String> params = api.getParameters();
            params.put("title", "HighRes");
            params.put("type", "image");

            api.setSubdomain(this.subdomain);
            api.setRequestPath("/album/" + album + "/upload");
            api.setMethod("post");
            api.setOauthToken(this.oauthToken);
            api.setOauthTokenSecret(this.oauthTokenSecret);
            api.setUploadFile(imgFile);
            api.addFileUploadProgressEventListener(new UploadProgressEventListener());

            RESTfulResponse response = api.execute();
            System.out.println(response.getResponseString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void uploadInputStreamWithStatus() {

        try {
            if ((this.oauthToken == null) || (this.oauthTokenSecret == null)) {
                this.login();
            }
            File imgFile = new File(ResourceBundle.getBundle("consumer").getString("file.path"));
            String album = URLEncoder.encode("pbapi/api-test");
            Map<String, String> params = api.getParameters();
            params.put("title", "HighRes");
            params.put("type", "image");

            api.setSubdomain(this.subdomain);
            api.setRequestPath("/album/" + album + "/upload");
            api.setMethod("post");
            api.setOauthToken(this.oauthToken);
            api.setOauthTokenSecret(this.oauthTokenSecret);
            api.setUploadStream(new FileInputStream(imgFile), (int) imgFile.length(), "Coors_Field.jpg");
            api.addFileUploadProgressEventListener(new UploadProgressEventListener());

            RESTfulResponse response = api.execute();
            System.out.println(response.getResponseString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void deleteAlbum() {
        if ((this.oauthToken == null) || (this.oauthTokenSecret == null)) {
            this.login();
        }

        String album = URLEncoder.encode("pbapi/album-bar");
        api.setSubdomain(subdomain);
        api.setRequestPath("/album/" + album);
        api.setMethod("delete");
        api.setOauthToken(this.oauthToken);
        api.setOauthTokenSecret(this.oauthTokenSecret);

        try {
            RESTfulResponse response = api.execute();
            System.out.println(response.getResponseString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private void login() {
        /*
        Map<String,String> params = new HashMap<String,String> ();
        params.put("password", "abc123");
            
        api.setSubdomain("api.photobucket.com");
        api.setRequestPath("/login/direct/pbapi");
        api.setMethod("post");
        api.setParameters(params);
            
            
        try {
        RESTfulResponse response = api.execute();
        System.out.println(response.getResponseCode());
        System.out.println(response.getResponseString());
                XPath xpath = XPathFactory.newInstance().newXPath();
                InputSource source = new InputSource(new StringReader(response.getResponseString()));
                this.oauthToken = xpath.evaluate("//content/oauth_token", source);
                source = new InputSource(new StringReader(response.getResponseString()));
                this.oauthTokenSecret = xpath.evaluate("//content/oauth_token_secret", source);
                source = new InputSource(new StringReader(response.getResponseString()));
                this.subdomain = xpath.evaluate("//content/subdomain", source);
                source = new InputSource(new StringReader(response.getResponseString()));
                this.homeUrl = xpath.evaluate("//content/homeurl", source);
            
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (OAuthMessageSignerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        */
    }
}