View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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  
23  import javax.servlet.DispatcherType;
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.eclipse.jetty.server.AsyncContinuation;
29  import org.eclipse.jetty.server.Request;
30  import org.eclipse.jetty.server.RequestLog;
31  import org.eclipse.jetty.server.Response;
32  import org.eclipse.jetty.server.Server;
33  import org.eclipse.jetty.util.log.Log;
34  import org.eclipse.jetty.util.log.Logger;
35  
36  
37  
38  /** 
39   * RequestLogHandler.
40   * This handler can be used to wrap an individual context for context logging.
41   * 
42   * 
43   * @org.apache.xbean.XBean
44   */
45  public class RequestLogHandler extends HandlerWrapper
46  {
47      private static final Logger LOG = Log.getLogger(RequestLogHandler.class);
48  
49      private RequestLog _requestLog;
50      
51      /* ------------------------------------------------------------ */
52      /* 
53       * @see org.eclipse.jetty.server.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
54       */
55      @Override
56      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
57              throws IOException, ServletException
58      {
59          AsyncContinuation continuation = baseRequest.getAsyncContinuation();
60          if (!continuation.isInitial())
61          {
62              baseRequest.setDispatchTime(System.currentTimeMillis());
63          }
64          
65          try
66          {
67              super.handle(target, baseRequest, request, response);
68          }
69          finally
70          {
71              if (_requestLog != null && DispatcherType.REQUEST.equals(baseRequest.getDispatcherType()))
72              {
73                  _requestLog.log(baseRequest, (Response)response);
74              }
75              
76          }
77      }
78  
79      /* ------------------------------------------------------------ */
80      public void setRequestLog(RequestLog requestLog)
81      {
82          //are we changing the request log impl?
83          try
84          {
85              if (_requestLog != null)
86                  _requestLog.stop();
87          }
88          catch (Exception e)
89          {
90              LOG.warn (e);
91          }
92          
93          if (getServer()!=null)
94              getServer().getContainer().update(this, _requestLog, requestLog, "logimpl",true);
95          
96          _requestLog = requestLog;
97          
98          //if we're already started, then start our request log
99          try
100         {
101             if (isStarted() && (_requestLog != null))
102                 _requestLog.start();
103         }
104         catch (Exception e)
105         {
106             throw new RuntimeException (e);
107         }
108     }
109 
110     /* ------------------------------------------------------------ */
111     /* 
112      * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#setServer(org.eclipse.jetty.server.server.Server)
113      */
114     @Override
115     public void setServer(Server server)
116     {
117         if (_requestLog!=null)
118         {
119             if (getServer()!=null && getServer()!=server)
120                 getServer().getContainer().update(this, _requestLog, null, "logimpl",true);
121             super.setServer(server);
122             if (server!=null && server!=getServer())
123                 server.getContainer().update(this, null,_requestLog, "logimpl",true);
124         }
125         else
126             super.setServer(server);
127     }
128 
129     /* ------------------------------------------------------------ */
130     public RequestLog getRequestLog() 
131     {
132         return _requestLog;
133     }
134 
135     /* ------------------------------------------------------------ */
136     /* 
137      * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStart()
138      */
139     @Override
140     protected void doStart() throws Exception
141     {
142         super.doStart();
143         if (_requestLog!=null)
144             _requestLog.start();
145     }
146 
147     /* ------------------------------------------------------------ */
148     /* 
149      * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStop()
150      */
151     @Override
152     protected void doStop() throws Exception
153     {
154         super.doStop();
155         if (_requestLog!=null)
156             _requestLog.stop();
157     }
158     
159 }