001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.upload;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.gwt.CmsGwtService;
034import org.opencms.gwt.shared.CmsUploadFileBean;
035import org.opencms.gwt.shared.CmsUploadProgessInfo;
036import org.opencms.gwt.shared.CmsUploadProgessInfo.UPLOAD_STATE;
037import org.opencms.gwt.shared.rpc.I_CmsUploadService;
038import org.opencms.main.CmsException;
039import org.opencms.util.CmsUUID;
040
041import java.util.ArrayList;
042import java.util.List;
043
044import org.apache.commons.fileupload.InvalidFileNameException;
045import org.apache.commons.fileupload.util.Streams;
046
047/**
048 * Handles all RPC services related to the upload dialog.<p>
049 *
050 * @since 8.0.0
051 *
052 * @see org.opencms.ade.upload.CmsUploadService
053 * @see org.opencms.gwt.shared.rpc.I_CmsUploadService
054 * @see org.opencms.gwt.shared.rpc.I_CmsUploadServiceAsync
055 */
056public class CmsUploadService extends CmsGwtService implements I_CmsUploadService {
057
058    /** The serial version UID. */
059    private static final long serialVersionUID = -2235662141861687012L;
060
061    /**
062     * @see org.opencms.gwt.shared.rpc.I_CmsUploadService#cancelUpload()
063     */
064    public Boolean cancelUpload() {
065
066        if (getRequest().getSession().getAttribute(CmsUploadBean.SESSION_ATTRIBUTE_LISTENER_ID) != null) {
067            CmsUUID listenerId = (CmsUUID)getRequest().getSession().getAttribute(
068                CmsUploadBean.SESSION_ATTRIBUTE_LISTENER_ID);
069            CmsUploadListener listener = CmsUploadBean.getCurrentListener(listenerId);
070            if ((listener != null) && !listener.isCanceled()) {
071                listener.cancelUpload(
072                    new CmsUploadException(Messages.get().getBundle().key(Messages.ERR_UPLOAD_USER_CANCELED_0)));
073                return Boolean.TRUE;
074            }
075        }
076        return Boolean.FALSE;
077    }
078
079    /**
080     * @see org.opencms.gwt.shared.rpc.I_CmsUploadService#checkUploadFiles(java.util.List, java.lang.String, boolean)
081     */
082    public CmsUploadFileBean checkUploadFiles(List<String> fileNames, String targetFolder, boolean isRootPath) {
083
084        List<String> existingResourceNames = new ArrayList<String>();
085        List<String> invalidFileNames = new ArrayList<String>();
086        List<String> existingDeleted = new ArrayList<String>();
087        boolean isActive = false;
088
089        // check if there is an active upload
090        if (getRequest().getSession().getAttribute(CmsUploadBean.SESSION_ATTRIBUTE_LISTENER_ID) == null) {
091
092            // check for existing files
093            for (String fileName : fileNames) {
094
095                try {
096                    Streams.checkFileName(fileName);
097                    String newResName = CmsUploadBean.getNewResourceName(getCmsObject(), fileName, targetFolder, false);
098                    if (existsResource(newResName, isRootPath)) {
099                        if (isDeletedResource(newResName, isRootPath)) {
100                            existingDeleted.add(fileName);
101                        } else {
102                            existingResourceNames.add(fileName);
103                        }
104                    }
105                } catch (InvalidFileNameException e) {
106                    invalidFileNames.add(fileName);
107                }
108            }
109        } else {
110            isActive = true;
111        }
112        return new CmsUploadFileBean(existingResourceNames, invalidFileNames, existingDeleted, isActive);
113    }
114
115    /**
116     * @see org.opencms.gwt.shared.rpc.I_CmsUploadService#getUploadProgressInfo()
117     */
118    public CmsUploadProgessInfo getUploadProgressInfo() {
119
120        CmsUploadProgessInfo info = new CmsUploadProgessInfo(0, 0, UPLOAD_STATE.notStarted, 0, 0);
121        if (getRequest().getSession().getAttribute(CmsUploadBean.SESSION_ATTRIBUTE_LISTENER_ID) != null) {
122            CmsUUID listenerId = (CmsUUID)getRequest().getSession().getAttribute(
123                CmsUploadBean.SESSION_ATTRIBUTE_LISTENER_ID);
124            CmsUploadListener listener = CmsUploadBean.getCurrentListener(listenerId);
125            if (listener != null) {
126                info = listener.getInfo();
127            }
128        }
129        return info;
130    }
131
132    /**
133     * Checks if a resource already exists for the given path.<p>
134     *
135     * @param path the path
136     * @param rootPath in case the path is a root path
137     *
138     * @return true if a resource already exists
139     */
140    private boolean existsResource(String path, boolean rootPath) {
141
142        CmsObject cms = getCmsObject();
143        if (rootPath) {
144            String origSiteRoot = cms.getRequestContext().getSiteRoot();
145            try {
146                cms.getRequestContext().setSiteRoot("");
147                if (cms.existsResource(path, CmsResourceFilter.ALL)) {
148                    return true;
149                }
150            } finally {
151                cms.getRequestContext().setSiteRoot(origSiteRoot);
152            }
153        } else {
154            if (cms.existsResource(path, CmsResourceFilter.ALL)) {
155                return true;
156            }
157        }
158        return false;
159    }
160
161    /**
162     * Checks if a the resource exists but is marked as deleted.<p>
163     *
164     * @param path the path
165     * @param rootPath in case the path is a root path
166     *
167     * @return true is the resource exists but is marked as deleted
168     */
169    private boolean isDeletedResource(String path, boolean rootPath) {
170
171        CmsObject cms = getCmsObject();
172        CmsResource res = null;
173        try {
174            if (rootPath) {
175                String origSiteRoot = cms.getRequestContext().getSiteRoot();
176                try {
177                    cms.getRequestContext().setSiteRoot("");
178                    res = cms.readResource(path, CmsResourceFilter.ALL);
179
180                } finally {
181                    cms.getRequestContext().setSiteRoot(origSiteRoot);
182                }
183            } else {
184                res = cms.readResource(path, CmsResourceFilter.ALL);
185            }
186        } catch (CmsException e) {
187            // ignore
188        }
189        return (res != null) && res.getState().isDeleted();
190    }
191}