/*
* Copyright 2010 WithOne Co. Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jp.co.withone.osgi.gadget.picasaconnector;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.media.mediarss.MediaContent;
import com.google.gdata.data.media.mediarss.MediaGroup;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.GphotoEntry;
import com.google.gdata.data.photos.GphotoFeed;
import com.google.gdata.data.photos.PhotoFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
/**
* PicasaConnector.
* @author ohsoko.hiroshi
*/
public class PicasaConnector {
private static final String API_PREFIX = "http://picasaweb.google.com/data/feed/api/user/";
private static final String PARAM_PHOTO = "photo";
private static final String PARAM_ALBUM = "album";
private static PicasaConnector connector = null;
private PicasawebService picasawebService = null;
boolean isAuthenticated = false;
List<URL> urlList = null;
private String USER_NAME = null;
private String PASSWORD = null;
/**
* private constructor.
*/
private PicasaConnector() {
// TODO app_name
picasawebService = new PicasawebService("app_name");
}
/**
* singleton instance.
* @return singleton instance
*/
public static PicasaConnector getInstance() {
if (connector == null) {
connector = new PicasaConnector();
}
return connector;
}
/**
* @throws AuthenticationException
*/
public void authenticate(String userName, String password)
throws AuthenticationException {
this.USER_NAME = userName;
this.PASSWORD = password;
try {
this.picasawebService.setUserCredentials(this.USER_NAME,
this.PASSWORD);
} catch (AuthenticationException e) {
// TODO error
throw e;
}
this.isAuthenticated = true;
}
/**
*
* @return
*/
public List<String> getAlbumNameList() {
List<String> albumNameList = new ArrayList<String>();
String albumUrl = API_PREFIX + this.USER_NAME;
albumUrl = addKindParameter(albumUrl, PARAM_ALBUM);
AlbumFeed feed = null;
try {
feed = getFeed(albumUrl, AlbumFeed.class);
} catch (Exception e) {
e.printStackTrace();
}
List<GphotoEntry> entries = feed.getEntries();
for (GphotoEntry gphotoEntry : entries) {
albumNameList.add(gphotoEntry.getTitle().getPlainText());
}
return albumNameList;
}
/**
*
* @return
*/
public List<URL> getPhotosURLList() {
String albumUrl = API_PREFIX + this.USER_NAME;
albumUrl = addKindParameter(albumUrl, PARAM_PHOTO);
PhotoFeed feed = null;
try {
feed = getFeed(albumUrl, PhotoFeed.class);
} catch (Exception e) {
// TODO error
return null;
}
ArrayList<URL> urlList = new ArrayList<URL>();
for (GphotoEntry entry : feed.getEntries()) {
MediaGroup extension = entry.getExtension(MediaGroup.class);
List<MediaContent> repeatingExtension = extension
.getRepeatingExtension(MediaContent.class);
for (MediaContent content : repeatingExtension) {
URL url = null;
try {
url = new URL(content.getUrl());
urlList.add(url);
} catch (MalformedURLException e) {
// TODO error
}
}
}
return urlList;
}
private <T extends GphotoFeed> T getFeed(String feedURL, Class<T> feedClass)
throws MalformedURLException, IOException, ServiceException {
return this.picasawebService.getFeed(new URL(feedURL), feedClass);
}
private String addKindParameter(String url, String kind) {
if (url.contains("?")) {
return url + "&kind=" + kind;
} else {
return url + "?kind=" + kind;
}
}
}
|