abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r617271 - in /incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples: appserver/ appserver/custom/ appserver/defaultprovider/ ext/
Date Thu, 31 Jan 2008 22:40:14 GMT
Author: jmsnell
Date: Thu Jan 31 14:40:10 2008
New Revision: 617271

URL: http://svn.apache.org/viewvc?rev=617271&view=rev
Log:
Add a defaultprovider example
Add a serializer example

Added:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/AppServer.java
      - copied, changed from r617258, incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/AppServer.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/CustomProvider.java
      - copied, changed from r617258, incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/CustomProvider.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/SimpleAdapter.java
      - copied, changed from r617258, incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleAdapter.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/AppServer.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/Customer.java
  (with props)
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/CustomerAdapter.java
  (with props)
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/ext/Serializer.java
Removed:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/AppServer.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/CustomProvider.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleAdapter.java

Copied: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/AppServer.java
(from r617258, incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/AppServer.java)
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/AppServer.java?p2=incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/AppServer.java&p1=incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/AppServer.java&r1=617258&r2=617271&rev=617271&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/AppServer.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/AppServer.java
Thu Jan 31 14:40:10 2008
@@ -1,4 +1,4 @@
-package org.apache.abdera.examples.appserver;
+package org.apache.abdera.examples.appserver.custom;
 
 import org.apache.abdera.protocol.server.ServiceManager;
 import org.apache.abdera.protocol.server.servlet.AbderaServlet;

Copied: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/CustomProvider.java
(from r617258, incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/CustomProvider.java)
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/CustomProvider.java?p2=incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/CustomProvider.java&p1=incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/CustomProvider.java&r1=617258&r2=617271&rev=617271&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/CustomProvider.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/CustomProvider.java
Thu Jan 31 14:40:10 2008
@@ -15,7 +15,7 @@
  * copyright in this work, please see the NOTICE file in the top level
  * directory of this distribution.
  */
-package org.apache.abdera.examples.appserver;
+package org.apache.abdera.examples.appserver.custom;
 
 import org.apache.abdera.protocol.server.CollectionAdapter;
 import org.apache.abdera.protocol.server.Filter;

Copied: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/SimpleAdapter.java
(from r617258, incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleAdapter.java)
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/SimpleAdapter.java?p2=incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/SimpleAdapter.java&p1=incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleAdapter.java&r1=617258&r2=617271&rev=617271&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleAdapter.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/custom/SimpleAdapter.java
Thu Jan 31 14:40:10 2008
@@ -15,7 +15,7 @@
 * copyright in this work, please see the NOTICE file in the top level
 * directory of this distribution.
 */
-package org.apache.abdera.examples.appserver;
+package org.apache.abdera.examples.appserver.custom;
 
 import java.io.IOException;
 import java.util.Date;

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/AppServer.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/AppServer.java?rev=617271&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/AppServer.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/AppServer.java
Thu Jan 31 14:40:10 2008
@@ -0,0 +1,45 @@
+package org.apache.abdera.examples.appserver.defaultprovider;
+
+import javax.servlet.http.HttpServlet;
+
+import org.apache.abdera.protocol.server.Provider;
+import org.apache.abdera.protocol.server.impl.DefaultProvider;
+import org.apache.abdera.protocol.server.impl.SimpleWorkspaceInfo;
+import org.apache.abdera.protocol.server.servlet.AbderaServlet;
+import org.apache.abdera.protocol.server.test.customer.CustomerAdapter;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.servlet.Context;
+import org.mortbay.jetty.servlet.ServletHolder;
+
+public class AppServer {
+
+  public static void main(String... args) throws Exception {
+    int port = 9002;
+    try {
+      port = args.length > 0 ? Integer.parseInt(args[0]) : 9002;
+    } catch (Exception e) {}
+    Server server = new Server(port);
+    Context context = new Context(server, "/", Context.SESSIONS);
+    ServletHolder servletHolder = new ServletHolder(initServlet());
+    context.addServlet(servletHolder, "/*");
+    server.start();
+    server.join();
+  }
+  
+  private static HttpServlet initServlet() {
+    final DefaultProvider customerProvider = new DefaultProvider("/");    
+    CustomerAdapter ca = new CustomerAdapter();
+    ca.setHref("customers");    
+    SimpleWorkspaceInfo wi = new SimpleWorkspaceInfo();
+    wi.setTitle("Customer Workspace");
+    wi.addCollection(ca);    
+    customerProvider.addWorkspace(wi);    
+    return new AbderaServlet() {
+      private static final long serialVersionUID = 0L;
+      protected Provider createProvider() {
+        customerProvider.init(getAbdera(), null);
+        return customerProvider;
+      }
+    };
+  }
+}

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/Customer.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/Customer.java?rev=617271&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/Customer.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/Customer.java
Thu Jan 31 14:40:10 2008
@@ -0,0 +1,20 @@
+package org.apache.abdera.examples.appserver.defaultprovider;
+
+public class Customer {
+  private int id;
+  private String name;
+  
+  public int getId() {
+    return id;
+  }
+  public void setId(int id) {
+    this.id = id;
+  }
+  public String getName() {
+    return name;
+  }
+  public void setName(String name) {
+    this.name = name;
+  }
+  
+}

