001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsReplace.java,v $
003: * Date : $Date: 2008-02-27 12:05:23 $
004: * Version: $Revision: 1.19 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.commons;
033:
034: import org.opencms.db.CmsDbSqlException;
035: import org.opencms.file.CmsFile;
036: import org.opencms.file.CmsResource;
037: import org.opencms.file.CmsResourceFilter;
038: import org.opencms.jsp.CmsJspActionElement;
039: import org.opencms.main.CmsException;
040: import org.opencms.main.OpenCms;
041: import org.opencms.security.CmsPermissionSet;
042: import org.opencms.workplace.CmsDialog;
043: import org.opencms.workplace.CmsWorkplaceSettings;
044:
045: import java.util.Iterator;
046:
047: import javax.servlet.http.HttpServletRequest;
048: import javax.servlet.http.HttpServletResponse;
049: import javax.servlet.jsp.JspException;
050: import javax.servlet.jsp.PageContext;
051:
052: import org.apache.commons.fileupload.FileItem;
053:
054: /**
055: * The replace resource dialog handles the replacement of a single VFS file.<p>
056: *
057: * The following files use this class:
058: * <ul>
059: * <li>/commons/replace.jsp
060: * </ul>
061: * <p>
062: *
063: * @author Andreas Zahner
064: *
065: * @version $Revision: 1.19 $
066: *
067: * @since 6.0.0
068: */
069: public class CmsReplace extends CmsDialog {
070:
071: /** The dialog type.<p> */
072: public static final String DIALOG_TYPE = "replace";
073:
074: /** Request parameter name for the upload file name.<p> */
075: public static final String PARAM_UPLOADFILE = "uploadfile";
076:
077: /**
078: * Public constructor with JSP action element.<p>
079: *
080: * @param jsp an initialized JSP action element
081: */
082: public CmsReplace(CmsJspActionElement jsp) {
083:
084: super (jsp);
085: }
086:
087: /**
088: * Public constructor with JSP variables.<p>
089: *
090: * @param context the JSP page context
091: * @param req the JSP request
092: * @param res the JSP response
093: */
094: public CmsReplace(PageContext context, HttpServletRequest req,
095: HttpServletResponse res) {
096:
097: this (new CmsJspActionElement(context, req, res));
098: }
099:
100: /**
101: * Uploads the specified file and replaces the VFS file.<p>
102: *
103: * @throws JspException if inclusion of error dialog fails
104: */
105: public void actionReplace() throws JspException {
106:
107: try {
108: // get the file item from the multipart request
109: Iterator i = getMultiPartFileItems().iterator();
110: FileItem fi = null;
111: while (i.hasNext()) {
112: fi = (FileItem) i.next();
113: if (fi.getName() != null) {
114: // found the file object, leave iteration
115: break;
116: }
117: }
118:
119: if (fi != null) {
120: // get file object information
121: long size = fi.getSize();
122: long maxFileSizeBytes = OpenCms.getWorkplaceManager()
123: .getFileBytesMaxUploadSize(getCms());
124: // check file size
125: if ((maxFileSizeBytes > 0) && (size > maxFileSizeBytes)) {
126: // file size is larger than maximum allowed file size, throw an error
127: throw new CmsException(Messages.get().container(
128: Messages.ERR_FILE_SIZE_TOO_LARGE_1,
129: new Long((maxFileSizeBytes / 1024))));
130: }
131: byte[] content = fi.get();
132: fi.delete();
133:
134: // determine the resource type id from the resource to replace
135: CmsResource res = getCms().readResource(
136: getParamResource(),
137: CmsResourceFilter.IGNORE_EXPIRATION);
138: CmsFile file = getCms().readFile(res);
139: byte[] contents = file.getContents();
140: int resTypeId = res.getTypeId();
141: // check the lock state and replace resource
142: checkLock(getParamResource());
143: try {
144: getCms().replaceResource(getParamResource(),
145: resTypeId, content, null);
146: } catch (CmsDbSqlException sqlExc) {
147: // SQL error, probably the file is too large for the database settings, restore old content
148: file.setContents(contents);
149: getCms().writeFile(file);
150: throw sqlExc;
151: }
152: // close dialog
153: actionCloseDialog();
154: } else {
155: throw new CmsException(Messages.get().container(
156: Messages.ERR_UPLOAD_FILE_NOT_FOUND_0));
157: }
158: } catch (Throwable e) {
159: // error replacing file, show error dialog
160: includeErrorpage(this , e);
161: }
162: }
163:
164: /**
165: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
166: */
167: protected void initWorkplaceRequestValues(
168: CmsWorkplaceSettings settings, HttpServletRequest request) {
169:
170: // fill the parameter values in the get/set methods
171: fillParamValues(request);
172:
173: // check the required permissions to replace the resource
174: if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE,
175: false)) {
176: // no write permissions for the resource, set cancel action to close dialog
177: setParamAction(DIALOG_CANCEL);
178: }
179:
180: // set the dialog type
181: setParamDialogtype(DIALOG_TYPE);
182: // set the action for the JSP switch
183: if (DIALOG_OK.equals(getParamAction())) {
184: // ok button pressed, replace file
185: setAction(ACTION_OK);
186: } else if (DIALOG_CANCEL.equals(getParamAction())) {
187: // cancel button pressed
188: setAction(ACTION_CANCEL);
189: } else {
190: // first call of dialog
191: setAction(ACTION_DEFAULT);
192: // build title for replace resource dialog
193: setParamTitle(key(Messages.GUI_REPLACE_FILE_1,
194: new Object[] { CmsResource
195: .getName(getParamResource()) }));
196: }
197: }
198:
199: }
|