giacomo 01/11/21 14:09:27
Modified: src/org/apache/cocoon/servlet Tag: cocoon_20_branch
CocoonServlet.java
Log:
Added configurable upload max size.
Added isFooEnabled() around loggin statements.
Cosmetic changes to coding standards
Revision Changes Path
No revision
No revision
1.13.2.34 +167 -81 xml-cocoon2/src/org/apache/cocoon/servlet/CocoonServlet.java
Index: CocoonServlet.java
===================================================================
RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/servlet/CocoonServlet.java,v
retrieving revision 1.13.2.33
retrieving revision 1.13.2.34
diff -u -r1.13.2.33 -r1.13.2.34
--- CocoonServlet.java 2001/11/13 16:58:42 1.13.2.33
+++ CocoonServlet.java 2001/11/21 22:09:27 1.13.2.34
@@ -15,7 +15,12 @@
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
import org.apache.avalon.framework.context.DefaultContext;
import org.apache.avalon.framework.logger.Loggable;
-import org.apache.cocoon.*;
+import org.apache.cocoon.Constants;
+import org.apache.cocoon.Notification;
+import org.apache.cocoon.Notifier;
+import org.apache.cocoon.ResourceNotFoundException;
+import org.apache.cocoon.ConnectionResetException;
+import org.apache.cocoon.Cocoon;
import org.apache.cocoon.components.classloader.RepositoryClassLoader;
import org.apache.cocoon.environment.Environment;
import org.apache.cocoon.environment.http.HttpContext;
@@ -45,7 +50,6 @@
import java.lang.reflect.Constructor;
import java.net.URL;
import java.util.Arrays;
-import java.util.Date;
import java.util.StringTokenizer;
/**
@@ -58,17 +62,16 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Carsten Ziegeler</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Leo Sutic</a>
- * @version CVS $Revision: 1.13.2.33 $ $Date: 2001/11/13 16:58:42 $
+ * @version CVS $Revision: 1.13.2.34 $ $Date: 2001/11/21 22:09:27 $
*/
-
public class CocoonServlet extends HttpServlet {
protected Logger log;
protected LogKitManager logKitManager;
- static final float second = 1000;
- static final float minute = 60 * second;
- static final float hour = 60 * minute;
+ static final float SECOND = 1000;
+ static final float MINUTE = 60 * SECOND;
+ static final float HOUR = 60 * MINUTE;
/** The time the cocoon instance was created */
protected long creationTime = 0;
@@ -91,6 +94,7 @@
private static final boolean SILENTLY_RENAME = true;
private static final boolean SAVE_UPLOADED_FILES_TO_DISK = true;
private static final int MAX_UPLOAD_SIZE = 10000000; // 10Mb
+ private int maxUploadSize = MAX_UPLOAD_SIZE; // 10Mb
private File uploadDir;
private File workDir;
private File cacheDir;
@@ -128,16 +132,20 @@
this.initLogger();
this.forceLoadParameter = conf.getInitParameter("load-class");
- if(conf.getInitParameter("load-class") == null) {
- log.debug("load-class was not set - defaulting to false?");
+ if (conf.getInitParameter("load-class") == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("load-class was not set - defaulting to false?");
+ }
}
this.forceSystemProperty = conf.getInitParameter("force-property");
value = conf.getInitParameter("init-classloader");
this.addClassDirs = "true".equalsIgnoreCase(value) ||
"yes".equalsIgnoreCase(value);
- if(value == null) {
- log.debug("init-classloader was not set - defaulting to false");
+ if (value == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("init-classloader was not set - defaulting to false");
+ }
}
String workDirParam = conf.getInitParameter("work-directory");
@@ -161,6 +169,11 @@
this.appContext.put(Constants.CONTEXT_UPLOAD_DIR, this.uploadDir);
this.uploadDir.mkdirs();
+ String maxSizeParam = conf.getInitParameter("upload-max-size");
+ if ((maxSizeParam != null) && (!maxSizeParam.trim().equals(""))) {
+ this.maxUploadSize = Integer.parseInt(maxSizeParam);
+ }
+
String cacheDirParam = conf.getInitParameter("cache-directory");
if ((cacheDirParam != null) && (!cacheDirParam.trim().equals(""))) {
this.cacheDir = IOUtils.createFile( new
File(this.servletContext.getRealPath("/")) , cacheDirParam);
@@ -173,27 +186,35 @@
this.appContext.put(Constants.CONTEXT_CONFIG_URL,
this.getConfigFile(conf.getInitParameter("configurations")));
- if(conf.getInitParameter("configurations") == null) {
- log.debug("configurations was not set - defaulting to... ?");
+ if (conf.getInitParameter("configurations") == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("configurations was not set - defaulting to... ?");
+ }
}
// get allow reload parameter, default is true
value = conf.getInitParameter("allow-reload");
this.allowReload = (value == null || value.equalsIgnoreCase("yes") ||
value.equalsIgnoreCase("true"));
- if(value == null) {
- log.debug("allow-reload was not set - defaulting to true");
+ if (value == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("allow-reload was not set - defaulting to true");
+ }
}
value = conf.getInitParameter("show-time");
this.showTime = "yes".equalsIgnoreCase(value) ||
"true".equalsIgnoreCase(value)
|| (this.hiddenShowTime = "hide".equals(value));
- if(value == null) {
- log.debug("show-time was not set - defaulting to false");
+ if (value == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("show-time was not set - defaulting to false");
+ }
}
parentComponentManagerClass =
conf.getInitParameter("parent-component-manager");
- if(parentComponentManagerClass == null) {
- log.debug("parent-component-manager was not set - defaulting to null.");
+ if (parentComponentManagerClass == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("parent-component-manager was not set - defaulting to
null.");
+ }
}
this.createCocoon();
@@ -206,7 +227,9 @@
{
if (this.cocoon != null)
{
- log.debug("Servlet destroyed - disposing Cocoon");
+ if (log.isDebugEnabled()) {
+ log.debug("Servlet destroyed - disposing Cocoon");
+ }
this.cocoon.dispose();
this.cocoon = null;
}
@@ -231,8 +254,6 @@
* In order to protect ourselves from skitzofrantic classloaders,
* we need to work with a known one.
*
- * @param context The ServletContext to perform the lookup.
- *
* @throws ServletException
*/
protected String getClassPath()
@@ -243,20 +264,23 @@
String libDir = this.servletContext.getRealPath("/WEB-INF/lib");
File root = null;
- if(libDir != null)
+ if (libDir != null) {
root = new File(libDir);
- else
+ } else {
root = workDir;
+ }
- if(classDir != null) {
+ if (classDir != null) {
buildClassPath.append(classDir);
if (this.addClassDirs) {
try {
classLoader.addDirectory(new File(classDir));
} catch (Exception e) {
- log.debug("Could not add directory" + classDir, e);
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add directory" + classDir, e);
+ }
}
}
}
@@ -272,7 +296,9 @@
try {
classLoader.addDirectory(libraries[i]);
} catch (Exception e) {
- log.debug("Could not add file" +
IOUtils.getFullFilename(libraries[i]));
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add file" +
IOUtils.getFullFilename(libraries[i]));
+ }
}
}
}
@@ -291,8 +317,6 @@
* Retreives the "extra-classpath" attribute, that needs to be
* added to the class path.
*
- * @param context The ServletContext to perform the lookup.
- *
* @throws ServletException
*/
protected String getExtraClassPath()
@@ -304,40 +328,53 @@
int i = 0;
while (st.hasMoreTokens()) {
String s = st.nextToken();
- if (i++ > 0)
+ if (i++ > 0) {
sb.append(java.io.File.pathSeparatorChar);
+ }
if ((s.charAt(0) == java.io.File.separatorChar) ||
(s.charAt(1) == ':')) {
- log.debug ("extraClassPath is absolute: " + s);
+ if (log.isDebugEnabled()) {
+ log.debug ("extraClassPath is absolute: " + s);
+ }
sb.append(s);
if (this.addClassDirs) {
try {
classLoader.addDirectory(s.toString());
} catch (Exception e) {
- log.debug("Could not add " + s.toString());
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add " + s.toString());
+ }
}
}
} else {
- if(s.indexOf("${")!=-1) {
+ if (s.indexOf("${") != -1) {
String path = StringUtils.replaceToken(s);
sb.append(path);
- log.debug ("extraClassPath is not absolute replacing using
token: [" + s + "] : " + path);
+ if (log.isDebugEnabled()) {
+ log.debug ("extraClassPath is not absolute replacing
using token: [" + s + "] : " + path);
+ }
if (this.addClassDirs) {
try {
classLoader.addDirectory(path);
} catch (Exception e) {
- log.debug("Could not add " + path);
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add " + path);
+ }
}
}
} else {
- log.debug ("extraClassPath is not absolute pre-pending
context path: " + this.servletContext.getRealPath("/") + s);
+ if (log.isDebugEnabled()) {
+ log.debug ("extraClassPath is not absolute pre-pending
context path: " + this.servletContext.getRealPath("/") + s);
+ }
sb.append(this.servletContext.getRealPath("/") + s);
if (this.addClassDirs) {
try {
classLoader.addDirectory(this.servletContext.getRealPath("/") + s);
} catch (Exception e) {
- log.debug("Could not add " +
this.servletContext.getRealPath("/") + s);
+ if (log.isDebugEnabled()) {
+ log.debug("Could not add " +
this.servletContext.getRealPath("/") + s);
+ }
}
}
}
@@ -358,8 +395,6 @@
* (Priority.DEBUG and above) as you want that get routed to the
* file.
*
- * @param context The ServletContext for the real path.
- *
* @throws ServletException
*/
private void initLogger()
@@ -416,7 +451,6 @@
* Set the ConfigFile for the Cocoon object.
*
* @param configFileName The file location for the cocoon.xconf
- * @param context The servlet context to get the file handle
*
* @throws ServletException
*/
@@ -425,18 +459,24 @@
final String usedFileName;
if (configFileName == null) {
- log.warn("Servlet initialization argument 'configurations' not
specified, attempting to use '/cocoon.xconf'");
+ if (log.isWarnEnabled()) {
+ log.warn("Servlet initialization argument 'configurations' not
specified, attempting to use '/cocoon.xconf'");
+ }
usedFileName = "/cocoon.xconf";
} else {
usedFileName = configFileName;
}
- log.debug("Using configuration file: " + usedFileName);
+ if (log.isDebugEnabled()) {
+ log.debug("Using configuration file: " + usedFileName);
+ }
try {
return this.servletContext.getResource(usedFileName);
} catch (Exception mue) {
- log.error("Servlet initialization argument 'configurations' not found
at " + usedFileName, mue);
+ if (log.isErrorEnabled()) {
+ log.error("Servlet initialization argument 'configurations' not
found at " + usedFileName, mue);
+ }
throw new ServletException("Servlet initialization argument
'configurations' not found at " + usedFileName);
}
}
@@ -452,8 +492,6 @@
* separate each entry with whitespace, a comma, or a semi-colon.
* Cocoon will strip any whitespace from the entry.
*
- * @param forceLoading The array of fully qualified classes to force loading.
- *
* @throws ServletException
*/
private void forceLoad() {
@@ -464,9 +502,14 @@
final String fqcn = fqcnTokenizer.nextToken().trim();
try {
- log.debug("Trying to load class: " + fqcn);
+ if (log.isDebugEnabled()) {
+ log.debug("Trying to load class: " + fqcn);
+ }
ClassUtils.loadClass(fqcn).newInstance();
- } catch (Exception e) { log.warn("Could not
force-load class: " + fqcn, e);
+ } catch (Exception e) {
+ if (log.isWarnEnabled()) {
+ log.warn("Could not force-load class: " + fqcn, e);
+ }
// Do not throw an exception, because it is not a fatal error.
}
}
@@ -489,18 +532,24 @@
java.util.Properties systemProps = System.getProperties();
while (tokenizer.hasMoreTokens()) {
final String property = tokenizer.nextToken().trim();
- if(property.indexOf('=')==-1)
+ if (property.indexOf('=') == -1) {
continue;
+ }
try {
String key = property.substring(0,property.indexOf('='));
- String value = property.substring(property.indexOf('=')+1);
- if(value.indexOf("${")!=-1)
+ String value = property.substring(property.indexOf('=') + 1);
+ if (value.indexOf("${") != -1) {
value = StringUtils.replaceToken(value);
- log.debug("setting " + key + "=" + value);
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("setting " + key + "=" + value);
+ }
systemProps.setProperty(key,value);
} catch (Exception e) {
- log.warn("Could not set property: " + property, e);
+ if (log.isWarnEnabled()) {
+ log.warn("Could not set property: " + property, e);
+ }
// Do not throw an exception, because it is not a fatal error.
}
}
@@ -550,9 +599,13 @@
// We got it... Process the request
String uri = request.getServletPath();
- if (uri == null) uri = "";
+ if (uri == null) {
+ uri = "";
+ }
String pathInfo = request.getPathInfo();
- if (pathInfo != null) uri += pathInfo;
+ if (pathInfo != null) {
+ uri += pathInfo;
+ }
if (uri.length() == 0) {
/* empty relative URI
@@ -563,7 +616,9 @@
*/
String prefix = request.getRequestURI();
- if (prefix == null) prefix = "";
+ if (prefix == null) {
+ prefix = "";
+ }
res.sendRedirect(res.encodeRedirectURL(prefix + "/"));
return;
@@ -610,7 +665,9 @@
res.setContentType(Notifier.notify(n, (OutputStream)null));
}
} catch (ResourceNotFoundException rse) {
- log.warn("The resource was not found", rse);
+ if (log.isWarnEnabled()) {
+ log.warn("The resource was not found", rse);
+ }
res.setStatus(res.SC_NOT_FOUND);
Notification n = new Notification(this);
@@ -627,7 +684,9 @@
// as the status SC_NOT_FOUND is enough
res.setContentType(Notifier.notify(n, (OutputStream)null));
} catch (ConnectionResetException cre) {
- log.warn("The connection was reset", cre);
+ if (log.isWarnEnabled()) {
+ log.warn("The connection was reset", cre);
+ }
Notification n = new Notification(this);
n.setType("resource-not-found");
@@ -643,7 +702,9 @@
// as the connection was reset anyway
res.setContentType(Notifier.notify(n, (OutputStream)null));
} catch (Exception e) {
- log.error("Problem with servlet", e);
+ if (log.isErrorEnabled()) {
+ log.error("Problem with servlet", e);
+ }
//res.setStatus(res.SC_INTERNAL_SERVER_ERROR);
Notification n = new Notification(this, e);
n.setType("internal-server-error");
@@ -656,17 +717,21 @@
long end = System.currentTimeMillis();
String timeString = processTime(end - start);
- log.info("'" + uri + "' " + timeString);
+ if (log.isInfoEnabled()) {
+ log.info("'" + uri + "' " + timeString);
+ }
if (contentType != null && contentType.equals("text/html")) {
String showTime = request.getParameter(Constants.SHOWTIME_PARAM);
boolean show = this.showTime;
- if (showTime != null)
+ if (showTime != null) {
show = !showTime.equalsIgnoreCase("no");
+ }
if (show) {
boolean hide = this.hiddenShowTime;
- if(showTime != null)
+ if (showTime != null) {
hide = showTime.equalsIgnoreCase("hide");
+ }
ServletOutputStream out = res.getOutputStream();
out.print((hide) ? "<!-- " : "<p>");
out.print(timeString);
@@ -688,20 +753,29 @@
URL url = null;
String path = this.servletContext.getRealPath("/");
- log.debug("getRealPath for /: " + path);
- if(path == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("getRealPath for /: " + path);
+ }
+ if (path == null) {
// Try to figure out the path of the root from that of WEB-INF
path = this.servletContext.getResource("/WEB-INF").toString();
- log.debug("getResource for /WEB-INF: " + path);
- path = path.substring(0,path.length()-"WEB-INF".length());
- log.debug("Path for Root: " + path);
+ if (log.isDebugEnabled()) {
+ log.debug("getResource for /WEB-INF: " + path);
+ }
+ path = path.substring(0,path.length() - "WEB-INF".length());
+ if (log.isDebugEnabled()) {
+ log.debug("Path for Root: " + path);
+ }
}
- if(path.indexOf(':')>1)
+ if (path.indexOf(':') > 1) {
url = new URL(path);
- else
+ } else {
url = (new File(path)).toURL();
- log.debug("URL for Root: " + url);
+ }
+ if (log.isDebugEnabled()) {
+ log.debug("URL for Root: " + url);
+ }
env = new HttpEnvironment(uri,
url,
@@ -743,7 +817,9 @@
((Initializable) parentComponentManager).initialize();
}
} catch (Exception e) {
- log.error("Could not initialize parent component manager.", e);
+ if (log.isErrorEnabled()) {
+ log.error("Could not initialize parent component manager.", e);
+ }
}
}
return parentComponentManager;
@@ -772,7 +848,9 @@
try {
URL configFile = (URL)
this.appContext.get(Constants.CONTEXT_CONFIG_URL);
- log.info("Reloading from: " + configFile.toExternalForm());
+ if (log.isInfoEnabled()) {
+ log.info("Reloading from: " + configFile.toExternalForm());
+ }
Cocoon c = (Cocoon) ClassUtils.newInstance("org.apache.cocoon.Cocoon");
final String rootlogger = getInitParameter("cocoon-logger");
if (rootlogger != null) {
@@ -793,7 +871,9 @@
this.cocoon = c;
} catch (Exception e) {
- log.error("Exception reloading", e);
+ if (log.isErrorEnabled()) {
+ log.error("Exception reloading", e);
+ }
this.exception = e;
if (cocoon != null) {
@@ -810,14 +890,14 @@
.append(Constants.COMPLETE_NAME)
.append(" in ");
- if (time > hour) {
- out.append(time / hour);
+ if (time > HOUR) {
+ out.append(time / HOUR);
out.append(" hours.");
- } else if (time > minute) {
- out.append(time / minute);
+ } else if (time > MINUTE) {
+ out.append(time / MINUTE);
out.append(" minutes.");
- } else if (time > second) {
- out.append(time / second);
+ } else if (time > SECOND) {
+ out.append(time / SECOND);
out.append(" seconds.");
} else {
out.append(time);
@@ -837,18 +917,24 @@
throws ServletException {
if (this.cocoon != null && this.allowReload == true) {
if (this.cocoon.modifiedSince(this.creationTime)) {
- log.info("Configuration changed reload attempt");
+ if (log.isInfoEnabled()) {
+ log.info("Configuration changed reload attempt");
+ }
this.initLogger();
this.createCocoon();
return this.cocoon;
} else if ((pathInfo == null) && (reloadParam != null)) {
- log.info("Forced reload attempt");
+ if (log.isInfoEnabled()) {
+ log.info("Forced reload attempt");
+ }
this.initLogger();
this.createCocoon();
return this.cocoon;
}
} else if ((pathInfo == null) && (this.allowReload == true) && (reloadParam
!= null)) {
- log.info("Invalid configurations reload");
+ if (log.isInfoEnabled()) {
+ log.info("Invalid configurations reload");
+ }
this.initLogger();
this.createCocoon();
return this.cocoon;
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]