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