Propchange: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/Customer.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/CustomerAdapter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/CustomerAdapter.java?rev=617271&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/CustomerAdapter.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/CustomerAdapter.java
Thu Jan 31 14:40:10 2008
@@ -0,0 +1,136 @@
+package org.apache.abdera.examples.appserver.defaultprovider;
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.i18n.iri.IRI;
+import org.apache.abdera.model.Content;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Person;
+import org.apache.abdera.protocol.server.RequestContext;
+import org.apache.abdera.protocol.server.ResponseContext;
+import org.apache.abdera.protocol.server.context.ResponseContextException;
+import org.apache.abdera.protocol.server.impl.AbstractEntityCollectionAdapter;
+
+public class CustomerAdapter extends AbstractEntityCollectionAdapter<Customer> {
+  private static final String ID_PREFIX = "urn:acme:customer:";
+  
+  private AtomicInteger nextId = new AtomicInteger(1000);
+  private Map<Integer, Customer> customers = new HashMap<Integer, Customer>();
+  private Factory factory = new Abdera().getFactory();
+  
+  public String getId(RequestContext request) {
+    return "tag:example.org,2007:feed";
+  }
+  
+  public ResponseContext getCategories(RequestContext request) {
+    return null;
+  }
+  
+  @Override
+  public Customer postEntry(String title, IRI id, String summary, 
+                            Date updated, List<Person> authors,
+                            Content content, RequestContext request) throws ResponseContextException
{   
+    Customer customer = contentToCustomer(content);
+    customers.put(customer.getId(), customer);
+    
+    return customer;
+  }
+
+  private Customer contentToCustomer(Content content) {
+    Customer customer = new Customer();
+    
+    return contentToCustomer(content, customer);
+  }
+
+  private Customer contentToCustomer(Content content, Customer customer) {
+    Element firstChild = content.getFirstChild();
+    customer.setName(firstChild.getAttributeValue("name"));
+    customer.setId(nextId.incrementAndGet());
+    return customer;
+  }
+
+  public void deleteEntry(String resourceName, RequestContext request) throws ResponseContextException
{
+    Integer id = getIdFromResourceName(resourceName);
+    customers.remove(id);
+  }
+
+  public String getAuthor() {
+    return "Acme Industries";
+  }
+
+  @Override
+  public List<Person> getAuthors(Customer entry, RequestContext request) throws ResponseContextException
{
+    Person author = request.getAbdera().getFactory().newAuthor();
+    author.setName("Acme Industries");
+    return Arrays.asList(author);
+  }
+
+  public Object getContent(Customer entry, RequestContext request) {
+    Content content = factory.newContent();
+    Element customerEl = factory.newElement(new QName("customer"));
+    customerEl.setAttributeValue(new QName("name"), entry.getName());
+   
+    content.setValueElement(customerEl);
+    return content;
+  }
+
+  public Iterable<Customer> getEntries(RequestContext request) {
+    return customers.values();
+  }
+
+  public Customer getEntry(String resourceName, RequestContext request) throws ResponseContextException
{
+    Integer id = getIdFromResourceName(resourceName);
+    return customers.get(id);
+  }
+
+  private Integer getIdFromResourceName(String resourceName) throws ResponseContextException
{
+    int idx = resourceName.indexOf("-");
+    if (idx == -1) {
+      throw new ResponseContextException(404);
+    }
+    Integer id = new Integer(resourceName.substring(0, idx));
+    return id;
+  }
+
+  public Customer getEntryFromId(String id, RequestContext request) {
+    return customers.get(new Integer(id));
+  }
+
+  public String getId(Customer entry) {
+    // TODO: is this valid?
+    return ID_PREFIX + entry.getId();
+  }
+
+  public String getName(Customer entry) {
+    return entry.getId() + "-" + entry.getName().replaceAll(" ", "_");
+  }
+
+  public String getTitle(RequestContext request) {
+    return "Acme Customer Database";
+  }
+
+  public String getTitle(Customer entry) {
+    return entry.getName();
+  }
+
+  public Date getUpdated(Customer entry) {
+    return new Date();
+  }
+
+  @Override
+  public void putEntry(Customer entry, String title, Date updated, 
+                       List<Person> authors, String summary,
+                       Content content, RequestContext request) throws ResponseContextException
{
+    contentToCustomer(content, entry);
+  }
+
+}

