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.
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;
33 import org.eclipse.jetty.util.component.LifeCycle;
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.
40 @ManagedObject("Handler wrapping another Handler")
41 public class HandlerWrapper extends AbstractHandlerContainer
43 protected Handler _handler;
45 /* ------------------------------------------------------------ */
49 public HandlerWrapper()
53 /* ------------------------------------------------------------ */
55 * @return Returns the handlers.
57 @ManagedAttribute(value="Wrapped Handler", readonly=true)
58 public Handler getHandler()
63 /* ------------------------------------------------------------ */
65 * @return Returns the handlers.
68 public Handler[] getHandlers()
71 return new Handler[0];
72 return new Handler[] {_handler};
75 /* ------------------------------------------------------------ */
77 * @param handler Set the {@link Handler} which should be wrapped.
79 public void setHandler(Handler handler)
82 throw new IllegalStateException(STARTED);
85 handler.setServer(getServer());
89 updateBean(old,_handler);
92 /* ------------------------------------------------------------ */
94 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
96 if (_handler!=null && isStarted())
98 _handler.handle(target,baseRequest, request, response);
103 /* ------------------------------------------------------------ */
105 public void setServer(Server server)
107 if (server==getServer())
111 throw new IllegalStateException(STARTED);
113 super.setServer(server);
114 Handler h=getHandler();
120 /* ------------------------------------------------------------ */
122 protected void expandChildren(List<Handler> list, Class<?> byClass)
124 expandHandler(_handler,list,byClass);
127 /* ------------------------------------------------------------ */
129 public void destroy()
132 throw new IllegalStateException("!STOPPED");
133 Handler child=getHandler();