View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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  
34  /* ------------------------------------------------------------ */
35  /** A <code>HandlerWrapper</code> acts as a {@link Handler} but delegates the {@link Handler#handle handle} method and
36   * {@link LifeCycle life cycle} events to a delegate. This is primarily used to implement the <i>Decorator</i> pattern.
37   *
38   */
39  @ManagedObject("Handler wrapping another Handler")
40  public class HandlerWrapper extends AbstractHandlerContainer
41  {
42      protected Handler _handler;
43  
44      /* ------------------------------------------------------------ */
45      /**
46       *
47       */
48      public HandlerWrapper()
49      {
50      }
51  
52      /* ------------------------------------------------------------ */
53      /**
54       * @return Returns the handlers.
55       */
56      @ManagedAttribute(value="Wrapped Handler", readonly=true)
57      public Handler getHandler()
58      {
59          return _handler;
60      }
61  
62      /* ------------------------------------------------------------ */
63      /**
64       * @return Returns the handlers.
65       */
66      @Override
67      public Handler[] getHandlers()
68      {
69          if (_handler==null)
70              return new Handler[0];
71          return new Handler[] {_handler};
72      }
73  
74      /* ------------------------------------------------------------ */
75      /**
76       * @param handler Set the {@link Handler} which should be wrapped.
77       */
78      public void setHandler(Handler handler)
79      {
80          if (isStarted())
81              throw new IllegalStateException(STARTED);
82  
83          if (handler!=null)
84              handler.setServer(getServer());
85          
86          Handler old=_handler;
87          _handler=handler;
88          updateBean(old,_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 }