001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
039     * @author Igor Spasic
040     */
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            @Override
188            public String getTitle(RequestContext requestContext) {
189                    return _atomCollectionAdapter.getFeedTitle(
190                            new AtomRequestContextImpl(requestContext));
191            }
192    
193            @Override
194            public Date getUpdated(E entry) {
195                    return _atomCollectionAdapter.getEntryUpdated(entry);
196            }
197    
198            @Override
199            public E postEntry(
200                            String title, IRI id, String summary, Date updated,
201                            List<Person> authors, Content content,
202                            RequestContext requestContext)
203                    throws ResponseContextException {
204    
205                    try {
206                            return _atomCollectionAdapter.postEntry(
207                                    title, summary, content.getText(), updated,
208                                    new AtomRequestContextImpl(requestContext));
209                    }
210                    catch (AtomException ae) {
211                            throw new ResponseContextException(
212                                    ae.getErrorCode(), ae.getCause());
213                    }
214            }
215    
216            @Override
217            public E postMedia(
218                            MimeType mimeType, String slug, InputStream inputStream,
219                            RequestContext requestContext)
220                    throws ResponseContextException {
221    
222                    try {
223                            return _atomCollectionAdapter.postMedia(
224                                    mimeType.toString(), slug, inputStream,
225                                    new AtomRequestContextImpl(requestContext));
226                    }
227                    catch (AtomException ae) {
228                            throw new ResponseContextException(
229                                    ae.getErrorCode(), ae.getCause());
230                    }
231            }
232    
233            @Override
234            public void putEntry(
235                            E entry, String title, Date updated, List<Person> authors,
236                            String summary, Content content, RequestContext requestContext)
237                    throws ResponseContextException {
238    
239                    try {
240                            _atomCollectionAdapter.putEntry(
241                                    entry, title, summary, content.getText(), updated,
242                                    new AtomRequestContextImpl(requestContext));
243                    }
244                    catch (AtomException ae) {
245                            throw new ResponseContextException(
246                                    ae.getErrorCode(), ae.getCause());
247                    }
248            }
249    
250            @Override
251            public void putMedia(
252                            E entry, MimeType contentType, String slug, InputStream inputStream,
253                            RequestContext requestContext)
254                    throws ResponseContextException {
255    
256                    try {
257                            _atomCollectionAdapter.putMedia(
258                                    entry, contentType.toString(), slug, inputStream,
259                                    new AtomRequestContextImpl(requestContext));
260                    }
261                    catch (AtomException ae) {
262                            throw new ResponseContextException(
263                                    ae.getErrorCode(), ae.getCause());
264                    }
265            }
266    
267            @Override
268            protected String getEntryId(E entry) {
269                    return _atomCollectionAdapter.getEntryId(entry);
270            }
271    
272            protected Content newContent(
273                    AtomEntryContent.Type atomEntryContentType,
274                    RequestContext requestContext) {
275    
276                    Abdera abdera = requestContext.getAbdera();
277    
278                    Factory factory = abdera.getFactory();
279    
280                    if (atomEntryContentType == AtomEntryContent.Type.HTML) {
281                            return factory.newContent(Content.Type.HTML);
282                    }
283                    else if (atomEntryContentType == AtomEntryContent.Type.MEDIA) {
284                            return factory.newContent(Content.Type.MEDIA);
285                    }
286                    else if (atomEntryContentType == AtomEntryContent.Type.TEXT) {
287                            return factory.newContent(Content.Type.TEXT);
288                    }
289                    else if (atomEntryContentType == AtomEntryContent.Type.XHTML) {
290                            return factory.newContent(Content.Type.XHTML);
291                    }
292                    else if (atomEntryContentType == AtomEntryContent.Type.XML) {
293                            return factory.newContent(Content.Type.XML);
294                    }
295                    else {
296                            throw new IllegalArgumentException();
297                    }
298            }
299    
300            private AtomCollectionAdapter<E> _atomCollectionAdapter;
301    
302    }