|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| WebXml.java | 72.7% | 88% | 96.4% | 84% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ========================================================================
|
|
| 3 |
*
|
|
| 4 |
* Copyright 2003-2005 The Apache Software Foundation.
|
|
| 5 |
*
|
|
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
| 7 |
* you may not use this file except in compliance with the License.
|
|
| 8 |
* You may obtain a copy of the License at
|
|
| 9 |
*
|
|
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
| 11 |
*
|
|
| 12 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 15 |
* See the License for the specific language governing permissions and
|
|
| 16 |
* limitations under the License.
|
|
| 17 |
*
|
|
| 18 |
* ========================================================================
|
|
| 19 |
*/
|
|
| 20 |
package org.apache.cactus.integration.ant.deployment.webapp;
|
|
| 21 |
|
|
| 22 |
import java.util.ArrayList;
|
|
| 23 |
import java.util.Iterator;
|
|
| 24 |
import java.util.List;
|
|
| 25 |
|
|
| 26 |
import org.w3c.dom.Document;
|
|
| 27 |
import org.w3c.dom.DocumentType;
|
|
| 28 |
import org.w3c.dom.Element;
|
|
| 29 |
import org.w3c.dom.Node;
|
|
| 30 |
import org.w3c.dom.NodeList;
|
|
| 31 |
|
|
| 32 |
/**
|
|
| 33 |
* Encapsulates the DOM representation of a web deployment descriptor
|
|
| 34 |
* <code>web.xml</code> to provide convenience methods for easy access and
|
|
| 35 |
* manipulation.
|
|
| 36 |
*
|
|
| 37 |
* @since Cactus 1.5
|
|
| 38 |
* @version $Id: WebXml.java 239137 2005-02-10 19:51:34Z vmassol $
|
|
| 39 |
*/
|
|
| 40 |
public class WebXml |
|
| 41 |
{
|
|
| 42 |
// Private Constants -------------------------------------------------------
|
|
| 43 |
|
|
| 44 |
/**
|
|
| 45 |
* Specifies the order in which the top-level elements must appear in the
|
|
| 46 |
* descriptor, according to the DTD.
|
|
| 47 |
*/
|
|
| 48 |
private static final WebXmlTag[] ELEMENT_ORDER = { |
|
| 49 |
WebXmlTag.ICON, |
|
| 50 |
WebXmlTag.DISPLAY_NAME, |
|
| 51 |
WebXmlTag.DESCRIPTION, |
|
| 52 |
WebXmlTag.DISTRIBUTABLE, |
|
| 53 |
WebXmlTag.FILTER, |
|
| 54 |
WebXmlTag.FILTER_MAPPING, |
|
| 55 |
WebXmlTag.LISTENER, |
|
| 56 |
WebXmlTag.SERVLET, |
|
| 57 |
WebXmlTag.SERVLET_MAPPING, |
|
| 58 |
WebXmlTag.SESSION_CONFIG, |
|
| 59 |
WebXmlTag.MIME_MAPPING, |
|
| 60 |
WebXmlTag.WELCOME_FILE_LIST, |
|
| 61 |
WebXmlTag.ERROR_PAGE, |
|
| 62 |
WebXmlTag.TAGLIB, |
|
| 63 |
WebXmlTag.RESOURCE_ENV_REF, |
|
| 64 |
WebXmlTag.RESOURCE_REF, |
|
| 65 |
WebXmlTag.SECURITY_CONSTRAINT, |
|
| 66 |
WebXmlTag.LOGIN_CONFIG, |
|
| 67 |
WebXmlTag.SECURITY_ROLE, |
|
| 68 |
WebXmlTag.ENV_ENTRY, |
|
| 69 |
WebXmlTag.EJB_REF, |
|
| 70 |
WebXmlTag.EJB_LOCAL_REF, |
|
| 71 |
}; |
|
| 72 |
|
|
| 73 |
// Instance Variables ------------------------------------------------------
|
|
| 74 |
|
|
| 75 |
/**
|
|
| 76 |
* The DOM representation of the deployment descriptor.
|
|
| 77 |
*/
|
|
| 78 |
private final Document document;
|
|
| 79 |
|
|
| 80 |
/**
|
|
| 81 |
* The root element of the descriptor.
|
|
| 82 |
*/
|
|
| 83 |
private final Element rootElement;
|
|
| 84 |
|
|
| 85 |
// Constructors ------------------------------------------------------------
|
|
| 86 |
|
|
| 87 |
/**
|
|
| 88 |
* Constructor.
|
|
| 89 |
*
|
|
| 90 |
* @param theDocument The DOM document representing the parsed deployment
|
|
| 91 |
* descriptor
|
|
| 92 |
*/
|
|
| 93 | 116 |
public WebXml(Document theDocument)
|
| 94 |
{
|
|
| 95 | 116 |
this.document = theDocument;
|
| 96 | 116 |
this.rootElement = theDocument.getDocumentElement();
|
| 97 |
} |
|
| 98 |
|
|
| 99 |
// Public Methods ----------------------------------------------------------
|
|
| 100 |
|
|
| 101 |
/**
|
|
| 102 |
* Returns the DOM document representing the deployment descriptor. The
|
|
| 103 |
* document will contain any modifications made through this instance.
|
|
| 104 |
*
|
|
| 105 |
* @return The document representing the deploy descriptor
|
|
| 106 |
*/
|
|
| 107 | 0 |
public final Document getDocument()
|
| 108 |
{
|
|
| 109 | 0 |
return this.document; |
| 110 |
} |
|
| 111 |
|
|
| 112 |
/**
|
|
| 113 |
* Returns the servlet API version.
|
|
| 114 |
*
|
|
| 115 |
* @return The version
|
|
| 116 |
*/
|
|
| 117 | 4 |
public final WebXmlVersion getVersion()
|
| 118 |
{
|
|
| 119 | 4 |
DocumentType docType = this.document.getDoctype();
|
| 120 | 4 |
if (docType != null) |
| 121 |
{
|
|
| 122 | 3 |
return WebXmlVersion.valueOf(docType);
|
| 123 |
} |
|
| 124 | 1 |
return null; |
| 125 |
} |
|
| 126 |
|
|
| 127 |
/**
|
|
| 128 |
* Adds a new servlet filter to the descriptor.
|
|
| 129 |
*
|
|
| 130 |
* @param theFilterName The name of the filter to add
|
|
| 131 |
* @param theFilterClass The name of the class implementing the filter
|
|
| 132 |
*/
|
|
| 133 | 0 |
public final void addFilter(String theFilterName, String theFilterClass) |
| 134 |
{
|
|
| 135 | 0 |
if (theFilterName == null) |
| 136 |
{
|
|
| 137 | 0 |
throw new NullPointerException(); |
| 138 |
} |
|
| 139 | 0 |
if (hasFilter(theFilterName))
|
| 140 |
{
|
|
| 141 | 0 |
throw new IllegalStateException("Filter '" + theFilterName |
| 142 |
+ "' already defined");
|
|
| 143 |
} |
|
| 144 | 0 |
Element filterElement = |
| 145 |
this.document.createElement(WebXmlTag.FILTER.getTagName());
|
|
| 146 | 0 |
filterElement.appendChild( |
| 147 |
createNestedText(WebXmlTag.FILTER_NAME, theFilterName)); |
|
| 148 | 0 |
filterElement.appendChild( |
| 149 |
createNestedText(WebXmlTag.FILTER_CLASS, theFilterClass)); |
|
| 150 | 0 |
addElement(WebXmlTag.FILTER, filterElement); |
| 151 |
} |
|
| 152 |
|
|
| 153 |
/**
|
|
| 154 |
* Adds a new context-param element to the descriptor.
|
|
| 155 |
*
|
|
| 156 |
* @param theContextParam The element representing the context-param
|
|
| 157 |
* definition
|
|
| 158 |
*/
|
|
| 159 | 5 |
public final void addContextParam(Element theContextParam) |
| 160 |
{
|
|
| 161 | 5 |
checkElement(theContextParam, WebXmlTag.CONTEXT_PARAM); |
| 162 |
|
|
| 163 | 5 |
String paramName = |
| 164 |
getNestedText(theContextParam, WebXmlTag.PARAM_NAME); |
|
| 165 | 5 |
if (paramName == null) |
| 166 |
{
|
|
| 167 | 0 |
throw new IllegalArgumentException( |
| 168 |
"Not a valid context-param name element");
|
|
| 169 |
} |
|
| 170 |
|
|
| 171 | 5 |
String paramValue = |
| 172 |
getNestedText(theContextParam, WebXmlTag.PARAM_VALUE); |
|
| 173 | 5 |
if (paramValue == null) |
| 174 |
{
|
|
| 175 | 0 |
throw new IllegalArgumentException( |
| 176 |
"Not a valid context-param value element");
|
|
| 177 |
} |
|
| 178 |
|
|
| 179 | 5 |
if (hasContextParam(paramName))
|
| 180 |
{
|
|
| 181 | 1 |
throw new IllegalStateException("Context param '" + paramName |
| 182 |
+ "' already defined");
|
|
| 183 |
} |
|
| 184 | 4 |
addElement(WebXmlTag.CONTEXT_PARAM, theContextParam); |
| 185 |
} |
|
| 186 |
|
|
| 187 |
/**
|
|
| 188 |
* Adds a new servlet filter to the descriptor.
|
|
| 189 |
*
|
|
| 190 |
* @param theFilter The element representing the filter definition
|
|
| 191 |
*/
|
|
| 192 | 13 |
public final void addFilter(Element theFilter) |
| 193 |
{
|
|
| 194 | 13 |
checkElement(theFilter, WebXmlTag.FILTER); |
| 195 | 13 |
String filterName = getNestedText(theFilter, WebXmlTag.FILTER_NAME); |
| 196 | 13 |
if (filterName == null) |
| 197 |
{
|
|
| 198 | 0 |
throw new IllegalArgumentException("Not a valid filter element"); |
| 199 |
} |
|
| 200 | 13 |
if (hasFilter(filterName))
|
| 201 |
{
|
|
| 202 | 1 |
throw new IllegalStateException("Filter '" + filterName |
| 203 |
+ "' already defined");
|
|
| 204 |
} |
|
| 205 | 12 |
addElement(WebXmlTag.FILTER, theFilter); |
| 206 |
} |
|
| 207 |
|
|
| 208 |
/**
|
|
| 209 |
* Adds an initialization parameter to the specified filter.
|
|
| 210 |
*
|
|
| 211 |
* @param theFilterName The name of the filter
|
|
| 212 |
* @param theParamName The name of the parameter
|
|
| 213 |
* @param theParamValue The parameter value
|
|
| 214 |
*/
|
|
| 215 | 5 |
public final void addFilterInitParam(String theFilterName, |
| 216 |
String theParamName, String theParamValue) |
|
| 217 |
{
|
|
| 218 | 5 |
Element filterElement = getFilter(theFilterName); |
| 219 | 5 |
if (filterElement == null) |
| 220 |
{
|
|
| 221 | 0 |
throw new IllegalStateException("Filter '" + theFilterName |
| 222 |
+ "' not defined");
|
|
| 223 |
} |
|
| 224 | 5 |
addInitParam(filterElement, theParamName, theParamValue); |
| 225 |
} |
|
| 226 |
|
|
| 227 |
/**
|
|
| 228 |
* Adds a filter mapping to the descriptor.
|
|
| 229 |
*
|
|
| 230 |
* @param theFilterName The name of the filter
|
|
| 231 |
* @param theUrlPattern The URL pattern the filter should be mapped to
|
|
| 232 |
*/
|
|
| 233 | 4 |
public final void addFilterMapping(String theFilterName, |
| 234 |
String theUrlPattern) |
|
| 235 |
{
|
|
| 236 | 4 |
if (!hasFilter(theFilterName))
|
| 237 |
{
|
|
| 238 | 0 |
throw new IllegalStateException("Filter '" + theFilterName |
| 239 |
+ "' not defined");
|
|
| 240 |
} |
|
| 241 | 4 |
Element filterMappingElement = |
| 242 |
this.document.createElement(WebXmlTag.FILTER_MAPPING.getTagName());
|
|
| 243 | 4 |
filterMappingElement.appendChild( |
| 244 |
createNestedText(WebXmlTag.FILTER_NAME, theFilterName)); |
|
| 245 | 4 |
filterMappingElement.appendChild( |
| 246 |
createNestedText(WebXmlTag.URL_PATTERN, theUrlPattern)); |
|
| 247 | 4 |
addElement(WebXmlTag.FILTER_MAPPING, filterMappingElement); |
| 248 |
} |
|
| 249 |
|
|
| 250 |
/**
|
|
| 251 |
* Returns the element that contains the definition of a specific servlet
|
|
| 252 |
* filter, or <code>null</code> if a filter of the specified name is not
|
|
| 253 |
* defined in the descriptor.
|
|
| 254 |
*
|
|
| 255 |
* @param theFilterName The name of the servlet filter
|
|
| 256 |
* @return The DOM element representing the filter definition
|
|
| 257 |
*/
|
|
| 258 | 65 |
public final Element getFilter(String theFilterName)
|
| 259 |
{
|
|
| 260 | 65 |
if (theFilterName == null) |
| 261 |
{
|
|
| 262 | 1 |
throw new NullPointerException(); |
| 263 |
} |
|
| 264 | 64 |
Iterator filterElements = getElements(WebXmlTag.FILTER); |
| 265 | 64 |
while (filterElements.hasNext())
|
| 266 |
{
|
|
| 267 | 68 |
Element filterElement = (Element) filterElements.next(); |
| 268 | 68 |
if (theFilterName.equals(getNestedText(
|
| 269 |
filterElement, WebXmlTag.FILTER_NAME))) |
|
| 270 |
{
|
|
| 271 | 41 |
return filterElement;
|
| 272 |
} |
|
| 273 |
} |
|
| 274 | 23 |
return null; |
| 275 |
} |
|
| 276 |
|
|
| 277 |
/**
|
|
| 278 |
* Returns the element that contains the definition of a specific context
|
|
| 279 |
* param, or <code>null</code> if a context param of the specified name
|
|
| 280 |
* is not defined in the descriptor.
|
|
| 281 |
*
|
|
| 282 |
* @param theParamName The context param name
|
|
| 283 |
* @return The DOM element representing the context param definition
|
|
| 284 |
*/
|
|
| 285 | 17 |
public final Element getContextParam(String theParamName)
|
| 286 |
{
|
|
| 287 | 17 |
if (theParamName == null) |
| 288 |
{
|
|
| 289 | 0 |
throw new NullPointerException(); |
| 290 |
} |
|
| 291 | 17 |
Iterator contextParamElements = getElements(WebXmlTag.CONTEXT_PARAM); |
| 292 | 17 |
while (contextParamElements.hasNext())
|
| 293 |
{
|
|
| 294 | 16 |
Element contextParamElement = (Element) contextParamElements.next(); |
| 295 | 16 |
if (theParamName.equals(getNestedText(
|
| 296 |
contextParamElement, WebXmlTag.PARAM_NAME))) |
|
| 297 |
{
|
|
| 298 | 11 |
return contextParamElement;
|
| 299 |
} |
|
| 300 |
} |
|
| 301 | 6 |
return null; |
| 302 |
} |
|
| 303 |
|
|
| 304 |
/**
|
|
| 305 |
* @param theContextParam the context param element from which to extract
|
|
| 306 |
* the name
|
|
| 307 |
* @return the name of the passed context param element
|
|
| 308 |
*/
|
|
| 309 | 3 |
public final String getContextParamName(Element theContextParam)
|
| 310 |
{
|
|
| 311 | 3 |
return getNestedText(theContextParam, WebXmlTag.PARAM_NAME);
|
| 312 |
} |
|
| 313 |
|
|
| 314 |
/**
|
|
| 315 |
* Returns a list of names of filters that are mapped to the specified
|
|
| 316 |
* class.
|
|
| 317 |
*
|
|
| 318 |
* @param theClassName The fully qualified name of the filter class
|
|
| 319 |
* @return An iterator over the names of the filters mapped to the class
|
|
| 320 |
*/
|
|
| 321 | 6 |
public final Iterator getFilterNamesForClass(String theClassName)
|
| 322 |
{
|
|
| 323 | 6 |
if (theClassName == null) |
| 324 |
{
|
|
| 325 | 0 |
throw new NullPointerException(); |
| 326 |
} |
|
| 327 | 6 |
Iterator filterElements = getElements(WebXmlTag.FILTER); |
| 328 | 6 |
List filterNames = new ArrayList();
|
| 329 | 6 |
while (filterElements.hasNext())
|
| 330 |
{
|
|
| 331 | 4 |
Element filterElement = (Element) filterElements.next(); |
| 332 | 4 |
if (theClassName.equals(getNestedText(
|
| 333 |
filterElement, WebXmlTag.FILTER_CLASS))) |
|
| 334 |
{
|
|
| 335 | 3 |
filterNames.add(getNestedText( |
| 336 |
filterElement, WebXmlTag.FILTER_NAME)); |
|
| 337 |
} |
|
| 338 |
} |
|
| 339 | 6 |
return filterNames.iterator();
|
| 340 |
} |
|
| 341 |
|
|
| 342 |
/**
|
|
| 343 |
* Returns the value of an initialization parameter of the specified filter.
|
|
| 344 |
*
|
|
| 345 |
* @param theFilterName The name of the servlet filter
|
|
| 346 |
* @param theParamName The name of the initialization parameter
|
|
| 347 |
* @return The parameter value
|
|
| 348 |
*/
|
|
| 349 | 1 |
public final String getFilterInitParam(String theFilterName,
|
| 350 |
String theParamName) |
|
| 351 |
{
|
|
| 352 | 1 |
return getInitParam(getFilter(theFilterName), theParamName);
|
| 353 |
} |
|
| 354 |
|
|
| 355 |
/**
|
|
| 356 |
* Returns the names of the initialization parameters of the specified
|
|
| 357 |
* servlet filter.
|
|
| 358 |
*
|
|
| 359 |
* @param theFilterName The name of the servlet filter of which the
|
|
| 360 |
* parameter names should be retrieved
|
|
| 361 |
* @return An iterator over the ordered list of parameter names
|
|
| 362 |
*/
|
|
| 363 | 5 |
public final Iterator getFilterInitParamNames(String theFilterName)
|
| 364 |
{
|
|
| 365 | 5 |
return getInitParamNames(getFilter(theFilterName));
|
| 366 |
} |
|
| 367 |
|
|
| 368 |
/**
|
|
| 369 |
* Returns the URL-patterns that the specified filter is mapped to in an
|
|
| 370 |
* ordered list. If there are no mappings for the specified filter, an
|
|
| 371 |
* iterator over an empty list is returned.
|
|
| 372 |
*
|
|
| 373 |
* @param theFilterName The name of the servlet filter of which the
|
|
| 374 |
* mappings should be retrieved
|
|
| 375 |
* @return An iterator over the ordered list of URL-patterns
|
|
| 376 |
*/
|
|
| 377 | 15 |
public final Iterator getFilterMappings(String theFilterName)
|
| 378 |
{
|
|
| 379 | 15 |
if (theFilterName == null) |
| 380 |
{
|
|
| 381 | 0 |
throw new NullPointerException(); |
| 382 |
} |
|
| 383 | 15 |
List filterMappings = new ArrayList();
|
| 384 | 15 |
Iterator filterMappingElements = getElements(WebXmlTag.FILTER_MAPPING); |
| 385 | 15 |
while (filterMappingElements.hasNext())
|
| 386 |
{
|
|
| 387 | 13 |
Element filterMappingElement = (Element) |
| 388 |
filterMappingElements.next(); |
|
| 389 | 13 |
if (theFilterName.equals(getNestedText(
|
| 390 |
filterMappingElement, WebXmlTag.FILTER_NAME))) |
|
| 391 |
{
|
|
| 392 | 13 |
String urlPattern = getNestedText( |
| 393 |
filterMappingElement, WebXmlTag.URL_PATTERN); |
|
| 394 | 13 |
if (urlPattern != null) |
| 395 |
{
|
|
| 396 | 13 |
filterMappings.add(urlPattern); |
| 397 |
} |
|
| 398 |
} |
|
| 399 |
} |
|
| 400 | 15 |
return filterMappings.iterator();
|
| 401 |
} |
|
| 402 |
|
|
| 403 |
/**
|
|
| 404 |
* Returns the names of all filters defined in the deployment descriptor.
|
|
| 405 |
* The names are returned as an iterator over an ordered list.
|
|
| 406 |
*
|
|
| 407 |
* @return The filter names
|
|
| 408 |
*/
|
|
| 409 | 11 |
public final Iterator getFilterNames()
|
| 410 |
{
|
|
| 411 | 11 |
List filterNames = new ArrayList();
|
| 412 | 11 |
Iterator filterElements = getElements(WebXmlTag.FILTER); |
| 413 | 11 |
while (filterElements.hasNext())
|
| 414 |
{
|
|
| 415 | 20 |
Element filterElement = (Element) filterElements.next(); |
| 416 | 20 |
String filterName = |
| 417 |
getNestedText(filterElement, WebXmlTag.FILTER_NAME); |
|
| 418 | 20 |
if (filterName != null) |
| 419 |
{
|
|
| 420 | 20 |
filterNames.add(filterName); |
| 421 |
} |
|
| 422 |
} |
|
| 423 | 11 |
return filterNames.iterator();
|
| 424 |
} |
|
| 425 |
|
|
| 426 |
/**
|
|
| 427 |
* Returns whether a context param by the specified name is defined in the
|
|
| 428 |
* deployment descriptor.
|
|
| 429 |
*
|
|
| 430 |
* @param theParamName The name of the context param
|
|
| 431 |
* @return <code>true</code> if the context param is defined,
|
|
| 432 |
* <code>false</code> otherwise
|
|
| 433 |
*/
|
|
| 434 | 15 |
public final boolean hasContextParam(String theParamName) |
| 435 |
{
|
|
| 436 | 15 |
return (getContextParam(theParamName) != null); |
| 437 |
} |
|
| 438 |
|
|
| 439 |
/**
|
|
| 440 |
* Returns whether a servlet filter by the specified name is defined in the
|
|
| 441 |
* deployment descriptor.
|
|
| 442 |
*
|
|
| 443 |
* @param theFilterName The name of the filter
|
|
| 444 |
* @return <code>true</code> if the filter is defined, <code>false</code>
|
|
| 445 |
* otherwise
|
|
| 446 |
*/
|
|
| 447 | 45 |
public final boolean hasFilter(String theFilterName) |
| 448 |
{
|
|
| 449 | 45 |
return (getFilter(theFilterName) != null); |
| 450 |
} |
|
| 451 |
|
|
| 452 |
/**
|
|
| 453 |
* Adds a mapped JSP file to the descriptor.
|
|
| 454 |
*
|
|
| 455 |
* @param theServletName The name of the servlet to add
|
|
| 456 |
* @param theJspFile The path to the JSP file relative to the root of the
|
|
| 457 |
* web application
|
|
| 458 |
*/
|
|
| 459 | 1 |
public final void addJspFile(String theServletName, String theJspFile) |
| 460 |
{
|
|
| 461 | 1 |
if (theServletName == null) |
| 462 |
{
|
|
| 463 | 0 |
throw new NullPointerException(); |
| 464 |
} |
|
| 465 | 1 |
if (hasFilter(theServletName))
|
| 466 |
{
|
|
| 467 | 0 |
throw new IllegalStateException("Servlet '" + theServletName |
| 468 |
+ "' already defined");
|
|
| 469 |
} |
|
| 470 | 1 |
Element servletElement = |
| 471 |
this.document.createElement(WebXmlTag.SERVLET.getTagName());
|
|
| 472 | 1 |
servletElement.appendChild( |
| 473 |
createNestedText(WebXmlTag.SERVLET_NAME, theServletName)); |
|
| 474 | 1 |
servletElement.appendChild( |
| 475 |
createNestedText(WebXmlTag.JSP_FILE, theJspFile)); |
|
| 476 | 1 |
addElement(WebXmlTag.SERVLET, servletElement); |
| 477 |
} |
|
| 478 |
|
|
| 479 |
/**
|
|
| 480 |
* Adds a new servlet to the descriptor.
|
|
| 481 |
*
|
|
| 482 |
* @param theServletName The name of the servlet to add
|
|
| 483 |
* @param theServletClass The name of the class implementing the servlet
|
|
| 484 |
*/
|
|
| 485 | 2 |
public final void addServlet(String theServletName, String theServletClass) |
| 486 |
{
|
|
| 487 | 2 |
if (theServletName == null) |
| 488 |
{
|
|
| 489 | 0 |
throw new NullPointerException(); |
| 490 |
} |
|
| 491 | 2 |
if (hasServlet(theServletName))
|
| 492 |
{
|
|
| 493 | 0 |
throw new IllegalStateException("Servlet '" + theServletName |
| 494 |
+ "' already defined");
|
|
| 495 |
} |
|
| 496 | 2 |
Element servletElement = |
| 497 |
this.document.createElement(WebXmlTag.SERVLET.getTagName());
|
|
| 498 | 2 |
servletElement.appendChild( |
| 499 |
createNestedText(WebXmlTag.SERVLET_NAME, theServletName)); |
|
| 500 | 2 |
servletElement.appendChild( |
| 501 |
createNestedText(WebXmlTag.SERVLET_CLASS, theServletClass)); |
|
| 502 | 2 |
addElement(WebXmlTag.SERVLET, servletElement); |
| 503 |
} |
|
| 504 |
|
|
| 505 |
/**
|
|
| 506 |
* Adds a new servlet to the descriptor.
|
|
| 507 |
*
|
|
| 508 |
* @param theServlet The element representing the servlet definition
|
|
| 509 |
*/
|
|
| 510 | 13 |
public final void addServlet(Element theServlet) |
| 511 |
{
|
|
| 512 | 13 |
checkElement(theServlet, WebXmlTag.SERVLET); |
| 513 | 13 |
String servletName = getNestedText(theServlet, WebXmlTag.SERVLET_NAME); |
| 514 | 13 |
if (servletName == null) |
| 515 |
{
|
|
| 516 | 0 |
throw new IllegalArgumentException("Not a valid servlet element"); |
| 517 |
} |
|
| 518 | 13 |
if (hasServlet(servletName))
|
| 519 |
{
|
|
| 520 | 1 |
throw new IllegalStateException("Servlet '" + servletName |
| 521 |
+ "' already defined");
|
|
| 522 |
} |
|
| 523 | 12 |
addElement(WebXmlTag.SERVLET, theServlet); |
| 524 |
} |
|
| 525 |
|
|
| 526 |
/**
|
|
| 527 |
* Adds an initialization parameter to the specified servlet.
|
|
| 528 |
*
|
|
| 529 |
* @param theServletName The name of the filter
|
|
| 530 |
* @param theParamName The name of the parameter
|
|
| 531 |
* @param theParamValue The parameter value
|
|
| 532 |
*/
|
|
| 533 | 6 |
public final void addServletInitParam(String theServletName, |
| 534 |
String theParamName, String theParamValue) |
|
| 535 |
{
|
|
| 536 | 6 |
Element servletElement = getServlet(theServletName); |
| 537 | 6 |
if (servletElement == null) |
| 538 |
{
|
|
| 539 | 0 |
throw new IllegalStateException("Servlet '" + theServletName |
| 540 |
+ "' not defined");
|
|
| 541 |
} |
|
| 542 | 6 |
addInitParam(servletElement, theParamName, theParamValue); |
| 543 |
} |
|
| 544 |
|
|
| 545 |
/**
|
|
| 546 |
* Adds a run-as declaration to the specified servlet.
|
|
| 547 |
*
|
|
| 548 |
* @param theServletName the name of the servlet to manipulate
|
|
| 549 |
* @param theRoleName the role name that the servlet should be running as
|
|
| 550 |
*/
|
|
| 551 | 1 |
public final void addServletRunAsRoleName(String theServletName, |
| 552 |
String theRoleName) |
|
| 553 |
{
|
|
| 554 | 1 |
Element servlet = getServlet(theServletName); |
| 555 | 1 |
Element runAsElement = |
| 556 |
this.document.createElement(WebXmlTag.RUN_AS.getTagName());
|
|
| 557 | 1 |
runAsElement.appendChild(createNestedText(WebXmlTag.ROLE_NAME, |
| 558 |
theRoleName)); |
|
| 559 | 1 |
servlet.appendChild(runAsElement); |
| 560 |
} |
|
| 561 |
|
|
| 562 |
/**
|
|
| 563 |
* Adds a servlet mapping to the descriptor.
|
|
| 564 |
*
|
|
| 565 |
* @param theServletName The name of the servlet
|
|
| 566 |
* @param theUrlPattern The URL pattern the servlet should be mapped to
|
|
| 567 |
*/
|
|
| 568 | 4 |
public final void addServletMapping(String theServletName, |
| 569 |
String theUrlPattern) |
|
| 570 |
{
|
|
| 571 | 4 |
if (!hasServlet(theServletName))
|
| 572 |
{
|
|
| 573 | 0 |
throw new IllegalStateException("Servlet '" + theServletName |
| 574 |
+ "' not defined");
|
|
| 575 |
} |
|
| 576 | 4 |
Element servletMappingElement = |
| 577 |
this.document.createElement(WebXmlTag.SERVLET_MAPPING.getTagName());
|
|
| 578 | 4 |
servletMappingElement.appendChild( |
| 579 |
createNestedText(WebXmlTag.SERVLET_NAME, theServletName)); |
|
| 580 | 4 |
servletMappingElement.appendChild( |
| 581 |
createNestedText(WebXmlTag.URL_PATTERN, theUrlPattern)); |
|
| 582 | 4 |
addElement(WebXmlTag.SERVLET_MAPPING, servletMappingElement); |
| 583 |
} |
|
| 584 |
|
|
| 585 |
/**
|
|
| 586 |
* Returns the element that contains the definition of a specific servlet,
|
|
| 587 |
* or <code>null</code> if a servlet of the specified name is not defined
|
|
| 588 |
* in the descriptor.
|
|
| 589 |
*
|
|
| 590 |
* @param theServletName The name of the servlet
|
|
| 591 |
* @return The DOM element representing the servlet definition
|
|
| 592 |
*/
|
|
| 593 | 81 |
public final Element getServlet(String theServletName)
|
| 594 |
{
|
|
| 595 | 81 |
if (theServletName == null) |
| 596 |
{
|
|
| 597 | 1 |
throw new NullPointerException(); |
| 598 |
} |
|
| 599 | 80 |
Iterator servletElements = getElements(WebXmlTag.SERVLET); |
| 600 | 80 |
while (servletElements.hasNext())
|
| 601 |
{
|
|
| 602 | 83 |
Element servletElement = (Element) servletElements.next(); |
| 603 | 83 |
if (theServletName.equals(getNestedText(
|
| 604 |
servletElement, WebXmlTag.SERVLET_NAME))) |
|
| 605 |
{
|
|
| 606 | 56 |
return servletElement;
|
| 607 |
} |
|
| 608 |
} |
|
| 609 | 24 |
return null; |
| 610 |
} |
|
| 611 |
|
|
| 612 |
/**
|
|
| 613 |
* Returns the value of an initialization parameter of the specified
|
|
| 614 |
* servlet.
|
|
| 615 |
*
|
|
| 616 |
* @param theServletName The name of the servlet
|
|
| 617 |
* @param theParamName The name of the initialization parameter
|
|
| 618 |
* @return The parameter value
|
|
| 619 |
*/
|
|
| 620 | 3 |
public final String getServletInitParam(String theServletName,
|
| 621 |
String theParamName) |
|
| 622 |
{
|
|
| 623 | 3 |
return getInitParam(getServlet(theServletName), theParamName);
|
| 624 |
} |
|
| 625 |
|
|
| 626 |
/**
|
|
| 627 |
* Returns the names of the initialization parameters of the specified
|
|
| 628 |
* servlet.
|
|
| 629 |
*
|
|
| 630 |
* @param theServletName The name of the servlet of which the parameter
|
|
| 631 |
* names should be retrieved
|
|
| 632 |
* @return An iterator over the ordered list of parameter names
|
|
| 633 |
*/
|
|
| 634 | 6 |
public final Iterator getServletInitParamNames(String theServletName)
|
| 635 |
{
|
|
| 636 | 6 |
return getInitParamNames(getServlet(theServletName));
|
| 637 |
} |
|
| 638 |
|
|
| 639 |
/**
|
|
| 640 |
* Returns the role name that the servlet is running as.
|
|
| 641 |
*
|
|
| 642 |
* @param theServletName The name of the servlet of which the role name
|
|
| 643 |
* should be retrieved
|
|
| 644 |
* @return the roleName or null if non is specified
|
|
| 645 |
*/
|
|
| 646 | 5 |
public final String getServletRunAsRoleName(String theServletName)
|
| 647 |
{
|
|
| 648 | 5 |
if (theServletName == null) |
| 649 |
{
|
|
| 650 | 0 |
throw new NullPointerException(); |
| 651 |
} |
|
| 652 | 5 |
String roleName = null;
|
| 653 | 5 |
Element servlet = getServlet(theServletName); |
| 654 | 5 |
NodeList nodeList = |
| 655 |
servlet.getElementsByTagName(WebXmlTag.RUN_AS.getTagName()); |
|
| 656 | 5 |
if (nodeList != null) |
| 657 |
{
|
|
| 658 | 5 |
Element e = (Element) nodeList.item(0); |
| 659 | 5 |
if (e != null) |
| 660 |
{
|
|
| 661 | 2 |
roleName = getNestedText(e, WebXmlTag.ROLE_NAME); |
| 662 |
} |
|
| 663 |
} |
|
| 664 |
|
|
| 665 | 5 |
return roleName;
|
| 666 |
} |
|
| 667 |
|
|
| 668 |
/**
|
|
| 669 |
* Returns the URL-patterns that the specified servlet is mapped to in an
|
|
| 670 |
* ordered list. If there are no mappings for the specified servlet, an
|
|
| 671 |
* iterator over an empty list is returned.
|
|
| 672 |
*
|
|
| 673 |
* @param theServletName The name of the servlet of which the mappings
|
|
| 674 |
* should be retrieved
|
|
| 675 |
* @return An iterator over the ordered list of URL-patterns
|
|
| 676 |
*/
|
|
| 677 | 20 |
public final Iterator getServletMappings(String theServletName)
|
| 678 |
{
|
|
| 679 | 20 |
if (theServletName == null) |
| 680 |
{
|
|
| 681 | 0 |
throw new NullPointerException(); |
| 682 |
} |
|
| 683 | 20 |
List servletMappings = new ArrayList();
|
| 684 | 20 |
Iterator servletMappingElements = |
| 685 |
getElements(WebXmlTag.SERVLET_MAPPING); |
|
| 686 | 20 |
while (servletMappingElements.hasNext())
|
| 687 |
{
|
|
| 688 | 17 |
Element servletMappingElement = (Element) |
| 689 |
servletMappingElements.next(); |
|
| 690 | 17 |
if (theServletName.equals(getNestedText(
|
| 691 |
servletMappingElement, WebXmlTag.SERVLET_NAME))) |
|
| 692 |
{
|
|
| 693 | 17 |
String urlPattern = getNestedText( |
| 694 |
servletMappingElement, WebXmlTag.URL_PATTERN); |
|
| 695 | 17 |
if (urlPattern != null) |
| 696 |
{
|
|
| 697 | 17 |
servletMappings.add(urlPattern); |
| 698 |
} |
|
| 699 |
} |
|
| 700 |
} |
|
| 701 | 20 |
return servletMappings.iterator();
|
| 702 |
} |
|
| 703 |
|
|
| 704 |
/**
|
|
| 705 |
* Returns the names of all servlets defined in the deployment descriptor.
|
|
| 706 |
* The names are returned as an iterator over an ordered list.
|
|
| 707 |
*
|
|
| 708 |
* @return The servlet names
|
|
| 709 |
*/
|
|
| 710 | 12 |
public final Iterator getServletNames()
|
| 711 |
{
|
|
| 712 | 12 |
List servletNames = new ArrayList();
|
| 713 | 12 |
Iterator servletElements = getElements(WebXmlTag.SERVLET); |
| 714 | 12 |
while (servletElements.hasNext())
|
| 715 |
{
|
|
| 716 | 21 |
Element servletElement = (Element) servletElements.next(); |
| 717 | 21 |
String servletName = |
| 718 |
getNestedText(servletElement, WebXmlTag.SERVLET_NAME); |
|
| 719 | 21 |
if (servletName != null) |
| 720 |
{
|
|
| 721 | 21 |
servletNames.add(servletName); |
| 722 |
} |
|
| 723 |
} |
|
| 724 | 12 |
return servletNames.iterator();
|
| 725 |
} |
|
| 726 |
|
|
| 727 |
/**
|
|
| 728 |
* Returns a list of names of servlets that are mapped to the specified
|
|
| 729 |
* class.
|
|
| 730 |
*
|
|
| 731 |
* @param theClassName The fully qualified name of the servlet class
|
|
| 732 |
* @return An iterator over the names of the servlets mapped to the class
|
|
| 733 |
*/
|
|
| 734 | 9 |
public final Iterator getServletNamesForClass(String theClassName)
|
| 735 |
{
|
|
| 736 | 9 |
if (theClassName == null) |
| 737 |
{
|
|
| 738 | 0 |
throw new NullPointerException(); |
| 739 |
} |
|
| 740 | 9 |
Iterator servletElements = getElements(WebXmlTag.SERVLET); |
| 741 | 9 |
List servletNames = new ArrayList();
|
| 742 | 9 |
while (servletElements.hasNext())
|
| 743 |
{
|
|
| 744 | 9 |
Element servletElement = (Element) servletElements.next(); |
| 745 | 9 |
if (theClassName.equals(getNestedText(
|
| 746 |
servletElement, WebXmlTag.SERVLET_CLASS))) |
|
| 747 |
{
|
|
| 748 | 8 |
servletNames.add(getNestedText( |
| 749 |
servletElement, WebXmlTag.SERVLET_NAME)); |
|
| 750 |
} |
|
| 751 |
} |
|
| 752 | 9 |
return servletNames.iterator();
|
| 753 |
} |
|
| 754 |
|
|
| 755 |
/**
|
|
| 756 |
* Returns a list of names of servlets that are mapped to the specified
|
|
| 757 |
* JSP file.
|
|
| 758 |
*
|
|
| 759 |
* @param theJspFile The path to the JSP file, relative to the root of the
|
|
| 760 |
* web-application
|
|
| 761 |
* @return An iterator over the names of the servlets mapped to the JSP file
|
|
| 762 |
*/
|
|
| 763 | 2 |
public final Iterator getServletNamesForJspFile(String theJspFile)
|
| 764 |
{
|
|
| 765 | 2 |
if (theJspFile == null) |
| 766 |
{
|
|
| 767 | 0 |
throw new NullPointerException(); |
| 768 |
} |
|
| 769 | 2 |
Iterator servletElements = getElements(WebXmlTag.SERVLET); |
| 770 | 2 |
List servletNames = new ArrayList();
|
| 771 | 2 |
while (servletElements.hasNext())
|
| 772 |
{
|
|
| 773 | 4 |
Element servletElement = (Element) servletElements.next(); |
| 774 | 4 |
if (theJspFile.equals(getNestedText(
|
| 775 |
servletElement, WebXmlTag.JSP_FILE))) |
|
| 776 |
{
|
|
| 777 | 2 |
servletNames.add(getNestedText( |
| 778 |
servletElement, WebXmlTag.SERVLET_NAME)); |
|
| 779 |
} |
|
| 780 |
} |
|
| 781 | 2 |
return servletNames.iterator();
|
| 782 |
} |
|
| 783 |
|
|
| 784 |
/**
|
|
| 785 |
* Returns whether a servlet by the specified name is defined in the
|
|
| 786 |
* deployment descriptor.
|
|
| 787 |
*
|
|
| 788 |
* @param theServletName The name of the servlet
|
|
| 789 |
* @return <code>true</code> if the servlet is defined, <code>false</code>
|
|
| 790 |
* otherwise
|
|
| 791 |
*/
|
|
| 792 | 50 |
public final boolean hasServlet(String theServletName) |
| 793 |
{
|
|
| 794 | 50 |
return (getServlet(theServletName) != null); |
| 795 |
} |
|
| 796 |
|
|
| 797 |
/**
|
|
| 798 |
* Creates and adds a security-constraint to the descriptor.
|
|
| 799 |
*
|
|
| 800 |
* @param theWebResourceName The name of the web resource collection to
|
|
| 801 |
* protect
|
|
| 802 |
* @param theUrlPattern The URL pattern to apply the constraint to
|
|
| 803 |
* @param theRoles The list of authorized roles
|
|
| 804 |
*/
|
|
| 805 | 2 |
public final void addSecurityConstraint(String theWebResourceName, |
| 806 |
String theUrlPattern, List theRoles) |
|
| 807 |
{
|
|
| 808 | 2 |
if ((theWebResourceName == null) || (theUrlPattern == null) |
| 809 |
|| (theRoles == null))
|
|
| 810 |
{
|
|
| 811 | 0 |
throw new NullPointerException(); |
| 812 |
} |
|
| 813 | 2 |
if (hasSecurityConstraint(theUrlPattern))
|
| 814 |
{
|
|
| 815 | 0 |
throw new IllegalStateException("Security constraint for URL " |
| 816 |
+ "pattern " + theUrlPattern + " already defined"); |
|
| 817 |
} |
|
| 818 | 2 |
Element securityConstraintElement = |
| 819 |
this.document.createElement(
|
|
| 820 |
WebXmlTag.SECURITY_CONSTRAINT.getTagName()); |
|
| 821 | 2 |
Element webResourceCollectionElement = |
| 822 |
this.document.createElement(
|
|
| 823 |
WebXmlTag.WEB_RESOURCE_COLLECTION.getTagName()); |
|
| 824 | 2 |
webResourceCollectionElement.appendChild( |
| 825 |
createNestedText(WebXmlTag.WEB_RESOURCE_NAME, theWebResourceName)); |
|
| 826 | 2 |
webResourceCollectionElement.appendChild( |
| 827 |
createNestedText(WebXmlTag.URL_PATTERN, theUrlPattern)); |
|
| 828 | 2 |
securityConstraintElement.appendChild(webResourceCollectionElement); |
| 829 | 2 |
Element authConstraintElement = |
| 830 |
this.document.createElement(WebXmlTag.AUTH_CONSTRAINT.getTagName());
|
|
| 831 | 2 |
for (Iterator i = theRoles.iterator(); i.hasNext();)
|
| 832 |
{
|
|
| 833 | 2 |
authConstraintElement.appendChild( |
| 834 |
createNestedText(WebXmlTag.ROLE_NAME, (String) i.next())); |
|
| 835 |
} |
|
| 836 | 2 |
securityConstraintElement.appendChild(authConstraintElement); |
| 837 | 2 |
addElement(WebXmlTag.SECURITY_CONSTRAINT, securityConstraintElement); |
| 838 |
} |
|
| 839 |
|
|
| 840 |
/**
|
|
| 841 |
* Returns the element that contains the security constraint defined for the
|
|
| 842 |
* specified URL pattern.
|
|
| 843 |
*
|
|
| 844 |
* @param theUrlPattern The URL pattern
|
|
| 845 |
* @return The DOM element representing the security constraint
|
|
| 846 |
*/
|
|
| 847 | 11 |
public final Element getSecurityConstraint(String theUrlPattern)
|
| 848 |
{
|
|
| 849 | 11 |
if (theUrlPattern == null) |
| 850 |
{
|
|
| 851 | 0 |
throw new NullPointerException(); |
| 852 |
} |
|
| 853 | 11 |
Iterator securityConstraintElements = |
| 854 |
getElements(WebXmlTag.SECURITY_CONSTRAINT); |
|
| 855 | 11 |
while (securityConstraintElements.hasNext())
|
| 856 |
{
|
|
| 857 | 11 |
Element securityConstraintElement = (Element) |
| 858 |
securityConstraintElements.next(); |
|
| 859 | 11 |
Iterator webResourceCollectionElements = |
| 860 |
getNestedElements(securityConstraintElement, |
|
| 861 |
WebXmlTag.WEB_RESOURCE_COLLECTION); |
|
| 862 | 11 |
if (webResourceCollectionElements.hasNext())
|
| 863 |
{
|
|
| 864 | 11 |
Element webResourceCollectionElement = (Element) |
| 865 |
webResourceCollectionElements.next(); |
|
| 866 | 11 |
if (theUrlPattern.equals(getNestedText(
|
| 867 |
webResourceCollectionElement, WebXmlTag.URL_PATTERN))) |
|
| 868 |
{
|
|
| 869 | 8 |
return securityConstraintElement;
|
| 870 |
} |
|
| 871 |
} |
|
| 872 |
} |
|
| 873 | 3 |
return null; |
| 874 |
} |
|
| 875 |
|
|
| 876 |
/**
|
|
| 877 |
* Returns whether a security constraint has been mapped to the specified
|
|
| 878 |
* URL pattern.
|
|
| 879 |
*
|
|
| 880 |
* @param theUrlPattern The URL patterm
|
|
| 881 |
* @return <code>true</code> if a security constraint is defined,
|
|
| 882 |
* <code>false</code> otherwise
|
|
| 883 |
*/
|
|
| 884 | 9 |
public final boolean hasSecurityConstraint(String theUrlPattern) |
| 885 |
{
|
|
| 886 | 9 |
return (getSecurityConstraint(theUrlPattern) != null); |
| 887 |
} |
|
| 888 |
|
|
| 889 |
/**
|
|
| 890 |
* Returns whether the descriptor has a login configuration.
|
|
| 891 |
*
|
|
| 892 |
* @return <code>true</code> if a login config is defined,
|
|
| 893 |
* <code>false</code> otherwise
|
|
| 894 |
*/
|
|
| 895 | 4 |
public final boolean hasLoginConfig() |
| 896 |
{
|
|
| 897 | 4 |
return (getLoginConfig() != null); |
| 898 |
} |
|
| 899 |
|
|
| 900 |
/**
|
|
| 901 |
* Returns whether the descriptor has a login configuration.
|
|
| 902 |
*
|
|
| 903 |
* @return <code>true</code> if a login config is defined,
|
|
| 904 |
* <code>false</code> otherwise
|
|
| 905 |
*/
|
|
| 906 | 7 |
public final Element getLoginConfig()
|
| 907 |
{
|
|
| 908 | 7 |
Iterator loginConfigElements = getElements(WebXmlTag.LOGIN_CONFIG); |
| 909 | 7 |
if (loginConfigElements.hasNext())
|
| 910 |
{
|
|
| 911 | 6 |
return (Element) loginConfigElements.next();
|
| 912 |
} |
|
| 913 | 1 |
return null; |
| 914 |
} |
|
| 915 |
|
|
| 916 |
/**
|
|
| 917 |
* Returns the authorization method defined by the login configuration.
|
|
| 918 |
*
|
|
| 919 |
* @return The authorization method
|
|
| 920 |
*/
|
|
| 921 | 3 |
public final String getLoginConfigAuthMethod()
|
| 922 |
{
|
|
| 923 | 3 |
return getNestedText(getLoginConfig(), WebXmlTag.AUTH_METHOD);
|
| 924 |
} |
|
| 925 |
|
|
| 926 |
/**
|
|
| 927 |
* Sets the login configuration.
|
|
| 928 |
*
|
|
| 929 |
* @param theAuthMethod The authentication method (for example, BASIC)
|
|
| 930 |
* @param theRealmName The name of the realm
|
|
| 931 |
*/
|
|
| 932 | 2 |
public final void setLoginConfig(String theAuthMethod, String theRealmName) |
| 933 |
{
|
|
| 934 | 2 |
if ((theRealmName == null) || (theAuthMethod == null)) |
| 935 |
{
|
|
| 936 | 0 |
throw new NullPointerException(); |
| 937 |
} |
|
| 938 | 2 |
Element loginConfigElement = |
| 939 |
document.createElement(WebXmlTag.LOGIN_CONFIG.getTagName()); |
|
| 940 | 2 |
loginConfigElement.appendChild( |
| 941 |
createNestedText(WebXmlTag.AUTH_METHOD, theAuthMethod)); |
|
| 942 | 2 |
loginConfigElement.appendChild( |
| 943 |
createNestedText(WebXmlTag.REALM_NAME, theRealmName)); |
|
| 944 | 2 |
replaceElement(WebXmlTag.LOGIN_CONFIG, loginConfigElement); |
| 945 |
} |
|
| 946 |
|
|
| 947 |
/**
|
|
| 948 |
* Adds a new security role to the descriptor.
|
|
| 949 |
*
|
|
| 950 |
* @param theRoleName The name of the role to add
|
|
| 951 |
*/
|
|
| 952 | 1 |
public final void addSecurityRole(String theRoleName) |
| 953 |
{
|
|
| 954 | 1 |
if (theRoleName == null) |
| 955 |
{
|
|
| 956 | 0 |
throw new NullPointerException(); |
| 957 |
} |
|
| 958 | 1 |
if (hasSecurityRole(theRoleName))
|
| 959 |
{
|
|
| 960 | 0 |
throw new IllegalStateException("Security role '" + theRoleName |
| 961 |
+ "' already defined");
|
|
| 962 |
} |
|
| 963 | 1 |
Element securityRoleElement = |
| 964 |
this.document.createElement(WebXmlTag.SECURITY_ROLE.getTagName());
|
|
| 965 | 1 |
securityRoleElement.appendChild( |
| 966 |
createNestedText(WebXmlTag.ROLE_NAME, theRoleName)); |
|
| 967 | 1 |
addElement(WebXmlTag.SECURITY_ROLE, securityRoleElement); |
| 968 |
} |
|
| 969 |
|
|
| 970 |
/**
|
|
| 971 |
* Returns the element that contains the specified security role, or
|
|
| 972 |
* <code>null</code> if the role is not defined in the descriptor.
|
|
| 973 |
*
|
|
| 974 |
* @param theRoleName The name of the role
|
|
| 975 |
* @return The DOM element representing the security role
|
|
| 976 |
*/
|
|
| 977 | 12 |
public final Element getSecurityRole(String theRoleName)
|
| 978 |
{
|
|
| 979 | 12 |
if (theRoleName == null) |
| 980 |
{
|
|
| 981 | 0 |
throw new NullPointerException(); |
| 982 |
} |
|
| 983 | 12 |
Iterator securityRoleElements = getElements(WebXmlTag.SECURITY_ROLE); |
| 984 | 12 |
while (securityRoleElements.hasNext())
|
| 985 |
{
|
|
| 986 | 15 |
Element securityRoleElement = (Element) securityRoleElements.next(); |
| 987 | 15 |
if (theRoleName.equals(getNestedText(
|
| 988 |
securityRoleElement, WebXmlTag.ROLE_NAME))) |
|
| 989 |
{
|
|
| 990 | 9 |
return securityRoleElement;
|
| 991 |
} |
|
| 992 |
} |
|
| 993 | 3 |
return null; |
| 994 |
} |
|
| 995 |
|
|
| 996 |
/**
|
|
| 997 |
* Returns a list of the security role names defined in the deployment
|
|
| 998 |
* descriptor
|
|
| 999 |
*
|
|
| 1000 |
* @return An iterator over the list of security role names, or an empty
|
|
| 1001 |
* iterator if no security roles are defined in the descriptor
|
|
| 1002 |
*/
|
|
| 1003 | 7 |
public final Iterator getSecurityRoleNames()
|
| 1004 |
{
|
|
| 1005 | 7 |
List securityRoleNames = new ArrayList();
|
| 1006 | 7 |
Iterator securityRoleElements = getElements(WebXmlTag.SECURITY_ROLE); |
| 1007 | 7 |
while (securityRoleElements.hasNext())
|
| 1008 |
{
|
|
| 1009 | 8 |
Element securityRoleElement = (Element) securityRoleElements.next(); |
| 1010 | 8 |
String securityRoleName = |
| 1011 |
getNestedText(securityRoleElement, WebXmlTag.ROLE_NAME); |
|
| 1012 | 8 |
if (securityRoleName != null) |
| 1013 |
{
|
|
| 1014 | 8 |
securityRoleNames.add(securityRoleName); |
| 1015 |
} |
|
| 1016 |
} |
|
| 1017 | 7 |
return securityRoleNames.iterator();
|
| 1018 |
} |
|
| 1019 |
|
|
| 1020 |
/**
|
|
| 1021 |
* Returns whether a specific security role has been defined.
|
|
| 1022 |
*
|
|
| 1023 |
* @param theRoleName The name of the role
|
|
| 1024 |
* @return <code>true</code> if the security role is defined,
|
|
| 1025 |
* <code>false</code> otherwise
|
|
| 1026 |
*/
|
|
| 1027 | 8 |
public final boolean hasSecurityRole(String theRoleName) |
| 1028 |
{
|
|
| 1029 | 8 |
return (getSecurityRole(theRoleName) != null); |
| 1030 |
} |
|
| 1031 |
|
|
| 1032 |
/**
|
|
| 1033 |
* Returns an iterator over the elements that match the specified tag.
|
|
| 1034 |
*
|
|
| 1035 |
* @param theTag The descriptor tag of which the elements should be
|
|
| 1036 |
* returned
|
|
| 1037 |
* @return An iterator over the elements matching the tag, in the order
|
|
| 1038 |
* they occur in the descriptor
|
|
| 1039 |
*/
|
|
| 1040 | 285 |
public final Iterator getElements(WebXmlTag theTag)
|
| 1041 |
{
|
|
| 1042 | 285 |
List elements = new ArrayList();
|
| 1043 | 285 |
NodeList nodeList = |
| 1044 |
this.rootElement.getElementsByTagName(theTag.getTagName());
|
|
| 1045 | 285 |
for (int i = 0; i < nodeList.getLength(); i++) |
| 1046 |
{
|
|
| 1047 | 332 |
elements.add(nodeList.item(i)); |
| 1048 |
} |
|
| 1049 | 285 |
return elements.iterator();
|
| 1050 |
} |
|
| 1051 |
|
|
| 1052 |
/**
|
|
| 1053 |
* Adds an element of the specified tag to the descriptor.
|
|
| 1054 |
*
|
|
| 1055 |
* @param theTag The descriptor tag
|
|
| 1056 |
* @param theElement The element to add
|
|
| 1057 |
*/
|
|
| 1058 | 45 |
public final void addElement(WebXmlTag theTag, Element theElement) |
| 1059 |
{
|
|
| 1060 | 45 |
checkElement(theElement, theTag); |
| 1061 | 45 |
if (!theTag.isMultipleAllowed() && getElements(theTag).hasNext())
|
| 1062 |
{
|
|
| 1063 | 0 |
throw new IllegalStateException("The tag '" + theTag |
| 1064 |
+ "' may not occur more than once in the descriptor");
|
|
| 1065 |
} |
|
| 1066 | 45 |
Node importedNode = this.document.importNode(theElement, true); |
| 1067 | 45 |
Node refNode = getInsertionPointFor(theTag); |
| 1068 | 45 |
this.rootElement.insertBefore(importedNode, refNode);
|
| 1069 |
} |
|
| 1070 |
|
|
| 1071 |
/**
|
|
| 1072 |
* Replaces all elements of the specified tag with the provided element.
|
|
| 1073 |
*
|
|
| 1074 |
* @param theTag The descriptor tag
|
|
| 1075 |
* @param theElement The element to replace the current elements with
|
|
| 1076 |
*/
|
|
| 1077 | 2 |
public final void replaceElement(WebXmlTag theTag, Element theElement) |
| 1078 |
{
|
|
| 1079 | 2 |
Iterator elements = getElements(theTag); |
| 1080 | 2 |
while (elements.hasNext())
|
| 1081 |
{
|
|
| 1082 | 1 |
Element element = (Element) elements.next(); |
| 1083 | 1 |
element.getParentNode().removeChild(element); |
| 1084 |
} |
|
| 1085 | 2 |
addElement(theTag, theElement); |
| 1086 |
} |
|
| 1087 |
|
|
| 1088 |
// Private Methods ---------------------------------------------------------
|
|
| 1089 |
|
|
| 1090 |
/**
|
|
| 1091 |
* Adds an initialization parameter to the specified filter or servlet.
|
|
| 1092 |
*
|
|
| 1093 |
* @param theElement The filter or servlet element to which the
|
|
| 1094 |
* initialization parameter should be added
|
|
| 1095 |
* @param theParamName The name of the parameter
|
|
| 1096 |
* @param theParamValue The parameter value
|
|
| 1097 |
*/
|
|
| 1098 | 11 |
private void addInitParam(Element theElement, String theParamName, |
| 1099 |
String theParamValue) |
|
| 1100 |
{
|
|
| 1101 | 11 |
Element initParamElement = |
| 1102 |
this.document.createElement(WebXmlTag.INIT_PARAM.getTagName());
|
|
| 1103 | 11 |
initParamElement.appendChild( |
| 1104 |
createNestedText(WebXmlTag.PARAM_NAME, theParamName)); |
|
| 1105 | 11 |
initParamElement.appendChild( |
| 1106 |
createNestedText(WebXmlTag.PARAM_VALUE, theParamValue)); |
|
| 1107 | 11 |
Iterator loadOnStartupElements = getNestedElements(theElement, |
| 1108 |
WebXmlTag.LOAD_ON_STARTUP); |
|
| 1109 | 11 |
if (loadOnStartupElements.hasNext())
|
| 1110 |
{
|
|
| 1111 | 1 |
theElement.insertBefore(initParamElement, |
| 1112 |
(Element) loadOnStartupElements.next()); |
|
| 1113 |
} |
|
| 1114 |
else
|
|
| 1115 |
{
|
|
| 1116 | 10 |
theElement.appendChild(initParamElement); |
| 1117 |
} |
|
| 1118 |
} |
|
| 1119 |
|
|
| 1120 |
/**
|
|
| 1121 |
* Checks an element whether its name matches the specified name.
|
|
| 1122 |
*
|
|
| 1123 |
* @param theElement The element to check
|
|
| 1124 |
* @param theExpectedTag The expected tag name
|
|
| 1125 |
* @throws IllegalArgumentException If the element name doesn't match
|
|
| 1126 |
*/
|
|
| 1127 | 76 |
private void checkElement(Element theElement, WebXmlTag theExpectedTag) |
| 1128 |
throws IllegalArgumentException
|
|
| 1129 |
{
|
|
| 1130 | 76 |
if (!theExpectedTag.getTagName().equals(theElement.getNodeName()))
|
| 1131 |
{
|
|
| 1132 | 0 |
throw new IllegalArgumentException("Not a '" + theExpectedTag |
| 1133 |
+ "' element");
|
|
| 1134 |
} |
|
| 1135 |
} |
|
| 1136 |
|
|
| 1137 |
/**
|
|
| 1138 |
* Returns an iterator over the child elements of the specified element that
|
|
| 1139 |
* match the specified tag.
|
|
| 1140 |
*
|
|
| 1141 |
* @param theParent The element of which the nested elements should be
|
|
| 1142 |
* retrieved
|
|
| 1143 |
* @param theTag The descriptor tag of which the elements should be
|
|
| 1144 |
* returned
|
|
| 1145 |
* @return An iterator over the elements matching the tag, in the order
|
|
| 1146 |
* they occur in the descriptor
|
|
| 1147 |
*/
|
|
| 1148 | 22 |
private Iterator getNestedElements(Element theParent,
|
| 1149 |
WebXmlTag theTag) |
|
| 1150 |
{
|
|
| 1151 | 22 |
List elements = new ArrayList();
|
| 1152 | 22 |
NodeList nodeList = theParent.getElementsByTagName(theTag.getTagName()); |
| 1153 | 22 |
for (int i = 0; i < nodeList.getLength(); i++) |
| 1154 |
{
|
|
| 1155 | 12 |
elements.add(nodeList.item(i)); |
| 1156 |
} |
|
| 1157 | 22 |
return elements.iterator();
|
| 1158 |
} |
|
| 1159 |
|
|
| 1160 |
/**
|
|
| 1161 |
* Creates an element that contains nested text.
|
|
| 1162 |
*
|
|
| 1163 |
* @param theTag The tag to create an instance of
|
|
| 1164 |
* @param theText The text that should be nested in the element
|
|
| 1165 |
* @return The created DOM element
|
|
| 1166 |
*/
|
|
| 1167 | 56 |
private Element createNestedText(WebXmlTag theTag, String theText)
|
| 1168 |
{
|
|
| 1169 | 56 |
Element element = this.document.createElement(theTag.getTagName());
|
| 1170 | 56 |
element.appendChild(this.document.createTextNode(theText));
|
| 1171 | 56 |
return element;
|
| 1172 |
} |
|
| 1173 |
|
|
| 1174 |
/**
|
|
| 1175 |
* Returns the value of an initialization parameter of the specified filter
|
|
| 1176 |
* or servlet.
|
|
| 1177 |
*
|
|
| 1178 |
* @param theElement The filter or servlet element that contains the
|
|
| 1179 |
* initialization parameters
|
|
| 1180 |
* @param theParamName The name of the initialization parameter
|
|
| 1181 |
* @return The parameter value
|
|
| 1182 |
*/
|
|
| 1183 | 4 |
private String getInitParam(Element theElement, String theParamName)
|
| 1184 |
{
|
|
| 1185 | 4 |
if (theElement != null) |
| 1186 |
{
|
|
| 1187 | 4 |
NodeList initParamElements = |
| 1188 |
theElement.getElementsByTagName( |
|
| 1189 |
WebXmlTag.INIT_PARAM.getTagName()); |
|
| 1190 | 4 |
for (int i = 0; i < initParamElements.getLength(); i++) |
| 1191 |
{
|
|
| 1192 | 4 |
Element initParamElement = (Element) initParamElements.item(i); |
| 1193 | 4 |
String paramName = getNestedText( |
| 1194 |
initParamElement, WebXmlTag.PARAM_NAME); |
|
| 1195 | 4 |
if (theParamName.equals(paramName))
|
| 1196 |
{
|
|
| 1197 | 4 |
return getNestedText(
|
| 1198 |
initParamElement, WebXmlTag.PARAM_VALUE); |
|
| 1199 |
} |
|
| 1200 |
} |
|
| 1201 |
} |
|
| 1202 | 0 |
return null; |
| 1203 |
} |
|
| 1204 |
|
|
| 1205 |
/**
|
|
| 1206 |
* Returns the names of the initialization parameters of the specified
|
|
| 1207 |
* filter or servlet.
|
|
| 1208 |
*
|
|
| 1209 |
* @param theElement The filter or servlet element that contains the
|
|
| 1210 |
* initialization parameters
|
|
| 1211 |
* @return An iterator over the ordered list of parameter names
|
|
| 1212 |
*/
|
|
| 1213 | 11 |
private Iterator getInitParamNames(Element theElement)
|
| 1214 |
{
|
|
| 1215 | 11 |
List initParamNames = new ArrayList();
|
| 1216 | 11 |
if (theElement != null) |
| 1217 |
{
|
|
| 1218 | 11 |
NodeList initParamElements = |
| 1219 |
theElement.getElementsByTagName( |
|
| 1220 |
WebXmlTag.INIT_PARAM.getTagName()); |
|
| 1221 | 11 |
for (int i = 0; i < initParamElements.getLength(); i++) |
| 1222 |
{
|
|
| 1223 | 13 |
Element initParamElement = (Element) initParamElements.item(i); |
| 1224 | 13 |
String paramName = getNestedText( |
| 1225 |
initParamElement, WebXmlTag.PARAM_NAME); |
|
| 1226 | 13 |
if (paramName != null) |
| 1227 |
{
|
|
| 1228 | 13 |
initParamNames.add(paramName); |
| 1229 |
} |
|
| 1230 |
} |
|
| 1231 |
} |
|
| 1232 | 11 |
return initParamNames.iterator();
|
| 1233 |
} |
|
| 1234 |
|
|
| 1235 |
/**
|
|
| 1236 |
* Returns the node before which the specified tag should be inserted, or
|
|
| 1237 |
* <code>null</code> if the node should be inserted at the end of the
|
|
| 1238 |
* descriptor.
|
|
| 1239 |
*
|
|
| 1240 |
* @param theTag The tag that should be inserted
|
|
| 1241 |
* @return The node before which the tag can be inserted
|
|
| 1242 |
*/
|
|
| 1243 | 45 |
private Node getInsertionPointFor(WebXmlTag theTag)
|
| 1244 |
{
|
|
| 1245 | 45 |
for (int i = 0; i < ELEMENT_ORDER.length; i++) |
| 1246 |
{
|
|
| 1247 | 438 |
if (ELEMENT_ORDER[i] == theTag)
|
| 1248 |
{
|
|
| 1249 | 41 |
for (int j = i + 1; j < ELEMENT_ORDER.length; j++) |
| 1250 |
{
|
|
| 1251 | 524 |
NodeList elements = |
| 1252 |
this.rootElement.getElementsByTagName(
|
|
| 1253 |
ELEMENT_ORDER[j].getTagName()); |
|
| 1254 | 524 |
if (elements.getLength() > 0)
|
| 1255 |
{
|
|
| 1256 | 2 |
Node result = elements.item(0); |
| 1257 | 2 |
Node previous = result.getPreviousSibling(); |
| 1258 | 2 |
while ((previous != null) |
| 1259 |
&& ((previous.getNodeType() == Node.COMMENT_NODE) |
|
| 1260 |
|| (previous.getNodeType() == Node.TEXT_NODE))) |
|
| 1261 |
{
|
|
| 1262 | 1 |
result = previous; |
| 1263 | 1 |
previous = result.getPreviousSibling(); |
| 1264 |
} |
|
| 1265 | 2 |
return result;
|
| 1266 |
} |
|
| 1267 |
} |
|
| 1268 | 39 |
break;
|
| 1269 |
} |
|
| 1270 |
} |
|
| 1271 | 43 |
return null; |
| 1272 |
} |
|
| 1273 |
|
|
| 1274 |
/**
|
|
| 1275 |
* Returns the text nested inside a child element of the specified element.
|
|
| 1276 |
*
|
|
| 1277 |
* @param theElement The element of which the nested text should be
|
|
| 1278 |
* returned
|
|
| 1279 |
* @param theTag The descriptor tag in which the text is nested
|
|
| 1280 |
* @return The text nested in the element
|
|
| 1281 |
*/
|
|
| 1282 | 397 |
private String getNestedText(Element theElement,
|
| 1283 |
WebXmlTag theTag) |
|
| 1284 |
{
|
|
| 1285 | 397 |
NodeList nestedElements = |
| 1286 |
theElement.getElementsByTagName(theTag.getTagName()); |
|
| 1287 | 397 |
if (nestedElements.getLength() > 0)
|
| 1288 |
{
|
|
| 1289 | 396 |
Node nestedText = nestedElements.item(0).getFirstChild(); |
| 1290 | 396 |
if (nestedText != null) |
| 1291 |
{
|
|
| 1292 | 396 |
return nestedText.getNodeValue();
|
| 1293 |
} |
|
| 1294 |
} |
|
| 1295 | 1 |
return null; |
| 1296 |
} |
|
| 1297 |
|
|
| 1298 |
} |
|
| 1299 |
|
|
||||||||||