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