001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.components;
006:
007: import java.io.FileFilter;
008: import java.io.FileInputStream;
009: import java.io.FileNotFoundException;
010: import java.io.FileOutputStream;
011: import java.io.IOException;
012: import java.net.URI;
013: import java.net.URISyntaxException;
014: import java.util.ArrayList;
015: import java.util.HashMap;
016: import java.util.List;
017:
018: import javax.servlet.ServletContext;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: import com.opensymphony.webwork.util.ServletContextAware;
024: import com.opensymphony.webwork.views.util.UrlHelper;
025: import com.opensymphony.webwork.WebWorkException;
026:
027: /**
028: *
029: * @author tm_jee
030: * @version $Date: 2007-03-29 08:02:59 +0200 (Do, 29 Mrz 2007) $ $Id: DefaultRichtexteditorConnector.java 2883 2007-03-29 06:02:59Z tm_jee $
031: */
032: public class DefaultRichtexteditorConnector extends
033: AbstractRichtexteditorConnector implements ServletContextAware {
034:
035: private static final Log _log = LogFactory
036: .getLog(DefaultRichtexteditorConnector.class);
037:
038: private static final long serialVersionUID = -3792445192115623052L;
039:
040: protected String _actualServerPath = "/com/opensymphony/webwork/static/richtexteditor/data/";
041:
042: public String getActualServerPath() {
043: return _actualServerPath;
044: }
045:
046: public void setActualServerPath(String actualServerPath) {
047: _actualServerPath = actualServerPath;
048: }
049:
050: protected String calculateServerPath(String serverPath,
051: String folderPath, String type) throws Exception {
052: //return UrlHelper.buildUrl(serverPath, _request, _response, null, _request.getScheme(), true, true, true);
053: return UrlHelper.buildUrl(serverPath + type + folderPath,
054: _request, _response, new HashMap(), _request
055: .getScheme(), true, true, true);
056: }
057:
058: protected String calculateActualServerPath(String actualServerPath,
059: String type, String folderPath) throws Exception {
060: String path = "file:////"
061: + servletContext.getRealPath("/WEB-INF/classes"
062: + actualServerPath);
063: path = path.trim();
064: path = path.replace('\\', '/');
065: makeDirIfNotExists(path);
066: path = path.endsWith("/") ? path : path + "/";
067: return path + type + folderPath;
068: }
069:
070: private ServletContext servletContext;
071:
072: public void setServletContext(ServletContext servletContext) {
073: this .servletContext = servletContext;
074: }
075:
076: protected Folder[] getFolders(String virtualFolderPath, String type)
077: throws Exception {
078: String path = calculateActualServerPath(getActualServerPath(),
079: type, virtualFolderPath);
080: makeDirIfNotExists(path);
081: java.io.File f = new java.io.File(new URI(path));
082: java.io.File[] children = f.listFiles(new FileFilter() {
083: public boolean accept(java.io.File pathname) {
084: if (!pathname.isFile()) {
085: return true;
086: }
087: return false;
088: }
089: });
090:
091: List tmpFolders = new ArrayList();
092: for (int a = 0; a < children.length; a++) {
093: tmpFolders.add(new Folder(children[a].getName()));
094: }
095:
096: return (Folder[]) tmpFolders.toArray(new Folder[0]);
097: }
098:
099: protected FoldersAndFiles getFoldersAndFiles(
100: String virtualFolderPath, String type) throws Exception {
101: String path = calculateActualServerPath(getActualServerPath(),
102: type, virtualFolderPath);
103: makeDirIfNotExists(path);
104: java.io.File f = new java.io.File(new URI(path));
105: java.io.File[] children = f.listFiles();
106:
107: List directories = new ArrayList();
108: List files = new ArrayList();
109: for (int a = 0; a < children.length; a++) {
110: if (children[a].isDirectory()) {
111: directories.add(new Folder(children[a].getName()));
112: } else {
113: try {
114: files.add(new File(children[a].getName(),
115: fileSizeInKBytes(children[a])));
116: } catch (Exception e) {
117: _log.error("cannot deal with file " + children[a],
118: e);
119: }
120: }
121: }
122:
123: return new FoldersAndFiles((Folder[]) directories
124: .toArray(new Folder[0]), (File[]) files
125: .toArray(new File[0]));
126: }
127:
128: protected CreateFolderResult createFolder(String virtualFolderPath,
129: String type, String newFolderName) {
130: try {
131: String tmpPath = calculateActualServerPath(
132: getActualServerPath(), type, virtualFolderPath);
133: tmpPath = tmpPath + newFolderName;
134: boolean alreadyExists = makeDirIfNotExists(tmpPath);
135: if (alreadyExists) {
136: return CreateFolderResult.folderAlreadyExists();
137: }
138: } catch (Exception e) {
139: _log.error(e.toString(), e);
140: return CreateFolderResult.unknownError();
141: }
142: return CreateFolderResult.noErrors();
143: }
144:
145: protected FileUploadResult fileUpload(String virtualFolderPath,
146: String type, String filename, String contentType,
147: java.io.File newFile) {
148: try {
149: String tmpDir = calculateActualServerPath(
150: getActualServerPath(), type, virtualFolderPath);
151: makeDirIfNotExists(tmpDir);
152: String tmpFile = tmpDir + filename;
153: if (makeFileIfNotExists(tmpFile)) {
154: // already exists
155: int a = 0;
156: String ext = String.valueOf(a);
157: tmpFile = calculateActualServerPath(
158: getActualServerPath(), type, virtualFolderPath)
159: + filename + ext;
160: while (makeFileIfNotExists(tmpFile)) {
161: a = a + 1;
162: ext = String.valueOf(a);
163: if (a > 100) {
164: return FileUploadResult.invalidFile();
165: }
166: tmpFile = calculateActualServerPath(
167: getActualServerPath(), type,
168: virtualFolderPath)
169: + filename + ext;
170: }
171: copyFile(newFile, new java.io.File(new URI(tmpFile)));
172: return FileUploadResult
173: .uploadCompleteWithFilenamChanged(filename
174: + ext);
175: } else {
176: copyFile(newFile, new java.io.File(new URI(tmpFile)));
177: return FileUploadResult.uploadComplete();
178: }
179: } catch (Exception e) {
180: _log.error(e.toString(), e);
181: return FileUploadResult.invalidFile();
182: }
183: }
184:
185: protected void unknownCommand(String command,
186: String virtualFolderPath, String type, String filename,
187: String contentType, java.io.File newFile) {
188: throw new WebWorkException("unknown command " + command);
189: }
190:
191: /**
192: *
193: * @param path
194: * @return true if file already exists, false otherwise.
195: */
196: protected boolean makeDirIfNotExists(String path)
197: throws URISyntaxException {
198: java.io.File dir = new java.io.File(new URI(path));
199: if (!dir.exists()) {
200: if (_log.isDebugEnabled()) {
201: _log.debug("make directory " + dir);
202: }
203: boolean ok = dir.mkdirs();
204: if (!ok) {
205: throw new WebWorkException("cannot make directory "
206: + dir);
207: }
208: return false;
209: }
210: return true;
211: }
212:
213: /**
214: *
215: * @param filePath
216: * @return true if file already exists, false otherwise
217: */
218: protected boolean makeFileIfNotExists(String filePath)
219: throws IOException, URISyntaxException {
220: java.io.File f = new java.io.File(new URI(filePath));
221: if (!f.exists()) {
222: if (_log.isDebugEnabled()) {
223: _log.debug("creating file " + filePath);
224: }
225: boolean ok = f.createNewFile();
226: if (!ok) {
227: throw new WebWorkException("cannot create file "
228: + filePath);
229: }
230: return false;
231: }
232: return true;
233: }
234:
235: protected void copyFile(java.io.File from, java.io.File to)
236: throws FileNotFoundException, IOException {
237: FileInputStream fis = null;
238: FileOutputStream fos = null;
239: try {
240: if (_log.isDebugEnabled()) {
241: _log.debug("copy file from " + from + " to " + to);
242: }
243: fis = new FileInputStream(from);
244: fos = new FileOutputStream(to);
245: int tmpByte = fis.read();
246: while (tmpByte != -1) {
247: fos.write(tmpByte);
248: tmpByte = fis.read();
249: }
250: fos.flush();
251: } finally {
252: if (fis != null)
253: fis.close();
254: if (fos != null)
255: fos.close();
256: }
257: }
258:
259: protected long fileSizeInKBytes(java.io.File file)
260: throws FileNotFoundException, IOException {
261: FileInputStream fis = null;
262: long size = 0;
263: try {
264: fis = new FileInputStream(file);
265: size = fis.getChannel().size();
266: } finally {
267: if (fis != null)
268: fis.close();
269: }
270: if (size > 0) {
271: size = (size / 100);
272: }
273: if (_log.isDebugEnabled()) {
274: _log.debug("size of file " + file + " is " + size + " kb");
275: }
276: return size;
277: }
278: }
|