001
014
015 package com.liferay.portal.atom;
016
017 import com.liferay.portal.kernel.atom.AtomCollectionAdapter;
018 import com.liferay.portal.kernel.atom.AtomEntryContent;
019 import com.liferay.portal.kernel.atom.AtomException;
020
021 import java.io.InputStream;
022
023 import java.util.ArrayList;
024 import java.util.Date;
025 import java.util.List;
026
027 import javax.activation.MimeType;
028
029 import org.apache.abdera.Abdera;
030 import org.apache.abdera.factory.Factory;
031 import org.apache.abdera.i18n.iri.IRI;
032 import org.apache.abdera.model.Content;
033 import org.apache.abdera.model.Person;
034 import org.apache.abdera.model.Text;
035 import org.apache.abdera.protocol.server.RequestContext;
036 import org.apache.abdera.protocol.server.context.ResponseContextException;
037
038
041 public class AtomCollectionAdapterWrapper<E>
042 extends BaseEntityCollectionAdapter<E> {
043
044 public AtomCollectionAdapterWrapper(
045 AtomCollectionAdapter<E> atomCollectionAdapter) {
046
047 super(atomCollectionAdapter.getCollectionName().toLowerCase());
048
049 _atomCollectionAdapter = atomCollectionAdapter;
050 }
051
052 @Override
053 public void deleteEntry(String resourceName, RequestContext requestContext)
054 throws ResponseContextException {
055
056 try {
057 _atomCollectionAdapter.deleteEntry(
058 resourceName, new AtomRequestContextImpl(requestContext));
059 }
060 catch (AtomException ae) {
061 throw new ResponseContextException(
062 ae.getErrorCode(), ae.getCause());
063 }
064 }
065
066 @Override
067 public List<Person> getAuthors(E entry, RequestContext requestContext) {
068 List<Person> persons = new ArrayList<Person>();
069
070 List<String> authors = _atomCollectionAdapter.getEntryAuthors(entry);
071
072 for (String author : authors) {
073 Abdera abdera = requestContext.getAbdera();
074
075 Factory factory = abdera.getFactory();
076
077 Person person = factory.newAuthor();
078
079 person.setName(author);
080
081 persons.add(person);
082 }
083
084 return persons;
085 }
086
087 @Override
088 public Object getContent(E entry, RequestContext requestContext) {
089 AtomEntryContent atomEntryContent =
090 _atomCollectionAdapter.getEntryContent(
091 entry, new AtomRequestContextImpl(requestContext));
092
093 Content content = newContent(
094 atomEntryContent.getType(), requestContext);
095
096 if (atomEntryContent.getMimeType() != null) {
097 content.setMimeType(atomEntryContent.getMimeType());
098 }
099
100 if (atomEntryContent.getSrcLink() != null) {
101 content.setSrc(atomEntryContent.getSrcLink());
102 }
103
104 content.setText(atomEntryContent.getText());
105
106 return content;
107 }
108
109 @Override
110 public String getContentType(E entry) {
111 return _atomCollectionAdapter.getMediaContentType(entry);
112 }
113
114 @Override
115 public Iterable<E> getEntries(RequestContext requestContext)
116 throws ResponseContextException {
117
118 try {
119 return _atomCollectionAdapter.getFeedEntries(
120 new AtomRequestContextImpl(requestContext));
121 }
122 catch (AtomException ae) {
123 throw new ResponseContextException(
124 ae.getErrorCode(), ae.getCause());
125 }
126 }
127
128 @Override
129 public E getEntry(String resourceName, RequestContext requestContext)
130 throws ResponseContextException {
131
132 try {
133 if (resourceName.endsWith(":media")) {
134 resourceName = resourceName.substring(
135 0, resourceName.length() - 6);
136 }
137
138 return _atomCollectionAdapter.getEntry(
139 resourceName, new AtomRequestContextImpl(requestContext));
140 }
141 catch (AtomException ae) {
142 throw new ResponseContextException(
143 ae.getErrorCode(), ae.getCause());
144 }
145 }
146
147 @Override
148 public String getMediaName(E entry) throws ResponseContextException {
149 try {
150 return _atomCollectionAdapter.getMediaName(entry);
151 }
152 catch (AtomException ae) {
153 throw new ResponseContextException(
154 ae.getErrorCode(), ae.getCause());
155 }
156 }
157
158 @Override
159 public InputStream getMediaStream(E entry) throws ResponseContextException {
160 try {
161 return _atomCollectionAdapter.getMediaStream(entry);
162 }
163 catch (AtomException ae) {
164 throw new ResponseContextException(
165 ae.getErrorCode(), ae.getCause());
166 }
167 }
168
169 @Override
170 public Text getSummary(E entry, RequestContext request) {
171 Abdera abdera = new Abdera();
172
173 Factory factory = abdera.getFactory();
174
175 Text summary = factory.newSummary();
176
177 summary.setValue(_atomCollectionAdapter.getEntrySummary(entry));
178
179 return summary;
180 }
181
182 @Override
183 public String getTitle(E entry) {
184 return _atomCollectionAdapter.getEntryTitle(entry);
185 }
186
187 public String getTitle(RequestContext requestContext) {
188 return _atomCollectionAdapter.getFeedTitle(
189 new AtomRequestContextImpl(requestContext));
190 }
191
192 @Override
193 public Date getUpdated(E entry) {
194 return _atomCollectionAdapter.getEntryUpdated(entry);
195 }
196
197 @Override
198 public E postEntry(
199 String title, IRI id, String summary, Date updated,
200 List<Person> authors, Content content,
201 RequestContext requestContext)
202 throws ResponseContextException {
203
204 try {
205 return _atomCollectionAdapter.postEntry(
206 title, summary, content.getText(), updated,
207 new AtomRequestContextImpl(requestContext));
208 }
209 catch (AtomException ae) {
210 throw new ResponseContextException(
211 ae.getErrorCode(), ae.getCause());
212 }
213 }
214
215 @Override
216 public E postMedia(
217 MimeType mimeType, String slug, InputStream inputStream,
218 RequestContext requestContext)
219 throws ResponseContextException {
220
221 try {
222 return _atomCollectionAdapter.postMedia(
223 mimeType.toString(), slug, inputStream,
224 new AtomRequestContextImpl(requestContext));
225 }
226 catch (AtomException ae) {
227 throw new ResponseContextException(
228 ae.getErrorCode(), ae.getCause());
229 }
230 }
231
232 @Override
233 public void putEntry(
234 E entry, String title, Date updated, List<Person> authors,
235 String summary, Content content, RequestContext requestContext)
236 throws ResponseContextException {
237
238 try {
239 _atomCollectionAdapter.putEntry(
240 entry, title, summary, content.getText(), updated,
241 new AtomRequestContextImpl(requestContext));
242 }
243 catch (AtomException ae) {
244 throw new ResponseContextException(
245 ae.getErrorCode(), ae.getCause());
246 }
247 }
248
249 @Override
250 public void putMedia(
251 E entry, MimeType contentType, String slug, InputStream inputStream,
252 RequestContext requestContext)
253 throws ResponseContextException {
254
255 try {
256 _atomCollectionAdapter.putMedia(
257 entry, contentType.toString(), slug, inputStream,
258 new AtomRequestContextImpl(requestContext));
259 }
260 catch (AtomException ae) {
261 throw new ResponseContextException(
262 ae.getErrorCode(), ae.getCause());
263 }
264 }
265
266 @Override
267 protected String getEntryId(E entry) {
268 return _atomCollectionAdapter.getEntryId(entry);
269 }
270
271 protected Content newContent(
272 AtomEntryContent.Type atomEntryContentType,
273 RequestContext requestContext) {
274
275 Abdera abdera = requestContext.getAbdera();
276
277 Factory factory = abdera.getFactory();
278
279 if (atomEntryContentType == AtomEntryContent.Type.HTML) {
280 return factory.newContent(Content.Type.HTML);
281 }
282 else if (atomEntryContentType == AtomEntryContent.Type.MEDIA) {
283 return factory.newContent(Content.Type.MEDIA);
284 }
285 else if (atomEntryContentType == AtomEntryContent.Type.TEXT) {
286 return factory.newContent(Content.Type.TEXT);
287 }
288 else if (atomEntryContentType == AtomEntryContent.Type.XHTML) {
289 return factory.newContent(Content.Type.XHTML);
290 }
291 else if (atomEntryContentType == AtomEntryContent.Type.XML) {
292 return factory.newContent(Content.Type.XML);
293 }
294 else {
295 throw new IllegalArgumentException();
296 }
297 }
298
299 private AtomCollectionAdapter<E> _atomCollectionAdapter;
300
301 }