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