View Javadoc

1   //========================================================================
2   //Copyright 2009 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  package org.eclipse.jetty.server.handler; 
16  
17  import java.io.IOException;
18  import java.io.OutputStream;
19  import java.io.PrintStream;
20  import java.util.Locale;
21  
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.eclipse.jetty.server.Request;
27  import org.eclipse.jetty.server.Response;
28  import org.eclipse.jetty.util.DateCache;
29  import org.eclipse.jetty.util.RolloverFileOutputStream;
30  
31  
32  /** 
33   * Debug Handler.
34   * A lightweight debug handler that can be used in production code.
35   * Details of the request and response are written to an output stream
36   * and the current thread name is updated with information that will link
37   * to the details in that output.
38   */
39  public class DebugHandler extends HandlerWrapper
40  {
41      private DateCache _date=new DateCache("HH:mm:ss", Locale.US);
42      private OutputStream _out;
43      private PrintStream _print;
44      
45      /* ------------------------------------------------------------ */
46      /* 
47       * @see org.eclipse.jetty.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
48       */
49      @Override
50      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
51              throws IOException, ServletException
52      {
53          final Response base_response = baseRequest.getResponse();
54          final Thread thread=Thread.currentThread();
55          final String old_name=thread.getName();
56  
57          boolean suspend=false;
58          boolean retry=false;
59          String name=(String)request.getAttribute("org.eclipse.jetty.thread.name");
60          if (name==null)
61              name=old_name+":"+baseRequest.getScheme()+"://"+baseRequest.getLocalAddr()+":"+baseRequest.getLocalPort()+baseRequest.getUri();
62          else
63              retry=true;
64          
65          String ex=null;
66          try
67          {
68              final String d=_date.now();
69              final int ms=_date.lastMs();
70              
71              if (retry)
72                  _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" RETRY");
73              else
74                  _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+baseRequest.getRemoteAddr()+" "+request.getMethod()+" "+baseRequest.getHeader("Cookie")+"; "+baseRequest.getHeader("User-Agent"));
75              thread.setName(name);
76              
77              getHandler().handle(target,baseRequest,request,response);
78          }
79          catch(IOException ioe)
80          {
81              ex=ioe.toString();
82              throw ioe;
83          }
84          catch(ServletException se)
85          {
86              ex=se.toString()+":"+se.getCause();
87              throw se;
88          }
89          catch(RuntimeException rte)
90          {
91              ex=rte.toString();
92              throw rte;
93          }
94          catch(Error e)
95          {
96              ex=e.toString();
97              throw e;
98          }
99          finally
100         {
101             thread.setName(old_name);
102             final String d=_date.now();
103             final int ms=_date.lastMs();
104             suspend=baseRequest.getAsyncContinuation().isSuspended();
105             if (suspend)
106             {
107                 request.setAttribute("org.eclipse.jetty.thread.name",name);
108                 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" SUSPEND");
109             }
110             else
111                 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+base_response.getStatus()+
112 		        (ex==null?"":("/"+ex))+
113 		        " "+base_response.getContentType()+" "+base_response.getContentCount());
114         }
115     }
116 
117     /* (non-Javadoc)
118      * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
119      */
120     @Override
121     protected void doStart() throws Exception
122     {
123         if (_out==null)
124             _out=new RolloverFileOutputStream("./logs/yyyy_mm_dd.debug.log",true);
125         _print=new PrintStream(_out);
126         super.doStart();
127     }
128 
129     /* (non-Javadoc)
130      * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStop()
131      */
132     @Override
133     protected void doStop() throws Exception
134     {
135         super.doStop();
136         _print.close();
137     }
138 
139     /**
140      * @return the out
141      */
142     public OutputStream getOutputStream()
143     {
144         return _out;
145     }
146 
147     /**
148      * @param out the out to set
149      */
150     public void setOutputStream(OutputStream out)
151     {
152         _out = out;
153     }
154 }