View Javadoc
1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.DebugListener;
34  import org.eclipse.jetty.server.Request;
35  import org.eclipse.jetty.server.Response;
36  import org.eclipse.jetty.util.DateCache;
37  import org.eclipse.jetty.util.RolloverFileOutputStream;
38  
39  
40  /**
41   * Debug Handler.
42   * A lightweight debug handler that can be used in production code.
43   * Details of the request and response are written to an output stream
44   * and the current thread name is updated with information that will link
45   * to the details in that output.
46   * @deprecated Use {@link DebugListener}
47   */
48  public class DebugHandler extends HandlerWrapper implements Connection.Listener
49  {
50      private DateCache _date=new DateCache("HH:mm:ss", Locale.US);
51      private OutputStream _out;
52      private PrintStream _print;
53  
54      /* ------------------------------------------------------------ */
55      /*
56       * @see org.eclipse.jetty.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
57       */
58      @Override
59      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
60              throws IOException, ServletException
61      {
62          final Response base_response = baseRequest.getResponse();
63          final Thread thread=Thread.currentThread();
64          final String old_name=thread.getName();
65  
66          boolean suspend=false;
67          boolean retry=false;
68          String name=(String)request.getAttribute("org.eclipse.jetty.thread.name");
69          if (name == null)
70              name = old_name + ":" + baseRequest.getHttpURI();
71          else
72              retry=true;
73  
74          String ex=null;
75          try
76          {
77              if (retry)
78                  print(name,"RESUME");
79              else
80                  print(name,"REQUEST "+baseRequest.getRemoteAddr()+" "+request.getMethod()+" "+baseRequest.getHeader("Cookie")+"; "+baseRequest.getHeader("User-Agent"));
81              thread.setName(name);
82  
83              getHandler().handle(target,baseRequest,request,response);
84          }
85          catch(IOException ioe)
86          {
87              ex=ioe.toString();
88              throw ioe;
89          }
90          catch(ServletException se)
91          {
92              ex=se.toString()+":"+se.getCause();
93              throw se;
94          }
95          catch(RuntimeException rte)
96          {
97              ex=rte.toString();
98              throw rte;
99          }
100         catch(Error e)
101         {
102             ex=e.toString();
103             throw e;
104         }
105         finally
106         {
107             thread.setName(old_name);
108             suspend=baseRequest.getHttpChannelState().isSuspended();
109             if (suspend)
110             {
111                 request.setAttribute("org.eclipse.jetty.thread.name",name);
112                 print(name,"SUSPEND");
113             }
114             else
115                 print(name,"RESPONSE "+base_response.getStatus()+(ex==null?"":("/"+ex))+" "+base_response.getContentType());
116         }
117     }
118     
119     private void print(String name,String message)
120     {
121         long now=System.currentTimeMillis();
122         final String d=_date.formatNow(now);
123         final int ms=(int)(now%1000);
124 
125         _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+message);
126     }
127 
128     /* (non-Javadoc)
129      * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
130      */
131     @Override
132     protected void doStart() throws Exception
133     {
134         if (_out==null)
135             _out=new RolloverFileOutputStream("./logs/yyyy_mm_dd.debug.log",true);
136         _print=new PrintStream(_out);
137         
138         for (Connector connector : getServer().getConnectors())
139             if (connector instanceof AbstractConnector)
140                 ((AbstractConnector)connector).addBean(this,false);
141             
142         super.doStart();
143     }
144 
145     /* (non-Javadoc)
146      * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStop()
147      */
148     @Override
149     protected void doStop() throws Exception
150     {
151         super.doStop();
152         _print.close();
153         for (Connector connector : getServer().getConnectors())
154             if (connector instanceof AbstractConnector)
155                 ((AbstractConnector)connector).removeBean(this);
156     }
157 
158     /**
159      * @return the out
160      */
161     public OutputStream getOutputStream()
162     {
163         return _out;
164     }
165 
166     /**
167      * @param out the out to set
168      */
169     public void setOutputStream(OutputStream out)
170     {
171         _out = out;
172     }
173     
174     @Override
175     public void onOpened(Connection connection)
176     {
177         print(Thread.currentThread().getName(),"OPENED "+connection.toString());
178     }
179 
180     @Override
181     public void onClosed(Connection connection)
182     {
183         print(Thread.currentThread().getName(),"CLOSED "+connection.toString());
184     }
185 
186 }