Propchange: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/CustomerAdapter.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/ext/Serializer.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/ext/Serializer.java?rev=617271&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/ext/Serializer.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/ext/Serializer.java
Thu Jan 31 14:40:10 2008
@@ -0,0 +1,137 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* 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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.examples.ext;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.ext.serializer.ConventionSerializationContext;
+import org.apache.abdera.ext.serializer.annotation.Author;
+import org.apache.abdera.ext.serializer.annotation.ID;
+import org.apache.abdera.ext.serializer.annotation.Link;
+import org.apache.abdera.ext.serializer.annotation.Published;
+import org.apache.abdera.ext.serializer.annotation.Summary;
+import org.apache.abdera.ext.serializer.annotation.Title;
+import org.apache.abdera.ext.serializer.annotation.Updated;
+import org.apache.abdera.ext.serializer.impl.EntrySerializer;
+import org.apache.abdera.model.Document;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.writer.StreamWriter;
+
+/**
+ * The Serializer extension allows Java objects to be automatically serialized
+ * into Atom documents using the objects public getters or fields.  
+ * 
+ * The implementation is still largely experimental
+ */
+public class Serializer {
+
+  static Date date_now = new Date();
+  static Calendar cal_now = Calendar.getInstance();
+  
+  public static void main(String... args) throws Exception {
+    
+    Abdera abdera = Abdera.getInstance();
+    
+    // demonstrate serialization of a non-annotated java object
+    StreamWriter sw = abdera.newStreamWriter();
+    ByteArrayOutputStream out = new ByteArrayOutputStream();
+    sw.setOutputStream(out)
+      .setAutoIndent(true);
+    ConventionSerializationContext c = 
+      new ConventionSerializationContext(sw);
+    c.setSerializer(MyEntry.class, new EntrySerializer());
+    sw.startDocument();
+    c.serialize(new MyEntry());
+    sw.endDocument();
+    
+    // once the object has been serialized, we can see that it's a parseable Atom document
+    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+    Document<Entry> doc = abdera.getParser().parse(in);
+    Entry entry = doc.getRoot();
+    entry.writeTo(System.out);
+    
+    System.out.println();
+    
+    // demonstrate serialization using an annotated java object
+    // annotations allow the developer to customize the way the 
+    // object is serialized
+    sw = abdera.newStreamWriter();
+    out = new ByteArrayOutputStream();
+    sw.setOutputStream(out)
+      .setAutoIndent(true);
+    c = new ConventionSerializationContext(sw);
+    sw.startDocument();
+    c.serialize(new MyAnnotatedEntry());
+    sw.endDocument();
+    
+    in = new ByteArrayInputStream(out.toByteArray());
+    doc = abdera.getParser().parse(in);
+    entry = doc.getRoot();
+    entry.writeTo(System.out);
+  }
+  
+  public static class MyEntry {
+    public String getId() {
+      return "tag:example.org,2008:foo";
+    }
+    public String getTitle() {
+      return "This is the title";
+    }
+    public String getAuthor() {
+      return "James";
+    }
+    public Date getUpdated() {
+      return date_now;
+    }
+    public Calendar getPublished() {
+      return cal_now;
+    }
+    public String getSummary() {
+      return "this is the summary";
+    }
+    public String getLink() {
+      return "http://example.org/foo";
+    }
+  }
+  
+  @org.apache.abdera.ext.serializer.annotation.Entry
+  public static class MyAnnotatedEntry {
+    @ID public String getFoo() {
+      return "tag:example.org,2008:foo";
+    }
+    @Title public String getBar() {
+      return "This is the title";
+    }
+    @Author public String getBaz() {
+      return "James";
+    }
+    @Updated @Published public Date getLastModified() {
+      return date_now;
+    }
+    @Summary public String getText() {
+      return "this is the summary";
+    }
+    @Link public String getUri() {
+      return "http://example.org/foo";
+    }
+  }
+}



Mime
View raw message