View Javadoc

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