You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ug...@apache.org on 2004/08/02 16:03:56 UTC
svn commit: rev 35587 - in cocoon/branches/butterfly/src: java/org/apache/butterfly/servlet test/org/apache/butterfly/components/pipeline/impl
Author: ugo
Date: Mon Aug 2 07:03:55 2004
New Revision: 35587
Added:
cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/ButterflyServlet.java
Modified:
cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/GroovySitemapTestCase.java
cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/MyPipeline.groovy
cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/Pipeline.groovy
Log:
Starting implementation of Butterfly Servlet
Added: cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/ButterflyServlet.java
==============================================================================
--- (empty file)
+++ cocoon/branches/butterfly/src/java/org/apache/butterfly/servlet/ButterflyServlet.java Mon Aug 2 07:03:55 2004
@@ -0,0 +1,175 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.butterfly.servlet;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.butterfly.environment.Environment;
+import org.apache.butterfly.environment.http.HttpEnvironment;
+import org.apache.commons.lang.BooleanUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+/**
+ * Description of ButterflyServlet.
+ *
+ * @version CVS $Id$
+ */
+public class ButterflyServlet extends HttpServlet {
+
+ protected static final Log logger = LogFactory.getLog(ButterflyServlet.class);
+ private ServletContext servletContext;
+ private String containerEncoding;
+ private String defaultFormEncoding;
+
+ /* (non-Javadoc)
+ * @see javax.servlet.Servlet#init(javax.servlet.ServletConfig)
+ */
+ public void init(ServletConfig conf) throws ServletException {
+ this.servletContext = conf.getServletContext();
+ this.containerEncoding = getInitParameter("container-encoding", "ISO-8859-1");
+ this.defaultFormEncoding = getInitParameter("form-encoding", "ISO-8859-1");
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+ */
+ protected void service(HttpServletRequest req, HttpServletResponse res)
+ throws ServletException, IOException {
+ String uri = req.getServletPath();
+ if (uri == null) {
+ uri = "";
+ }
+ String pathInfo = req.getPathInfo();
+ if (pathInfo != null) {
+ // VG: WebLogic fix: Both uri and pathInfo starts with '/'
+ // This problem exists only in WL6.1sp2, not in WL6.0sp2 or WL7.0b.
+ if (uri.length() > 0 && uri.charAt(0) == '/') {
+ uri = uri.substring(1);
+ }
+ uri += pathInfo;
+ }
+
+ if (uri.length() == 0) {
+ /* empty relative URI
+ -> HTTP-redirect from /cocoon to /cocoon/ to avoid
+ StringIndexOutOfBoundsException when calling
+ "".charAt(0)
+ else process URI normally
+ */
+ String prefix = req.getRequestURI();
+ if (prefix == null) {
+ prefix = "";
+ }
+
+ res.sendRedirect(res.encodeRedirectURL(prefix + "/"));
+ return;
+ }
+ if (uri.charAt(0) == '/') {
+ uri = uri.substring(1);
+ }
+ // Pass uri into environment without URLDecoding, as it is already decoded.
+
+ Environment env = getEnvironment(uri, req, res);
+
+ // TODO: process the request with the given environment
+ }
+
+ /**
+ * @param uri
+ * @param req
+ * @param res
+ * @throws MalformedURLException
+ * @throws IOException
+ */
+ protected Environment getEnvironment(String uri, HttpServletRequest req, HttpServletResponse res) throws MalformedURLException, IOException {
+ String formEncoding = req.getParameter("cocoon-form-encoding");
+ if (formEncoding == null) {
+ formEncoding = this.defaultFormEncoding;
+ }
+ return new HttpEnvironment(uri,
+ null, // this.servletContextURL,
+ req,
+ res,
+ this.servletContext,
+ null, // (HttpContext) this.appContext.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT),
+ this.containerEncoding,
+ formEncoding);
+ }
+
+ /**
+ * Get an initialisation parameter. The value is trimmed, and null is returned if the trimmed value
+ * is empty.
+ */
+ public String getInitParameter(String name) {
+ String result = super.getInitParameter(name);
+ if (result != null) {
+ result = result.trim();
+ if (result.length() == 0) {
+ result = null;
+ }
+ }
+
+ return result;
+ }
+
+ /** Convenience method to access servlet parameters */
+ protected String getInitParameter(String name, String defaultValue) {
+ String result = getInitParameter(name);
+ if (result == null) {
+ if (logger != null && logger.isDebugEnabled()) {
+ logger.debug(name + " was not set - defaulting to '" + defaultValue + "'");
+ }
+ return defaultValue;
+ } else {
+ return result;
+ }
+ }
+
+ /** Convenience method to access boolean servlet parameters */
+ protected boolean getInitParameterAsBoolean(String name, boolean defaultValue) {
+ String value = getInitParameter(name);
+ if (value == null) {
+ if (logger != null && logger.isDebugEnabled()) {
+ logger.debug(name + " was not set - defaulting to '" + defaultValue + "'");
+ }
+ return defaultValue;
+ }
+
+ return BooleanUtils.toBoolean(value);
+ }
+
+ protected int getInitParameterAsInteger(String name, int defaultValue) {
+ String value = getInitParameter(name);
+ if (value == null) {
+ if (logger != null && logger.isDebugEnabled()) {
+ logger.debug(name + " was not set - defaulting to '" + defaultValue + "'");
+ }
+ return defaultValue;
+ } else {
+ return Integer.parseInt(value);
+ }
+ }
+}
Modified: cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/GroovySitemapTestCase.java
==============================================================================
--- cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/GroovySitemapTestCase.java (original)
+++ cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/GroovySitemapTestCase.java Mon Aug 2 07:03:55 2004
@@ -17,15 +17,15 @@
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyObject;
-
-import java.io.IOException;
-
import junit.framework.TestCase;
-import org.codehaus.groovy.control.CompilationFailedException;
+import org.apache.butterfly.xml.WhitespaceFilter;
+import org.apache.butterfly.xml.dom.DOMBuilder;
+import org.custommonkey.xmlunit.XMLUnit;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
+import org.xml.sax.InputSource;
/**
* Description of GroovySitemapTestCase.
@@ -48,7 +48,8 @@
beanFactory = new XmlBeanFactory(res);
}
- public void testGroovySitemap() throws CompilationFailedException, IOException, InstantiationException, IllegalAccessException {
+ public void testGroovySitemap() throws Exception {
+ XMLUnit.setIgnoreWhitespace(true);
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class pipelineClass = loader.parseClass(getClass().getResourceAsStream("Pipeline.groovy"));
@@ -56,7 +57,12 @@
GroovyObject pipeline = (GroovyObject) myPipelineClass.newInstance();
pipeline.setProperty("beanFactory", beanFactory);
Object[] args = { "index.html" };
- pipeline.invokeMethod("define", args);
- pipeline.invokeMethod("process", new Object[] {});
+ pipeline.invokeMethod("setup", args);
+ DOMBuilder builder = new DOMBuilder();
+ pipeline.invokeMethod("process", new Object[] { null, new WhitespaceFilter(builder) });
+ assertTrue("Output from pipeline does not match control file.",
+ XMLUnit.compareXML(
+ XMLUnit.buildControlDocument(new InputSource("testdata/traxtest-result.xml")),
+ builder.getDocument()).similar());
}
}
Modified: cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/MyPipeline.groovy
==============================================================================
--- cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/MyPipeline.groovy (original)
+++ cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/MyPipeline.groovy Mon Aug 2 07:03:55 2004
@@ -15,7 +15,7 @@
*/
class MyPipeline extends Pipeline {
- void define(String requestPath) {
+ void setup(String requestPath) {
if (requestPath =~ ".*\.html") {
generate "testdata/traxtest-input.xml"
transform "trax", "testdata/traxtest-style.xsl"
Modified: cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/Pipeline.groovy
==============================================================================
--- cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/Pipeline.groovy (original)
+++ cocoon/branches/butterfly/src/test/org/apache/butterfly/components/pipeline/impl/Pipeline.groovy Mon Aug 2 07:03:55 2004
@@ -42,9 +42,7 @@
this.pipeline.serializer = serializer
}
- public void process() {
- builder = new DOMBuilder()
- this.pipeline.process(null, builder)
- println(builder.document.documentElement)
+ public void process(environment, consumer) {
+ this.pipeline.process(environment, consumer)
}
}