Google App Engine Atomサンプルプログラム

デモサイト

ソース

  • Google Code
    • http://reflexworks.googlecode.com/svn/trunk/reflexatom

ReflexAtomServlet

package jp.reflexworks.atom;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jp.reflexworks.atom.entry.Author;
import jp.reflexworks.atom.entry.Category;
import jp.reflexworks.atom.entry.Content;
import jp.reflexworks.atom.entry.Contributor;
import jp.reflexworks.atom.entry.Entry;
import jp.reflexworks.atom.entry.Link;
import jp.reflexworks.atom.feed.Feed;
import jp.reflexworks.atom.source.Generator;
import jp.reflexworks.atom.source.Source;
import jp.reflexworks.atom.util.DateUtil;
import jp.reflexworks.atom.util.JdoUtils;
import jp.sourceforge.reflex.IResourceMapper;
import jp.sourceforge.reflex.core.ResourceMapper;

import com.google.appengine.api.datastore.Text;

public class ReflexAtomServlet extends HttpServlet {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {

                resp.setContentType("text/plain");
                resp.setCharacterEncoding("UTF-8");

                // Entry
        Entry entry = new Entry();
        entry.author = new ArrayList<Author>();
        Author author = new Author();
        author.email = "foo@bar.jp";
        author.name ="foo";
        author.uri = "abc";
        entry.author.add(author);

        Category category = new Category();

        category.label = "reflex itext";
        entry.category = new ArrayList<Category>();
        entry.category.add(category);

        entry.contributor = new ArrayList<Contributor>();

        Contributor contributor = new Contributor();
        contributor.name ="田中";
        entry.contributor.add(contributor);
        entry.title = "Atom sft test";    // Entry's title.
        
        entry.link = new ArrayList<Link>();
        Link link4 = new Link();

        link4._$href="http://example.org/2009/05/23/atom";
        link4._$rel="alternate";
        link4._$type="text/html";
        entry.link.add(link4);

        Link link5 = new Link();
        link5._$href="http://example.org/audio/ph34r_my_podcast.mp3";
        link5._$rel="enclosure";
        link5._$type="audio/mpeg";
        link5._$length="1337";
        entry.link.add(link5);
        
        entry.content = new Content();
        entry.content._$$text = new Text("<div xmlns=\"http://www.w3.org/1999/xhtml\">" +
                        "<p><i>[Update: The Atom draft is finished.]</i></p></div>");
        entry.content._$type = "xhtml";
        entry.content._$xml$base = "http://diveintomark.org/";
        entry.content._$xml$lang = "en";
        entry.updated = DateUtil.getDateTime(new Date());

        // Source
        entry.source = new Source();
        
        Generator generator = new Generator();
        entry.source.generator = generator;
        
        generator._$xml$base = "http://diveintomark.org/";
        generator._$xml$lang = "en";
        generator._$uri = "xxxxx";
        generator._$version = "1.0";
        
        entry.source._$xml$base = "aaaa";
        entry.source.title = "Source Title";
        
                // モデルビーンのパッケージ名を指定してリソースマッパーをnewする
                Map<String,String> param = new HashMap<String,String>();
                
        param.put("jp.reflexworks.atom.feed","http://www.w3.org/2005/Atom");    // パッケージ名と名前空間のペアを指定する  
        param.put("jp.reflexworks.atom.entry","");                                                              // 名前空間""はデフォルト空間
                param.put("jp.reflexworks.atom.source","");                             
                
                IResourceMapper mapper = new ResourceMapper(param);             // mapをパラメータにしてMapperをnewする
 
                // そしてXMLにシリアライズ
                String toXML = mapper.toXML(entry);
                resp.getWriter().print("\n【EntryをXMLにシリアライズ】");
                resp.getWriter().println("\n");
                resp.getWriter().println(toXML);

                // デシリアライズテスト
                resp.getWriter().print("\n【Entryのデシリアライズテスト】");
                resp.getWriter().println("\n");
                Entry entry2 = (Entry) mapper.fromXML(toXML);

