View Javadoc

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