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.layoutsadmin.action;
016    
017    import com.liferay.portal.LARFileException;
018    import com.liferay.portal.LARFileSizeException;
019    import com.liferay.portal.LARTypeException;
020    import com.liferay.portal.LayoutImportException;
021    import com.liferay.portal.LayoutPrototypeException;
022    import com.liferay.portal.LocaleException;
023    import com.liferay.portal.NoSuchGroupException;
024    import com.liferay.portal.kernel.exception.PortalException;
025    import com.liferay.portal.kernel.exception.SystemException;
026    import com.liferay.portal.kernel.json.JSONFactoryUtil;
027    import com.liferay.portal.kernel.json.JSONObject;
028    import com.liferay.portal.kernel.lar.ExportImportHelper;
029    import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
030    import com.liferay.portal.kernel.lar.MissingReference;
031    import com.liferay.portal.kernel.lar.MissingReferences;
032    import com.liferay.portal.kernel.log.Log;
033    import com.liferay.portal.kernel.log.LogFactoryUtil;
034    import com.liferay.portal.kernel.repository.model.FileEntry;
035    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
036    import com.liferay.portal.kernel.servlet.SessionErrors;
037    import com.liferay.portal.kernel.staging.StagingUtil;
038    import com.liferay.portal.kernel.upload.UploadException;
039    import com.liferay.portal.kernel.upload.UploadPortletRequest;
040    import com.liferay.portal.kernel.util.Constants;
041    import com.liferay.portal.kernel.util.ContentTypes;
042    import com.liferay.portal.kernel.util.ParamUtil;
043    import com.liferay.portal.kernel.util.StreamUtil;
044    import com.liferay.portal.security.auth.PrincipalException;
045    import com.liferay.portal.service.LayoutServiceUtil;
046    import com.liferay.portal.struts.PortletAction;
047    import com.liferay.portal.theme.ThemeDisplay;
048    import com.liferay.portal.util.PortalUtil;
049    import com.liferay.portal.util.WebKeys;
050    import com.liferay.portlet.documentlibrary.FileSizeException;
051    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
052    import com.liferay.portlet.sites.action.ActionUtil;
053    
054    import java.io.InputStream;
055    
056    import java.util.Map;
057    
058    import javax.portlet.ActionRequest;
059    import javax.portlet.ActionResponse;
060    import javax.portlet.PortletConfig;
061    import javax.portlet.PortletContext;
062    import javax.portlet.PortletRequestDispatcher;
063    import javax.portlet.RenderRequest;
064    import javax.portlet.RenderResponse;
065    import javax.portlet.ResourceRequest;
066    import javax.portlet.ResourceResponse;
067    
068    import javax.servlet.http.HttpServletRequest;
069    import javax.servlet.http.HttpServletResponse;
070    
071    import org.apache.commons.fileupload.FileUploadBase;
072    import org.apache.struts.action.ActionForm;
073    import org.apache.struts.action.ActionForward;
074    import org.apache.struts.action.ActionMapping;
075    
076    /**
077     * @author Alexander Chow
078     * @author Raymond Aug??
079     */
080    public class ImportLayoutsAction extends PortletAction {
081    
082            @Override
083            public void processAction(
084                            ActionMapping actionMapping, ActionForm actionForm,
085                            PortletConfig portletConfig, ActionRequest actionRequest,
086                            ActionResponse actionResponse)
087                    throws Exception {
088    
089                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
090    
091                    try {
092                            if (cmd.equals(Constants.ADD_TEMP)) {
093                                    addTempFileEntry(
094                                            actionRequest, actionResponse,
095                                            ExportImportHelper.TEMP_FOLDER_NAME);
096    
097                                    validateFile(
098                                            actionRequest, actionResponse,
099                                            ExportImportHelper.TEMP_FOLDER_NAME);
100                            }
101                            else if (cmd.equals(Constants.DELETE_TEMP)) {
102                                    deleteTempFileEntry(
103                                            actionRequest, actionResponse,
104                                            ExportImportHelper.TEMP_FOLDER_NAME);
105                            }
106                            else if (cmd.equals(Constants.IMPORT)) {
107                                    hideDefaultSuccessMessage(actionRequest);
108    
109                                    importData(
110                                            actionRequest, actionResponse,
111                                            ExportImportHelper.TEMP_FOLDER_NAME);
112    
113                                    String redirect = ParamUtil.getString(
114                                            actionRequest, "redirect");
115    
116                                    sendRedirect(actionRequest, actionResponse, redirect);
117                            }
118                    }
119                    catch (Exception e) {
120                            if (cmd.equals(Constants.ADD_TEMP) ||
121                                    cmd.equals(Constants.DELETE_TEMP)) {
122    
123                                    handleUploadException(
124                                            portletConfig, actionRequest, actionResponse,
125                                            ExportImportHelper.TEMP_FOLDER_NAME, e);
126                            }
127                            else {
128                                    if ((e instanceof LARFileException) ||
129                                            (e instanceof LARFileSizeException) ||
130                                            (e instanceof LARTypeException)) {
131    
132                                            SessionErrors.add(actionRequest, e.getClass());
133                                    }
134                                    else if ((e instanceof LayoutPrototypeException) ||
135                                                     (e instanceof LocaleException)) {
136    
137                                            SessionErrors.add(actionRequest, e.getClass(), e);
138                                    }
139                                    else {
140                                            _log.error(e, e);
141    
142                                            SessionErrors.add(
143                                                    actionRequest, LayoutImportException.class.getName());
144                                    }
145                            }
146                    }
147            }
148    
149            @Override
150            public ActionForward render(
151                            ActionMapping actionMapping, ActionForm actionForm,
152                            PortletConfig portletConfig, RenderRequest renderRequest,
153                            RenderResponse renderResponse)
154                    throws Exception {
155    
156                    try {
157                            ActionUtil.getGroup(renderRequest);
158                    }
159                    catch (Exception e) {
160                            if (e instanceof NoSuchGroupException ||
161                                    e instanceof PrincipalException) {
162    
163                                    SessionErrors.add(renderRequest, e.getClass());
164    
165                                    return actionMapping.findForward("portlet.layouts_admin.error");
166                            }
167                            else {
168                                    throw e;
169                            }
170                    }
171    
172                    return actionMapping.findForward(
173                            getForward(renderRequest, "portlet.layouts_admin.import_layouts"));
174            }
175    
176            @Override
177            public void serveResource(
178                            ActionMapping actionMapping, ActionForm actionForm,
179                            PortletConfig portletConfig, ResourceRequest resourceRequest,
180                            ResourceResponse resourceResponse)
181                    throws Exception {
182    
183                    String cmd = ParamUtil.getString(resourceRequest, Constants.CMD);
184    
185                    PortletContext portletContext = portletConfig.getPortletContext();
186    
187                    PortletRequestDispatcher portletRequestDispatcher = null;
188    
189                    if (cmd.equals(Constants.IMPORT)) {
190                            portletRequestDispatcher = portletContext.getRequestDispatcher(
191                                    "/html/portlet/layouts_admin/import_layouts_processes.jsp");
192                    }
193                    else {
194                            portletRequestDispatcher = portletContext.getRequestDispatcher(
195                                    "/html/portlet/layouts_admin/import_layouts_resources.jsp");
196                    }
197    
198                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
199            }
200    
201            protected void addTempFileEntry(
202                            ActionRequest actionRequest, ActionResponse actionResponse,
203                            String folderName)
204                    throws Exception {
205    
206                    UploadPortletRequest uploadPortletRequest =
207                            PortalUtil.getUploadPortletRequest(actionRequest);
208    
209                    checkExceededSizeLimit(uploadPortletRequest);
210    
211                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
212    
213                    deleteTempFileEntry(groupId, folderName);
214    
215                    InputStream inputStream = null;
216    
217                    try {
218                            String sourceFileName = uploadPortletRequest.getFileName("file");
219    
220                            inputStream = uploadPortletRequest.getFileAsStream("file");
221    
222                            String contentType = uploadPortletRequest.getContentType("file");
223    
224                            LayoutServiceUtil.addTempFileEntry(
225                                    groupId, sourceFileName, folderName, inputStream, contentType);
226                    }
227                    catch (Exception e) {
228                            UploadException uploadException =
229                                    (UploadException)actionRequest.getAttribute(
230                                            WebKeys.UPLOAD_EXCEPTION);
231    
232                            if ((uploadException != null) &&
233                                    (uploadException.getCause()
234                                            instanceof FileUploadBase.IOFileUploadException)) {
235    
236                                    // Cancelled a temporary upload
237    
238                            }
239                            else if ((uploadException != null) &&
240                                             uploadException.isExceededSizeLimit()) {
241    
242                                    throw new FileSizeException(uploadException.getCause());
243                            }
244                            else {
245                                    throw e;
246                            }
247                    }
248                    finally {
249                            StreamUtil.cleanUp(inputStream);
250                    }
251            }
252    
253            protected void checkExceededSizeLimit(HttpServletRequest request)
254                    throws PortalException {
255    
256                    UploadException uploadException = (UploadException)request.getAttribute(
257                            WebKeys.UPLOAD_EXCEPTION);
258    
259                    if (uploadException != null) {
260                            if (uploadException.isExceededSizeLimit()) {
261                                    throw new LARFileSizeException(uploadException.getCause());
262                            }
263    
264                            throw new PortalException(uploadException.getCause());
265                    }
266            }
267    
268            protected void deleteTempFileEntry(
269                            ActionRequest actionRequest, ActionResponse actionResponse,
270                            String folderName)
271                    throws Exception {
272    
273                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
274                            WebKeys.THEME_DISPLAY);
275    
276                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
277    
278                    try {
279                            String fileName = ParamUtil.getString(actionRequest, "fileName");
280    
281                            LayoutServiceUtil.deleteTempFileEntry(
282                                    themeDisplay.getScopeGroupId(), fileName, folderName);
283    
284                            jsonObject.put("deleted", Boolean.TRUE);
285                    }
286                    catch (Exception e) {
287                            String errorMessage = themeDisplay.translate(
288                                    "an-unexpected-error-occurred-while-deleting-the-file");
289    
290                            jsonObject.put("deleted", Boolean.FALSE);
291                            jsonObject.put("errorMessage", errorMessage);
292                    }
293    
294                    writeJSON(actionRequest, actionResponse, jsonObject);
295            }
296    
297            protected void deleteTempFileEntry(long groupId, String folderName)
298                    throws PortalException, SystemException {
299    
300                    String[] tempFileEntryNames = LayoutServiceUtil.getTempFileEntryNames(
301                            groupId, folderName);
302    
303                    for (String tempFileEntryName : tempFileEntryNames) {
304                            LayoutServiceUtil.deleteTempFileEntry(
305                                    groupId, tempFileEntryName, folderName);
306                    }
307            }
308    
309            protected void handleUploadException(
310                            PortletConfig portletConfig, ActionRequest actionRequest,
311                            ActionResponse actionResponse, String folderName, Exception e)
312                    throws Exception {
313    
314                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
315                            actionResponse);
316    
317                    response.setContentType(ContentTypes.TEXT_HTML);
318                    response.setStatus(HttpServletResponse.SC_OK);
319    
320                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
321                            WebKeys.THEME_DISPLAY);
322    
323                    deleteTempFileEntry(themeDisplay.getScopeGroupId(), folderName);
324    
325                    JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
326                            themeDisplay.getLocale(), e, null);
327    
328                    writeJSON(actionRequest, actionResponse, jsonObject);
329    
330                    ServletResponseUtil.write(
331                            response, String.valueOf(jsonObject.getInt("status")));
332            }
333    
334            protected void importData(
335                            ActionRequest actionRequest, ActionResponse actionResponse,
336                            String folderName)
337                    throws Exception {
338    
339                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
340                            WebKeys.THEME_DISPLAY);
341    
342                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
343    
344                    FileEntry fileEntry = ExportImportHelperUtil.getTempFileEntry(
345                            groupId, themeDisplay.getUserId(), folderName);
346    
347                    InputStream inputStream = null;
348    
349                    try {
350                            inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(
351                                    themeDisplay.getUserId(), fileEntry.getFileEntryId(),
352                                    fileEntry.getVersion(), false);
353    
354                            importData(actionRequest, fileEntry.getTitle(), inputStream);
355    
356                            deleteTempFileEntry(groupId, folderName);
357    
358                            addSuccessMessage(actionRequest, actionResponse);
359                    }
360                    finally {
361                            StreamUtil.cleanUp(inputStream);
362                    }
363            }
364    
365            protected void importData(
366                            ActionRequest actionRequest, String fileName,
367                            InputStream inputStream)
368                    throws Exception {
369    
370                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
371                    boolean privateLayout = ParamUtil.getBoolean(
372                            actionRequest, "privateLayout");
373    
374                    LayoutServiceUtil.importLayoutsInBackground(
375                            fileName, groupId, privateLayout, actionRequest.getParameterMap(),
376                            inputStream);
377            }
378    
379            protected void validateFile(
380                            ActionRequest actionRequest, ActionResponse actionResponse,
381                            String folderName)
382                    throws Exception {
383    
384                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
385                            WebKeys.THEME_DISPLAY);
386    
387                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
388    
389                    FileEntry fileEntry = ExportImportHelperUtil.getTempFileEntry(
390                            groupId, themeDisplay.getUserId(), folderName);
391    
392                    InputStream inputStream = null;
393    
394                    try {
395                            inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(
396                                    themeDisplay.getUserId(), fileEntry.getFileEntryId(),
397                                    fileEntry.getVersion(), false);
398    
399                            MissingReferences missingReferences = validateFile(
400                                    actionRequest, inputStream);
401    
402                            Map<String, MissingReference> weakMissingReferences =
403                                    missingReferences.getWeakMissingReferences();
404    
405                            if (weakMissingReferences.isEmpty()) {
406                                    return;
407                            }
408    
409                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
410    
411                            if ((weakMissingReferences != null) &&
412                                    !weakMissingReferences.isEmpty()) {
413    
414                                    jsonObject.put(
415                                            "warningMessages",
416                                            StagingUtil.getWarningMessagesJSONArray(
417                                                    themeDisplay.getLocale(), weakMissingReferences, null));
418                            }
419    
420                            writeJSON(actionRequest, actionResponse, jsonObject);
421                    }
422                    finally {
423                            StreamUtil.cleanUp(inputStream);
424                    }
425            }
426    
427            protected MissingReferences validateFile(
428                            ActionRequest actionRequest, InputStream inputStream)
429                    throws Exception {
430    
431                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
432                    boolean privateLayout = ParamUtil.getBoolean(
433                            actionRequest, "privateLayout");
434    
435                    return LayoutServiceUtil.validateImportLayoutsFile(
436                            groupId, privateLayout, actionRequest.getParameterMap(),
437                            inputStream);
438            }
439    
440            private static Log _log = LogFactoryUtil.getLog(ImportLayoutsAction.class);
441    
442    }