abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dand...@apache.org
Subject svn commit: r634473 - in /incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver: defaultprovider/ employee/ employee/AppServer.java employee/Employee.java employee/EmployeeCollectionAdapter.java
Date Fri, 07 Mar 2008 00:13:51 GMT
Author: dandiep
Date: Thu Mar  6 16:13:49 2008
New Revision: 634473

URL: http://svn.apache.org/viewvc?rev=634473&view=rev
Log:
Redo customer example as employee database as I think its a clearer example.

Added:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/AppServer.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/Employee.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/EmployeeCollectionAdapter.java
Removed:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/defaultprovider/

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/AppServer.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/AppServer.java?rev=634473&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/AppServer.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/AppServer.java
Thu Mar  6 16:13:49 2008
@@ -0,0 +1,47 @@
+package org.apache.abdera.examples.appserver.employee;
+
+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.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() {
+    EmployeeCollectionAdapter ca = new EmployeeCollectionAdapter();
+    ca.setHref("employee");    
+    
+    SimpleWorkspaceInfo wi = new SimpleWorkspaceInfo();
+    wi.setTitle("Employee Directory Workspace");
+    wi.addCollection(ca);    
+    
+    final DefaultProvider p = new DefaultProvider("/");  
+    p.addWorkspace(wi);    
+    
+    return new AbderaServlet() {
+      private static final long serialVersionUID = 0L;
+      protected Provider createProvider() {
+        p.init(getAbdera(), null);
+        return p;
+      }
+    };
+  }
+}

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/Employee.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/Employee.java?rev=634473&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/Employee.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/Employee.java
Thu Mar  6 16:13:49 2008
@@ -0,0 +1,27 @@
+package org.apache.abdera.examples.appserver.employee;
+
+import java.util.Date;
+
+public class Employee {
+  private int id;
+  private String name;
+  private Date updated;
+  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;
+  }
+  public Date getUpdated() {
+    return updated;
+  }
+  public void setUpdated(Date updated) {
+    this.updated = updated;
+  }
+}

Added: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/EmployeeCollectionAdapter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/EmployeeCollectionAdapter.java?rev=634473&view=auto
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/EmployeeCollectionAdapter.java
(added)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/employee/EmployeeCollectionAdapter.java
Thu Mar  6 16:13:49 2008
@@ -0,0 +1,134 @@
+package org.apache.abdera.examples.appserver.employee;
+
+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 EmployeeCollectionAdapter extends AbstractEntityCollectionAdapter<Employee>
{
+  private static final String ID_PREFIX = "tag:acme.com,2007:employee:entry:";
+  
+  private AtomicInteger nextId = new AtomicInteger(1000);
+  private Map<Integer, Employee> employees = new HashMap<Integer, Employee>();
+  private Factory factory = new Abdera().getFactory();
+  
+  /**
+   * A unique ID for this feed.
+   */
+  public String getId(RequestContext request) {
+    return "tag:acme.com,2007:employee:feed";
+  }
+
+  /**
+   * The title of our collection.
+   */
+  public String getTitle(RequestContext request) {
+    return "Acme Employee Database";
+  }
+
+  /**
+   * The author of this collection.
+   */
+  public String getAuthor(RequestContext request) {
+    return "Acme Industries";
+  }
+  
+  @Override
+  public Employee postEntry(String title, IRI id, String summary, 
+                            Date updated, List<Person> authors,
+                            Content content, RequestContext request) throws ResponseContextException
{   
+    Employee employee = contentToCustomer(content);
+    employees.put(employee.getId(), employee);
+    
+    return employee;
+  }
+
+  private Employee contentToCustomer(Content content) {
+    Employee employee = new Employee();
+    
+    return contentToCustomer(content, employee);
+  }
+
+  private Employee contentToCustomer(Content content, Employee employee) {
+    Element firstChild = content.getFirstChild();
+    employee.setName(firstChild.getAttributeValue("name"));
+    employee.setId(nextId.incrementAndGet());
+    return employee;
+  }
+
+  public void deleteEntry(String resourceName, RequestContext request) throws ResponseContextException
{
+    Integer id = getIdFromResourceName(resourceName);
+    employees.remove(id);
+  }
+
+  @Override
+  public List<Person> getAuthors(Employee entry, RequestContext request) throws ResponseContextException
{
+    Person author = request.getAbdera().getFactory().newAuthor();
+    author.setName("Acme Industries");
+    return Arrays.asList(author);
+  }
+
+  public Object getContent(Employee entry, RequestContext request) {
+    Content content = factory.newContent();
+    Element employeeEl = factory.newElement(new QName("employee"));
+    employeeEl.setAttributeValue(new QName("name"), entry.getName());
+   
+    content.setValueElement(employeeEl);
+    return content;
+  }
+
+  public Iterable<Employee> getEntries(RequestContext request) {
+    return employees.values();
+  }
+
+  public Employee getEntry(String resourceName, RequestContext request) throws ResponseContextException
{
+    Integer id = getIdFromResourceName(resourceName);
+    return employees.get(id);
+  }
+
+  private Integer getIdFromResourceName(String resourceName) throws ResponseContextException
{
+    int idx = resourceName.indexOf("-");
+    if (idx == -1) {
+      throw new ResponseContextException(404);
+    }
+    return new Integer(resourceName.substring(0, idx));
+  }
+
+  public String getId(Employee entry) {
+    return ID_PREFIX + entry.getId();
+  }
+
+  public String getName(Employee entry) {
+    return entry.getId() + "-" + entry.getName().replaceAll(" ", "_");
+  }
+  public String getTitle(Employee entry) {
+    return entry.getName();
+  }
+
+  public Date getUpdated(Employee entry) {
+    return entry.getUpdated();
+  }
+
+  @Override
+  public void putEntry(Employee entry, String title, Date updated, 
+                       List<Person> authors, String summary,
+                       Content content, RequestContext request) throws ResponseContextException
{
+    contentToCustomer(content, entry);
+  }
+
+}



Mime
View raw message