001
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
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 importData(
108 actionRequest, actionResponse,
109 ExportImportHelper.TEMP_FOLDER_NAME);
110
111 String redirect = ParamUtil.getString(
112 actionRequest, "redirect");
113
114 sendRedirect(actionRequest, actionResponse, redirect);
115 }
116 }
117 catch (Exception e) {
118 if (cmd.equals(Constants.ADD_TEMP) ||
119 cmd.equals(Constants.DELETE_TEMP)) {
120
121 handleUploadException(
122 portletConfig, actionRequest, actionResponse,
123 ExportImportHelper.TEMP_FOLDER_NAME, e);
124 }
125 else {
126 if ((e instanceof LARFileException) ||
127 (e instanceof LARFileSizeException) ||
128 (e instanceof LARTypeException)) {
129
130 SessionErrors.add(actionRequest, e.getClass());
131 }
132 else if ((e instanceof LayoutPrototypeException) ||
133 (e instanceof LocaleException)) {
134
135 SessionErrors.add(actionRequest, e.getClass(), e);
136 }
137 else {
138 _log.error(e, e);
139
140 SessionErrors.add(
141 actionRequest, LayoutImportException.class.getName());
142 }
143 }
144 }
145 }
146
147 @Override
148 public ActionForward render(
149 ActionMapping actionMapping, ActionForm actionForm,
150 PortletConfig portletConfig, RenderRequest renderRequest,
151 RenderResponse renderResponse)
152 throws Exception {
153
154 try {
155 ActionUtil.getGroup(renderRequest);
156 }
157 catch (Exception e) {
158 if (e instanceof NoSuchGroupException ||
159 e instanceof PrincipalException) {
160
161 SessionErrors.add(renderRequest, e.getClass());
162
163 return actionMapping.findForward("portlet.layouts_admin.error");
164 }
165 else {
166 throw e;
167 }
168 }
169
170 return actionMapping.findForward(
171 getForward(renderRequest, "portlet.layouts_admin.import_layouts"));
172 }
173
174 @Override
175 public void serveResource(
176 ActionMapping actionMapping, ActionForm actionForm,
177 PortletConfig portletConfig, ResourceRequest resourceRequest,
178 ResourceResponse resourceResponse)
179 throws Exception {
180
181 String cmd = ParamUtil.getString(resourceRequest, Constants.CMD);
182
183 PortletContext portletContext = portletConfig.getPortletContext();
184
185 PortletRequestDispatcher portletRequestDispatcher = null;
186
187 if (cmd.equals(Constants.IMPORT)) {
188 portletRequestDispatcher = portletContext.getRequestDispatcher(
189 "/html/portlet/layouts_admin/import_layouts_processes.jsp");
190 }
191 else {
192 portletRequestDispatcher = portletContext.getRequestDispatcher(
193 "/html/portlet/layouts_admin/import_layouts_resources.jsp");
194 }
195
196 portletRequestDispatcher.include(resourceRequest, resourceResponse);
197 }
198
199 protected void addTempFileEntry(
200 ActionRequest actionRequest, ActionResponse actionResponse,
201 String folderName)
202 throws Exception {
203
204 UploadPortletRequest uploadPortletRequest =
205 PortalUtil.getUploadPortletRequest(actionRequest);
206
207 checkExceededSizeLimit(uploadPortletRequest);
208
209 long groupId = ParamUtil.getLong(actionRequest, "groupId");
210
211 deleteTempFileEntry(groupId, folderName);
212
213 InputStream inputStream = null;
214
215 try {
216 String sourceFileName = uploadPortletRequest.getFileName("file");
217
218 inputStream = uploadPortletRequest.getFileAsStream("file");
219
220 String contentType = uploadPortletRequest.getContentType("file");
221
222 LayoutServiceUtil.addTempFileEntry(
223 groupId, sourceFileName, folderName, inputStream, contentType);
224 }
225 catch (Exception e) {
226 UploadException uploadException =
227 (UploadException)actionRequest.getAttribute(
228 WebKeys.UPLOAD_EXCEPTION);
229
230 if ((uploadException != null) &&
231 (uploadException.getCause()
232 instanceof FileUploadBase.IOFileUploadException)) {
233
234
235
236 }
237 else if ((uploadException != null) &&
238 uploadException.isExceededSizeLimit()) {
239
240 throw new FileSizeException(uploadException.getCause());
241 }
242 else {
243 throw e;
244 }
245 }
246 finally {
247 StreamUtil.cleanUp(inputStream);
248 }
249 }
250
251 protected void checkExceededSizeLimit(HttpServletRequest request)
252 throws PortalException {
253
254 UploadException uploadException = (UploadException)request.getAttribute(
255 WebKeys.UPLOAD_EXCEPTION);
256
257 if (uploadException != null) {
258 if (uploadException.isExceededSizeLimit()) {
259 throw new LARFileSizeException(uploadException.getCause());
260 }
261
262 throw new PortalException(uploadException.getCause());
263 }
264 }
265
266 protected void deleteTempFileEntry(
267 ActionRequest actionRequest, ActionResponse actionResponse,
268 String folderName)
269 throws Exception {
270
271 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
272 WebKeys.THEME_DISPLAY);
273
274 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
275
276 try {
277 String fileName = ParamUtil.getString(actionRequest, "fileName");
278
279 LayoutServiceUtil.deleteTempFileEntry(
280 themeDisplay.getScopeGroupId(), fileName, folderName);
281
282 jsonObject.put("deleted", Boolean.TRUE);
283 }
284 catch (Exception e) {
285 String errorMessage = themeDisplay.translate(
286 "an-unexpected-error-occurred-while-deleting-the-file");
287
288 jsonObject.put("deleted", Boolean.FALSE);
289 jsonObject.put("errorMessage", errorMessage);
290 }
291
292 writeJSON(actionRequest, actionResponse, jsonObject);
293 }
294
295 protected void deleteTempFileEntry(long groupId, String folderName)
296 throws PortalException, SystemException {
297
298 String[] tempFileEntryNames = LayoutServiceUtil.getTempFileEntryNames(
299 groupId, folderName);
300
301 for (String tempFileEntryName : tempFileEntryNames) {
302 LayoutServiceUtil.deleteTempFileEntry(
303 groupId, tempFileEntryName, folderName);
304 }
305 }
306
307 protected void handleUploadException(
308 PortletConfig portletConfig, ActionRequest actionRequest,
309 ActionResponse actionResponse, String folderName, Exception e)
310 throws Exception {
311
312 HttpServletResponse response = PortalUtil.getHttpServletResponse(
313 actionResponse);
314
315 response.setContentType(ContentTypes.TEXT_HTML);
316 response.setStatus(HttpServletResponse.SC_OK);
317
318 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
319 WebKeys.THEME_DISPLAY);
320
321 deleteTempFileEntry(themeDisplay.getScopeGroupId(), folderName);
322
323 JSONObject jsonObject = StagingUtil.getExceptionMessagesJSONObject(
324 themeDisplay.getLocale(), e, null);
325
326 writeJSON(actionRequest, actionResponse, jsonObject);
327
328 ServletResponseUtil.write(
329 response, String.valueOf(jsonObject.getInt("status")));
330 }
331
332 protected void importData(
333 ActionRequest actionRequest, ActionResponse actionResponse,
334 String folderName)
335 throws Exception {
336
337 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
338 WebKeys.THEME_DISPLAY);
339
340 long groupId = ParamUtil.getLong(actionRequest, "groupId");
341
342 FileEntry fileEntry = ExportImportHelperUtil.getTempFileEntry(
343 groupId, themeDisplay.getUserId(), folderName);
344
345 InputStream inputStream = null;
346
347 try {
348 inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(
349 themeDisplay.getUserId(), fileEntry.getFileEntryId(),
350 fileEntry.getVersion(), false);
351
352 importData(actionRequest, fileEntry.getTitle(), inputStream);
353
354 deleteTempFileEntry(groupId, folderName);
355
356 addSuccessMessage(actionRequest, actionResponse);
357 }
358 finally {
359 StreamUtil.cleanUp(inputStream);
360 }
361 }
362
363 protected void importData(
364 ActionRequest actionRequest, String fileName,
365 InputStream inputStream)
366 throws Exception {
367
368 long groupId = ParamUtil.getLong(actionRequest, "groupId");
369 boolean privateLayout = ParamUtil.getBoolean(
370 actionRequest, "privateLayout");
371
372 LayoutServiceUtil.importLayoutsInBackground(
373 fileName, groupId, privateLayout, actionRequest.getParameterMap(),
374 inputStream);
375 }
376
377 protected void validateFile(
378 ActionRequest actionRequest, ActionResponse actionResponse,
379 String folderName)
380 throws Exception {
381
382 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
383 WebKeys.THEME_DISPLAY);
384
385 long groupId = ParamUtil.getLong(actionRequest, "groupId");
386
387 FileEntry fileEntry = ExportImportHelperUtil.getTempFileEntry(
388 groupId, themeDisplay.getUserId(), folderName);
389
390 InputStream inputStream = null;
391
392 try {
393 inputStream = DLFileEntryLocalServiceUtil.getFileAsStream(
394 themeDisplay.getUserId(), fileEntry.getFileEntryId(),
395 fileEntry.getVersion(), false);
396
397 MissingReferences missingReferences = validateFile(
398 actionRequest, inputStream);
399
400 Map<String, MissingReference> weakMissingReferences =
401 missingReferences.getWeakMissingReferences();
402
403 if (weakMissingReferences.isEmpty()) {
404 return;
405 }
406
407 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
408
409 if ((weakMissingReferences != null) &&
410 !weakMissingReferences.isEmpty()) {
411
412 jsonObject.put(
413 "warningMessages",
414 StagingUtil.getWarningMessagesJSONArray(
415 themeDisplay.getLocale(), weakMissingReferences, null));
416 }
417
418 writeJSON(actionRequest, actionResponse, jsonObject);
419 }
420 finally {
421 StreamUtil.cleanUp(inputStream);
422 }
423 }
424
425 protected MissingReferences validateFile(
426 ActionRequest actionRequest, InputStream inputStream)
427 throws Exception {
428
429 long groupId = ParamUtil.getLong(actionRequest, "groupId");
430 boolean privateLayout = ParamUtil.getBoolean(
431 actionRequest, "privateLayout");
432
433 return LayoutServiceUtil.validateImportLayoutsFile(
434 groupId, privateLayout, actionRequest.getParameterMap(),
435 inputStream);
436 }
437
438 private static Log _log = LogFactoryUtil.getLog(ImportLayoutsAction.class);
439
440 }