001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.coyote.http11;
017:
018: import org.apache.tomcat.util.buf.MessageBytes;
019: import org.apache.tomcat.util.buf.ByteChunk;
020: import org.apache.tomcat.util.http.MimeHeaders;
021:
022: import org.apache.coyote.ActionCode;
023: import org.apache.coyote.Adapter;
024: import org.apache.coyote.Request;
025: import org.apache.coyote.Response;
026:
027: /**
028: * Adapter which will return random responses using pipelining, which means it
029: * will never try to generate bogus responses.
030: *
031: * @author Remy Maucherat
032: */
033: public class RandomAdapter implements Adapter {
034:
035: public static final String CRLF = "\r\n";
036: public static final byte[] b = "0123456789\r\n".getBytes();
037: public static final ByteChunk bc = new ByteChunk();
038:
039: /**
040: * Service method, which dumps the request to the console.
041: */
042: public void service(Request req, Response res) throws Exception {
043:
044: double rand = Math.random();
045: int n = (int) Math.round(10 * rand);
046:
047: // Temp variables
048: byte[] buf = null;
049: int nRead = 0;
050: StringBuffer sbuf = new StringBuffer();
051: MimeHeaders headers = req.getMimeHeaders();
052: int size = headers.size();
053:
054: switch (n) {
055:
056: case 0:
057:
058: // 0) Do nothing
059: System.out.println("Response 0");
060: break;
061:
062: case 1:
063:
064: // 1) Set content length, and write the appropriate content
065: System.out.println("Response 1");
066: res.setContentLength(b.length);
067: bc.setBytes(b, 0, b.length);
068: res.doWrite(bc);
069: break;
070:
071: case 2:
072:
073: // 2) Read the request data, and print out the number of bytes
074: // read
075: System.out.println("Response 2");
076: while (nRead >= 0) {
077: nRead = req.doRead(bc);
078: buf = ("Read " + nRead + " bytes\r\n").getBytes();
079: bc.setBytes(buf, 0, buf.length);
080: res.doWrite(bc);
081: }
082: break;
083:
084: case 3:
085:
086: // 3) Return 204 (no content), while reading once on input
087: System.out.println("Response 3");
088: res.setStatus(204);
089: nRead = req.doRead(bc);
090: res.setHeader("Info", "Read " + nRead + " bytes");
091: break;
092:
093: case 4:
094:
095: // 4) Do a request dump
096: System.out.println("Response 4");
097: sbuf.append("Request dump:");
098: sbuf.append(CRLF);
099: sbuf.append(req.method());
100: sbuf.append(" ");
101: sbuf.append(req.unparsedURI());
102: sbuf.append(" ");
103: sbuf.append(req.protocol());
104: sbuf.append(CRLF);
105: for (int i = 0; i < size; i++) {
106: sbuf.append(headers.getName(i) + ": ");
107: sbuf.append(headers.getValue(i).toString());
108: sbuf.append(CRLF);
109: }
110: sbuf.append("Request body:");
111: sbuf.append(CRLF);
112: res.action(ActionCode.ACTION_ACK, null);
113: ByteChunk bc2 = new ByteChunk();
114: byte[] b2 = sbuf.toString().getBytes();
115: bc2.setBytes(b2, 0, b2.length);
116: res.doWrite(bc2);
117: while (nRead >= 0) {
118: nRead = req.doRead(bc2);
119: if (nRead > 0)
120: res.doWrite(bc2);
121: }
122: break;
123:
124: default:
125:
126: // Response not implemented yet
127: System.out.println("Response " + n
128: + " is not implemented yet");
129:
130: }
131:
132: /*
133: StringBuffer buf = new StringBuffer();
134: buf.append("Request dump:");
135: buf.append(CRLF);
136: buf.append(req.method());
137: buf.append(" ");
138: buf.append(req.unparsedURI());
139: buf.append(" ");
140: buf.append(req.protocol());
141: buf.append(CRLF);
142:
143: MimeHeaders headers = req.getMimeHeaders();
144: int size = headers.size();
145: for (int i = 0; i < size; i++) {
146: buf.append(headers.getName(i) + ": ");
147: buf.append(headers.getValue(i).toString());
148: buf.append(CRLF);
149: }
150:
151: buf.append("Request body:");
152: buf.append(CRLF);
153:
154: res.action(ActionCode.ACTION_ACK, null);
155:
156: ByteChunk bc = new ByteChunk();
157: byte[] b = buf.toString().getBytes();
158: bc.setBytes(b, 0, b.length);
159: res.doWrite(bc);
160:
161: int nRead = 0;
162:
163: while (nRead >= 0) {
164: nRead = req.doRead(bc);
165: if (nRead > 0)
166: res.doWrite(bc);
167: }
168: */
169:
170: }
171:
172: }
|