View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2009 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  package net.fckeditor.connector.impl;
22  
23  import javax.servlet.ServletContext;
24  
25  import net.fckeditor.handlers.PropertiesLoader;
26  import net.fckeditor.requestcycle.impl.ContextPathBuilder;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Context filesystem backend connector. The file access will be restricted to
33   * the real context root of the currently deployed webapp.
34   * <p>
35   * Though, this connector has some drawbacks:
36   * <ul>
37   * <li>Stored files and folders will be gone if the context is undeployed,
38   * unless you have saved them before.</li>
39   * <li>Some servers are unable to write to the real context root (like
40   * WebSphere), see {@link #init(ServletContext)} for more details.</li>
41   * <li>Some containers (like Jetty) unpack the war file to a different directory
42   * at server start which means that files are gone in temp.</li>
43   * </ul>
44   * </p>
45   * Use the {@link ContextPathBuilder} in conjunction with this class.
46   * 
47   * @version $Id: ContextConnector.java 3591 2009-06-01 13:04:42Z mosipov $
48   */
49  public class ContextConnector extends AbstractLocalFileSystemConnector {
50  	
51  	private final Logger logger = LoggerFactory.getLogger(ContextConnector.class);
52  
53  	/**
54  	 * {@inheritDoc}
55  	 * This method will prepare the connector for further usage, additionally it
56  	 * will check if the aforementioned drawback exists. It will try to resolve
57  	 * the default {@link PropertiesLoader#getUserFilesPath() UserFilesPath}
58  	 * with
59  	 * <code><a href="http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/ServletContext.html#getRealPath(java.lang.String)"
60  	 * target="_blank">ServletContext.getRealPath(String)</a></code> against the
61  	 * local filesystem (real path). If it fails, it will emit helpful log
62  	 * messages and will throw an exception too.
63  	 */
64  	public void init(final ServletContext servletContext) throws Exception {
65  		this.servletContext = servletContext;
66  		String defaultAbsolutePath = getRealUserFilesAbsolutePath(PropertiesLoader
67  				.getUserFilesPath());
68  
69  		if (defaultAbsolutePath == null) {
70  			logger.error("The context root cannot be resolved against the local filesystem");
71  			logger.info("Your servlet container/application server does not expand deployed war files");
72  			logger.debug("Use another Connector implementation (e.g. LocalConnector) and consult http://www.fckeditor.net/forums/viewtopic.php?f=6&t=11568");
73  			throw new NullPointerException(
74  					"The real context root cannot be resolved against the local filesystem");
75  		}
76  	}
77  
78  	/**
79  	 * Resolves the userfiles absolute path against the current context real
80  	 * path.
81  	 */
82  	@Override
83  	protected String getRealUserFilesAbsolutePath(String userFilesAbsolutePath) {
84  		return servletContext.getRealPath(userFilesAbsolutePath);
85  	}
86  
87  }