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      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
50              throws IOException, ServletException
51      {
52          final Response base_response = baseRequest.getResponse();
53          final Thread thread=Thread.currentThread();
54          final String old_name=thread.getName();
55  
56          boolean suspend=false;
57          boolean retry=false;
58          String name=(String)request.getAttribute("org.mortbay.jetty.thread.name");
59          if (name==null)
60              name=old_name+"://"+baseRequest.getHeader("Host") +baseRequest.getUri();
61          else
62              retry=true;
63          
64          String ex=null;
65          try
66          {
67              final String d=_date.now();
68              final int ms=_date.lastMs();
69              
70              if (retry)
71                  _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" RETRY");
72              else
73                  _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+baseRequest.getRemoteAddr()+" "+request.getMethod()+" "+baseRequest.getHeader("Cookie")+"; "+baseRequest.getHeader("User-Agent"));
74              thread.setName(name);
75              
76              getHandler().handle(target,baseRequest,request,response);
77          }
78          catch(IOException ioe)
79          {
80              ex=ioe.toString();
81              throw ioe;
82          }
83          catch(ServletException se)
84          {
85              ex=se.toString()+":"+se.getCause();
86              throw se;
87          }
88          catch(RuntimeException rte)
89          {
90              ex=rte.toString();
91              throw rte;
92          }
93          catch(Error e)
94          {
95              ex=e.toString();
96              throw e;
97          }
98          finally
99          {
100             thread.setName(old_name);
101             final String d=_date.now();
102             final int ms=_date.lastMs();
103             suspend=baseRequest.getAsyncContinuation().isSuspended();
104             if (suspend)
105             {
106                 request.setAttribute("org.mortbay.jetty.thread.name",name);
107                 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" SUSPEND");
108             }
109             else
110                 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+base_response.getStatus()+
111 		        (ex==null?"":("/"+ex))+
112 		        " "+base_response.getContentType()+" "+base_response.getContentCount());
113         }
114     }
115 
116     /* (non-Javadoc)
117      * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
118      */
119     protected void doStart() throws Exception
120     {
121         if (_out==null)
122             _out=new RolloverFileOutputStream("./logs/yyyy_mm_dd.debug.log",true);
123         _print=new PrintStream(_out);
124         super.doStart();
125     }
126 
127     /* (non-Javadoc)
128      * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStop()
129      */
130     protected void doStop() throws Exception
131     {
132         super.doStop();
133         _print.close();
134     }
135 
136     /**
137      * @return the out
138      */
139     public OutputStream getOutputStream()
140     {
141         return _out;
142     }
143 
144     /**
145      * @param out the out to set
146      */
147     public void setOutputStream(OutputStream out)
148     {
149         _out = out;
150     }
151 }