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.portlet.wiki.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.servlet.ServletResponseConstants;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.upload.UploadException;
024    import com.liferay.portal.kernel.upload.UploadPortletRequest;
025    import com.liferay.portal.kernel.util.Constants;
026    import com.liferay.portal.kernel.util.KeyValuePair;
027    import com.liferay.portal.kernel.util.ObjectValuePair;
028    import com.liferay.portal.kernel.util.ParamUtil;
029    import com.liferay.portal.kernel.util.StreamUtil;
030    import com.liferay.portal.kernel.util.TempFileUtil;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.security.auth.PrincipalException;
033    import com.liferay.portal.struts.ActionConstants;
034    import com.liferay.portal.theme.ThemeDisplay;
035    import com.liferay.portal.util.PortalUtil;
036    import com.liferay.portal.util.WebKeys;
037    import com.liferay.portlet.documentlibrary.DuplicateFileException;
038    import com.liferay.portlet.documentlibrary.FileExtensionException;
039    import com.liferay.portlet.documentlibrary.FileNameException;
040    import com.liferay.portlet.documentlibrary.FileSizeException;
041    import com.liferay.portlet.documentlibrary.action.EditFileEntryAction;
042    import com.liferay.portlet.wiki.NoSuchNodeException;
043    import com.liferay.portlet.wiki.NoSuchPageException;
044    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
045    
046    import java.io.InputStream;
047    
048    import java.util.ArrayList;
049    import java.util.List;
050    
051    import javax.portlet.ActionRequest;
052    import javax.portlet.ActionResponse;
053    import javax.portlet.PortletConfig;
054    import javax.portlet.RenderRequest;
055    import javax.portlet.RenderResponse;
056    
057    import javax.servlet.http.HttpServletResponse;
058    
059    import org.apache.struts.action.ActionForm;
060    import org.apache.struts.action.ActionForward;
061    import org.apache.struts.action.ActionMapping;
062    
063    /**
064     * @author Jorge Ferrer
065     */
066    public class EditPageAttachmentAction extends EditFileEntryAction {
067    
068            @Override
069            public void processAction(
070                            ActionMapping actionMapping, ActionForm actionForm,
071                            PortletConfig portletConfig, ActionRequest actionRequest,
072                            ActionResponse actionResponse)
073                    throws Exception {
074    
075                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
076    
077                    try {
078                            if (Validator.isNull(cmd)) {
079                                    UploadException uploadException =
080                                            (UploadException)actionRequest.getAttribute(
081                                                    WebKeys.UPLOAD_EXCEPTION);
082    
083                                    if (uploadException != null) {
084                                            if (uploadException.isExceededSizeLimit()) {
085                                                    throw new FileSizeException(uploadException.getCause());
086                                            }
087    
088                                            throw new PortalException(uploadException.getCause());
089                                    }
090                            }
091                            else if (cmd.equals(Constants.ADD)) {
092                                    addAttachment(actionRequest);
093                            }
094                            else if (cmd.equals(Constants.ADD_MULTIPLE)) {
095                                    addMultipleFileEntries(actionRequest, actionResponse);
096                            }
097                            else if (cmd.equals(Constants.ADD_TEMP)) {
098                                    addTempAttachment(actionRequest);
099                            }
100                            else if (cmd.equals(Constants.DELETE)) {
101                                    deleteAttachment(actionRequest);
102                            }
103                            else if (cmd.equals(Constants.DELETE_TEMP)) {
104                                    deleteTempAttachment(actionRequest, actionResponse);
105                            }
106    
107                            if (cmd.equals(Constants.ADD_TEMP) ||
108                                    cmd.equals(Constants.DELETE_TEMP)) {
109    
110                                    setForward(actionRequest, ActionConstants.COMMON_NULL);
111                            }
112                            else {
113                                    sendRedirect(actionRequest, actionResponse);
114                            }
115                    }
116                    catch (Exception e) {
117                            if (e instanceof NoSuchNodeException ||
118                                    e instanceof NoSuchPageException ||
119                                    e instanceof PrincipalException) {
120    
121                                    SessionErrors.add(actionRequest, e.getClass());
122    
123                                    setForward(actionRequest, "portlet.wiki.error");
124                            }
125                            else if (e instanceof DuplicateFileException ||
126                                             e instanceof FileNameException) {
127    
128                                    SessionErrors.add(actionRequest, e.getClass().getName());
129    
130                                    HttpServletResponse response =
131                                            PortalUtil.getHttpServletResponse(actionResponse);
132    
133                                    if (e instanceof DuplicateFileException) {
134                                            response.setStatus(
135                                                    ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
136                                    }
137                                    else {
138                                            response.setStatus(
139                                                    ServletResponseConstants.SC_FILE_NAME_EXCEPTION);
140                                    }
141                            }
142                            else if (e instanceof FileExtensionException ||
143                                             e instanceof FileSizeException) {
144    
145                                    SessionErrors.add(actionRequest, e.getClass().getName());
146                            }
147                            else {
148                                    throw e;
149                            }
150                    }
151            }
152    
153            @Override
154            public ActionForward render(
155                            ActionMapping actionMapping, ActionForm actionForm,
156                            PortletConfig portletConfig, RenderRequest renderRequest,
157                            RenderResponse renderResponse)
158                    throws Exception {
159    
160                    try {
161                            ActionUtil.getNode(renderRequest);
162                            ActionUtil.getPage(renderRequest);
163                    }
164                    catch (Exception e) {
165                            if (e instanceof NoSuchNodeException ||
166                                    e instanceof NoSuchPageException ||
167                                    e instanceof PrincipalException) {
168    
169                                    SessionErrors.add(renderRequest, e.getClass());
170    
171                                    return actionMapping.findForward("portlet.wiki.error");
172                            }
173                            else {
174                                    throw e;
175                            }
176                    }
177    
178                    return actionMapping.findForward(
179                            getForward(renderRequest, "portlet.wiki.edit_page_attachment"));
180            }
181    
182            protected void addAttachment(ActionRequest actionRequest) throws Exception {
183                    UploadPortletRequest uploadPortletRequest =
184                            PortalUtil.getUploadPortletRequest(actionRequest);
185    
186                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
187                    String title = ParamUtil.getString(actionRequest, "title");
188    
189                    int numOfFiles = ParamUtil.getInteger(actionRequest, "numOfFiles");
190    
191                    List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
192                            new ArrayList<ObjectValuePair<String, InputStream>>();
193    
194                    try {
195                            if (numOfFiles == 0) {
196                                    String fileName = uploadPortletRequest.getFileName("file");
197                                    InputStream inputStream = uploadPortletRequest.getFileAsStream(
198                                            "file");
199    
200                                    if (inputStream != null) {
201                                            ObjectValuePair<String, InputStream> inputStreamOVP =
202                                                    new ObjectValuePair<String, InputStream>(
203                                                            fileName, inputStream);
204    
205                                            inputStreamOVPs.add(inputStreamOVP);
206                                    }
207                            }
208                            else {
209                                    for (int i = 1; i <= numOfFiles; i++) {
210                                            String fileName = uploadPortletRequest.getFileName(
211                                                    "file" + i);
212                                            InputStream inputStream =
213                                                    uploadPortletRequest.getFileAsStream("file" + i);
214    
215                                            if (inputStream == null) {
216                                                    continue;
217                                            }
218    
219                                            ObjectValuePair<String, InputStream> inputStreamOVP =
220                                                    new ObjectValuePair<String, InputStream>(
221                                                            fileName, inputStream);
222    
223                                            inputStreamOVPs.add(inputStreamOVP);
224                                    }
225                            }
226    
227                            WikiPageServiceUtil.addPageAttachments(
228                                    nodeId, title, inputStreamOVPs);
229                    }
230                    finally {
231                            for (ObjectValuePair<String, InputStream> inputStreamOVP :
232                                            inputStreamOVPs) {
233    
234                                    InputStream inputStream = inputStreamOVP.getValue();
235    
236                                    StreamUtil.cleanUp(inputStream);
237                            }
238                    }
239            }
240    
241            @Override
242            protected void addMultipleFileEntries(
243                            ActionRequest actionRequest, ActionResponse actionResponse,
244                            String selectedFileName, List<String> validFileNames,
245                            List<KeyValuePair> invalidFileNameKVPs)
246                    throws Exception {
247    
248                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
249                            WebKeys.THEME_DISPLAY);
250    
251                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
252                    String title = ParamUtil.getString(actionRequest, "title");
253    
254                    String tempFileName = TempFileUtil.getTempFileName(
255                            themeDisplay.getUserId(), selectedFileName, _TEMP_FOLDER_NAME);
256    
257                    try {
258                            InputStream inputStream = TempFileUtil.getTempFileAsStream(
259                                    tempFileName);
260    
261                            WikiPageServiceUtil.addPageAttachment(
262                                    nodeId, title, selectedFileName, inputStream);
263    
264                            validFileNames.add(selectedFileName);
265                    }
266                    catch (Exception e) {
267                            String errorMessage = getAddMultipleFileEntriesErrorMessage(
268                                    themeDisplay, e);
269    
270                            KeyValuePair invalidFileNameKVP = new KeyValuePair(
271                                    selectedFileName, errorMessage);
272    
273                            invalidFileNameKVPs.add(invalidFileNameKVP);
274                    }
275                    finally {
276                            TempFileUtil.deleteTempFile(tempFileName);
277                    }
278            }
279    
280            protected void addTempAttachment(ActionRequest actionRequest)
281                    throws Exception {
282    
283                    UploadPortletRequest uploadPortletRequest =
284                            PortalUtil.getUploadPortletRequest(actionRequest);
285    
286                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
287                    String sourceFileName = uploadPortletRequest.getFileName("file");
288    
289                    InputStream inputStream = null;
290    
291                    try {
292                            inputStream = uploadPortletRequest.getFileAsStream("file");
293    
294                            WikiPageServiceUtil.addTempPageAttachment(
295                                    nodeId, sourceFileName, _TEMP_FOLDER_NAME, inputStream);
296                    }
297                    finally {
298                            StreamUtil.cleanUp(inputStream);
299                    }
300            }
301    
302            protected void deleteAttachment(ActionRequest actionRequest)
303                    throws Exception {
304    
305                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
306                    String title = ParamUtil.getString(actionRequest, "title");
307                    String attachment = ParamUtil.getString(actionRequest, "fileName");
308    
309                    WikiPageServiceUtil.deletePageAttachment(nodeId, title, attachment);
310            }
311    
312            protected void deleteTempAttachment(
313                            ActionRequest actionRequest, ActionResponse actionResponse)
314                    throws Exception {
315    
316                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
317                            WebKeys.THEME_DISPLAY);
318    
319                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
320                    String fileName = ParamUtil.getString(actionRequest, "fileName");
321    
322                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
323    
324                    try {
325                            WikiPageServiceUtil.deleteTempPageAttachment(
326                                    nodeId, fileName, _TEMP_FOLDER_NAME);
327    
328                            jsonObject.put("deleted", Boolean.TRUE);
329                    }
330                    catch (Exception e) {
331                            String errorMessage = LanguageUtil.get(
332                                    themeDisplay.getLocale(),
333                                    "an-unexpected-error-occurred-while-deleting-the-file");
334    
335                            jsonObject.put("deleted", Boolean.FALSE);
336                            jsonObject.put("errorMessage", errorMessage);
337                    }
338    
339                    writeJSON(actionRequest, actionResponse, jsonObject);
340            }
341    
342            private static final String _TEMP_FOLDER_NAME =
343                    EditPageAttachmentAction.class.getName();
344    
345    }