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          try
65          {
66              final String d=_date.now();
67              final int ms=_date.lastMs();
68              
69              if (retry)
70                  _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" RETRY");
71              else
72                  _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+baseRequest.getRemoteAddr()+" "+request.getMethod()+" "+baseRequest.getHeader("Cookie")+"; "+baseRequest.getHeader("User-Agent"));
73              thread.setName(name);
74              
75              getHandler().handle(target,baseRequest,request,response);
76          }
77          finally
78          {
79              thread.setName(old_name);
80              final String d=_date.now();
81              final int ms=_date.lastMs();
82              suspend=baseRequest.getAsyncContinuation().isSuspended();
83              if (suspend)
84              {
85                  request.setAttribute("org.mortbay.jetty.thread.name",name);
86                  _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" SUSPEND");
87              }
88              else
89                  _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+base_response.getStatus()+" "+base_response.getContentType()+" "+base_response.getContentCount());
90          }
91      }
92  
93      /* (non-Javadoc)
94       * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
95       */
96      protected void doStart() throws Exception
97      {
98          if (_out==null)
99              _out=new RolloverFileOutputStream("./logs/yyyy_mm_dd.debug.log",true);
100         _print=new PrintStream(_out);
101         super.doStart();
102     }
103 
104     /* (non-Javadoc)
105      * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStop()
106      */
107     protected void doStop() throws Exception
108     {
109         super.doStop();
110         _print.close();
111     }
112 
113     /**
114      * @return the out
115      */
116     public OutputStream getOutputStream()
117     {
118         return _out;
119     }
120 
121     /**
122      * @param out the out to set
123      */
124     public void setOutputStream(OutputStream out)
125     {
126         _out = out;
127     }
128 }