View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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  import java.util.List;
23  
24  import javax.servlet.ServletException;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.http.HttpServletResponse;
27  
28  import org.eclipse.jetty.server.Handler;
29  import org.eclipse.jetty.server.Request;
30  import org.eclipse.jetty.server.Server;
31  import org.eclipse.jetty.util.annotation.ManagedAttribute;
32  import org.eclipse.jetty.util.annotation.ManagedObject;
33  import org.eclipse.jetty.util.component.LifeCycle;
34  
35  /* ------------------------------------------------------------ */
36  /** A <code>HandlerWrapper</code> acts as a {@link Handler} but delegates the {@link Handler#handle handle} method and
37   * {@link LifeCycle life cycle} events to a delegate. This is primarily used to implement the <i>Decorator</i> pattern.
38   *
39   */
40  @ManagedObject("Handler wrapping another Handler")
41  public class HandlerWrapper extends AbstractHandlerContainer
42  {
43      protected Handler _handler;
44  
45      /* ------------------------------------------------------------ */
46      /**
47       *
48       */
49      public HandlerWrapper()
50      {
51      }
52  
53      /* ------------------------------------------------------------ */
54      /**
55       * @return Returns the handlers.
56       */
57      @ManagedAttribute(value="Wrapped Handler", readonly=true)
58      public Handler getHandler()
59      {
60          return _handler;
61      }
62  
63      /* ------------------------------------------------------------ */
64      /**
65       * @return Returns the handlers.
66       */
67      @Override
68      public Handler[] getHandlers()
69      {
70          if (_handler==null)
71              return new Handler[0];
72          return new Handler[] {_handler};
73      }
74  
75      /* ------------------------------------------------------------ */
76      /**
77       * @param handler Set the {@link Handler} which should be wrapped.
78       */
79      public void setHandler(Handler handler)
80      {
81          if (isStarted())
82              throw new IllegalStateException(STARTED);
83  
84          if (handler!=null)
85              handler.setServer(getServer());
86          
87          updateBean(_handler,handler);
88          _handler=handler;
89      }
90  
91      /* ------------------------------------------------------------ */
92      @Override
93      public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
94      {
95          if (_handler!=null && isStarted())
96          {
97              _handler.handle(target,baseRequest, request, response);
98          }
99      }
100 
101 
102     /* ------------------------------------------------------------ */
103     @Override
104     public void setServer(Server server)
105     {
106         if (server==getServer())
107             return;
108         
109         if (isStarted())
110             throw new IllegalStateException(STARTED);
111 
112         super.setServer(server);
113         Handler h=getHandler();
114         if (h!=null)
115             h.setServer(server);
116     }
117 
118 
119     /* ------------------------------------------------------------ */
120     @Override
121     protected void expandChildren(List<Handler> list, Class<?> byClass)
122     {
123         expandHandler(_handler,list,byClass);
124     }
125 
126     /* ------------------------------------------------------------ */
127     @Override
128     public void destroy()
129     {
130         if (!isStopped())
131             throw new IllegalStateException("!STOPPED");
132         Handler child=getHandler();
133         if (child!=null)
134         {
135             setHandler(null);
136             child.destroy();
137         }
138         super.destroy();
139     }
140 
141 }