You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/10/31 16:08:10 UTC
cvs commit: jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/jetty jellyResourceHandlerRequestBody.jelly jettyLogFile.jelly TestJettyHttpServerTags.java
jstrachan 2002/10/31 07:08:10
Modified: jelly/src/java/org/apache/commons/jelly/tags/jetty
JettyHttpServerTag.java
JellyResourceHttpHandler.java JettyTagLibrary.java
jelly/src/test/org/apache/commons/jelly/jetty
TestJettyHttpServerTags.java
Added: jelly/src/java/org/apache/commons/jelly/tags/jetty
ResponseCodeTag.java
jelly/src/test/org/apache/commons/jelly/jetty
jellyResourceHandlerRequestBody.jelly
jettyLogFile.jelly
Log:
Added the second version of the Jetty librariy from Robert Leftwich. Yesterday I actually committed the older version of the patch. This new patch supports response code as well as cusomizing where the Jetty log file goes
Revision Changes Path
1.3 +61 -0 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jetty/JettyHttpServerTag.java
Index: JettyHttpServerTag.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jetty/JettyHttpServerTag.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JettyHttpServerTag.java 30 Oct 2002 13:22:45 -0000 1.2
+++ JettyHttpServerTag.java 31 Oct 2002 15:08:10 -0000 1.3
@@ -63,6 +63,7 @@
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
+import org.apache.commons.logging.LogFactory;
import org.mortbay.http.HttpContext;
import org.mortbay.http.HttpListener;
@@ -71,9 +72,13 @@
import org.mortbay.http.UserRealm;
import org.mortbay.http.handler.NotFoundHandler;
import org.mortbay.http.handler.ResourceHandler;
+import org.mortbay.util.Log;
+import org.mortbay.util.LogSink;
+import org.mortbay.util.OutputStreamLogSink;
import org.mortbay.util.Resource;
import java.net.URL;
+import java.net.MalformedURLException;
/**
* Declare an instance of a Jetty http server
@@ -95,14 +100,41 @@
/** default resource base to use for context */
public static final String DEFAULT_RESOURCE_BASE = "./docRoot";
+ /** default log file for Jetty */
+ public static final String DEFAULT_LOG_FILE = "jetty.log";
+
+ /** The Log to which logging calls will be made. */
+ private static final org.apache.commons.logging.Log log =
+ LogFactory.getLog(JettyHttpServerTag.class);
+
+ /** the log sink for the Jety server */
+ private static OutputStreamLogSink _logSink;
+
+ // static initialisation
+ {
+ // setup a log for Jetty with a default filename
+ try {
+ _logSink = new OutputStreamLogSink(DEFAULT_LOG_FILE);
+ //_logSink.start();
+ Log.instance().add(_logSink);
+ } catch (Exception ex ) {
+ log.error(ex.getLocalizedMessage());
+ }
+
+ }
+
/** unique identifier of the tag/ variable to store result in */
private String _var;
/** the http server for this tag */
private HttpServer _server;
+ /** filename of Jetty log file - with default */
+ private String _logFileName = DEFAULT_LOG_FILE;
+
/** Creates a new instance of JettyHttpServerTag */
public JettyHttpServerTag() {
+
// Create the server
_server=new HttpServer();
@@ -121,6 +153,14 @@
*/
public void doTag(XMLOutput xmlOutput) throws Exception {
+ try {
+ URL logFileURL = getContext().getResource(getLogFileName());
+ _logSink.setFilename(logFileURL.getPath());
+ _logSink.start();
+ } catch (Exception ex ) {
+ log.error(ex.getLocalizedMessage());
+ }
+
// allow nested tags first, e.g body
invokeBody(xmlOutput);
@@ -134,6 +174,7 @@
// if no context/s create a default context
if (_server.getContexts().length == 0) {
+ log.info("Creating a default context");
// Create a context
HttpContext context = _server.getContext(DEFAULT_HOST,
DEFAULT_CONTEXT_PATH);
@@ -151,6 +192,8 @@
for (int i = 0; i < allContexts.length; i++) {
HttpContext currContext = allContexts[i];
if (currContext.getHandlers().length == 0) {
+ log.info("Adding resource and not found handlers to context:" +
+ currContext.getContextPath());
currContext.addHandler(new ResourceHandler());
currContext.addHandler(new NotFoundHandler());
}
@@ -215,6 +258,24 @@
*/
public void setVar(String var) {
_var = var;
+ }
+
+ /**
+ * Getter for property logFileName.
+ *
+ * @return Value of property logFileName.
+ */
+ public String getLogFileName() {
+ return _logFileName;
+ }
+
+ /**
+ * Setter for property logFileName.
+ *
+ * @param logFileName New value of property logFileName.
+ */
+ public void setLogFileName(String logFileName) {
+ _logFileName = logFileName;
}
}
1.3 +26 -0 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jetty/JellyResourceHttpHandler.java
Index: JellyResourceHttpHandler.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jetty/JellyResourceHttpHandler.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JellyResourceHttpHandler.java 30 Oct 2002 13:22:45 -0000 1.2
+++ JellyResourceHttpHandler.java 31 Oct 2002 15:08:10 -0000 1.3
@@ -73,7 +73,11 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.StringBuffer;
import java.util.HashMap;
import java.util.Map;
@@ -137,6 +141,7 @@
jellyContext.setVariable( "pathInContext", pathInContext);
jellyContext.setVariable( "pathParams", pathParams);
jellyContext.setVariable( "request", request);
+ jellyContext.setVariable( "requestBody", getRequestBody(request));
jellyContext.setVariable( "response", response);
try {
@@ -145,6 +150,7 @@
// if it has requested an override then reset the request
if (null == jellyContext.getVariable(OVERRIDE_SET_HANDLED_VAR)) {
request.setHandled(true);
+ response.commit();
} else {
jellyContext.removeVariable(OVERRIDE_SET_HANDLED_VAR);
}
@@ -160,6 +166,26 @@
}
return;
+ }
+
+ public String getRequestBody(HttpRequest request) throws IOException {
+
+ // read the body as a string from the input stream
+ InputStream is = request.getInputStream();
+ InputStreamReader isr = new InputStreamReader(is);
+ BufferedReader br = new BufferedReader(isr);
+ StringBuffer sb = new StringBuffer();
+ char[] buffer = new char[1024];
+ int len;
+
+ while ((len = isr.read(buffer, 0, 1024)) != -1)
+ sb.append(buffer, 0, len);
+
+ if (sb.length() > 0)
+ return sb.toString();
+ else
+ return null;
+
}
}
1.3 +1 -0 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jetty/JettyTagLibrary.java
Index: JettyTagLibrary.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jetty/JettyTagLibrary.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JettyTagLibrary.java 30 Oct 2002 13:22:45 -0000 1.2
+++ JettyTagLibrary.java 31 Oct 2002 15:08:10 -0000 1.3
@@ -93,6 +93,7 @@
registerTag("deleteRequest", DeleteRequestTag.class);
registerTag("responseHeader", ResponseHeaderTag.class);
registerTag("responseBody", ResponseBodyTag.class);
+ registerTag("responseCode", ResponseCodeTag.class);
}
/**
1.1 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jetty/ResponseCodeTag.java
Index: ResponseCodeTag.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/ResponseCodeTag.java,v 1.3 2002/07/14 12:38:22 dion Exp $
* $Revision: 1.3 $
* $Date: 2002/07/14 12:38:22 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.jelly.tags.jetty;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
import org.mortbay.http.HttpResponse;
/**
* Set the response code in the request handler of a Jetty http server
*
* @author rtl
*/
public class ResponseCodeTag extends TagSupport {
/** parameter value */
private int _value;
/**
* Perform the tag functionality. In this case, set the response code in the
* http response found in the jelly context
*
* @param xmlOutput where to send output
* @throws Exception when an error occurs
*/
public void doTag(XMLOutput xmlOutput) throws Exception {
if (getValue() <= 100) {
throw new JellyException("<responseCode> tag must have a value of at least 100");
}
// get the response from the context
HttpResponse httpResponse = (HttpResponse) getContext().getVariable("response");
if (null == httpResponse) {
throw new JellyException("HttpResponse variable not available in Jelly context");
}
// set response code
httpResponse.setStatus(getValue());
}
//--------------------------------------------------------------------------
// Property accessors/mutators
//--------------------------------------------------------------------------
/**
* Getter for property value.
*
* @return value of property value.
*/
public int getValue() {
return _value;
}
/**
* Setter for property value.
*
* @param value New value of property value.
*/
public void setValue(int value) {
_value = value;
}
}
1.3 +27 -9 jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/jetty/TestJettyHttpServerTags.java
Index: TestJettyHttpServerTags.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/jetty/TestJettyHttpServerTags.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestJettyHttpServerTags.java 30 Oct 2002 19:16:33 -0000 1.2
+++ TestJettyHttpServerTags.java 31 Oct 2002 15:08:10 -0000 1.3
@@ -7,7 +7,7 @@
*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 2002 The Apache Software Foundation. All rights
+ * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -110,49 +110,67 @@
public void testDefaultServer() throws Exception {
String text = evaluteScriptAsText(
- "src/test/org/apache/commons/jelly//jetty/defaultServer.jelly"
+ "src/test/org/apache/commons/jelly/jetty/defaultServer.jelly"
);
assertEquals("Produces the correct output", "It works!", text);
}
+ public void testJettyLogFile() throws Exception {
+ File logFile = new File("src/test/org/apache/commons/jelly/jetty/JellyLogFileTest.log");
+ if (logFile.exists()) {
+ logFile.delete();
+ }
+ assertTrue("Logfile does not exist", !logFile.exists());
+ String text = evaluteScriptAsText(
+ "src/test/org/apache/commons/jelly/jetty/jettyLogFile.jelly"
+ );
+ assertEquals("Produces the correct output", "It works!", text);
+ assertTrue("Logfile exists", logFile.exists());
+ }
+
public void testSocketListener() throws Exception {
String text = evaluteScriptAsText(
- "src/test/org/apache/commons/jelly//jetty/socketListener.jelly"
+ "src/test/org/apache/commons/jelly/jetty/socketListener.jelly"
);
assertEquals("Produces the correct output", "It works!", text);
}
public void testHttpContext() throws Exception {
String text = evaluteScriptAsText(
- "src/test/org/apache/commons/jelly//jetty/httpContext.jelly"
+ "src/test/org/apache/commons/jelly/jetty/httpContext.jelly"
);
assertEquals("Produces the correct output", "It works!", text);
}
public void testResourceHandler() throws Exception {
String text = evaluteScriptAsText(
- "src/test/org/apache/commons/jelly//jetty/resourceHandler.jelly"
+ "src/test/org/apache/commons/jelly/jetty/resourceHandler.jelly"
);
assertEquals("Produces the correct output", "It works!", text);
}
public void testSecurityHandler() throws Exception {
String text = evaluteScriptAsText(
- "src/test/org/apache/commons/jelly//jetty/securityHandlerForbidden.jelly"
+ "src/test/org/apache/commons/jelly/jetty/securityHandlerForbidden.jelly"
);
assertEquals("Forbidden test produces the correct output", "It works!", text);
text = evaluteScriptAsText(
- "src/test/org/apache/commons/jelly//jetty/securityHandlerUnauthorized.jelly"
+ "src/test/org/apache/commons/jelly/jetty/securityHandlerUnauthorized.jelly"
);
assertEquals("Unauthorized produces the correct output", "It works!", text);
}
public void testJellyResourceHandler() throws Exception {
String text = evaluteScriptAsText(
- "src/test/org/apache/commons/jelly//jetty/jellyResourceHandler.jelly"
+ "src/test/org/apache/commons/jelly/jetty/jellyResourceHandler.jelly"
);
- assertEquals("Produces the correct output", "It works!", text);
+ assertEquals("jellyResourceHandler produces the correct output", "It works!", text);
+
+ text = evaluteScriptAsText(
+ "src/test/org/apache/commons/jelly/jetty/jellyResourceHandlerRequestBody.jelly"
+ );
+ assertEquals("jellyResourceHandlerRequestBody produces the correct output", "It works!", text);
}
/**
1.1 jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/jetty/jellyResourceHandlerRequestBody.jelly
Index: jellyResourceHandlerRequestBody.jelly
===================================================================
<?xml version="1.0"?>
<j:jelly
xmlns:j="jelly:core"
xmlns="jelly:jetty"
xmlns:http="jelly:http"
trim="false">
<j:set var="contextPathVar" value="/resourceHandlerTest"/>
<j:set var="testPort1" value="8100"/>
<j:set var="testUri1" value="http://localhost:${testPort1}${contextPathVar}/resourceHandlerTest.txt"/>
<jettyHttpServer var="httpServer">
<socketListener port="${testPort1}"/>
<httpContext contextPath="${contextPathVar}" resourceBase="./docRoot/resourceHandlerTest">
<jellyResourceHandler>
<postRequest>
<!-- return the request body -->
<responseHeader name="Content-Type" value="text/plain"/>
<responseBody>
${requestBody}
</responseBody>
<!-- also test response code -->
<responseCode value="202"/>
</postRequest>
</jellyResourceHandler>
</httpContext>
</jettyHttpServer>
<!-- post something -->
<http:post var="mtc1" uri="${testUri1}">
<http:header name="Content-Type" value="text/plain"/>
<http:body>
It works!
</http:body>
</http:post>
<j:choose>
<j:when test="${mtc1.statusCode == 202}">
${mtc1.responseBodyAsString}
</j:when>
<j:otherwise>
Results for mtc1 url are:
http return code = ${mtc1.statusCode}
http status text = '${mtc1.statusText}'
</j:otherwise>
</j:choose>
${httpServer.stop(false)}
</j:jelly>
1.1 jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/jetty/jettyLogFile.jelly
Index: jettyLogFile.jelly
===================================================================
<?xml version="1.0"?>
<j:jelly
xmlns:j="jelly:core"
xmlns="jelly:jetty"
xmlns:http="jelly:http"
trim="false">
<j:set var="contextPathVar" value=""/>
<j:set var="testPort" value="8100"/>
<j:set var="testUri" value="http://localhost:${testPort}${contextPathVar}/test1.txt"/>
<jettyHttpServer var="httpServer" logFileName="JellyLogFileTest.log"/>
<http:get var="mtc" uri="${testUri}"/>
<j:if test='${mtc.statusCode == 200}'>
${mtc.responseBodyAsString}
</j:if>
<j:if test='${mtc.statusCode != 200}'>
Results for mtc url are:
http return code = ${mtc.statusCode}
http status text = '${mtc.statusText}'
size of result = ${mtc.responseBodyAsString.length()}
response time = ${mtc.responseTime}
<j:forEach items="${mtc.responseHeaders}" var="header" indexVar="i">
header[${i}] = ${header}
</j:forEach>
</j:if>
${httpServer.stop()}
</j:jelly>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>