                //
                Feed feed = new Feed();
                feed._$xmlns = "http://www.w3.org/2005/Atom";
                feed.title = "Example Feed";
                feed.link = new ArrayList<jp.reflexworks.atom.feed.Link>();
                jp.reflexworks.atom.feed.Link link3  = new jp.reflexworks.atom.feed.Link();
                link3._$href = "http://example.org/";
                
                feed.link.add(link3);
                feed.entry = new ArrayList<Entry>();
                feed.entry.add(entry);

                feed.updated = DateUtil.getDateTime(new Date());
                feed.author = new ArrayList<jp.reflexworks.atom.feed.Author>();
                jp.reflexworks.atom.feed.Author author2 = new jp.reflexworks.atom.feed.Author();
                author2.name = "Takezaki";
                author2.uri = "http://blog.virtual-tech.net";
                feed.author.add(author2);
                
                // そしてXMLにシリアライズ
                toXML = mapper.toXML(feed);
                resp.getWriter().print("\n【Feedを生成して上のEntryを追加】");
                resp.getWriter().println("\n");
                resp.getWriter().println(toXML);

                // デシリアライズテスト

                Feed feed2 = (Feed) mapper.fromXML(toXML);

                // そしてXMLにシリアライズ
                toXML = mapper.toXML(feed2);
                resp.getWriter().print("\n【Feedのデシリアライズテスト】");
                resp.getWriter().println("\n");
                resp.getWriter().println(toXML);
/*
                // Datastoreに格納
                resp.getWriter().print("\n【Datastoreに格納】");
                resp.getWriter().println("\n");
                JdoUtils.insert(feed2);
*/
                // entryのAuthorをfeedのAuthorにDeepCopy
                resp.getWriter().print("\n【entryのAuthorをfeedのAutorにDeepcopy】");
                resp.getWriter().println("\n");
                IResourceMapper mapper2 = new ResourceMapper("jp.reflexworks.atom.feed");
                resp.getWriter().println("\n");
                jp.reflexworks.atom.feed.Author author3 = (jp.reflexworks.atom.feed.Author) mapper2.fromXML(mapper.toXML(author));

