1 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 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 CRLF = "\r\n"; 40 public static final byte[] b = "0123456789\r\n".getBytes(); 41 public static final ByteChunk bc = new ByteChunk(); 42 43 44 47 public void service(Request req, Response res) 48 throws Exception { 49 50 double rand = Math.random(); 51 int n = (int) Math.round(10 * rand); 52 53 byte[] buf = null; 55 int nRead = 0; 56 StringBuffer sbuf = new StringBuffer (); 57 MimeHeaders headers = req.getMimeHeaders(); 58 int size = headers.size(); 59 60 switch (n) { 61 62 case 0: 63 64 if (log.isDebugEnabled()) 66 log.debug("Response 0"); 67 break; 68 69 case 1: 70 71 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 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 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 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 if (log.isDebugEnabled()) 139 log.debug("Response " + n + " is not implemented yet"); 140 141 } 142 143 180 181 } 182 183 184 } 185 | Popular Tags |