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