View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.server.handler;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.PrintStream;
24  import java.util.Locale;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
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;
37  
38  
39  /**
40   * Debug Handler.
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.
45   */
46  public class DebugHandler extends HandlerWrapper implements Connection.Listener
47  {
48      private DateCache _date=new DateCache("HH:mm:ss", Locale.US);
49      private OutputStream _out;
50      private PrintStream _print;
51  
52      /* ------------------------------------------------------------ */
53      /*
54       * @see org.eclipse.jetty.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
55       */
56      @Override
57      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
58              throws IOException, ServletException
59      {
60          final Response base_response = baseRequest.getResponse();
61          final Thread thread=Thread.currentThread();
62          final String old_name=thread.getName();
63  
64          boolean suspend=false;
65          boolean retry=false;
66          String name=(String)request.getAttribute("org.eclipse.jetty.thread.name");
67          if (name==null)
68              name=old_name+":"+baseRequest.getScheme()+"://"+baseRequest.getLocalAddr()+":"+baseRequest.getLocalPort()+baseRequest.getUri();
69          else
70              retry=true;
71  
72          String ex=null;
73          try
74          {
75              if (retry)
76                  print(name,"RESUME");
77              else
78                  print(name,"REQUEST "+baseRequest.getRemoteAddr()+" "+request.getMethod()+" "+baseRequest.getHeader("Cookie")+"; "+baseRequest.getHeader("User-Agent"));
79              thread.setName(name);
80  
81              getHandler().handle(target,baseRequest,request,response);
82          }
83          catch(IOException ioe)
84          {
85              ex=ioe.toString();
86              throw ioe;
87          }
88          catch(ServletException se)
89          {
90              ex=se.toString()+":"+se.getCause();
91              throw se;
92          }
93          catch(RuntimeException rte)
94          {
95              ex=rte.toString();
96              throw rte;
97          }
98          catch(Error e)
99          {
100             ex=e.toString();
101             throw e;
102         }
103         finally
104         {
105             thread.setName(old_name);
106             suspend=baseRequest.getHttpChannelState().isSuspended();
107             if (suspend)
108             {
109                 request.setAttribute("org.eclipse.jetty.thread.name",name);
110                 print(name,"SUSPEND");
111             }
112             else
113                 print(name,"RESPONSE "+base_response.getStatus()+(ex==null?"":("/"+ex))+" "+base_response.getContentType());
114         }
115     }
116     
117     private void print(String name,String message)
118     {
119         long now=System.currentTimeMillis();
120         final String d=_date.formatNow(now);
121         final int ms=(int)(now%1000);
122 
123         _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+message);
124     }
125 
126     /* (non-Javadoc)
127      * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
128      */
129     @Override
130     protected void doStart() throws Exception
131     {
132         if (_out==null)
133             _out=new RolloverFileOutputStream("./logs/yyyy_mm_dd.debug.log",true);
134         _print=new PrintStream(_out);
135         
136         for (Connector connector : getServer().getConnectors())
137             if (connector instanceof AbstractConnector)
138                 ((AbstractConnector)connector).addBean(this,false);
139             
140         super.doStart();
141     }
142 
143     /* (non-Javadoc)
144      * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStop()
145      */
146     @Override
147     protected void doStop() throws Exception
148     {
149         super.doStop();
150         _print.close();
151         for (Connector connector : getServer().getConnectors())
152             if (connector instanceof AbstractConnector)
153                 ((AbstractConnector)connector).removeBean(this);
154     }
155 
156     /**
157      * @return the out
158      */
159     public OutputStream getOutputStream()
160     {
161         return _out;
162     }
163 
164     /**
165      * @param out the out to set
166      */
167     public void setOutputStream(OutputStream out)
168     {
169         _out = out;
170     }
171     
172     @Override
173     public void onOpened(Connection connection)
174     {
175         print(Thread.currentThread().getName(),"OPENED "+connection.toString());
176     }
177 
178     @Override
179     public void onClosed(Connection connection)
180     {
181         print(Thread.currentThread().getName(),"CLOSED "+connection.toString());
182     }
183 
184 }