001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.documentlibrary.action;
016    
017    import com.liferay.portal.DuplicateLockException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.json.JSONArray;
020    import com.liferay.portal.kernel.json.JSONFactoryUtil;
021    import com.liferay.portal.kernel.json.JSONObject;
022    import com.liferay.portal.kernel.language.LanguageUtil;
023    import com.liferay.portal.kernel.portlet.LiferayWindowState;
024    import com.liferay.portal.kernel.repository.model.FileEntry;
025    import com.liferay.portal.kernel.servlet.ServletResponseConstants;
026    import com.liferay.portal.kernel.servlet.SessionErrors;
027    import com.liferay.portal.kernel.upload.UploadException;
028    import com.liferay.portal.kernel.upload.UploadPortletRequest;
029    import com.liferay.portal.kernel.util.Constants;
030    import com.liferay.portal.kernel.util.FileUtil;
031    import com.liferay.portal.kernel.util.KeyValuePair;
032    import com.liferay.portal.kernel.util.MimeTypesUtil;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.PropsKeys;
035    import com.liferay.portal.kernel.util.StreamUtil;
036    import com.liferay.portal.kernel.util.StringPool;
037    import com.liferay.portal.kernel.util.StringUtil;
038    import com.liferay.portal.kernel.util.TempFileUtil;
039    import com.liferay.portal.kernel.util.Validator;
040    import com.liferay.portal.security.auth.PrincipalException;
041    import com.liferay.portal.service.ServiceContext;
042    import com.liferay.portal.service.ServiceContextFactory;
043    import com.liferay.portal.struts.ActionConstants;
044    import com.liferay.portal.struts.PortletAction;
045    import com.liferay.portal.theme.ThemeDisplay;
046    import com.liferay.portal.util.PortalUtil;
047    import com.liferay.portal.util.PortletKeys;
048    import com.liferay.portal.util.PrefsPropsUtil;
049    import com.liferay.portal.util.WebKeys;
050    import com.liferay.portlet.PortletPreferencesFactoryUtil;
051    import com.liferay.portlet.asset.AssetCategoryException;
052    import com.liferay.portlet.asset.AssetTagException;
053    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
054    import com.liferay.portlet.documentlibrary.DuplicateFileException;
055    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
056    import com.liferay.portlet.documentlibrary.FileExtensionException;
057    import com.liferay.portlet.documentlibrary.FileMimeTypeException;
058    import com.liferay.portlet.documentlibrary.FileNameException;
059    import com.liferay.portlet.documentlibrary.FileSizeException;
060    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
061    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
062    import com.liferay.portlet.documentlibrary.SourceFileNameException;
063    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
064    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
065    import com.liferay.portlet.documentlibrary.util.DLUtil;
066    
067    import java.io.File;
068    import java.io.InputStream;
069    
070    import java.util.ArrayList;
071    import java.util.Arrays;
072    import java.util.List;
073    
074    import javax.portlet.ActionRequest;
075    import javax.portlet.ActionResponse;
076    import javax.portlet.PortletConfig;
077    import javax.portlet.PortletContext;
078    import javax.portlet.PortletPreferences;
079    import javax.portlet.PortletRequestDispatcher;
080    import javax.portlet.RenderRequest;
081    import javax.portlet.RenderResponse;
082    import javax.portlet.ResourceRequest;
083    import javax.portlet.ResourceResponse;
084    import javax.portlet.WindowState;
085    
086    import javax.servlet.http.HttpServletResponse;
087    
088    import org.apache.struts.action.ActionForm;
089    import org.apache.struts.action.ActionForward;
090    import org.apache.struts.action.ActionMapping;
091    
092    /**
093     * @author Brian Wing Shun Chan
094     * @author Alexander Chow
095     * @author Sergio González
096     */
097    public class EditFileEntryAction extends PortletAction {
098    
099            @Override
100            public void processAction(
101                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
102                            ActionRequest actionRequest, ActionResponse actionResponse)
103                    throws Exception {
104    
105                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
106    
107                    try {
108                            if (Validator.isNull(cmd)) {
109                                    UploadException uploadException =
110                                            (UploadException)actionRequest.getAttribute(
111                                                    WebKeys.UPLOAD_EXCEPTION);
112    
113                                    if (uploadException != null) {
114                                            if (uploadException.isExceededSizeLimit()) {
115                                                    throw new FileSizeException(uploadException.getCause());
116                                            }
117    
118                                            throw new PortalException(uploadException.getCause());
119                                    }
120                            }
121                            else if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)
122                                    || cmd.equals(Constants.UPDATE_AND_CHECKIN)) {
123    
124                                    updateFileEntry(portletConfig, actionRequest, actionResponse);
125                            }
126                            else if (cmd.equals(Constants.ADD_MULTIPLE)) {
127                                    addMultipleFileEntries(actionRequest, actionResponse);
128                            }
129                            else if (cmd.equals(Constants.ADD_TEMP)) {
130                                    addTempFileEntry(actionRequest);
131                            }
132                            else if (cmd.equals(Constants.DELETE)) {
133                                    deleteFileEntries(actionRequest);
134                            }
135                            else if (cmd.equals(Constants.DELETE_TEMP)) {
136                                    deleteTempFileEntry(actionRequest, actionResponse);
137                            }
138                            else if (cmd.equals(Constants.CANCEL_CHECKOUT)) {
139                                    cancelFileEntriesCheckOut(actionRequest);
140                            }
141                            else if (cmd.equals(Constants.CHECKIN)) {
142                                    checkInFileEntries(actionRequest);
143                            }
144                            else if (cmd.equals(Constants.CHECKOUT)) {
145                                    checkOutFileEntries(actionRequest);
146                            }
147                            else if (cmd.equals(Constants.MOVE)) {
148                                    moveFileEntries(actionRequest);
149                            }
150                            else if (cmd.equals(Constants.REVERT)) {
151                                    revertFileEntry(actionRequest);
152                            }
153    
154                            WindowState windowState = actionRequest.getWindowState();
155    
156                            if (cmd.equals(Constants.ADD_TEMP) ||
157                                    cmd.equals(Constants.DELETE_TEMP)) {
158    
159                                    setForward(actionRequest, ActionConstants.COMMON_NULL);
160                            }
161                            else if (cmd.equals(Constants.PREVIEW)) {
162                            }
163                            else if (!windowState.equals(LiferayWindowState.POP_UP)) {
164                                    sendRedirect(actionRequest, actionResponse);
165                            }
166                            else {
167                                    String redirect = PortalUtil.escapeRedirect(
168                                            ParamUtil.getString(actionRequest, "redirect"));
169    
170                                    if (Validator.isNotNull(redirect)) {
171                                            actionResponse.sendRedirect(redirect);
172                                    }
173                            }
174                    }
175                    catch (Exception e) {
176                            if (e instanceof DuplicateLockException ||
177                                    e instanceof NoSuchFileEntryException ||
178                                    e instanceof PrincipalException) {
179    
180                                    if (e instanceof DuplicateLockException) {
181                                            DuplicateLockException dle = (DuplicateLockException)e;
182    
183                                            SessionErrors.add(
184                                                    actionRequest, dle.getClass().getName(), dle.getLock());
185                                    }
186                                    else {
187                                            SessionErrors.add(actionRequest, e.getClass().getName());
188                                    }
189    
190                                    setForward(actionRequest, "portlet.document_library.error");
191                            }
192                            else if (e instanceof DuplicateFileException ||
193                                             e instanceof DuplicateFolderNameException ||
194                                             e instanceof FileExtensionException ||
195                                             e instanceof FileMimeTypeException ||
196                                             e instanceof FileNameException ||
197                                             e instanceof FileSizeException ||
198                                             e instanceof NoSuchFolderException ||
199                                             e instanceof SourceFileNameException) {
200    
201                                    if (!cmd.equals(Constants.ADD_MULTIPLE) &&
202                                            !cmd.equals(Constants.ADD_TEMP)) {
203    
204                                            SessionErrors.add(actionRequest, e.getClass().getName());
205    
206                                            return;
207                                    }
208    
209                                    if (e instanceof DuplicateFileException) {
210                                            HttpServletResponse response =
211                                                    PortalUtil.getHttpServletResponse(actionResponse);
212    
213                                            response.setStatus(
214                                                    ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
215                                    }
216                                    else if (e instanceof FileExtensionException) {
217                                            HttpServletResponse response =
218                                                    PortalUtil.getHttpServletResponse(actionResponse);
219    
220                                            response.setStatus(
221                                                    ServletResponseConstants.SC_FILE_EXTENSION_EXCEPTION);
222                                    }
223                                    else if (e instanceof FileNameException) {
224                                            HttpServletResponse response =
225                                                    PortalUtil.getHttpServletResponse(actionResponse);
226    
227                                            response.setStatus(
228                                                    ServletResponseConstants.SC_FILE_NAME_EXCEPTION);
229                                    }
230                                    else if (e instanceof FileSizeException) {
231                                            HttpServletResponse response =
232                                                    PortalUtil.getHttpServletResponse(actionResponse);
233    
234                                            response.setStatus(
235                                                    ServletResponseConstants.SC_FILE_SIZE_EXCEPTION);
236                                    }
237    
238                                    SessionErrors.add(actionRequest, e.getClass().getName());
239                            }
240                            else if (e instanceof AssetCategoryException ||
241                                             e instanceof AssetTagException) {
242    
243                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
244                            }
245                            else {
246                                    throw e;
247                            }
248                    }
249            }
250    
251            @Override
252            public ActionForward render(
253                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
254                            RenderRequest renderRequest, RenderResponse renderResponse)
255                    throws Exception {
256    
257                    try {
258                            ActionUtil.getFileEntry(renderRequest);
259                    }
260                    catch (Exception e) {
261                            if (e instanceof NoSuchFileEntryException ||
262                                    e instanceof PrincipalException) {
263    
264                                    SessionErrors.add(renderRequest, e.getClass().getName());
265    
266                                    return mapping.findForward("portlet.document_library.error");
267                            }
268                            else {
269                                    throw e;
270                            }
271                    }
272    
273                    String forward = "portlet.document_library.edit_file_entry";
274    
275                    return mapping.findForward(getForward(renderRequest, forward));
276            }
277    
278            @Override
279            public void serveResource(
280                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
281                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
282                    throws Exception {
283    
284                    PortletContext portletContext = portletConfig.getPortletContext();
285    
286                    PortletRequestDispatcher portletRequestDispatcher =
287                            portletContext.getRequestDispatcher(
288                                    "/html/portlet/document_library/" +
289                                            "upload_multiple_file_entries_resources.jsp");
290    
291                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
292            }
293    
294            protected void addMultipleFileEntries(
295                            ActionRequest actionRequest, ActionResponse actionResponse)
296                    throws Exception {
297    
298                    List<String> validFileNames = new ArrayList<String>();
299                    List<KeyValuePair> invalidFileNameKVPs =
300                            new ArrayList<KeyValuePair>();
301    
302                    String[] selectedFileNames = ParamUtil.getParameterValues(
303                            actionRequest, "selectedFileName");
304    
305                    for (String selectedFileName : selectedFileNames) {
306                            addMultipleFileEntries(
307                                    actionRequest, actionResponse, selectedFileName,
308                                    validFileNames, invalidFileNameKVPs);
309                    }
310    
311                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
312    
313                    for (String validFileName : validFileNames) {
314                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
315    
316                            jsonObject.put("added", Boolean.TRUE);
317                            jsonObject.put("fileName", validFileName);
318    
319                            jsonArray.put(jsonObject);
320                    }
321    
322                    for (KeyValuePair invalidFileNameKVP : invalidFileNameKVPs) {
323                            String fileName = invalidFileNameKVP.getKey();
324                            String errorMessage = invalidFileNameKVP.getValue();
325    
326                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
327    
328                            jsonObject.put("added", Boolean.FALSE);
329                            jsonObject.put("errorMessage", errorMessage);
330                            jsonObject.put("fileName", fileName);
331    
332                            jsonArray.put(jsonObject);
333                    }
334    
335                    writeJSON(actionRequest, actionResponse, jsonArray);
336            }
337    
338            protected void addMultipleFileEntries(
339                            ActionRequest actionRequest, ActionResponse actionResponse,
340                            String selectedFileName, List<String> validFileNames,
341                            List<KeyValuePair> invalidFileNameKVPs)
342                    throws Exception {
343    
344                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
345                            WebKeys.THEME_DISPLAY);
346    
347                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
348                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
349                    String contentType = MimeTypesUtil.getContentType(selectedFileName);
350                    String description = ParamUtil.getString(actionRequest, "description");
351                    String changeLog = ParamUtil.getString(actionRequest, "changeLog");
352    
353                    File file = null;
354    
355                    try {
356                            file = TempFileUtil.getTempFile(
357                                    themeDisplay.getUserId(), selectedFileName, _TEMP_FOLDER_NAME);
358    
359                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
360                                    DLFileEntry.class.getName(), actionRequest);
361    
362                            FileEntry fileEntry = DLAppServiceUtil.addFileEntry(
363                                    repositoryId, folderId, selectedFileName, contentType,
364                                    selectedFileName, description, changeLog, file, serviceContext);
365    
366                            AssetPublisherUtil.addAndStoreSelection(
367                                    actionRequest, DLFileEntry.class.getName(),
368                                    fileEntry.getFileEntryId(), -1);
369    
370                            AssetPublisherUtil.addRecentFolderId(
371                                    actionRequest, DLFileEntry.class.getName(), folderId);
372    
373                            validFileNames.add(selectedFileName);
374    
375                            return;
376                    }
377                    catch (Exception e) {
378                            String errorMessage = getAddMultipleFileEntriesErrorMessage(
379                                    themeDisplay, e);
380    
381                            invalidFileNameKVPs.add(
382                                    new KeyValuePair(selectedFileName, errorMessage));
383                    }
384                    finally {
385                            FileUtil.delete(file);
386                    }
387            }
388    
389            protected String getAddMultipleFileEntriesErrorMessage(
390                            ThemeDisplay themeDisplay, Exception e)
391                    throws Exception {
392    
393                    String errorMessage = null;
394    
395                    if (e instanceof DuplicateFileException) {
396                            errorMessage = LanguageUtil.get(
397                                    themeDisplay.getLocale(),
398                                    "the-folder-you-selected-already-has-an-entry-with-this-name." +
399                                            "-please-select-a-different-folder");
400                    }
401                    else if (e instanceof FileExtensionException) {
402                            errorMessage = LanguageUtil.format(
403                                    themeDisplay.getLocale(),
404                                    "please-enter-a-file-with-a-valid-extension-x",
405                                    StringUtil.merge(
406                                            PrefsPropsUtil.getStringArray(
407                                                    PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA)));
408                    }
409                    else if (e instanceof FileNameException) {
410                            errorMessage = LanguageUtil.get(
411                                    themeDisplay.getLocale(),
412                                    "please-enter-a-file-with-a-valid-file-name");
413                    }
414                    else if (e instanceof FileSizeException) {
415                            long maxSizeMB = PrefsPropsUtil.getLong(
416                                    PropsKeys.DL_FILE_MAX_SIZE) / 1024 / 1024;
417    
418                            errorMessage = LanguageUtil.format(
419                                    themeDisplay.getLocale(),
420                                    "file-size-is-larger-than-x-megabytes", maxSizeMB);
421                    }
422                    else {
423                            errorMessage = LanguageUtil.get(
424                                    themeDisplay.getLocale(),
425                                    "an-unexpected-error-occurred-while-saving-your-document");
426                    }
427    
428                    return errorMessage;
429            }
430    
431            protected void addTempFileEntry(ActionRequest actionRequest)
432                    throws Exception {
433    
434                    UploadPortletRequest uploadPortletRequest =
435                            PortalUtil.getUploadPortletRequest(actionRequest);
436    
437                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
438                            WebKeys.THEME_DISPLAY);
439    
440                    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
441                    String sourceFileName = uploadPortletRequest.getFileName("file");
442    
443                    InputStream inputStream = null;
444    
445                    try {
446                            inputStream = uploadPortletRequest.getFileAsStream("file");
447    
448                            DLAppServiceUtil.addTempFileEntry(
449                                    themeDisplay.getScopeGroupId(), folderId, sourceFileName,
450                                    _TEMP_FOLDER_NAME, inputStream);
451                    }
452                    finally {
453                            StreamUtil.cleanUp(inputStream);
454                    }
455            }
456    
457            protected void cancelFileEntriesCheckOut(ActionRequest actionRequest)
458                    throws Exception {
459    
460                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
461    
462                    if (fileEntryId > 0) {
463                            DLAppServiceUtil.cancelCheckOut(fileEntryId);
464                    }
465                    else {
466                            long[] fileEntryIds = StringUtil.split(
467                                    ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
468    
469                            for (int i = 0; i < fileEntryIds.length; i++) {
470                                    DLAppServiceUtil.cancelCheckOut(fileEntryIds[i]);
471                            }
472                    }
473            }
474    
475            protected void checkInFileEntries(ActionRequest actionRequest)
476                    throws Exception {
477    
478                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
479    
480                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
481                            actionRequest);
482    
483                    if (fileEntryId > 0) {
484                            DLAppServiceUtil.checkInFileEntry(
485                                    fileEntryId, false, StringPool.BLANK, serviceContext);
486                    }
487                    else {
488                            long[] fileEntryIds = StringUtil.split(
489                                    ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
490    
491                            for (int i = 0; i < fileEntryIds.length; i++) {
492                                    DLAppServiceUtil.checkInFileEntry(
493                                            fileEntryIds[i], false, StringPool.BLANK, serviceContext);
494                            }
495                    }
496            }
497    
498            protected void checkOutFileEntries(ActionRequest actionRequest)
499                    throws Exception {
500    
501                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
502    
503                    if (fileEntryId > 0) {
504                            DLAppServiceUtil.checkOutFileEntry(fileEntryId);
505                    }
506                    else {
507                            long[] fileEntryIds = StringUtil.split(
508                                    ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
509    
510                            for (int i = 0; i < fileEntryIds.length; i++) {
511                                    DLAppServiceUtil.checkOutFileEntry(fileEntryIds[i]);
512                            }
513                    }
514            }
515    
516            protected void deleteFileEntries(ActionRequest actionRequest)
517                    throws Exception {
518    
519                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
520    
521                    if (fileEntryId > 0) {
522                            DLAppServiceUtil.deleteFileEntry(fileEntryId);
523                    }
524                    else {
525                            long[] deleteFileEntryIds = StringUtil.split(
526                                    ParamUtil.getString(actionRequest, "deleteEntryIds"), 0L);
527    
528                            for (int i = 0; i < deleteFileEntryIds.length; i++) {
529                                    DLAppServiceUtil.deleteFileEntry(deleteFileEntryIds[i]);
530                            }
531                    }
532            }
533    
534            protected void deleteTempFileEntry(
535                            ActionRequest actionRequest, ActionResponse actionResponse)
536                    throws Exception {
537    
538                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
539                            WebKeys.THEME_DISPLAY);
540    
541                    long folderId = ParamUtil.getLong(actionRequest, "folderId");
542                    String fileName = ParamUtil.getString(actionRequest, "fileName");
543    
544                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
545    
546                    try {
547                            DLAppServiceUtil.deleteTempFileEntry(
548                                    themeDisplay.getScopeGroupId(), folderId, fileName,
549                                    _TEMP_FOLDER_NAME);
550    
551                            jsonObject.put("deleted", Boolean.TRUE);
552                    }
553                    catch (Exception e) {
554                            String errorMessage = LanguageUtil.get(
555                                    themeDisplay.getLocale(),
556                                    "an-unexpected-error-occurred-while-deleting-the-file");
557    
558                            jsonObject.put("deleted", Boolean.FALSE);
559                            jsonObject.put("errorMessage", errorMessage);
560                    }
561    
562                    writeJSON(actionRequest, actionResponse, jsonObject);
563            }
564    
565            protected void moveFileEntries(ActionRequest actionRequest)
566                    throws Exception {
567    
568                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
569                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
570    
571                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
572                            DLFileEntry.class.getName(), actionRequest);
573    
574                    if (fileEntryId > 0) {
575                            DLAppServiceUtil.moveFileEntry(
576                                    fileEntryId, newFolderId, serviceContext);
577                    }
578                    else {
579                            long[] fileEntryIds = StringUtil.split(
580                                    ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
581    
582                            for (int i = 0; i < fileEntryIds.length; i++) {
583                                    DLAppServiceUtil.moveFileEntry(
584                                            fileEntryIds[i], newFolderId, serviceContext);
585                            }
586                    }
587            }
588    
589            protected void revertFileEntry(ActionRequest actionRequest)
590                    throws Exception {
591    
592                    long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId");
593                    String version = ParamUtil.getString(actionRequest, "version");
594    
595                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
596                            DLFileEntry.class.getName(), actionRequest);
597    
598                    DLAppServiceUtil.revertFileEntry(fileEntryId, version, serviceContext);
599            }
600    
601            protected void updateFileEntry(
602                            PortletConfig portletConfig, ActionRequest actionRequest,
603                            ActionResponse actionResponse)
604                    throws Exception {
605    
606                    UploadPortletRequest uploadPortletRequest =
607                            PortalUtil.getUploadPortletRequest(actionRequest);
608    
609                    String cmd = ParamUtil.getString(uploadPortletRequest, Constants.CMD);
610    
611                    long fileEntryId = ParamUtil.getLong(
612                            uploadPortletRequest, "fileEntryId");
613    
614                    long repositoryId = ParamUtil.getLong(
615                            uploadPortletRequest, "repositoryId");
616                    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
617                    String sourceFileName = uploadPortletRequest.getFileName("file");
618                    String title = ParamUtil.getString(uploadPortletRequest, "title");
619                    String description = ParamUtil.getString(
620                            uploadPortletRequest, "description");
621                    String changeLog = ParamUtil.getString(
622                            uploadPortletRequest, "changeLog");
623                    boolean majorVersion = ParamUtil.getBoolean(
624                            uploadPortletRequest, "majorVersion");
625    
626                    InputStream inputStream = null;
627    
628                    try {
629                            String contentType = uploadPortletRequest.getContentType("file");
630    
631                            long size = uploadPortletRequest.getSize("file");
632    
633                            if (cmd.equals(Constants.ADD) && (size == 0)) {
634                                    contentType = MimeTypesUtil.getContentType(title);
635                            }
636    
637                            if (cmd.equals(Constants.ADD) || (size > 0)) {
638                                    String portletName = portletConfig.getPortletName();
639    
640                                    if (portletName.equals(PortletKeys.MEDIA_GALLERY_DISPLAY)) {
641                                            String portletResource = ParamUtil.getString(
642                                                    actionRequest, "portletResource");
643    
644                                            PortletPreferences portletPreferences = null;
645    
646                                            if (Validator.isNotNull(portletResource)) {
647                                                    PortletPreferencesFactoryUtil.getPortletSetup(
648                                                            actionRequest, portletResource);
649                                            }
650                                            else {
651                                                    portletPreferences = actionRequest.getPreferences();
652                                            }
653    
654                                            String[] mimeTypes = DLUtil.getMediaGalleryMimeTypes(
655                                                    portletPreferences, actionRequest);
656    
657                                            if (Arrays.binarySearch(mimeTypes, contentType) < 0) {
658                                                    throw new FileMimeTypeException(contentType);
659                                            }
660                                    }
661                            }
662    
663                            inputStream = uploadPortletRequest.getFileAsStream("file");
664    
665                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
666                                    DLFileEntry.class.getName(), actionRequest);
667    
668                            FileEntry fileEntry = null;
669    
670                            if (cmd.equals(Constants.ADD)) {
671                                    if (Validator.isNull(title)) {
672                                            title = sourceFileName;
673                                    }
674    
675                                    // Add file entry
676    
677                                    fileEntry = DLAppServiceUtil.addFileEntry(
678                                            repositoryId, folderId, sourceFileName, contentType, title,
679                                            description, changeLog, inputStream, size, serviceContext);
680    
681                                    AssetPublisherUtil.addAndStoreSelection(
682                                            actionRequest, DLFileEntry.class.getName(),
683                                            fileEntry.getFileEntryId(), -1);
684                            }
685                            else if (cmd.equals(Constants.UPDATE_AND_CHECKIN)) {
686    
687                                    // Update file entry and checkin
688    
689                                    fileEntry = DLAppServiceUtil.updateFileEntryAndCheckIn(
690                                            fileEntryId, sourceFileName, contentType, title,
691                                            description, changeLog, majorVersion, inputStream,
692                                            size, serviceContext);
693                            }
694                            else {
695    
696                                    // Update file entry
697    
698                                    fileEntry = DLAppServiceUtil.updateFileEntry(
699                                            fileEntryId, sourceFileName, contentType, title,
700                                            description, changeLog, majorVersion, inputStream,
701                                            size, serviceContext);
702                            }
703    
704                            AssetPublisherUtil.addRecentFolderId(
705                                    actionRequest, DLFileEntry.class.getName(), folderId);
706                    }
707                    finally {
708                            StreamUtil.cleanUp(inputStream);
709                    }
710            }
711    
712            private static final String _TEMP_FOLDER_NAME =
713                    EditFileEntryAction.class.getName();
714    
715    }