1 16 package org.mortbay.http.handler; 17 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 21 import org.apache.commons.logging.Log; 22 import org.mortbay.log.LogFactory; 23 import org.mortbay.http.HttpContext; 24 import org.mortbay.http.HttpFields; 25 import org.mortbay.http.HttpHandler; 26 import org.mortbay.http.HttpRequest; 27 import org.mortbay.http.HttpResponse; 28 import org.mortbay.util.ByteArrayISO8859Writer; 29 30 31 37 abstract public class AbstractHttpHandler implements HttpHandler 38 { 39 private static Log log = LogFactory.getLog(AbstractHttpHandler.class); 40 41 42 private String _name; 43 44 private transient HttpContext _context; 45 private transient boolean _started=false; 46 47 48 49 public void setName(String name) 50 { 51 _name=name; 52 } 53 54 55 public String getName() 56 { 57 if (_name==null) 58 { 59 _name=this.getClass().getName(); 60 if (!log.isDebugEnabled()) 61 _name=_name.substring(_name.lastIndexOf('.')+1); 62 } 63 return _name; 64 } 65 66 67 public HttpContext getHttpContext() 68 { 69 return _context; 70 } 71 72 73 77 public void initialize(HttpContext context) 78 { 79 if (_context==null) 80 _context=context; 81 else if (_context!=context) 82 throw new IllegalStateException ("Can't initialize handler for different context"); 83 } 84 85 86 public void start() 87 throws Exception 88 { 89 if (_context==null) 90 throw new IllegalStateException ("No context for "+this); 91 _started=true; 92 if(log.isDebugEnabled())log.debug("Started "+this); 93 } 94 95 96 public void stop() 97 throws InterruptedException 98 { 99 _started=false; 100 if(log.isDebugEnabled())log.debug("Stopped "+this); 101 } 102 103 104 public boolean isStarted() 105 { 106 return _started; 107 } 108 109 110 public String toString() 111 { 112 return getName()+" in "+_context; 113 } 114 115 116 public void handleTrace(HttpRequest request, 117 HttpResponse response) 118 throws IOException 119 { 120 boolean trace=getHttpContext().getHttpServer().getTrace(); 121 122 response.setField(HttpFields.__ContentType, 124 HttpFields.__MessageHttp); 125 if (trace) 126 { 127 OutputStream out = response.getOutputStream(); 128 ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(); 129 writer.write(request.toString()); 130 writer.flush(); 131 response.setIntField(HttpFields.__ContentLength,writer.size()); 132 writer.writeTo(out); 133 out.flush(); 134 } 135 request.setHandled(true); 136 } 137 } 138 139 140 141 142 | Popular Tags |