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