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.ui.apps.modules; 029 030import org.opencms.main.CmsLog; 031import org.opencms.main.OpenCms; 032import org.opencms.ui.components.CmsAutoItemCreatingComboBox; 033import org.opencms.util.CmsStringUtil; 034 035import java.io.File; 036import java.io.FileNotFoundException; 037import java.io.FileOutputStream; 038import java.io.OutputStream; 039 040import org.apache.commons.logging.Log; 041 042import com.vaadin.event.dd.DragAndDropEvent; 043import com.vaadin.event.dd.DropHandler; 044import com.vaadin.event.dd.acceptcriteria.AcceptAll; 045import com.vaadin.event.dd.acceptcriteria.AcceptCriterion; 046import com.vaadin.server.StreamVariable; 047import com.vaadin.ui.Button; 048import com.vaadin.ui.DragAndDropWrapper; 049import com.vaadin.ui.DragAndDropWrapper.WrapperTransferable; 050import com.vaadin.ui.Html5File; 051import com.vaadin.v7.ui.Label; 052import com.vaadin.v7.ui.Upload; 053import com.vaadin.v7.ui.Upload.ChangeEvent; 054import com.vaadin.v7.ui.Upload.ChangeListener; 055import com.vaadin.v7.ui.Upload.StartedEvent; 056import com.vaadin.v7.ui.Upload.StartedListener; 057import com.vaadin.v7.ui.Upload.SucceededEvent; 058import com.vaadin.v7.ui.Upload.SucceededListener; 059import com.vaadin.v7.ui.VerticalLayout; 060 061/** 062 * The form for importing modules via HTTP.<p> 063 */ 064public class CmsModuleImportForm extends A_CmsModuleImportForm { 065 066 /** The logger instance for this class. */ 067 private static final Log LOG = CmsLog.getLog(CmsModuleImportForm.class); 068 069 /** The serial version id. */ 070 private static final long serialVersionUID = 1L; 071 072 /** Drag and drop wrapper. */ 073 private DragAndDropWrapper m_dnd; 074 075 /** The cancel button. */ 076 protected Button m_cancel; 077 078 /** The OK button. */ 079 protected Button m_ok; 080 081 /** The site selector. */ 082 protected CmsAutoItemCreatingComboBox m_siteSelect; 083 084 /** The upload widget. */ 085 protected Upload m_upload; 086 087 /** The label for the upload widget. */ 088 protected Label m_uploadLabel; 089 090 /** 091 * Creates a new instance.<p> 092 * 093 * @param app the module manager app instance for which this was opened 094 */ 095 @SuppressWarnings("deprecation") 096 public CmsModuleImportForm(CmsModuleApp app, VerticalLayout start, VerticalLayout report, Runnable run) { 097 098 super(app, start, report, run); 099 m_upload.setImmediate(true); 100 m_dnd.setDropHandler(new DropHandler() { 101 102 private static final long serialVersionUID = 1L; 103 104 public void drop(DragAndDropEvent event) { 105 106 WrapperTransferable transfer = (WrapperTransferable)event.getTransferable(); 107 Html5File[] files = transfer.getFiles(); 108 if (files.length == 1) { 109 final Html5File file = files[0]; 110 m_uploadLabel.setValue(file.getFileName()); 111 file.setStreamVariable(new StreamVariable() { 112 113 private static final long serialVersionUID = 1L; 114 115 public OutputStream getOutputStream() { 116 117 String path = CmsStringUtil.joinPaths( 118 OpenCms.getSystemInfo().getWebInfRfsPath(), 119 "packages/modules", 120 processFileName(file.getFileName())); 121 m_importFile = new CmsModuleImportFile(path); 122 try { 123 return new FileOutputStream(m_importFile.getPath()); 124 } catch (FileNotFoundException e) { 125 throw new RuntimeException(e); // shouldn't happen, but if it does, there is no point in continuing 126 } 127 } 128 129 public boolean isInterrupted() { 130 131 return false; 132 } 133 134 public boolean listenProgress() { 135 136 return false; 137 } 138 139 public void onProgress(StreamingProgressEvent evt) { 140 // do nothing 141 } 142 143 @SuppressWarnings("synthetic-access") 144 public void streamingFailed(StreamingErrorEvent evt) { 145 146 LOG.info("Upload streaming failed: " + evt.getFileName(), evt.getException()); 147 } 148 149 public void streamingFinished(StreamingEndEvent evt) { 150 151 validateModuleFile(); 152 } 153 154 public void streamingStarted(StreamingStartEvent evt) { 155 156 m_ok.setEnabled(false); 157 m_siteSelect.setEnabled(true); 158 } 159 }); 160 } 161 } 162 163 public AcceptCriterion getAcceptCriterion() { 164 165 return AcceptAll.get(); 166 } 167 }); 168 m_upload.addStartedListener(new StartedListener() { 169 170 private static final long serialVersionUID = 1L; 171 172 public void uploadStarted(StartedEvent event) { 173 174 m_ok.setEnabled(false); 175 m_siteSelect.setEnabled(true); 176 177 String name = event.getFilename(); 178 name = processFileName(name); 179 m_uploadLabel.setValue(name); 180 181 } 182 183 }); 184 185 m_upload.addChangeListener(new ChangeListener() { 186 187 private static final long serialVersionUID = 1L; 188 189 public void filenameChanged(ChangeEvent event) { 190 191 m_ok.setEnabled(false); 192 m_siteSelect.setEnabled(true); 193 194 String name = processFileName(event.getFilename()); 195 m_uploadLabel.setValue(name); 196 } 197 }); 198 199 m_upload.setReceiver(new Upload.Receiver() { 200 201 private static final long serialVersionUID = 1L; 202 203 public OutputStream receiveUpload(String filename, String mimeType) { 204 205 String path = CmsStringUtil.joinPaths( 206 OpenCms.getSystemInfo().getWebInfRfsPath(), 207 "packages/modules", 208 processFileName(filename)); 209 // make sure parent folders exist 210 File rfsFile = new File(path); 211 rfsFile.getParentFile().mkdirs(); 212 213 m_importFile = new CmsModuleImportFile(path); 214 try { 215 return new FileOutputStream(m_importFile.getPath()); 216 } catch (FileNotFoundException e) { 217 throw new RuntimeException(e); // shouldn't happen, but if it does, there is no point in continuing 218 } 219 } 220 }); 221 m_upload.addSucceededListener(new SucceededListener() { 222 223 private static final long serialVersionUID = 1L; 224 225 public void uploadSucceeded(SucceededEvent event) { 226 227 validateModuleFile(); 228 } 229 }); 230 } 231 232 /** 233 * @see org.opencms.ui.apps.modules.A_CmsModuleImportForm#getCancelButton() 234 */ 235 @Override 236 protected Button getCancelButton() { 237 238 return m_cancel; 239 } 240 241 /** 242 * @see org.opencms.ui.apps.modules.A_CmsModuleImportForm#getOkButton() 243 */ 244 @Override 245 protected Button getOkButton() { 246 247 return m_ok; 248 } 249 250 /** 251 * @see org.opencms.ui.apps.modules.A_CmsModuleImportForm#getSiteSelector() 252 */ 253 @Override 254 protected CmsAutoItemCreatingComboBox getSiteSelector() { 255 256 return m_siteSelect; 257 } 258 259}