                // それをXMLにシリアライズ
                toXML = mapper2.toXML(author3);
                resp.getWriter().println(toXML);

                
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {

                resp.setContentType("text/plain");
                resp.setCharacterEncoding("UTF-8");

                // モデルビーンのパッケージ名を指定してリソースマッパーをnewする
                Map<String,String> param = new HashMap<String,String>();
                
        param.put("jp.reflexworks.atom.feed","http://www.w3.org/2005/Atom");    // パッケージ名と名前空間のペアを指定する  
        param.put("jp.reflexworks.atom.entry","");                                                              // 名前空間""はデフォルト空間
                param.put("jp.reflexworks.atom.source","");                             
                
                IResourceMapper mapper = new ResourceMapper(param);             // mapをパラメータにしてMapperをnewする

                // デシリアライズテスト
                resp.getWriter().print("\n【オリジナルのFeed】");
                resp.getWriter().println("\n"+req.getParameter("xml"));

                Feed feed = (Feed) mapper.fromXML(req.getParameter("xml"));

                resp.getWriter().println("\n【content】:"+feed.entry.get(0).content._$$text.getValue());
                
                
                // XMLにシリアライズ
                String toXML = mapper.toXML(feed);
                resp.getWriter().println("\n【デシリアイアライズされたFeed】:");
                resp.getWriter().println("\n:");
                resp.getWriter().println(toXML);
                
                // Datastoreに格納
                resp.getWriter().print("\n【Datastoreに格納】");
                resp.getWriter().println("\n");
                JdoUtils.insert(feed);

                // INSERTしたものを読み取る
                List<Feed> feeds = JdoUtils.getFeeds();

                resp.getWriter().println("\n【DataStoreに格納されているものを全部取り出して表示】");
                
                for (Feed aFeed : feeds) {

                        // XMLにシリアライズ
                        toXML = mapper.toXML(aFeed);
                        resp.getWriter().println("\n【Feed】:");
                        resp.getWriter().println("\n:");
                        resp.getWriter().println(toXML);
                        
                }
                
        }

        
}

Model(jp.reflexworks.atom.entry)

package jp.reflexworks.atom.entry;


import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Author  {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        private Entry entry;

        @Persistent
        public String _$xml$lang; 
        
        @Persistent
        public String _$xml$base;
        
        @Persistent
        public String name;
        
        @Persistent
        public String uri;
        
        @Persistent
        public String email;

}

package jp.reflexworks.atom.entry;


import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Category  {

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        private Entry entry;

        @Persistent
        public String _$xml$lang; 
        
        @Persistent
        public String _$xml$base; 
        
        @Persistent
        public String term;
        
        @Persistent
        public String scheme;
        
        @Persistent
        public String label;
        
}

package jp.reflexworks.atom.entry;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Content {

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        private Entry entry;

        @Persistent
        public String _$xml$lang; 
        
        @Persistent
        public String _$xml$base; 
        
        @Persistent
        public String _$src;
        
        @Persistent
        public String _$type;
        
        @Persistent
        public Text _$$text;
        
}

package jp.reflexworks.atom.entry;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Contributor  {
        
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        private Entry entry;

        @Persistent
        public String _$xml$lang; 
        
        @Persistent
        public String _$xml$base;
        
        @Persistent
        public String name;
        
        @Persistent
        public String uri;
        
        @Persistent
        public String email;

}

package jp.reflexworks.atom.entry;

import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import jp.reflexworks.atom.feed.Feed;
import jp.reflexworks.atom.source.Source;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Entry  {

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        private Feed feed;

        @Persistent
        public static String _$xmlns;
        
        @Persistent
        public String _$xml$lang;
        
        @Persistent
        public String _$xml$base;
        
        @Persistent(mappedBy = "entry")
        public List<Author> author; 
        
        @Persistent(mappedBy = "entry")
        public List<Category> category;

        @Persistent
        public Content content;

        @Persistent(mappedBy = "entry")
        public List<Contributor> contributor;
        
        @Persistent
        public String id;
        
        @Persistent
        public String id_$xml$lang;
        
        @Persistent
        public String id_$xml$base;
        
        @Persistent(mappedBy = "entry")
        public List<Link> link;
        
        @Persistent
        public String published;
        
        @Persistent
        public String published_$xml$lang;
        
        @Persistent
        public String published_$xml$base;
        
        @Persistent
        public String rights;
        
        @Persistent
        public String rights_$type;
        
        @Persistent
        public String rights_$xml$lang;
        
        @Persistent
        public String rights_$xml$base;
        
        @Persistent
        public Source source;
        
        @Persistent
        public String summmary;
        
        @Persistent
        public String summmary_$type;
        
        @Persistent
        public String summmary_$xml$lang;
        
        @Persistent
        public String summmary_$xml$base;
        
        @Persistent
        public String title;
        
        @Persistent
        public String title_$type;
        
        @Persistent
        public String title_$xml$lang;
        
        @Persistent
        public String title_$xml$base;
        
        @Persistent
        public String updated;
        
        @Persistent
        public String updated_$xml$lang;
        
        @Persistent
        public String updated_$xml$base;

}

package jp.reflexworks.atom.entry;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Link {

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        private Entry entry;

        @Persistent
        public String _$xml$lang; 
        
        @Persistent
        public String _$xml$base; 
        
        @Persistent
        public String _$href;
        
        @Persistent
        public String _$rel;
        
        @Persistent
        public String _$type;
        
        @Persistent
        public String _$hreflang;
        
        @Persistent
        public String _$title;
        
        @Persistent
        public String _$length;

}

package jp.reflexworks.atom.feed;

import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import jp.reflexworks.atom.entry.Entry;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Feed  {

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent 
        public static String _$xmlns;
        
        @Persistent
        public static String _$xmlns$rx;
        
        @Persistent(mappedBy = "feed")
        public List<Author> author; 
        
        @Persistent(mappedBy = "feed")
        public List<Category> category; 
        
        @Persistent(mappedBy = "feed")  
        public List<Contributor> contributor; 
        
        @Persistent
        public Generator generator; 
        
        @Persistent
        public String icon; 
        
        @Persistent
        public String id; 
        
        @Persistent(mappedBy = "feed")
        public List<Link> link;
        
        @Persistent
        public String logo;
        
        @Persistent
        public String rights;
        
        @Persistent
        public String title;
        
        @Persistent
        public String title_$type;
        
        @Persistent
        public String subtitle;
        
        @Persistent
        public String subtitle_$type;
        
        @Persistent
        public String updated;
        
        @Persistent(mappedBy = "feed")
        public List<Entry> entry; 
        

}

package jp.reflexworks.atom.feed;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Generator {

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;
 
        @Persistent
        public String _$xml$lang; 
        
        @Persistent
        public String _$xml$base; 
        
        @Persistent
        public String _$uri;
        
        @Persistent
        public String _$version;
        
        @Persistent
        public String _$$text;

}

package jp.reflexworks.atom.source;

import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Source {

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;

        @Persistent
        public String _$xml$lang; 
        
        @Persistent
        public String _$xml$base; 
        
        @Persistent(mappedBy = "source")
        public List<Author> author;
        
        @Persistent(mappedBy = "source")
        public List<Category> category;
        
        @Persistent(mappedBy = "source")
        public List<Contributor> contributor;

        @Persistent
        public Generator generator;
        
        @Persistent
        public String icon;
        
        @Persistent
        public String icon_$xml$lang; 
        
        @Persistent
        public String icon_$xml$base; 
        
        @Persistent
        public String id;
        
        @Persistent
        public String id_$xml$lang; 
        
        @Persistent
        public String id_$xml$base; 
        
        @Persistent(mappedBy = "source")
        public List<Link> link;
        
        @Persistent
        public String logo;
         
        @Persistent
        public String logo_$xml$lang; 
        
        @Persistent
        public String logo_$xml$base; 
        
        @Persistent
        public String rights;
        
        @Persistent
        public String rights_$type; 
        
        @Persistent
        public String rights_$xml$lang; 
        
        @Persistent
        public String rights_$xml$base; 
        
        @Persistent
        public String subtitle;
        
        @Persistent
        public String subtitle_$type; 
        
        @Persistent
        public String subtitle_$xml$lang; 
        
        @Persistent
        public String subtitle_$xml$base; 
        
        @Persistent
        public String title;
        
        @Persistent
        public String title_$type; 
        
        @Persistent
        public String title_$xml$lang; 
        
        @Persistent
        public String title_$xml$base; 
        
        @Persistent
        public String updated;
        
        @Persistent
        public String updated_$xml$lang; 
        
        @Persistent
        public String updated_$xml$base; 

}

web.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
        <servlet>
                <servlet-name>ReflexAtomServlet</servlet-name>
                <servlet-class>jp.reflexworks.atom.ReflexAtomServlet</servlet-class>
        </servlet>
        <servlet-mapping>
                <servlet-name>ReflexAtomServlet</servlet-name>
                <url-pattern>/reflexAtom</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
                <welcome-file>index.html</welcome-file>
        </welcome-file-list>
</web-app>

index.html

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>

<html>
  <head>
    <meta http-equiv='content-type' content='text/html; charset=UTF-8'>
    <title>Reflex/Atom Test</title>
  </head>

  <body>
    <h1>Reflex/Atom Test</h1>
        
    <table>
      <tr>
        <td colspan='2' style='font-weight:bold;'>JDOから直接Atomに変換できます:</td>        
      </tr>
      <tr>
        <td><a href='reflexAtom'/>1.参照テスト(JDOに値をセットしてAtomを出力)</td>
      </tr>
      <tr>
        <form action='/reflexAtom' method='post'>
        <td>2.更新テスト(以下をPOSTしてJDOに変換しDatastoreに保存する<p><input type='submit' value='実行' /></p>
                </td>
      </tr>
      <tr>
        <td>
<textarea name="xml" rows="20" cols="80">
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/&amp;quot;CUcASXk6eCp7ImA9WxJQE04.&amp;quot;'>
<id>tag:blogger.com,1999:blog-15645609</id>
<updated>2009-05-26T19:17:28.710+09:00</updated>
<title>ぶいてく</title>
<subtitle type='html'>XML, クラウドコンピューティング, Reflexなど</subtitle>
・・・・

</textarea>

</p>
</form>
    </table>
  </body>
</html>