001    /**
002     * Copyright (c) 2000-present 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.portlet.documentlibrary.atom;
016    
017    import com.liferay.portal.atom.AtomPager;
018    import com.liferay.portal.atom.AtomUtil;
019    import com.liferay.portal.kernel.atom.AtomEntryContent;
020    import com.liferay.portal.kernel.atom.AtomException;
021    import com.liferay.portal.kernel.atom.AtomRequestContext;
022    import com.liferay.portal.kernel.atom.BaseMediaAtomCollectionAdapter;
023    import com.liferay.portal.kernel.repository.model.FileEntry;
024    import com.liferay.portal.kernel.repository.model.Folder;
025    import com.liferay.portal.kernel.util.Base64;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.MimeTypesUtil;
028    import com.liferay.portal.kernel.util.StreamUtil;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.util.PortletKeys;
031    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
032    import com.liferay.portlet.documentlibrary.util.comparator.RepositoryModelNameComparator;
033    
034    import java.io.ByteArrayInputStream;
035    import java.io.ByteArrayOutputStream;
036    import java.io.InputStream;
037    
038    import java.util.ArrayList;
039    import java.util.Date;
040    import java.util.List;
041    
042    /**
043     * @author Igor Spasic
044     */
045    public class FileEntryAtomCollectionAdapter
046            extends BaseMediaAtomCollectionAdapter<FileEntry> {
047    
048            @Override
049            public String getCollectionName() {
050                    return COLLECTION_NAME;
051            }
052    
053            @Override
054            public List<String> getEntryAuthors(FileEntry fileEntry) {
055                    List<String> authors = new ArrayList<String>();
056    
057                    authors.add(fileEntry.getUserName());
058    
059                    return authors;
060            }
061    
062            @Override
063            public AtomEntryContent getEntryContent(
064                    FileEntry fileEntry, AtomRequestContext atomRequestContext) {
065    
066                    AtomEntryContent atomEntryContent = new AtomEntryContent(
067                            AtomEntryContent.Type.MEDIA);
068    
069                    atomEntryContent.setMimeType(fileEntry.getMimeType());
070    
071                    String srcLink = AtomUtil.createEntryLink(
072                            atomRequestContext, COLLECTION_NAME,
073                            fileEntry.getFileEntryId() + ":media");
074    
075                    atomEntryContent.setSrcLink(srcLink);
076    
077                    return atomEntryContent;
078            }
079    
080            @Override
081            public String getEntryId(FileEntry fileEntry) {
082                    return String.valueOf(fileEntry.getPrimaryKey());
083            }
084    
085            @Override
086            public String getEntrySummary(FileEntry fileEntry) {
087                    return fileEntry.getDescription();
088            }
089    
090            @Override
091            public String getEntryTitle(FileEntry fileEntry) {
092                    return fileEntry.getTitle();
093            }
094    
095            @Override
096            public Date getEntryUpdated(FileEntry fileEntry) {
097                    return fileEntry.getModifiedDate();
098            }
099    
100            @Override
101            public String getFeedTitle(AtomRequestContext atomRequestContext) {
102                    return AtomUtil.createFeedTitleFromPortletName(
103                            atomRequestContext, PortletKeys.DOCUMENT_LIBRARY) + " files";
104            }
105    
106            @Override
107            public String getMediaContentType(FileEntry fileEntry) {
108                    return fileEntry.getMimeType();
109            }
110    
111            @Override
112            public String getMediaName(FileEntry fileEntry) {
113                    return fileEntry.getTitle();
114            }
115    
116            @Override
117            public InputStream getMediaStream(FileEntry fileEntry)
118                    throws AtomException {
119    
120                    try {
121                            return fileEntry.getContentStream();
122                    }
123                    catch (Exception ex) {
124                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, ex);
125                    }
126            }
127    
128            @Override
129            protected void doDeleteEntry(
130                            String resourceName, AtomRequestContext atomRequestContext)
131                    throws Exception {
132    
133                    long fileEntryId = GetterUtil.getLong(resourceName);
134    
135                    DLAppServiceUtil.deleteFileEntry(fileEntryId);
136            }
137    
138            @Override
139            protected FileEntry doGetEntry(
140                            String resourceName, AtomRequestContext atomRequestContext)
141                    throws Exception {
142    
143                    long fileEntryId = GetterUtil.getLong(resourceName);
144    
145                    return DLAppServiceUtil.getFileEntry(fileEntryId);
146            }
147    
148            @Override
149            protected Iterable<FileEntry> doGetFeedEntries(
150                            AtomRequestContext atomRequestContext)
151                    throws Exception {
152    
153                    long folderId = atomRequestContext.getLongParameter("folderId");
154    
155                    long repositoryId = 0;
156    
157                    if (folderId != 0) {
158                            Folder folder = DLAppServiceUtil.getFolder(folderId);
159    
160                            repositoryId = folder.getRepositoryId();
161                    }
162                    else {
163                            repositoryId = atomRequestContext.getLongParameter("repositoryId");
164                    }
165    
166                    int count = DLAppServiceUtil.getFileEntriesCount(
167                            repositoryId, folderId);
168    
169                    AtomPager atomPager = new AtomPager(atomRequestContext, count);
170    
171                    AtomUtil.saveAtomPagerInRequest(atomRequestContext, atomPager);
172    
173                    return DLAppServiceUtil.getFileEntries(
174                            repositoryId, folderId, atomPager.getStart(),
175                            atomPager.getEnd() + 1,
176                            new RepositoryModelNameComparator<FileEntry>());
177            }
178    
179            @Override
180            protected FileEntry doPostEntry(
181                            String title, String summary, String content, Date date,
182                            AtomRequestContext atomRequestContext)
183                    throws Exception {
184    
185                    long folderId = atomRequestContext.getLongParameter("folderId");
186    
187                    long repositoryId = 0;
188    
189                    if (folderId != 0) {
190                            Folder folder = DLAppServiceUtil.getFolder(folderId);
191    
192                            repositoryId = folder.getRepositoryId();
193                    }
194                    else {
195                            repositoryId = atomRequestContext.getLongParameter("repositoryId");
196                    }
197    
198                    String mimeType = atomRequestContext.getHeader("Media-Content-Type");
199    
200                    if (mimeType == null) {
201                            mimeType = MimeTypesUtil.getContentType(title);
202                    }
203    
204                    byte[] contentDecoded = Base64.decode(content);
205    
206                    ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
207                            contentDecoded);
208    
209                    ServiceContext serviceContext = new ServiceContext();
210    
211                    FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
212                            repositoryId, folderId, title, mimeType, title, summary, null,
213                            contentInputStream, contentDecoded.length, serviceContext);
214    
215                    return fileEntry;
216            }
217    
218            @Override
219            protected FileEntry doPostMedia(
220                            String mimeType, String slug, InputStream inputStream,
221                            AtomRequestContext atomRequestContext)
222                    throws Exception {
223    
224                    long folderId = atomRequestContext.getLongParameter("folderId");
225    
226                    long repositoryId = 0;
227    
228                    if (folderId != 0) {
229                            Folder folder = DLAppServiceUtil.getFolder(folderId);
230    
231                            repositoryId = folder.getRepositoryId();
232                    }
233                    else {
234                            repositoryId = atomRequestContext.getLongParameter("repositoryId");
235                    }
236    
237                    String title = atomRequestContext.getHeader("Title");
238                    String description = atomRequestContext.getHeader("Summary");
239    
240                    ByteArrayOutputStream byteArrayOutputStream =
241                            new ByteArrayOutputStream();
242    
243                    StreamUtil.transfer(inputStream, byteArrayOutputStream);
244    
245                    byte[] content = byteArrayOutputStream.toByteArray();
246    
247                    ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
248                            content);
249    
250                    ServiceContext serviceContext = new ServiceContext();
251    
252                    FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
253                            repositoryId, folderId, title, mimeType, title, description, null,
254                            contentInputStream, content.length, serviceContext);
255    
256                    return fileEntry;
257            }
258    
259            @Override
260            protected void doPutEntry(
261                            FileEntry fileEntry, String title, String summary, String content,
262                            Date date, AtomRequestContext atomRequestContext)
263                    throws Exception {
264    
265                    String mimeType = atomRequestContext.getHeader("Media-Content-Type");
266    
267                    if (mimeType == null) {
268                            mimeType = MimeTypesUtil.getContentType(title);
269                    }
270    
271                    byte[] contentDecoded = Base64.decode(content);
272    
273                    ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
274                            contentDecoded);
275    
276                    ServiceContext serviceContext = new ServiceContext();
277    
278                    DLAppServiceUtil.updateFileEntry(
279                            fileEntry.getFileEntryId(), title, mimeType, title, summary, null,
280                            true, contentInputStream, contentDecoded.length, serviceContext);
281            }
282    
283            @Override
284            protected void doPutMedia(
285                            FileEntry fileEntry, String mimeType, String slug,
286                            InputStream inputStream, AtomRequestContext atomRequestContext)
287                    throws Exception {
288    
289                    String title = atomRequestContext.getHeader("Title");
290                    String description = atomRequestContext.getHeader("Summary");
291    
292                    ByteArrayOutputStream byteArrayOutputStream =
293                            new ByteArrayOutputStream();
294    
295                    StreamUtil.transfer(inputStream, byteArrayOutputStream);
296    
297                    byte[] content = byteArrayOutputStream.toByteArray();
298    
299                    ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
300                            content);
301    
302                    ServiceContext serviceContext = new ServiceContext();
303    
304                    DLAppServiceUtil.updateFileEntry(
305                            fileEntry.getFileEntryId(), slug, mimeType, title, description,
306                            null, true, contentInputStream, content.length, serviceContext);
307            }
308    
309            protected static final String COLLECTION_NAME = "files";
310    
311    }