]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/handler/HandlerWrapper.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / server / handler / HandlerWrapper.java
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 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         Handler old=_handler;
88         _handler=handler;
89         updateBean(old,_handler);
90     }
91
92     /* ------------------------------------------------------------ */
93     @Override
94     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
95     {
96         if (_handler!=null && isStarted())
97         {
98             _handler.handle(target,baseRequest, request, response);
99         }
100     }
101
102
103     /* ------------------------------------------------------------ */
104     @Override
105     public void setServer(Server server)
106     {
107         if (server==getServer())
108             return;
109         
110         if (isStarted())
111             throw new IllegalStateException(STARTED);
112
113         super.setServer(server);
114         Handler h=getHandler();
115         if (h!=null)
116             h.setServer(server);
117     }
118
119
120     /* ------------------------------------------------------------ */
121     @Override
122     protected void expandChildren(List<Handler> list, Class<?> byClass)
123     {
124         expandHandler(_handler,list,byClass);
125     }
126
127     /* ------------------------------------------------------------ */
128     @Override
129     public void destroy()
130     {
131         if (!isStopped())
132             throw new IllegalStateException("!STOPPED");
133         Handler child=getHandler();
134         if (child!=null)
135         {
136             setHandler(null);
137             child.destroy();
138         }
139         super.destroy();
140     }
141
142 }