View Javadoc

1   /*
2    * $Id: CustomerAdapter.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.atom;
11  
12  import java.util.Arrays;
13  import java.util.Date;
14  import java.util.HashMap;
15  import java.util.List;
16  import java.util.Map;
17  import java.util.concurrent.atomic.AtomicInteger;
18  
19  import javax.xml.namespace.QName;
20  
21  import org.apache.abdera.Abdera;
22  import org.apache.abdera.factory.Factory;
23  import org.apache.abdera.i18n.iri.IRI;
24  import org.apache.abdera.model.Content;
25  import org.apache.abdera.model.Element;
26  import org.apache.abdera.model.Person;
27  import org.apache.abdera.protocol.server.RequestContext;
28  import org.apache.abdera.protocol.server.ResponseContext;
29  import org.apache.abdera.protocol.server.context.ResponseContextException;
30  import org.apache.abdera.protocol.server.impl.AbstractEntityCollectionAdapter;
31  
32  public class CustomerAdapter extends AbstractEntityCollectionAdapter<Customer>
33  {
34      private static final String ID_PREFIX = "urn:acme:customer:";
35  
36      private AtomicInteger nextId = new AtomicInteger(1000);
37      private Map<Integer, Customer> customers = new HashMap<Integer, Customer>();
38      private Factory factory = new Abdera().getFactory();
39  
40      public String getId(RequestContext request)
41      {
42          return "tag:example.org,2007:feed";
43      }
44  
45      public ResponseContext getCategories(RequestContext request)
46      {
47          return null;
48      }
49  
50      @Override
51      public Customer postEntry(String title, IRI id, String summary,
52                                Date updated, List<Person> authors,
53                                Content content, RequestContext request) throws ResponseContextException
54      {
55          Customer customer = contentToCustomer(content);
56          customers.put(customer.getId(), customer);
57  
58          return customer;
59      }
60  
61      private Customer contentToCustomer(Content content)
62      {
63          Customer customer = new Customer();
64  
65          return contentToCustomer(content, customer);
66      }
67  
68      private Customer contentToCustomer(Content content, Customer customer)
69      {
70          Element firstChild = content.getFirstChild();
71          customer.setName(firstChild.getAttributeValue("name"));
72          customer.setId(nextId.incrementAndGet());
73          return customer;
74      }
75  
76      public void deleteEntry(String resourceName, RequestContext request) throws ResponseContextException
77      {
78          Integer id = getIdFromResourceName(resourceName);
79          customers.remove(id);
80      }
81  
82      public String getAuthor(RequestContext request)
83      {
84          return "Acme Industries";
85      }
86  
87      @Override
88      public List<Person> getAuthors(Customer entry, RequestContext request) throws ResponseContextException
89      {
90          Person author = request.getAbdera().getFactory().newAuthor();
91          author.setName("Acme Industries");
92          return Arrays.asList(author);
93      }
94  
95      public Object getContent(Customer entry, RequestContext request)
96      {
97          Content content = factory.newContent();
98          Element customerEl = factory.newElement(new QName("customer"));
99          customerEl.setAttributeValue(new QName("name"), entry.getName());
100 
101         content.setValueElement(customerEl);
102         return content;
103     }
104 
105     public Iterable<Customer> getEntries(RequestContext request)
106     {
107         return customers.values();
108     }
109 
110     public Customer getEntry(String resourceName, RequestContext request) throws ResponseContextException
111     {
112         Integer id = getIdFromResourceName(resourceName);
113         return customers.get(id);
114     }
115 
116     private Integer getIdFromResourceName(String resourceName) throws ResponseContextException
117     {
118         int idx = resourceName.indexOf("-");
119         if (idx == -1)
120         {
121             throw new ResponseContextException(404);
122         }
123         Integer id = new Integer(resourceName.substring(0, idx));
124         return id;
125     }
126 
127     public Customer getEntryFromId(String id, RequestContext request)
128     {
129         return customers.get(new Integer(id));
130     }
131 
132     public String getId(Customer entry)
133     {
134         // TODO: is this valid?
135         return ID_PREFIX + entry.getId();
136     }
137 
138     public String getName(Customer entry)
139     {
140         return entry.getId() + "-" + entry.getName().replaceAll(" ", "_");
141     }
142 
143     public String getTitle(RequestContext request)
144     {
145         return "Acme Customer Database";
146     }
147 
148     public String getTitle(Customer entry)
149     {
150         return entry.getName();
151     }
152 
153     public Date getUpdated(Customer entry)
154     {
155         return new Date();
156     }
157 
158     @Override
159     public void putEntry(Customer entry, String title, Date updated,
160                          List<Person> authors, String summary,
161                          Content content, RequestContext request) throws ResponseContextException
162     {
163         contentToCustomer(content, entry);
164     }
165 
166 }