KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > commons > CmsReplace


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsReplace.java,v $
3  * Date : $Date: 2005/07/13 14:30:36 $
4  * Version: $Revision: 1.15 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.commons;
33
34 import org.opencms.db.CmsDbSqlException;
35 import org.opencms.file.CmsFile;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsResourceFilter;
38 import org.opencms.jsp.CmsJspActionElement;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.OpenCms;
41 import org.opencms.security.CmsPermissionSet;
42 import org.opencms.workplace.CmsDialog;
43 import org.opencms.workplace.CmsWorkplaceSettings;
44
45 import java.util.Iterator JavaDoc;
46
47 import javax.servlet.http.HttpServletRequest JavaDoc;
48 import javax.servlet.http.HttpServletResponse JavaDoc;
49 import javax.servlet.jsp.JspException JavaDoc;
50 import javax.servlet.jsp.PageContext JavaDoc;
51
52 import org.apache.commons.fileupload.FileItem;
53
54 /**
55  * The replace resource dialog handles the replacement of a single VFS file.<p>
56  *
57  * The following files use this class:
58  * <ul>
59  * <li>/commons/replace.jsp
60  * </ul>
61  * <p>
62  *
63  * @author Andreas Zahner
64  *
65  * @version $Revision: 1.15 $
66  *
67  * @since 6.0.0
68  */

69 public class CmsReplace extends CmsDialog {
70
71     /** The dialog type.<p> */
72     public static final String JavaDoc DIALOG_TYPE = "replace";
73
74     /** Request parameter name for the upload file name.<p> */
75     public static final String JavaDoc PARAM_UPLOADFILE = "uploadfile";
76
77     /**
78      * Public constructor with JSP action element.<p>
79      *
80      * @param jsp an initialized JSP action element
81      */

82     public CmsReplace(CmsJspActionElement jsp) {
83
84         super(jsp);
85     }
86
87     /**
88      * Public constructor with JSP variables.<p>
89      *
90      * @param context the JSP page context
91      * @param req the JSP request
92      * @param res the JSP response
93      */

94     public CmsReplace(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
95
96         this(new CmsJspActionElement(context, req, res));
97     }
98
99     /**
100      * Uploads the specified file and replaces the VFS file.<p>
101      *
102      * @throws JspException if inclusion of error dialog fails
103      */

104     public void actionReplace() throws JspException JavaDoc {
105
106         try {
107             // get the file item from the multipart request
108
Iterator JavaDoc i = getMultiPartFileItems().iterator();
109             FileItem fi = null;
110             while (i.hasNext()) {
111                 fi = (FileItem)i.next();
112                 if (fi.getName() != null) {
113                     // found the file object, leave iteration
114
break;
115                 }
116             }
117
118             if (fi != null) {
119                 // get file object information
120
long size = fi.getSize();
121                 long maxFileSizeBytes = OpenCms.getWorkplaceManager().getFileBytesMaxUploadSize(getCms());
122                 // check file size
123
if (maxFileSizeBytes > 0 && size > maxFileSizeBytes) {
124                     // file size is larger than maximum allowed file size, throw an error
125
throw new CmsException(Messages.get().container(
126                         Messages.ERR_FILE_SIZE_TOO_LARGE_1,
127                         new Long JavaDoc((maxFileSizeBytes / 1024))));
128                 }
129                 byte[] content = fi.get();
130                 fi.delete();
131
132                 // determine the resource type id from the resource to replace
133
CmsResource res = getCms().readResource(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION);
134                 CmsFile file = CmsFile.upgrade(res, getCms());
135                 byte[] contents = file.getContents();
136                 int resTypeId = res.getTypeId();
137                 // check the lock state and replace resource
138
checkLock(getParamResource());
139                 try {
140                     getCms().replaceResource(getParamResource(), resTypeId, content, null);
141                 } catch (CmsDbSqlException sqlExc) {
142                     // SQL error, probably the file is too large for the database settings, restore old content
143
file.setContents(contents);
144                     getCms().writeFile(file);
145                     throw sqlExc;
146                 }
147                 // close dialog
148
actionCloseDialog();
149             } else {
150                 throw new CmsException(Messages.get().container(Messages.ERR_UPLOAD_FILE_NOT_FOUND_0));
151             }
152         } catch (Throwable JavaDoc e) {
153             // error replacing file, show error dialog
154
includeErrorpage(this, e);
155         }
156     }
157
158     /**
159      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
160      */

161     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
162
163         // fill the parameter values in the get/set methods
164
fillParamValues(request);
165         
166         // check the required permissions to replace the resource
167
if (! checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
168             // no write permissions for the resource, set cancel action to close dialog
169
setParamAction(DIALOG_CANCEL);
170         }
171         
172         // set the dialog type
173
setParamDialogtype(DIALOG_TYPE);
174         // set the action for the JSP switch
175
if (DIALOG_OK.equals(getParamAction())) {
176             // ok button pressed, replace file
177
setAction(ACTION_OK);
178         } else if (DIALOG_CANCEL.equals(getParamAction())) {
179             // cancel button pressed
180
setAction(ACTION_CANCEL);
181         } else {
182             // first call of dialog
183
setAction(ACTION_DEFAULT);
184             // build title for replace resource dialog
185
setParamTitle(key(Messages.GUI_REPLACE_FILE_1,
186                 new Object JavaDoc[] {CmsResource.getName(getParamResource())}));
187         }
188     }
189
190 }
191
Popular Tags