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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server.handler;
21 import java.io.IOException;
22 import java.util.List;
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
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;
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.
39 @ManagedObject("Handler wrapping another Handler")
40 public class HandlerWrapper extends AbstractHandlerContainer
42 protected Handler _handler;
44 /* ------------------------------------------------------------ */
48 public HandlerWrapper()
52 /* ------------------------------------------------------------ */
54 * @return Returns the handlers.
56 @ManagedAttribute(value="Wrapped Handler", readonly=true)
57 public Handler getHandler()
62 /* ------------------------------------------------------------ */
64 * @return Returns the handlers.
67 public Handler[] getHandlers()
70 return new Handler[0];
71 return new Handler[] {_handler};
74 /* ------------------------------------------------------------ */
76 * @param handler Set the {@link Handler} which should be wrapped.
78 public void setHandler(Handler handler)
81 throw new IllegalStateException(STARTED);
84 handler.setServer(getServer());
88 updateBean(old,_handler);
91 /* ------------------------------------------------------------ */
93 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
95 if (_handler!=null && isStarted())
97 _handler.handle(target,baseRequest, request, response);
102 /* ------------------------------------------------------------ */
104 public void setServer(Server server)
106 if (server==getServer())
110 throw new IllegalStateException(STARTED);
112 super.setServer(server);
113 Handler h=getHandler();
119 /* ------------------------------------------------------------ */
121 protected void expandChildren(List<Handler> list, Class<?> byClass)
123 expandHandler(_handler,list,byClass);
126 /* ------------------------------------------------------------ */
128 public void destroy()
131 throw new IllegalStateException("!STOPPED");
132 Handler child=getHandler();