View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.server.handler; 
15  
16  import java.io.IOException;
17  
18  import javax.servlet.ServletException;
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.eclipse.jetty.server.AsyncContinuation;
23  import org.eclipse.jetty.server.DispatcherType;
24  import org.eclipse.jetty.server.Request;
25  import org.eclipse.jetty.server.RequestLog;
26  import org.eclipse.jetty.server.Response;
27  import org.eclipse.jetty.server.Server;
28  import org.eclipse.jetty.util.log.Log;
29  
30  
31  
32  /** 
33   * RequestLogHandler.
34   * This handler can be used to wrap an individual context for context logging.
35   * 
36   * 
37   * @org.apache.xbean.XBean
38   */
39  public class RequestLogHandler extends HandlerWrapper
40  {
41      private RequestLog _requestLog;
42      
43      /* ------------------------------------------------------------ */
44      /* 
45       * @see org.eclipse.jetty.server.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
46       */
47      @Override
48      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
49              throws IOException, ServletException
50      {
51          AsyncContinuation continuation = baseRequest.getAsyncContinuation();
52          if (!continuation.isInitial())
53          {
54              baseRequest.setDispatchTime(System.currentTimeMillis());
55          }
56          
57          try
58          {
59              super.handle(target, baseRequest, request, response);
60          }
61          finally
62          {
63              if (_requestLog != null && DispatcherType.REQUEST.equals(baseRequest.getDispatcherType()))
64              {
65                  _requestLog.log(baseRequest, (Response)response);
66              }
67              
68          }
69      }
70  
71      /* ------------------------------------------------------------ */
72      public void setRequestLog(RequestLog requestLog)
73      {
74          //are we changing the request log impl?
75          try
76          {
77              if (_requestLog != null)
78                  _requestLog.stop();
79          }
80          catch (Exception e)
81          {
82              Log.warn (e);
83          }
84          
85          if (getServer()!=null)
86              getServer().getContainer().update(this, _requestLog, requestLog, "logimpl",true);
87          
88          _requestLog = requestLog;
89          
90          //if we're already started, then start our request log
91          try
92          {
93              if (isStarted() && (_requestLog != null))
94                  _requestLog.start();
95          }
96          catch (Exception e)
97          {
98              throw new RuntimeException (e);
99          }
100     }
101 
102     /* ------------------------------------------------------------ */
103     /* 
104      * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#setServer(org.eclipse.jetty.server.server.Server)
105      */
106     @Override
107     public void setServer(Server server)
108     {
109         if (_requestLog!=null)
110         {
111             if (getServer()!=null && getServer()!=server)
112                 getServer().getContainer().update(this, _requestLog, null, "logimpl",true);
113             super.setServer(server);
114             if (server!=null && server!=getServer())
115                 server.getContainer().update(this, null,_requestLog, "logimpl",true);
116         }
117         else
118             super.setServer(server);
119     }
120 
121     /* ------------------------------------------------------------ */
122     public RequestLog getRequestLog() 
123     {
124         return _requestLog;
125     }
126 
127     /* ------------------------------------------------------------ */
128     /* 
129      * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStart()
130      */
131     @Override
132     protected void doStart() throws Exception
133     {
134         super.doStart();
135         if (_requestLog!=null)
136             _requestLog.start();
137     }
138 
139     /* ------------------------------------------------------------ */
140     /* 
141      * @see org.eclipse.jetty.server.server.handler.HandlerWrapper#doStop()
142      */
143     @Override
144     protected void doStop() throws Exception
145     {
146         super.doStop();
147         if (_requestLog!=null)
148             _requestLog.stop();
149     }
150     
151 }