1 /*
2 * ========================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * ========================================================================
20 */
21 package org.apache.cactus.integration.api.cactify;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.StringTokenizer;
26
27 import org.codehaus.cargo.module.webapp.WebXml;
28 import org.codehaus.cargo.module.webapp.WebXmlUtils;
29 import org.codehaus.cargo.util.log.Logger;
30
31 /**
32 * Abstract base class for nested redirector elements.
33 * @version
34 */
35 public abstract class Redirector
36 {
37 /**
38 * Cargo logger - when used by Ant, AntLogger
39 * must instantiated, when used by Maven
40 * MavenLogger should be instantiated;
41 */
42 protected Logger logger;
43 /**
44 * Default constructor.
45 */
46 public Redirector()
47 { }
48
49 /**
50 * Constructor with ownerTask.
51 *
52 * @param theOwnerTask to be instantiated
53 */
54 public Redirector(Logger logger)
55 {
56 this.logger = logger;
57 }
58
59 // Instance Variables --------------------------------------------------
60
61 /**
62 * The name of the redirector.
63 */
64 protected String name;
65
66 /**
67 * The URL pattern that the redirector will be mapped to.
68 */
69 protected String mapping;
70
71 /**
72 * Comma-separated list of role names that should be granted access to
73 * the redirector.
74 */
75 protected String roles;
76
77 // Abstract Methods ----------------------------------------------------
78
79 /**
80 * Merges the definition of the redirector into the provided deployment
81 * descriptor.
82 *
83 * @param theWebXml The deployment descriptor into which the redirector
84 * definition should be merged
85 */
86 public abstract void mergeInto(WebXml theWebXml);
87
88 // Public Methods ------------------------------------------------------
89
90 /**
91 * Sets the name of the redirector.
92 *
93 * @param theName The name to set
94 */
95 public final void setName(String theName)
96 {
97 this.name = theName;
98 }
99
100 /**
101 * Sets the URL pattern that the redirector should be mapped to.
102 *
103 * @param theMapping The URL pattern to set
104 */
105 public final void setMapping(String theMapping)
106 {
107 this.mapping = theMapping;
108 }
109
110 /**
111 * Sets the comma-separated list of role names that should be granted
112 * access to the redirector.
113 *
114 * @param theRoles The roles to set
115 */
116 public final void setRoles(String theRoles)
117 {
118 this.roles = theRoles;
119 }
120
121 // Protected Methods ---------------------------------------------------
122
123 /**
124 * Adds the comma-separated list of security roles to a deployment
125 * descriptor.
126 *
127 * @param theWebXml The deployment descriptor
128 */
129 protected final void addSecurity(WebXml theWebXml)
130 {
131 StringTokenizer tokenizer = new StringTokenizer(this.roles, ",");
132 List roles = new ArrayList();
133 while (tokenizer.hasMoreTokens())
134 {
135 String role = tokenizer.nextToken().trim();
136 if (!WebXmlUtils.hasSecurityRole(theWebXml,role))
137 {
138 WebXmlUtils.addSecurityRole(theWebXml,role);
139 }
140 roles.add(role);
141 }
142 if (!roles.isEmpty())
143 {
144 if (!WebXmlUtils.hasLoginConfig(theWebXml))
145 {
146 WebXmlUtils.setLoginConfig(theWebXml, "BASIC", "myrealm");
147 }
148 if (!WebXmlUtils.hasSecurityConstraint(theWebXml,this.mapping))
149 {
150 WebXmlUtils.addSecurityConstraint(theWebXml,"Cactus Test Redirector",
151 this.mapping, roles);
152 }
153 }
154 }
155
156 }