rjack-jetty 6.1.22.1-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,272 @@
1
+ #--
2
+ # Copyright (C) 2008-2009 David Kellum
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you
5
+ # may not use this file except in compliance with the License. You
6
+ # may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13
+ # implied. See the License for the specific language governing
14
+ # permissions and limitations under the License.
15
+ #++
16
+
17
+ require 'rjack-jetty/base'
18
+
19
+ module RJack
20
+
21
+ # {Jetty Web Server}[http://www.mortbay.org/jetty/] module including
22
+ # a ServerFactory
23
+ module Jetty
24
+
25
+ def self.require_jar( name )
26
+ require File.join( JETTY_DIR, "#{name}-#{ JETTY_VERSION }.jar" )
27
+ end
28
+
29
+ require_jar 'jetty'
30
+ require_jar 'jetty-util'
31
+
32
+ require File.join( JETTY_DIR,
33
+ "servlet-api-#{ SERVLET_API_VERSION }-#{ SERVLET_API_DATE }.jar" )
34
+
35
+ import 'org.mortbay.jetty.Connector'
36
+ import 'org.mortbay.jetty.Handler'
37
+ import 'org.mortbay.jetty.NCSARequestLog'
38
+ import 'org.mortbay.jetty.Server'
39
+ import 'org.mortbay.jetty.handler.ContextHandler'
40
+ import 'org.mortbay.jetty.handler.ContextHandlerCollection'
41
+ import 'org.mortbay.jetty.handler.DefaultHandler'
42
+ import 'org.mortbay.jetty.handler.HandlerCollection'
43
+ import 'org.mortbay.jetty.handler.RequestLogHandler'
44
+ import 'org.mortbay.jetty.handler.ResourceHandler'
45
+ import 'org.mortbay.jetty.nio.SelectChannelConnector'
46
+ import 'org.mortbay.jetty.servlet.Context'
47
+ import 'org.mortbay.jetty.servlet.ServletHolder'
48
+ import 'org.mortbay.jetty.webapp.WebAppContext'
49
+ import 'org.mortbay.thread.QueuedThreadPool'
50
+
51
+ # A factory for creating complete org.morbay.jetty.Server
52
+ # instances. Provides a general purpose facade for setup including
53
+ # the Server, a ThreadPool, a Connector, and various Handlers. It
54
+ # is non-exhaustive (not every Jetty facility is provided) but is
55
+ # designed to be easily extended.
56
+ #
57
+ # == Example
58
+ #
59
+ # factory = Jetty::ServerFactory.new
60
+ # factory.max_threads = 20
61
+ # factory.port = 8080
62
+ #
63
+ # # Set static resource context mapping URI to directory
64
+ # factory.static_contexts[ '/html' ] = '/var/www/html'
65
+ #
66
+ # # Implement custom handler and register it.
67
+ # import 'org.mortbay.jetty.handler.AbstractHandler'
68
+ # class RedirectHandler < AbstractHandler
69
+ #
70
+ # def initialize( redirects )
71
+ # super()
72
+ # @redirects = redirects
73
+ # end
74
+ #
75
+ # def handle( target, request, response, dispatch )
76
+ # goto = @redirects[ target ]
77
+ # unless goto.nil?
78
+ # response.send_redirect( goto )
79
+ # request.handled = true
80
+ # end
81
+ # end
82
+ # end
83
+ #
84
+ # def factory.create_pre_handlers
85
+ # [ RedirectHandler.new( '/' => '/html/' ) ] + super
86
+ # end
87
+ #
88
+ # # Create a webapp context (war file or webapp expanded)
89
+ # factory.webapp_contexts[ '/test' ] = Jetty::TestServlets::WEBAPP_TEST_WAR
90
+ #
91
+ # # Create a context for a custom HelloServlet
92
+ # import 'javax.servlet.http.HttpServlet'
93
+ # class HelloServlet < HttpServlet
94
+ # def doGet( request, response )
95
+ # response.content_type = "text/plain"
96
+ # response.writer.write( 'Hello World!' )
97
+ # end
98
+ # end
99
+ #
100
+ # factory.set_context_servlets( '/hello', { '/*' => HelloServlet.new } )
101
+ #
102
+ # # Create, start, and join (wait for shutdown)
103
+ # server = factory.create
104
+ # server.start
105
+ # server.join
106
+ #
107
+ class ServerFactory
108
+ attr_accessor( :port, :max_idle_time_ms,
109
+ :max_threads, :low_threads, :min_threads,
110
+ :static_contexts, :static_welcome_files,
111
+ :webapp_contexts,
112
+ :servlet_contexts,
113
+ :stop_at_shutdown,
114
+ :request_log_file )
115
+
116
+ def initialize
117
+ @port = 0 # Use any available port
118
+ @max_threads = 20
119
+ @low_threads = 0 # No low thread threshold
120
+ @min_threads = nil # Compute from max_threads
121
+ @max_idle_time_ms = 10000
122
+ @static_contexts = {}
123
+ @static_welcome_files = [ 'index.html' ]
124
+ @webapp_contexts = {}
125
+ @request_log_file = nil
126
+ @servlet_contexts = {}
127
+ @stop_at_shutdown = true
128
+ end
129
+
130
+ # Returns a new org.morbay.jetty.Server that is ready to
131
+ # be started.
132
+ def create
133
+ server = Server.new
134
+
135
+ server.thread_pool = create_pool
136
+
137
+ server.connectors = create_connectors.to_java( Connector )
138
+
139
+ hcol = HandlerCollection.new
140
+ hcol.handlers = create_handlers.compact.to_java( Handler )
141
+ server.handler = hcol
142
+
143
+ server.stop_at_shutdown = @stop_at_shutdown
144
+
145
+ server
146
+ end
147
+
148
+ # Return a org.mortbay.thread.ThreadPool implementation.
149
+ #
150
+ # This implementation creates a QueuedThreadPool with min_threads
151
+ # (default max_threads / 4), any low_threads, and max_threads
152
+ # (default 20).
153
+ def create_pool
154
+ pool = QueuedThreadPool.new
155
+ pool.min_threads = [ @min_threads || ( @max_threads / 4 ), 1 ].max
156
+ pool.low_threads = @low_threads
157
+ pool.max_threads = [ @max_threads, 2 ].max
158
+ pool
159
+ end
160
+
161
+ # Return array of org.mortbay.jetty.Connector instances.
162
+ #
163
+ # This implementation returns a single SelectChannelConnector
164
+ # listening to the given port or an auto-selected avaiable
165
+ # port. Connections are retained for max_idle_time_ms.
166
+ def create_connectors
167
+ connector = SelectChannelConnector.new
168
+ connector.port = @port
169
+ connector.max_idle_time = @max_idle_time_ms
170
+ [ connector ]
171
+ end
172
+
173
+ # Returns an Array of org.mortbay.jetty.Handler instances.
174
+ #
175
+ # This implementation concatenates create_pre_handlers and
176
+ # create_post_handlers.
177
+ def create_handlers
178
+ ( create_pre_handlers + create_post_handlers )
179
+ end
180
+
181
+ # Returns an Array of "pre" org.mortbay.jetty.Handler instances.
182
+ #
183
+ # This implementation returns an array containing a single
184
+ # ContextHandlerCollection which itself contains the context
185
+ # handlers set by create_context_handlers, or an empty array
186
+ # if no context handlers were set.
187
+ def create_pre_handlers
188
+ ctx_handlers = ContextHandlerCollection.new
189
+ create_context_handlers( ctx_handlers )
190
+ h = ctx_handlers.handlers
191
+ if( h.nil? || h.length == 0 )
192
+ [ ]
193
+ else
194
+ [ ctx_handlers ]
195
+ end
196
+ end
197
+
198
+ # Returns an Array of "post" org.mortbay.jetty.Handler instances.
199
+ #
200
+ # This implementation returns a DefaultHandler instance, and any
201
+ # handler returned by create_request_log_handler.
202
+ def create_post_handlers
203
+ [ DefaultHandler.new, # Handle errors, etc.
204
+ create_request_log_handler ]
205
+ end
206
+
207
+ # Create context handlers on the provided ContextHandlerCollection
208
+ #
209
+ # This implementation calls create_static_contexts,
210
+ # create_webapp_contexts, and create_servlet_context.
211
+ def create_context_handlers( context_handler_collection )
212
+ create_static_contexts( context_handler_collection )
213
+ create_webapp_contexts( context_handler_collection )
214
+ create_servlet_contexts( context_handler_collection )
215
+ end
216
+
217
+ # Create context handlers for static resources from static_contexts
218
+ def create_static_contexts( context_handler_collection )
219
+ @static_contexts.each do |ctx, rpath|
220
+ ch = ContextHandler.new( context_handler_collection, ctx )
221
+ ch.resource_base = rpath
222
+ ch.handler = ResourceHandler.new
223
+ ch.handler.welcome_files =
224
+ @static_welcome_files.to_java( java.lang.String )
225
+ end
226
+ end
227
+
228
+ # Set a context of servlets given context_path, a servlets hash
229
+ # (mapping path to Servlet), and options.
230
+ def set_context_servlets( context_path, servlets,
231
+ options = Context::NO_SESSIONS )
232
+ @servlet_contexts[ context_path ] = [ servlets, options ]
233
+ end
234
+
235
+ # Create context handlers from servlet_contexts.
236
+ def create_servlet_contexts( context_handler_collection )
237
+ @servlet_contexts.each do |ctx, s_o|
238
+ servlets, options = s_o
239
+ context = Context.new( context_handler_collection, ctx, options )
240
+ servlets.each do |path, servlet|
241
+ context.add_servlet( ServletHolder.new( servlet ), path )
242
+ end
243
+ end
244
+ end
245
+
246
+ # Create webapp context handlers from webapp_contexts.
247
+ def create_webapp_contexts( context_handler_collection )
248
+ @webapp_contexts.each do |ctx, webapp_path|
249
+ WebAppContext.new( context_handler_collection, webapp_path, ctx )
250
+ end
251
+ end
252
+
253
+ # Create RequestLogHandler from any set request_log_file
254
+ def create_request_log_handler
255
+ if @request_log_file
256
+ log_handler = RequestLogHandler.new
257
+ log_handler.request_log = create_request_log( @request_log_file )
258
+ log_handler
259
+ end
260
+ end
261
+
262
+ # Create a NCSARequestLog to append to log_file
263
+ def create_request_log( log_file )
264
+ log = NCSARequestLog.new( log_file )
265
+ log.log_time_zone = java.util.TimeZone::getDefault.getID
266
+ log.append = true;
267
+ log
268
+ end
269
+
270
+ end
271
+ end
272
+ end
data/pom.xml ADDED
@@ -0,0 +1,67 @@
1
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2
+
3
+ <modelVersion>4.0.0</modelVersion>
4
+ <groupId>rjack</groupId>
5
+ <artifactId>rjack-jetty</artifactId>
6
+ <packaging>jar</packaging>
7
+ <version>1.0</version>
8
+ <name>Gravitext Test Servlets</name> -->
9
+
10
+ <dependencies>
11
+
12
+ <dependency>
13
+ <groupId>org.mortbay.jetty</groupId>
14
+ <artifactId>jetty</artifactId>
15
+ <version>6.1.22</version>
16
+ </dependency>
17
+
18
+ <dependency>
19
+ <groupId>org.mortbay.jetty</groupId>
20
+ <artifactId>jetty-util</artifactId>
21
+ <version>6.1.22</version>
22
+ </dependency>
23
+
24
+ <dependency>
25
+ <groupId>org.mortbay.jetty</groupId>
26
+ <artifactId>jetty-rewrite-handler</artifactId>
27
+ <version>6.1.22</version>
28
+ </dependency>
29
+
30
+ </dependencies>
31
+
32
+ <build>
33
+ <plugins>
34
+ <plugin>
35
+ <artifactId>maven-assembly-plugin</artifactId>
36
+ <configuration>
37
+ <descriptors>
38
+ <descriptor>assembly.xml</descriptor>
39
+ </descriptors>
40
+ <tarLongFileMode>gnu</tarLongFileMode>
41
+ </configuration>
42
+ <executions>
43
+ <execution>
44
+ <id>assembly</id>
45
+ <phase>package</phase>
46
+ <goals>
47
+ <goal>attached</goal>
48
+ </goals>
49
+ </execution>
50
+ </executions>
51
+ </plugin>
52
+ <plugin>
53
+ <artifactId>maven-compiler-plugin</artifactId>
54
+ <configuration>
55
+ <source>1.5</source>
56
+ <target>1.5</target>
57
+ <optimize>true</optimize>
58
+ <debug>true</debug>
59
+ <encoding>UTF-8</encoding>
60
+ <showDeprecation>true</showDeprecation>
61
+ <showWarnings>true</showWarnings>
62
+ </configuration>
63
+ </plugin>
64
+ </plugins>
65
+ </build>
66
+
67
+ </project>
@@ -0,0 +1,153 @@
1
+ /*
2
+ * Copyright (C) 2008 David Kellum
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package rjack.testservlets;
18
+
19
+ import java.io.IOException;
20
+ import java.io.PrintWriter;
21
+ import java.math.BigInteger;
22
+ import java.util.Random;
23
+
24
+ import javax.servlet.ServletException;
25
+ import javax.servlet.http.HttpServlet;
26
+ import javax.servlet.http.HttpServletRequest;
27
+ import javax.servlet.http.HttpServletResponse;
28
+
29
+ /**
30
+ * Servlet for perform and chunked output testing.
31
+ * @author David Kellum
32
+ */
33
+ public class PerfTestServlet extends HttpServlet
34
+ {
35
+
36
+ @Override
37
+ protected void doGet( HttpServletRequest req, HttpServletResponse resp )
38
+ throws ServletException, IOException
39
+ {
40
+ int delayConst = getParam( req, "delay", 0 );
41
+ int delayRandom = getParam( req, "rdelay", 0 );
42
+
43
+ int burnConst = getParam( req, "burn", 0 );
44
+ int burnRandom = getParam( req, "rburn", 0 );
45
+
46
+ int size = getParam( req, "size", 0 );
47
+ int sizeRandom = getParam( req, "rsize", 0 );
48
+
49
+ int count = getParam( req, "count", 0 );
50
+ count += random( getParam( req, "rcount", 0 ) );
51
+
52
+ resp.setContentType("text/html; charset=ISO-8859-1" );
53
+
54
+ PrintWriter out = resp.getWriter();
55
+
56
+ out.println( "<html>" );
57
+ out.println( "<head>" );
58
+ out.println( "<title>Wait/Flush Test</title>" );
59
+ out.println( "</head>" );
60
+ out.println( "<body>" );
61
+
62
+ out.println( "<h2>Usage</h2>" );
63
+ out.println( "<table><tr><th>GET Parameter</th><th>Meaning</th></tr>" );
64
+ out.println( "<tr><td>size</td><td>Chunk constant size ~characters.</td></tr>" );
65
+ out.println( "<tr><td>rsize</td><td>Chunk random [0,n) size ~characters.</td></tr>" );
66
+ out.println( "<tr><td>delay</td><td>Constant chunk delay in ms</td></tr>" );
67
+ out.println( "<tr><td>rdelay</td><td>Random [0,n) chunk delay in ms</td></tr>" );
68
+ out.println( "<tr><td>burn</td><td>Constant chunk CPU burn time in ms</td></tr>" );
69
+ out.println( "<tr><td>rburn</td><td>Random [0,n) chunk burn time in ms</td></tr>" );
70
+ out.println( "</table>" );
71
+
72
+ out.println( "<h2>Output</h2>" );
73
+ out.flush();
74
+
75
+ out.println( "<p>Writing " + count + " (delayed) chunks...</p>" );
76
+ out.flush();
77
+
78
+ try {
79
+ for( int b = 0; b < count; ++b ) {
80
+
81
+ long delay = delayConst + random( delayRandom );
82
+ if( delay > 0 ) Thread.sleep( delay );
83
+
84
+ int burn = burnConst + random ( burnRandom );
85
+ if( burn > 0 ) {
86
+ delay += burnTime( burn );
87
+ }
88
+
89
+ out.println( "<p>(Delayed " + delay + " ms) " );
90
+
91
+ int f = size + random( sizeRandom );
92
+ while( f > 0 ) {
93
+ out.print( FILLER );
94
+ f -= FILLER.length();
95
+ }
96
+
97
+ out.println( "</p>" );
98
+ out.flush();
99
+ }
100
+ }
101
+ catch( InterruptedException x ) {}
102
+ out.println( "</body>" );
103
+ out.println( "</html>" );
104
+ out.flush();
105
+ }
106
+
107
+ private long burnTime( int burn_ms )
108
+ {
109
+ long prime = 2;
110
+ long begin = System.currentTimeMillis();
111
+ long endTime = begin + burn_ms;
112
+ long last = 0;
113
+ do {
114
+ prime = BigInteger.probablePrime( 64, new Random() ).longValue();
115
+ last = System.currentTimeMillis();
116
+ } while( last < endTime );
117
+
118
+ if( prime == 2 ) {
119
+ throw new IllegalStateException( "Not a prime!" );
120
+ }
121
+ return last - begin;
122
+ }
123
+
124
+ private int getParam( HttpServletRequest req, String name, int defVal )
125
+ {
126
+ int value = defVal;
127
+ String strVal = req.getParameter( name );
128
+ if( strVal != null ) {
129
+ value = Integer.parseInt( strVal );
130
+ }
131
+
132
+ return value;
133
+ }
134
+
135
+ private int random( int range )
136
+ {
137
+ Random _random = new Random();
138
+ if( range > 0 ) return _random.nextInt( range );
139
+ return 0;
140
+ }
141
+
142
+ private static final String FILLER =
143
+ "Lorem ipsum dolor sit amet, consectetur adipisicing elit, " +
144
+ "sed do eiusmod tempor incididunt ut labore et dolore magna " +
145
+ "aliqua. Ut enim ad minim veniam, quis nostrud exercitation " +
146
+ "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis " +
147
+ "aute irure dolor in reprehenderit in voluptate velit esse cillum " +
148
+ "dolore eu fugiat nulla pariatur. Excepteur sint occaecat " +
149
+ "cupidatat non proident, sunt in culpa qui officia deserunt " +
150
+ "mollit anim id est laborum. ";
151
+
152
+ private static final long serialVersionUID = 1L;
153
+ }
@@ -0,0 +1,97 @@
1
+ /*
2
+ * Copyright (C) 2008 David Kellum
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package rjack.testservlets;
18
+
19
+ import java.io.IOException;
20
+ import java.io.PrintWriter;
21
+ import java.util.Enumeration;
22
+
23
+ import javax.servlet.ServletException;
24
+ import javax.servlet.http.Cookie;
25
+ import javax.servlet.http.HttpServlet;
26
+ import javax.servlet.http.HttpServletRequest;
27
+ import javax.servlet.http.HttpServletResponse;
28
+
29
+ public class SnoopServlet extends HttpServlet
30
+ {
31
+
32
+ @Override
33
+ protected void doGet( HttpServletRequest request,
34
+ HttpServletResponse response )
35
+ throws ServletException, IOException
36
+ {
37
+ response.setContentType("text/html; charset=UTF-8" );
38
+
39
+ PrintWriter out = response.getWriter();
40
+
41
+ out.println( "<html>" );
42
+ out.println( "<head>" );
43
+ out.println( "<title>Wait/Flush Test</title>" );
44
+ out.println( "</head>" );
45
+ out.println( "<body>" );
46
+
47
+ writeTableHeader( out, "Request Properties", "Property" );
48
+ writeRow( out, "Request URI", request.getRequestURI() );
49
+ writeRow( out, "HTTP Method", request.getMethod() );
50
+ writeRow( out, "Path Info", request.getPathInfo() );
51
+ writeRow( out, "Path Trans", request.getPathTranslated() );
52
+ writeRow( out, "Query String", request.getQueryString() );
53
+ writeRow( out, "Context Path", request.getContextPath() );
54
+ writeRow( out, "Servlet Path", request.getServletPath() );
55
+ writeRow( out, "Is Secure", String.valueOf( request.isSecure() ) );
56
+ writeRow( out, "Auth Type", request.getAuthType() );
57
+ writeRow( out, "Remote User", request.getRemoteUser() );
58
+ out.println( "</table>" );
59
+
60
+ writeTableHeader( out, "Request Headers", "Header" );
61
+ Enumeration<?> hNames = request.getHeaderNames();
62
+ while( hNames.hasMoreElements() ) {
63
+ String hname = hNames.nextElement().toString();
64
+ String hvalue = request.getHeader( hname );
65
+ writeRow( out, hname, hvalue );
66
+ }
67
+ out.println( "</table>" );
68
+
69
+ Cookie[] cookies = request.getCookies();
70
+ if (cookies != null) {
71
+ writeTableHeader( out, "Cookies", "Cookie" );
72
+ for( Cookie cookie : cookies ) {
73
+ writeRow( out, cookie.getName(), cookie.getValue() );
74
+ }
75
+ out.println( "</table>" );
76
+ }
77
+
78
+ out.println( "</body>" );
79
+ out.println( "</html>" );
80
+ out.close();
81
+ }
82
+
83
+ private void writeTableHeader( PrintWriter out,
84
+ String heading, String name )
85
+ {
86
+ out.println( "<h2>" + heading + "</h2>" );
87
+ out.println( "<table><tr><th>" + name + "</th><th>Value</th></tr>" );
88
+ }
89
+
90
+ private void writeRow( PrintWriter out, String name, String value )
91
+ {
92
+ out.println( String.format( "<tr><td>%s</td><td>%s</td></tr>",
93
+ name, value ) );
94
+ }
95
+
96
+ private static final long serialVersionUID = 1L;
97
+ }
data/test/test.txt ADDED
@@ -0,0 +1 @@
1
+ test text over http