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.mortbay.jetty.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.mortbay.jetty.Request;
    27  import org.mortbay.jetty.Response;
    28  import org.mortbay.jetty.RetryRequest;
    29  import org.mortbay.util.DateCache;
    30  import org.mortbay.util.RolloverFileOutputStream;
    31
    32
    33  /**
    34   * Debug Handler.
    35   * A lightweight debug handler that can be used in production code.
    36   * Details of the request and response are written to an output stream
    37   * and the current thread name is updated with information that will link
    38   * to the details in that output.
    39   */
    40  public class DebugHandler extends HandlerWrapper
    41  {
    42      private DateCache _date=new DateCache("HH:mm:ss", Locale.US);
    43      private OutputStream _out;
    44      private PrintStream _print;
    45
    46      /* ------------------------------------------------------------ */
    47      /*
    48       * @see org.mortbay.jetty.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
    49       */
    50      public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
    51              throws IOException, ServletException
    52      {
    53          final Request srequest = (Request)request;
    54          final Response sresponse = (Response)response;
    55          final Thread thread=Thread.currentThread();
    56          final String old_name=thread.getName();
    57
    58          boolean suspend=false;
    59          boolean retry=false;
    60          String name=(String)request.getAttribute("org.mortbay.jetty.thread.name");
    61          if (name==null)
    62              name=old_name+"://"+srequest.getHeader("Host") +srequest.getUri();
    63          else
    64              retry=true;
    65
    66          String ex=null;
    67          try
    68          {
    69              final String d=_date.now();
    70              final int ms=_date.lastMs();
    71
    72              if (retry)
    73                  _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" RETRY");
    74              else
    75                  _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+srequest.getRemoteAddr()+" "+request.getMethod()+" "+srequest.getHeader("Cookie")+"; "+srequest.getHeader("User-Agent"));
    76              thread.setName(name);
    77              super.handle(target, request, response, dispatch);
    78          }
    79          catch(RetryRequest r)
    80          {
    81              suspend=true;
    82              request.setAttribute("org.mortbay.jetty.thread.name",name);
    83              throw r;
    84          }
    85          catch(IOException ioe)
    86          {
    87              ex=ioe.toString();
    88              throw ioe;
    89          }
    90          catch(ServletException se)
    91          {
    92              ex=se.toString()+":"+se.getCause();
    93              throw se;
    94          }
    95          catch(RuntimeException rte)
    96          {
    97              ex=rte.toString();
    98              throw rte;
    99          }
    100         catch(Error e)
    101         {
    102             ex=e.toString();
    103             throw e;
    104         }
    105         finally
    106         {
    107             thread.setName(old_name);
    108             final String d=_date.now();
    109             final int ms=_date.lastMs();
    110             if (suspend)
    111                 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" SUSPEND");
    112             else
    113                 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+sresponse.getStatus()+
    114                         " "+sresponse.getContentType()+" "+sresponse.getContentCount()+
    115                         (ex==null?"":("/"+ex)));
    116         }
    117     }
    118
    119     /* (non-Javadoc)
    120      * @see org.mortbay.jetty.handler.HandlerWrapper#doStart()
    121      */
    122     protected void doStart() throws Exception
    123     {
    124         if (_out==null)
    125             _out=new RolloverFileOutputStream("./logs/yyyy_mm_dd.debug.log",true);
    126         _print=new PrintStream(_out);
    127         super.doStart();
    128     }
    129
    130     /* (non-Javadoc)
    131      * @see org.mortbay.jetty.handler.HandlerWrapper#doStop()
    132      */
    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 }