1 package org.apache.turbine.util.uri;
2
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.apache.fulcrum.parser.ParameterParser;
25 import org.apache.turbine.util.RunData;
26 import org.apache.turbine.util.ServerData;
27
28 import java.util.stream.Stream;
29
30 /**
31 * This class allows you to keep all the information needed for a single
32 * link at one place. It keeps your query data, path info, the server
33 * scheme, name, port and the script path. It is tuned for usage with a
34 * Template System e.g. Velocity.
35 *
36 * If you must generate a Turbine Link in a Template System, use this class.
37 *
38 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
39 * @version $Id$
40 */
41
42 public class TemplateURI
43 extends TurbineURI
44 {
45 /**
46 * Empty C'tor. Uses Turbine.getDefaultServerData().
47 *
48 */
49 public TemplateURI()
50 {
51 super();
52 }
53
54 /**
55 * Constructor with a RunData object
56 *
57 * @param runData A RunData object
58 */
59 public TemplateURI(RunData runData)
60 {
61 super(runData);
62 }
63
64 /**
65 * Constructor, set explicit redirection
66 *
67 * @param runData A RunData object
68 * @param redirect True if redirection allowed.
69 */
70 public TemplateURI(RunData runData, boolean redirect)
71 {
72 super(runData, redirect);
73 }
74
75 /**
76 * Constructor, set Template
77 *
78 * @param runData A RunData object
79 * @param template A Template Name
80 */
81 public TemplateURI(RunData runData, String template)
82 {
83 super(runData);
84 setTemplate(template);
85 }
86
87 /**
88 * Constructor, set Template, set explicit redirection
89 *
90 * @param runData A RunData object
91 * @param template A Template Name
92 * @param redirect True if redirection allowed.
93 */
94 public TemplateURI(RunData runData, String template, boolean redirect)
95 {
96 super(runData, redirect);
97 setTemplate(template);
98 }
99
100 /**
101 * Constructor, set Template and Action
102 *
103 * @param runData A RunData object
104 * @param template A Template Name
105 * @param action An Action Name
106 */
107 public TemplateURI(RunData runData, String template, String action)
108 {
109 this(runData, template);
110 setAction(action);
111 }
112
113 /**
114 * Constructor, set Template and Action, set explicit redirection
115 *
116 * @param runData A RunData object
117 * @param template A Template Name
118 * @param action An Action Name
119 * @param redirect True if redirection allowed.
120 */
121 public TemplateURI(RunData runData, String template, String action, boolean redirect)
122 {
123 this(runData, template, redirect);
124 setAction(action);
125 }
126
127 /**
128 * Constructor with a ServerData object
129 *
130 * @param serverData A ServerData object
131 */
132 public TemplateURI(ServerData serverData)
133 {
134 super(serverData);
135 }
136
137 /**
138 * Constructor, set explicit redirection
139 *
140 * @param serverData A ServerData object
141 * @param redirect True if redirection allowed.
142 */
143 public TemplateURI(ServerData serverData, boolean redirect)
144 {
145 super(serverData, redirect);
146 }
147
148 /**
149 * Constructor, set Template
150 *
151 * @param serverData A ServerData object
152 * @param template A Template Name
153 */
154 public TemplateURI(ServerData serverData, String template)
155 {
156 super(serverData);
157 setTemplate(template);
158 }
159
160 /**
161 * Constructor, set Template, set explicit redirection
162 *
163 * @param serverData A ServerData object
164 * @param template A Template Name
165 * @param redirect True if redirection allowed.
166 */
167 public TemplateURI(ServerData serverData, String template, boolean redirect)
168 {
169 super(serverData, redirect);
170 setTemplate(template);
171 }
172
173 /**
174 * Constructor, set Template and Action
175 *
176 * @param serverData A ServerData object
177 * @param template A Template Name
178 * @param action An Action Name
179 */
180 public TemplateURI(ServerData serverData, String template, String action)
181 {
182 this(serverData, template);
183 setAction(action);
184 }
185
186 /**
187 * Constructor, set Template and Action, set explicit redirection
188 *
189 * @param serverData A ServerData object
190 * @param template A Template Name
191 * @param action An Action Name
192 * @param redirect True if redirection allowed.
193 */
194 public TemplateURI(ServerData serverData, String template, String action, boolean redirect)
195 {
196 this(serverData, template, redirect);
197 setAction(action);
198 }
199
200 /**
201 * Constructor, user Turbine.getDefaultServerData(), set Template and Action
202 *
203 * @param template A Template Name
204 * @param action An Action Name
205 */
206 public TemplateURI(String template, String action)
207 {
208 this();
209 setTemplate(template);
210 setAction(action);
211 }
212
213 /**
214 * Sets the template= value for this URL.
215 *
216 * By default it adds the information to the path_info instead
217 * of the query data. An empty value (null or "") cleans out
218 * an existing value.
219 *
220 * @param template A String with the template value.
221 */
222 public void setTemplate(String template)
223 {
224 if(StringUtils.isNotEmpty(template))
225 {
226 add(PATH_INFO, CGI_TEMPLATE_PARAM, template);
227 }
228 else
229 {
230 clearTemplate();
231 }
232 }
233
234 /**
235 * Clears the template= value for this URL.
236 *
237 */
238 public void clearTemplate()
239 {
240 removePathInfo(CGI_TEMPLATE_PARAM);
241 }
242
243 /*
244 * ========================================================================
245 *
246 * Protected / Private Methods
247 *
248 * ========================================================================
249 *
250 */
251
252 /**
253 * Method for a quick way to add all the parameters in a
254 * ParameterParser.
255 *
256 * <p>If the type is P (0), then add name/value to the pathInfo
257 * hashtable.
258 *
259 * <p>If the type is Q (1), then add name/value to the queryData
260 * hashtable.
261 *
262 * @param type Type of insertion (@see #add(char type, String name, String value))
263 * @param pp A ParameterParser.
264 */
265 @Override
266 protected void add(int type,
267 ParameterParser pp)
268 {
269 for (Object name : pp.keySet())
270 {
271 String key = (String) name;
272
273 if (Stream.of(CGI_ACTION_PARAM, CGI_SCREEN_PARAM, CGI_TEMPLATE_PARAM).noneMatch(key::equalsIgnoreCase))
274 {
275 String[] values = pp.getStrings(key);
276 if(values != null)
277 {
278 for (String value : values)
279 {
280 add(type, key, value);
281 }
282 }
283 else
284 {
285 add(type, key, "");
286 }
287 }
288 }
289 }
290 }