2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server.handler;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.PrintStream;
24 import java.util.Locale;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import org.eclipse.jetty.io.Connection;
31 import org.eclipse.jetty.server.AbstractConnector;
32 import org.eclipse.jetty.server.Connector;
33 import org.eclipse.jetty.server.Request;
34 import org.eclipse.jetty.server.Response;
35 import org.eclipse.jetty.util.DateCache;
36 import org.eclipse.jetty.util.RolloverFileOutputStream;
41 * A lightweight debug handler that can be used in production code.
42 * Details of the request and response are written to an output stream
43 * and the current thread name is updated with information that will link
44 * to the details in that output.
46 public class DebugHandler extends HandlerWrapper implements Connection.Listener
48 private DateCache _date=new DateCache("HH:mm:ss", Locale.US);
49 private OutputStream _out;
50 private PrintStream _print;
52 /* ------------------------------------------------------------ */
54 * @see org.eclipse.jetty.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
57 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
58 throws IOException, ServletException
60 final Response base_response = baseRequest.getResponse();
61 final Thread thread=Thread.currentThread();
62 final String old_name=thread.getName();
64 boolean suspend=false;
66 String name=(String)request.getAttribute("org.eclipse.jetty.thread.name");
68 name=old_name+":"+baseRequest.getScheme()+"://"+baseRequest.getLocalAddr()+":"+baseRequest.getLocalPort()+baseRequest.getUri();
78 print(name,"REQUEST "+baseRequest.getRemoteAddr()+" "+request.getMethod()+" "+baseRequest.getHeader("Cookie")+"; "+baseRequest.getHeader("User-Agent"));
81 getHandler().handle(target,baseRequest,request,response);
83 catch(IOException ioe)
88 catch(ServletException se)
90 ex=se.toString()+":"+se.getCause();
93 catch(RuntimeException rte)
105 thread.setName(old_name);
106 suspend=baseRequest.getHttpChannelState().isSuspended();
109 request.setAttribute("org.eclipse.jetty.thread.name",name);
110 print(name,"SUSPEND");
113 print(name,"RESPONSE "+base_response.getStatus()+(ex==null?"":("/"+ex))+" "+base_response.getContentType());
117 private void print(String name,String message)
119 long now=System.currentTimeMillis();
120 final String d=_date.formatNow(now);
121 final int ms=(int)(now%1000);
123 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+message);
127 * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
130 protected void doStart() throws Exception
133 _out=new RolloverFileOutputStream("./logs/yyyy_mm_dd.debug.log",true);
134 _print=new PrintStream(_out);
136 for (Connector connector : getServer().getConnectors())
137 if (connector instanceof AbstractConnector)
138 ((AbstractConnector)connector).addBean(this,false);
144 * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStop()
147 protected void doStop() throws Exception
151 for (Connector connector : getServer().getConnectors())
152 if (connector instanceof AbstractConnector)
153 ((AbstractConnector)connector).removeBean(this);
159 public OutputStream getOutputStream()
165 * @param out the out to set
167 public void setOutputStream(OutputStream out)
173 public void onOpened(Connection connection)
175 print(Thread.currentThread().getName(),"OPENED "+connection.toString());
179 public void onClosed(Connection connection)
181 print(Thread.currentThread().getName(),"CLOSED "+connection.toString());