1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts.atom;
20
21 import java.io.IOException;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import org.apache.abdera.model.Element;
26 import org.apache.abdera.model.Entry;
27 import org.springframework.extensions.surf.util.Content;
28 import org.springframework.extensions.surf.util.URLEncoder;
29 import org.springframework.extensions.webscripts.Format;
30 import org.springframework.extensions.webscripts.FormatReader;
31 import org.springframework.extensions.webscripts.WebScriptException;
32 import org.springframework.extensions.webscripts.WebScriptRequest;
33 import org.springframework.extensions.webscripts.WebScriptResponse;
34
35
36
37
38
39
40
41
42 public class AtomReader implements FormatReader<Element>
43 {
44
45 protected AbderaService abderaService;
46
47
48
49
50
51
52 public void setAbderaService(AbderaService abderaService)
53 {
54 this.abderaService = abderaService;
55 }
56
57
58
59
60 public Class<Element> getDestinationClass()
61 {
62 return Element.class;
63 }
64
65
66
67
68 public String getSourceMimetype()
69 {
70 return Format.ATOM.mimetype();
71 }
72
73
74
75
76 public Element read(WebScriptRequest req)
77 {
78 Content content = req.getContent();
79 if (content == null)
80 {
81 throw new WebScriptException("Failed to convert request to Atom");
82 }
83 try
84 {
85 return abderaService.parse(content.getReader(), req.getServerPath() + URLEncoder.encodeUri(req.getServicePath()));
86 }
87 catch (IOException e)
88 {
89 throw new WebScriptException("Failed to convert request to Atom", e);
90 }
91 }
92
93
94
95
96 public Map<String, Object> createScriptParameters(WebScriptRequest req, WebScriptResponse res)
97 {
98 Map<String, Object> params = new HashMap<String, Object>();
99 Element element = (Element)read(req);
100 if (element instanceof Entry)
101 {
102 params.put("entry", element);
103 params.put("slug", req.getHeader("Slug"));
104 }
105 else
106 {
107 params.put("feed", element);
108 }
109 return params;
110 }
111 }