2008-07-04

最初の一筆。

昔から日記は続いたことはないし、人に文章を見てもらうことにもなれていない。
でもメモはよくとってきたので、肩肘はらず、メモを残すつもりではじめたいと思う。

まずは旅行にいくので、googleを旅行先で活用できないか調べたい。

使ってみたいのは、Picasa Web Albums。
携帯電話からも Picasa は使えるけど、あくまで閲覧だけの様子。

旅行先で写真を撮って、簡単に写真と場所を保存できたらいいなあ。
で調べたのは Picasa Web Albums Data API

google Data API は使ったことがないので、よい勉強になるだろう。
ざっと見てみると google Data APIは、 AtomRSSAtom Publishing Protocol (APP) といったフォーマット、RESTのアプローチでサービスを提供しているよう。

動くものを作ってみたくなったので、Javaによる実装を挑戦。
google のサンプルソースコードは読みやすく、すぐ応用できた。

まずは簡単に写真を登録する Proxy Server を作ってみた。
以下はそのソースコード。


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.OtherContent;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class PicasaProxy extends PicasawebClient {
private static final Logger logger = Logger.getLogger(PicasaProxy.class);

final HttpServer server;

AlbumEntry albumEntry;

public PicasaProxy(PicasawebService service, String uname, String passwd,
int alubum, String address, int port)
throws IOException, ServiceException {

super(service, uname, passwd);

logger.debug("uname:"+uname);
logger.debug("passwd:"+passwd);
logger.debug("alubum:"+alubum);
logger.debug("address:"+address);
logger.debug("port:"+port);

selectAlubum(alubum);

String[] parts = address.split("\\.");
byte [] addr = new byte[parts.length];
for (int i=0; i < parts.length; i++) {
addr[i] = (byte)Integer.parseInt(parts[i]);
}
InetAddress inetAddress = InetAddress.getByAddress(addr);
InetSocketAddress hostPort = new InetSocketAddress(inetAddress, port);

server = HttpServer.create(hostPort, 1);
server.createContext("/createPhoto.do", new CreatePhoto());
}

public void start() {
server.start();
}

private void selectAlubum(int index) throws IOException, ServiceException {
List albums = getAlbums();
albumEntry = albums.get(index);
System.out.println(albumEntry.getId());
}

class CreatePhoto implements HttpHandler {
public void handle(HttpExchange httpExchange) throws IOException {
logger.debug("CreatePhoto Start");
try {
InputStream in = httpExchange.getRequestBody();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte buff[] = new byte[1024];
int len;
while((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
in.close();

PhotoEntry photoEntry = new PhotoEntry();
photoEntry.setTitle(albumEntry.getTitle());
photoEntry.setTimestamp(new Date());
OtherContent content = new OtherContent();
content.setBytes(out.toByteArray());
content.setMimeType(new ContentType("image/jpeg"));
photoEntry.setContent(content);

insert(albumEntry, photoEntry);

photoEntry = null;

final String response = "OK";
httpExchange.sendResponseHeaders(200, response.getBytes().length);
httpExchange.getResponseBody().write(response.getBytes());
httpExchange.close();
} catch (Exception e) {
logger.equals(e);
final String responseError = "ERROR: 500 Internal Server Error";
httpExchange.sendResponseHeaders(500, responseError.getBytes().length);
httpExchange.getResponseBody().write(responseError.getBytes());
httpExchange.close();
}
logger.debug("CreatePhoto End");
}
}

public static void main(String[] args) throws Exception {
PicasawebService service = new PicasawebService("PicasaProxy");

ResourceBundle rb = ResourceBundle.getBundle(PicasaProxy.class.getName());
String uname = rb.getString("uname");
String passwd = rb.getString("passwd");
int alubum = Integer.parseInt(rb.getString("alubum"));
String address = rb.getString("address");
int port = Integer.parseInt(rb.getString("port"));

PicasaProxy proxy =
new PicasaProxy(service, uname, passwd, alubum, address, port);
proxy.start();
}
}

今度はiアプリを使り、連携して写真を登録できるようにしよう。