%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.jetty.HttpContextTag |
|
|
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,v 1.3 2002/07/14 12:38:22 dion Exp $ |
|
38 | */ |
|
39 | 1 | public class HttpContextTag extends TagSupport { |
40 | ||
41 | /** parameter path with default*/ |
|
42 | 10 | private String _contextPath = JettyHttpServerTag.DEFAULT_CONTEXT_PATH; |
43 | ||
44 | /** parameter resourceBase, with default */ |
|
45 | 10 | 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 | 10 | public HttpContextTag() { |
55 | // create an actual context for this tag |
|
56 | 10 | _context = new HttpContext(); |
57 | 10 | } |
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 | 8 | JettyHttpServerTag httpserver = (JettyHttpServerTag) findAncestorWithClass( |
69 | JettyHttpServerTag.class); |
|
70 | 8 | if ( httpserver == null ) { |
71 | 0 | throw new JellyTagException( "<httpContext> tag must be enclosed inside a <server> tag" ); |
72 | } |
|
73 | ||
74 | // allow nested tags first, e.g body |
|
75 | 8 | invokeBody(xmlOutput); |
76 | ||
77 | 8 | _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 | 8 | URL baseResourceURL = getContext().getResource(getResourceBase()); |
83 | 8 | _context.setBaseResource(Resource.newResource(baseResourceURL)); |
84 | 8 | } |
85 | catch (MalformedURLException e) { |
|
86 | 0 | throw new JellyTagException(e); |
87 | } |
|
88 | catch (IOException e) { |
|
89 | 0 | throw new JellyTagException(e); |
90 | } |
|
91 | ||
92 | 8 | if (null != getRealmName()) { |
93 | 1 | _context.setRealmName(getRealmName()); |
94 | } |
|
95 | 8 | httpserver.addContext(_context); |
96 | ||
97 | 8 | } |
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 | 9 | _context.addHandler(httHandler); |
106 | 9 | } |
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 | 2 | _context.addSecurityConstraint(pathSpec, sc); |
117 | 2 | } |
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 | 1 | _context.setAuthenticator(authenticator); |
127 | 1 | } |
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 | 8 | 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 | 8 | _contextPath = contextPath; |
148 | 8 | } |
149 | ||
150 | /** |
|
151 | * Getter for property resourceBase. |
|
152 | * |
|
153 | * @return value of property resourceBase. |
|
154 | */ |
|
155 | public String getResourceBase() { |
|
156 | 8 | 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 | 8 | _resourceBase = resourceBase; |
166 | 8 | } |
167 | ||
168 | /** |
|
169 | * Getter for property realm name. |
|
170 | * |
|
171 | * @return value of property realm name. |
|
172 | */ |
|
173 | public String getRealmName() { |
|
174 | 9 | 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 | 2 | _realmName = realmName; |
184 | 2 | } |
185 | ||
186 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |