KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > coyote > http11 > RandomAdapter


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
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 package org.apache.coyote.http11;
17
18 import org.apache.tomcat.util.buf.MessageBytes;
19 import org.apache.tomcat.util.buf.ByteChunk;
20 import org.apache.tomcat.util.http.MimeHeaders;
21
22 import org.apache.coyote.ActionCode;
23 import org.apache.coyote.Adapter;
24 import org.apache.coyote.Request;
25 import org.apache.coyote.Response;
26
27 /**
28  * Adapter which will return random responses using pipelining, which means it
29  * will never try to generate bogus responses.
30  *
31  * @author Remy Maucherat
32  */

33 public class RandomAdapter
34     implements Adapter {
35
36     private static org.apache.commons.logging.Log log=
37         org.apache.commons.logging.LogFactory.getLog( RandomAdapter.class );
38
39     public static final String JavaDoc CRLF = "\r\n";
40     public static final byte[] b = "0123456789\r\n".getBytes();
41     public static final ByteChunk bc = new ByteChunk();
42
43
44     /**
45      * Service method, which dumps the request to the console.
46      */

47     public void service(Request req, Response res)
48     throws Exception JavaDoc {
49
50         double rand = Math.random();
51         int n = (int) Math.round(10 * rand);
52
53         // Temp variables
54
byte[] buf = null;
55         int nRead = 0;
56         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
57         MimeHeaders headers = req.getMimeHeaders();
58         int size = headers.size();
59
60         switch (n) {
61
62         case 0:
63
64             // 0) Do nothing
65
if (log.isDebugEnabled())
66                 log.debug("Response 0");
67             break;
68
69         case 1:
70
71             // 1) Set content length, and write the appropriate content
72
if (log.isDebugEnabled())
73                 log.debug("Response 1");
74             res.setContentLength(b.length);
75             bc.setBytes(b, 0, b.length);
76             res.doWrite(bc);
77             break;
78
79         case 2:
80
81             // 2) Read the request data, and print out the number of bytes
82
// read
83
if (log.isDebugEnabled())
84                 log.debug("Response 2");
85             while (nRead >= 0) {
86                 nRead = req.doRead(bc);
87                 buf = ("Read " + nRead + " bytes\r\n").getBytes();
88                 bc.setBytes(buf, 0, buf.length);
89                 res.doWrite(bc);
90             }
91             break;
92
93         case 3:
94
95             // 3) Return 204 (no content), while reading once on input
96
if (log.isDebugEnabled())
97                 log.debug("Response 3");
98             res.setStatus(204);
99             nRead = req.doRead(bc);
100             res.setHeader("Info", "Read " + nRead + " bytes");
101             break;
102
103         case 4:
104
105             // 4) Do a request dump
106
if (log.isDebugEnabled())
107                 log.debug("Response 4");
108             sbuf.append("Request dump:");
109             sbuf.append(CRLF);
110             sbuf.append(req.method());
111             sbuf.append(" ");
112             sbuf.append(req.unparsedURI());
113             sbuf.append(" ");
114             sbuf.append(req.protocol());
115             sbuf.append(CRLF);
116             for (int i = 0; i < size; i++) {
117                 sbuf.append(headers.getName(i) + ": ");
118                 sbuf.append(headers.getValue(i).toString());
119                 sbuf.append(CRLF);
120             }
121             sbuf.append("Request body:");
122             sbuf.append(CRLF);
123             res.action(ActionCode.ACTION_ACK, null);
124             ByteChunk bc2 = new ByteChunk();
125             byte[] b2 = sbuf.toString().getBytes();
126             bc2.setBytes(b2, 0, b2.length);
127             res.doWrite(bc2);
128             while (nRead >= 0) {
129                 nRead = req.doRead(bc2);
130                 if (nRead > 0)
131                     res.doWrite(bc2);
132             }
133             break;
134
135         default:
136
137             // Response not implemented yet
138
if (log.isDebugEnabled())
139                 log.debug("Response " + n + " is not implemented yet");
140
141         }
142
143 /*
144         StringBuffer buf = new StringBuffer();
145         buf.append("Request dump:");
146         buf.append(CRLF);
147         buf.append(req.method());
148         buf.append(" ");
149         buf.append(req.unparsedURI());
150         buf.append(" ");
151         buf.append(req.protocol());
152         buf.append(CRLF);
153         
154         MimeHeaders headers = req.getMimeHeaders();
155         int size = headers.size();
156         for (int i = 0; i < size; i++) {
157             buf.append(headers.getName(i) + ": ");
158             buf.append(headers.getValue(i).toString());
159             buf.append(CRLF);
160         }
161
162         buf.append("Request body:");
163         buf.append(CRLF);
164
165         res.action(ActionCode.ACTION_ACK, null);
166
167         ByteChunk bc = new ByteChunk();
168         byte[] b = buf.toString().getBytes();
169         bc.setBytes(b, 0, b.length);
170         res.doWrite(bc);
171
172         int nRead = 0;
173
174         while (nRead >= 0) {
175             nRead = req.doRead(bc);
176             if (nRead > 0)
177                 res.doWrite(bc);
178         }
179 */

180
181     }
182
183
184 }
185
Popular Tags