View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.tags.jetty;
18  
19  import org.apache.commons.jelly.JellyTagException;
20  import org.apache.commons.jelly.TagSupport;
21  import org.apache.commons.jelly.XMLOutput;
22  
23  import org.mortbay.http.HttpContext;
24  import org.mortbay.http.HttpHandler;
25  import org.mortbay.http.SecurityConstraint;
26  import org.mortbay.http.SecurityConstraint.Authenticator;
27  import org.mortbay.util.Resource;
28  
29  import java.io.IOException;
30  import java.net.URL;
31  import java.net.MalformedURLException;
32  
33  /***
34   * Declare a context for a Jetty http server
35   *
36   * @author  rtl
37   * @version $Id: HttpContextTag.java 155420 2005-02-26 13:06:03Z dirkv $
38   */
39  public class HttpContextTag extends TagSupport {
40  
41      /*** parameter path with default*/
42      private String _contextPath = JettyHttpServerTag.DEFAULT_CONTEXT_PATH;
43  
44      /*** parameter resourceBase, with default */
45      private String _resourceBase = JettyHttpServerTag.DEFAULT_RESOURCE_BASE;
46  
47      /*** parameter realmName*/
48      private String _realmName;
49  
50      /*** the actual context this tag refers to */
51      private HttpContext _context;
52  
53      /*** Creates a new instance of HttpContextTag */
54      public HttpContextTag() {
55          // create an actual context for this tag
56          _context = new HttpContext();
57      }
58  
59      /***
60       * Perform the tag functionality. In this case, setup the context path
61       * and resource base before adding the context to the parent server
62       *
63       * @param xmlOutput where to send output
64       * @throws Exception when an error occurs
65       */
66      public void doTag(XMLOutput xmlOutput) throws JellyTagException {
67  
68          JettyHttpServerTag httpserver = (JettyHttpServerTag) findAncestorWithClass(
69              JettyHttpServerTag.class);
70          if ( httpserver == null ) {
71              throw new JellyTagException( "<httpContext> tag must be enclosed inside a <server> tag" );
72          }
73  
74          // allow nested tags first, e.g body
75          invokeBody(xmlOutput);
76  
77          _context.setContextPath(getContextPath());
78  
79          // convert the resource string to a URL
80          // (this makes URL's relative to the location of the script
81          try {
82              URL baseResourceURL = getContext().getResource(getResourceBase());
83              _context.setBaseResource(Resource.newResource(baseResourceURL));
84          }
85          catch (MalformedURLException e) {
86              throw new JellyTagException(e);
87          }
88          catch (IOException e) {
89              throw new JellyTagException(e);
90          }
91  
92          if (null != getRealmName()) {
93              _context.setRealmName(getRealmName());
94          }
95          httpserver.addContext(_context);
96  
97      }
98  
99      /***
100      * Add an http handler to the context instance
101      *
102      * @param handler the handler to add
103      */
104     public void addHandler(HttpHandler httHandler) {
105         _context.addHandler(httHandler);
106     }
107 
108     /***
109      * Add a security constraint for the specified path specification
110      * to the context instance
111      *
112      * @param pathSpec the path specification for the security constraint
113      * @param sc the security constraint to add
114      */
115     public void addSecurityConstraint(String pathSpec, SecurityConstraint sc) {
116         _context.addSecurityConstraint(pathSpec, sc);
117     }
118 
119     /***
120      * Add an authenticator to the context instance
121      *
122      * @param authenticator the authenticator to add
123      */
124     public void setAuthenticator(Authenticator authenticator)
125     {
126         _context.setAuthenticator(authenticator);
127     }
128 
129     //--------------------------------------------------------------------------
130     // Property accessors/mutators
131     //--------------------------------------------------------------------------
132     /***
133      * Getter for property context path.
134      *
135      * @return value of property context path.
136      */
137     public String getContextPath() {
138         return _contextPath;
139     }
140 
141     /***
142      * Setter for property context path.
143      *
144      * @param path New resourceBase of property context path.
145      */
146     public void setContextPath(String contextPath) {
147         _contextPath = contextPath;
148     }
149 
150     /***
151      * Getter for property resourceBase.
152      *
153      * @return value of property resourceBase.
154      */
155     public String getResourceBase() {
156         return _resourceBase;
157     }
158 
159     /***
160      * Setter for property resourceBase.
161      *
162      * @param resourceBase New value of property resourceBase.
163      */
164     public void setResourceBase(String resourceBase) {
165         _resourceBase = resourceBase;
166     }
167 
168     /***
169      * Getter for property realm name.
170      *
171      * @return value of property realm name.
172      */
173     public String getRealmName() {
174         return _realmName;
175     }
176 
177     /***
178      * Setter for property context path.
179      *
180      * @param path New resourceBase of property context path.
181      */
182     public void setRealmName(String realmName) {
183         _realmName = realmName;
184     }
185 
186 }