KickJava   Java API By Example, From Geeks To Geeks.

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


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 generate content.
29  *
30  * @author Remy Maucherat
31  */

32 public class TestAdapter
33     implements Adapter {
34
35
36     public static final String JavaDoc CRLF = "\r\n";
37
38
39     /**
40      * Service method, which dumps the request to the console.
41      */

42     public void service(Request req, Response res)
43     throws Exception JavaDoc {
44         
45         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
46         buf.append("Request dump:");
47         buf.append(CRLF);
48         buf.append(req.method());
49         buf.append(" ");
50         buf.append(req.unparsedURI());
51         buf.append(" ");
52         buf.append(req.protocol());
53         buf.append(CRLF);
54         
55         MimeHeaders headers = req.getMimeHeaders();
56         int size = headers.size();
57         for (int i = 0; i < size; i++) {
58             buf.append(headers.getName(i) + ": ");
59             buf.append(headers.getValue(i).toString());
60             buf.append(CRLF);
61         }
62
63         buf.append("Request body:");
64         buf.append(CRLF);
65
66         res.action(ActionCode.ACTION_ACK, null);
67
68         ByteChunk bc = new ByteChunk();
69         byte[] b = buf.toString().getBytes();
70         bc.setBytes(b, 0, b.length);
71         res.doWrite(bc);
72
73         int nRead = 0;
74
75         while (nRead >= 0) {
76             nRead = req.doRead(bc);
77             if (nRead > 0)
78                 res.doWrite(bc);
79         }
80
81     }
82
83
84 }
85
Popular Tags