Package api.http.google.picasa

Source Code of api.http.google.picasa.PicasaWeb

package api.http.google.picasa;

import api.KeyValuePair;
import api.http.HttpRequest;
import api.http.google.GoogleAPI;
import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class PicasaWeb{

  private GoogleAPI googleapi;
  private String user;
  private String userId;
  private String author;
  private String description;
  private URL cover;
  private Date lastModified;
  private URL feedUrl;
  private URL webUrl;
  private URL entryUrl;

  /**
   * returns the user.
   *
   * @return the user.
   */
  public String getUser(){
    return this.user;
  }

  /**
   * returns the user id.
   *
   * @return the user id.
   */
  public String getUserId(){
    return this.userId;
  }

  /**
   * returns the Google API object.
   *
   * @return the Google API object.
   */
  public GoogleAPI getGoogleapi(){
    return this.googleapi;
  }

  /**
   * returns the author.
   *
   * @return the author.
   */
  public String getAuthor(){
    return this.author;
  }

  /**
   * returns the description.
   *
   * @return the description.
   */
  public String getDescription(){
    return this.description;
  }

  /**
   * returns the cover.
   *
   * @return the cover.
   */
  public URL getCover(){
    return this.cover;
  }

  /**
   * returns the last modified.
   *
   * @return the last modified.
   */
  public Date getLastModified(){
    return this.lastModified;
  }

  /**
   * returns the feed url.
   *
   * @return the feed url.
   */
  public URL getFeedUrl(){
    return this.feedUrl;
  }

  /**
   * returns the web url.
   *
   * @return the web url.
   */
  public URL getWebUrl(){
    return this.webUrl;
  }

  /**
   * returns the entry url.
   *
   * @return the entry url.
   */
  public URL getEntryUrl(){
    return this.entryUrl;
  }

  /**
   * constructs this object with given user.
   *
   * @param user Google user.
   * @param googleapi the Google API with exist auth token.
   * @throws IOException the url is not exist or something wrong of the server
   * of target url.
   * @throws ParserConfigurationException the response is not a parsable XML.
   * @throws SAXException the response is not a parsable XML.
   */
  public PicasaWeb(String user, GoogleAPI googleapi) throws IOException, ParserConfigurationException, SAXException{
    this.googleapi = googleapi;
    URL url = new URL("https://picasaweb.google.com/data/feed/api/user/" + user);
    HttpRequest httpRequest = new HttpRequest(url);
    if (googleapi != null){
      KeyValuePair pair = googleapi.toAuthHeader();
      httpRequest.addHeader(pair.key, pair.value);
    }
    String response = httpRequest.send();
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(response)));
    this.initial(document.getDocumentElement());
  }

  /**
   * constructs this object with given user.
   *
   * @param user Google user.
   * @throws IOException the url is not exist or something wrong of the server
   * of target url.
   * @throws ParserConfigurationException the response is not a parsable XML.
   * @throws SAXException the response is not a parsable XML.
   */
  public PicasaWeb(String user) throws IOException, ParserConfigurationException, SAXException{
    this(user, null);
  }

  private void initial(Element documentRoot){
    try{
      String id = documentRoot.getElementsByTagName("id").item(0).getTextContent();
      int index = id.lastIndexOf('/');
      this.user = id.substring(index + 1);
    } catch (Exception ex){
    }
    try{
      this.userId = documentRoot.getElementsByTagName("title").item(0).getTextContent();
      this.entryUrl = new URL("https://picasaweb.google.com/data/entry/api/user/" + this.userId);
    } catch (Exception ex){
    }
    try{
      this.author = ((Element)documentRoot.getElementsByTagName("author").item(0)).getElementsByTagName("name").item(0).getTextContent();
    } catch (Exception ex){
    }
    try{
      this.description = documentRoot.getElementsByTagName("subtitle").item(0).getTextContent();
    } catch (Exception ex){
    }
    try{
      this.cover = new URL(documentRoot.getElementsByTagName("icon").item(0).getTextContent());
    } catch (Exception ex){
    }
    try{
      this.lastModified = new Date(Long.parseLong(documentRoot.getElementsByTagName("gphoto:timestamp").item(0).getTextContent()));
    } catch (Exception ex){
    }
    try{
      NodeList links = documentRoot.getElementsByTagName("link");
      for (int i = 0; i < links.getLength(); i++){
        Node link = links.item(i);
        if (link.getParentNode() == documentRoot){
          try{
            NamedNodeMap attributes = link.getAttributes();
            String rel = attributes.getNamedItem("rel").getTextContent();
            URL href = new URL(attributes.getNamedItem("href").getTextContent());
            if (rel.equals("http://schemas.google.com/g/2005#feed")){
              this.feedUrl = href;
            } else if (rel.equals("alternate")){
              this.webUrl = href;
            }
          } catch (Exception ex){
          }
        }
      }
    } catch (Exception ex){
    }
  }

  /**
   * get a Picasa Album object with given album id.
   *
   * @param id album id.
   * @return a Picasa Album object.
   * @throws IOException the url is not exist or something wrong of the server
   * of target url.
   * @throws ParserConfigurationException the response is not a parsable XML.
   * @throws SAXException the response is not a parsable XML.
   */
  public PicasaAlbum getAlbum(String id) throws IOException, ParserConfigurationException, SAXException{
    URL url = new URL(this.entryUrl.toString() + "/albumid/" + id);
    return this.getAlbum(url);
  }

  private PicasaAlbum getAlbum(URL url) throws IOException, ParserConfigurationException, SAXException{
    HttpRequest httpRequest = new HttpRequest(url);
    if (this.googleapi != null){
      KeyValuePair pair = this.googleapi.toAuthHeader();
      httpRequest.addHeader(pair.key, pair.value);
    }
    String response = httpRequest.send();
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(response)));
    return new PicasaAlbum(this, document.getDocumentElement());
  }

  /**
   * get an array of Picasa Album objects.
   *
   * @return an array Picasa Album objects.
   * @throws IOException the url is not exist or something wrong of the server
   * of target url.
   * @throws ParserConfigurationException the response is not a parsable XML.
   * @throws SAXException the response is not a parsable XML.
   */
  public PicasaAlbum[] listAlbums() throws IOException, ParserConfigurationException, SAXException{
    HttpRequest httpRequest = new HttpRequest(this.feedUrl);
    if (this.googleapi != null){
      KeyValuePair pair = this.googleapi.toAuthHeader();
      httpRequest.addHeader(pair.key, pair.value);
    }
    String response = httpRequest.send();
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(response)));
    List<PicasaAlbum> picasaAlbums = new ArrayList<PicasaAlbum>();
    NodeList entries = document.getElementsByTagName("entry");
    for (int i = 0; i < entries.getLength(); i++){
      Element entry = (Element)entries.item(i);
      picasaAlbums.add(new PicasaAlbum(this, entry));
    }
    return picasaAlbums.toArray(new PicasaAlbum[picasaAlbums.size()]);
  }

  /**
   * creates a Picasa Album.
   *
   * @param title the title of Picasa Album.
   * @param description the description of Picasa Album.
   * @param access the access level of Picasa Album, can be "public",
   * "private" or "protected".
   * @param lastModified the last modified of Picasa Album.
   * @param canComment can Picasa Album post the comment.
   * @param position the position of Picasa Album.
   * @param location the location of Picasa Album.
   * @param cover the cover of Picasa Album.
   * @return the created Picasa Album.
   * @throws IOException the url is not exist or something wrong of the server
   * of target url.
   * @throws ParserConfigurationException the response is not a parsable XML.
   * @throws SAXException the response is not a parsable XML.
   */
  public PicasaAlbum createAlbum(String title, String description, String access, Date lastModified, Boolean canComment, String position, String location, URL cover) throws IOException, ParserConfigurationException, SAXException{
    HttpRequest httpRequest = new HttpRequest(this.feedUrl);
    if (this.googleapi != null){
      KeyValuePair pair = this.googleapi.toAuthHeader();
      httpRequest.addHeader(pair.key, pair.value);
    }
    String data = PicasaAlbum.createAlbumXML(title, description, access, lastModified, canComment, position, location, cover, null);
    httpRequest.setData(data);
    httpRequest.setMethod("POST");
    httpRequest.setContentType("application/atom+xml");
    String response = httpRequest.send();
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(response)));
    return new PicasaAlbum(this, document.getDocumentElement());
  }

  /**
   * returns the string represent this object.
   *
   * @return the string represent this object.
   */
  @Override
  public String toString(){
    StringBuilder sbr = new StringBuilder();
    sbr.append(String.format("user = \"%s\"\n", this.user));
    sbr.append(String.format("user id = \"%s\"\n", this.userId));
    sbr.append(String.format("author = \"%s\"\n", this.author));
    sbr.append(String.format("description = \"%s\"\n", this.description));
    sbr.append(String.format("cover = \"%s\"\n", ((this.cover == null) ? "" : this.cover.toString())));
    sbr.append(String.format("last modified = \"%s\"\n", ((this.lastModified == null) ? System.currentTimeMillis() : this.lastModified.getTime())));
    sbr.append(String.format("feed url = \"%s\"\n", ((this.feedUrl == null) ? "" : this.feedUrl.toString())));
    sbr.append(String.format("web url = \"%s\"\n", ((this.webUrl == null) ? "" : this.webUrl.toString())));
    sbr.append(String.format("entry url = \"%s\"\n", ((this.entryUrl == null) ? "" : this.entryUrl.toString())));
    return sbr.toString();
  }
}
TOP

Related Classes of api.http.google.picasa.PicasaWeb

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.