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 GmbH & Co. KG, 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.workplace.commons; 029 030import org.opencms.db.CmsDbSqlException; 031import org.opencms.file.CmsFile; 032import org.opencms.file.CmsResource; 033import org.opencms.file.CmsResourceFilter; 034import org.opencms.jsp.CmsJspActionElement; 035import org.opencms.main.CmsException; 036import org.opencms.main.OpenCms; 037import org.opencms.security.CmsPermissionSet; 038import org.opencms.workplace.CmsDialog; 039import org.opencms.workplace.CmsWorkplaceSettings; 040 041import java.util.Iterator; 042 043import javax.servlet.http.HttpServletRequest; 044import javax.servlet.http.HttpServletResponse; 045import javax.servlet.jsp.JspException; 046import javax.servlet.jsp.PageContext; 047 048import org.apache.commons.fileupload.FileItem; 049 050/** 051 * The replace resource dialog handles the replacement of a single VFS file.<p> 052 * 053 * The following files use this class: 054 * <ul> 055 * <li>/commons/replace.jsp 056 * </ul> 057 * <p> 058 * 059 * @since 6.0.0 060 */ 061public class CmsReplace extends CmsDialog { 062 063 /** The dialog type.<p> */ 064 public static final String DIALOG_TYPE = "replace"; 065 066 /** Request parameter name for the upload file name.<p> */ 067 public static final String PARAM_UPLOADFILE = "uploadfile"; 068 069 /** 070 * Public constructor with JSP action element.<p> 071 * 072 * @param jsp an initialized JSP action element 073 */ 074 public CmsReplace(CmsJspActionElement jsp) { 075 076 super(jsp); 077 } 078 079 /** 080 * Public constructor with JSP variables.<p> 081 * 082 * @param context the JSP page context 083 * @param req the JSP request 084 * @param res the JSP response 085 */ 086 public CmsReplace(PageContext context, HttpServletRequest req, HttpServletResponse res) { 087 088 this(new CmsJspActionElement(context, req, res)); 089 } 090 091 /** 092 * Uploads the specified file and replaces the VFS file.<p> 093 * 094 * @throws JspException if inclusion of error dialog fails 095 */ 096 public void actionReplace() throws JspException { 097 098 try { 099 // get the file item from the multipart request 100 Iterator<FileItem> i = getMultiPartFileItems().iterator(); 101 FileItem fi = null; 102 while (i.hasNext()) { 103 fi = i.next(); 104 if (fi.getName() != null) { 105 // found the file object, leave iteration 106 break; 107 } 108 } 109 110 if (fi != null) { 111 // get file object information 112 long size = fi.getSize(); 113 long maxFileSizeBytes = OpenCms.getWorkplaceManager().getFileBytesMaxUploadSize(getCms()); 114 // check file size 115 if ((maxFileSizeBytes > 0) && (size > maxFileSizeBytes)) { 116 // file size is larger than maximum allowed file size, throw an error 117 throw new CmsException( 118 Messages.get().container( 119 Messages.ERR_FILE_SIZE_TOO_LARGE_1, 120 Long.valueOf((maxFileSizeBytes / 1024)))); 121 } 122 byte[] content = fi.get(); 123 fi.delete(); 124 125 // determine the resource type id from the resource to replace 126 CmsResource res = getCms().readResource(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION); 127 CmsFile file = getCms().readFile(res); 128 byte[] contents = file.getContents(); 129 int resTypeId = res.getTypeId(); 130 // check the lock state and replace resource 131 checkLock(getParamResource()); 132 try { 133 getCms().replaceResource(getParamResource(), resTypeId, content, null); 134 } catch (CmsDbSqlException sqlExc) { 135 // SQL error, probably the file is too large for the database settings, restore old content 136 file.setContents(contents); 137 getCms().writeFile(file); 138 throw sqlExc; 139 } 140 // close dialog 141 actionCloseDialog(); 142 } else { 143 throw new CmsException(Messages.get().container(Messages.ERR_UPLOAD_FILE_NOT_FOUND_0)); 144 } 145 } catch (Throwable e) { 146 // error replacing file, show error dialog 147 includeErrorpage(this, e); 148 } 149 } 150 151 /** 152 * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest) 153 */ 154 @Override 155 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 156 157 // fill the parameter values in the get/set methods 158 fillParamValues(request); 159 160 // check the required permissions to replace the resource 161 if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) { 162 // no write permissions for the resource, set cancel action to close dialog 163 setParamAction(DIALOG_CANCEL); 164 } 165 166 // set the dialog type 167 setParamDialogtype(DIALOG_TYPE); 168 // set the action for the JSP switch 169 if (DIALOG_OK.equals(getParamAction())) { 170 // ok button pressed, replace file 171 setAction(ACTION_OK); 172 } else if (DIALOG_CANCEL.equals(getParamAction())) { 173 // cancel button pressed 174 setAction(ACTION_CANCEL); 175 } else { 176 // first call of dialog 177 setAction(ACTION_DEFAULT); 178 // build title for replace resource dialog 179 setParamTitle(key(Messages.GUI_REPLACE_FILE_1, new Object[] {CmsResource.getName(getParamResource())})); 180 } 181 } 182 183}