01: /*
02: * Copyright 1999,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 generate content.
29: *
30: * @author Remy Maucherat
31: */
32: public class TestAdapter implements Adapter {
33:
34: public static final String CRLF = "\r\n";
35:
36: /**
37: * Service method, which dumps the request to the console.
38: */
39: public void service(Request req, Response res) throws Exception {
40:
41: StringBuffer buf = new StringBuffer();
42: buf.append("Request dump:");
43: buf.append(CRLF);
44: buf.append(req.method());
45: buf.append(" ");
46: buf.append(req.unparsedURI());
47: buf.append(" ");
48: buf.append(req.protocol());
49: buf.append(CRLF);
50:
51: MimeHeaders headers = req.getMimeHeaders();
52: int size = headers.size();
53: for (int i = 0; i < size; i++) {
54: buf.append(headers.getName(i) + ": ");
55: buf.append(headers.getValue(i).toString());
56: buf.append(CRLF);
57: }
58:
59: buf.append("Request body:");
60: buf.append(CRLF);
61:
62: res.action(ActionCode.ACTION_ACK, null);
63:
64: ByteChunk bc = new ByteChunk();
65: byte[] b = buf.toString().getBytes();
66: bc.setBytes(b, 0, b.length);
67: res.doWrite(bc);
68:
69: int nRead = 0;
70:
71: while (nRead >= 0) {
72: nRead = req.doRead(bc);
73: if (nRead > 0)
74: res.doWrite(bc);
75: }
76:
77: }
78:
79: }
|