001
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.bookmarks.util.comparator.EntryNameComparator;
032 import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
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
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, new EntryNameComparator());
176 }
177
178 @Override
179 protected FileEntry doPostEntry(
180 String title, String summary, String content, Date date,
181 AtomRequestContext atomRequestContext)
182 throws Exception {
183
184 long folderId = atomRequestContext.getLongParameter("folderId");
185
186 long repositoryId = 0;
187
188 if (folderId != 0) {
189 Folder folder = DLAppServiceUtil.getFolder(folderId);
190
191 repositoryId = folder.getRepositoryId();
192 }
193 else {
194 repositoryId = atomRequestContext.getLongParameter("repositoryId");
195 }
196
197 String mimeType = atomRequestContext.getHeader("Media-Content-Type");
198
199 if (mimeType == null) {
200 mimeType = MimeTypesUtil.getContentType(title);
201 }
202
203 byte[] contentDecoded = Base64.decode(content);
204
205 ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
206 contentDecoded);
207
208 ServiceContext serviceContext = new ServiceContext();
209
210 FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
211 repositoryId, folderId, title, mimeType, title, summary, null,
212 contentInputStream, contentDecoded.length, serviceContext);
213
214 return fileEntry;
215 }
216
217 @Override
218 protected FileEntry doPostMedia(
219 String mimeType, String slug, InputStream inputStream,
220 AtomRequestContext atomRequestContext)
221 throws Exception {
222
223 long folderId = atomRequestContext.getLongParameter("folderId");
224
225 long repositoryId = 0;
226
227 if (folderId != 0) {
228 Folder folder = DLAppServiceUtil.getFolder(folderId);
229
230 repositoryId = folder.getRepositoryId();
231 }
232 else {
233 repositoryId = atomRequestContext.getLongParameter("repositoryId");
234 }
235
236 String title = atomRequestContext.getHeader("Title");
237 String description = atomRequestContext.getHeader("Summary");
238
239 ByteArrayOutputStream byteArrayOutputStream =
240 new ByteArrayOutputStream();
241
242 StreamUtil.transfer(inputStream, byteArrayOutputStream);
243
244 byte[] content = byteArrayOutputStream.toByteArray();
245
246 ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
247 content);
248
249 ServiceContext serviceContext = new ServiceContext();
250
251 FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
252 repositoryId, folderId, title, mimeType, title, description, null,
253 contentInputStream, content.length, serviceContext);
254
255 return fileEntry;
256 }
257
258 @Override
259 protected void doPutEntry(
260 FileEntry fileEntry, String title, String summary, String content,
261 Date date, AtomRequestContext atomRequestContext)
262 throws Exception {
263
264 String mimeType = atomRequestContext.getHeader("Media-Content-Type");
265
266 if (mimeType == null) {
267 mimeType = MimeTypesUtil.getContentType(title);
268 }
269
270 byte[] contentDecoded = Base64.decode(content);
271
272 ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
273 contentDecoded);
274
275 ServiceContext serviceContext = new ServiceContext();
276
277 DLAppServiceUtil.updateFileEntry(
278 fileEntry.getFileEntryId(), title, mimeType, title, summary, null,
279 true, contentInputStream, contentDecoded.length, serviceContext);
280 }
281
282 @Override
283 protected void doPutMedia(
284 FileEntry fileEntry, String mimeType, String slug,
285 InputStream inputStream, AtomRequestContext atomRequestContext)
286 throws Exception {
287
288 String title = atomRequestContext.getHeader("Title");
289 String description = atomRequestContext.getHeader("Summary");
290
291 ByteArrayOutputStream byteArrayOutputStream =
292 new ByteArrayOutputStream();
293
294 StreamUtil.transfer(inputStream, byteArrayOutputStream);
295
296 byte[] content = byteArrayOutputStream.toByteArray();
297
298 ByteArrayInputStream contentInputStream = new ByteArrayInputStream(
299 content);
300
301 ServiceContext serviceContext = new ServiceContext();
302
303 DLAppServiceUtil.updateFileEntry(
304 fileEntry.getFileEntryId(), slug, mimeType, title, description,
305 null, true, contentInputStream, content.length, serviceContext);
306 }
307
308 protected static final String COLLECTION_NAME = "files";
309
310 }