001: package org.apache.turbine.util.uri;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import javax.servlet.http.HttpServletResponse;
023:
024: import org.apache.commons.lang.StringUtils;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import org.apache.turbine.Turbine;
030: import org.apache.turbine.TurbineConstants;
031:
032: import org.apache.turbine.util.RunData;
033: import org.apache.turbine.util.ServerData;
034:
035: /**
036: * This is the base class for all dynamic URIs in the Turbine System.
037: *
038: * All of the classes used for generating URIs are derived from this.
039: *
040: * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a>
041: * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
042: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
043: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
044: * @version $Id: BaseURI.java 534527 2007-05-02 16:10:59Z tv $
045: */
046:
047: public abstract class BaseURI implements URI, URIConstants {
048: /** Logging */
049: private static Log log = LogFactory.getLog(BaseURI.class);
050:
051: /** ServerData Object for scheme, name, port etc. */
052: private ServerData serverData = new ServerData(null, HTTP_PORT,
053: HTTP, null, null);
054:
055: /** Whether we want to redirect or not. */
056: private boolean redirect = false;
057:
058: /** Servlet response interface. */
059: private HttpServletResponse response = null;
060:
061: /** Reference Anchor (#ref) */
062: private String reference = null;
063:
064: /*
065: * ========================================================================
066: *
067: * Constructors
068: *
069: * ========================================================================
070: *
071: */
072:
073: /**
074: * Empty C'tor. Uses Turbine.getDefaultServerData().
075: *
076: */
077: public BaseURI() {
078: init(Turbine.getDefaultServerData());
079: setResponse(null);
080: }
081:
082: /**
083: * Constructor with a RunData object
084: *
085: * @param runData A RunData object
086: */
087: public BaseURI(RunData runData) {
088: init(runData.getServerData());
089: setResponse(runData.getResponse());
090: }
091:
092: /**
093: * Constructor, set explicit redirection
094: *
095: * @param runData A RunData object
096: * @param redirect True if redirection allowed.
097: */
098: public BaseURI(RunData runData, boolean redirect) {
099: init(runData.getServerData());
100: setResponse(runData.getResponse());
101: setRedirect(redirect);
102: }
103:
104: /**
105: * Constructor with a ServerData object
106: *
107: * @param serverData A ServerData object
108: */
109: public BaseURI(ServerData serverData) {
110: init(serverData);
111: setResponse(null);
112: }
113:
114: /**
115: * Constructor, set explicit redirection
116: *
117: * @param serverData A ServerData object
118: * @param redirect True if redirection allowed.
119: */
120: public BaseURI(ServerData serverData, boolean redirect) {
121: init(serverData);
122: setResponse(null);
123: setRedirect(redirect);
124: }
125:
126: /*
127: * ========================================================================
128: *
129: * Init
130: *
131: * ========================================================================
132: *
133: */
134:
135: /**
136: * Init with a ServerData object
137: *
138: * @param serverData A ServerData object
139: *
140: */
141: private void init(ServerData serverData) {
142: log.debug("init(" + serverData + ")");
143:
144: if (serverData != null) {
145: // We must clone this, because if BaseURI is used in a pull tool,
146: // then the fields might be changed. If we don't clone, this might pull
147: // through to the ServerData object saved at firstRequest() in the
148: // Turbine object.
149: this .serverData = (ServerData) serverData.clone();
150: } else {
151: log.error("Passed null ServerData object!");
152: }
153: reference = null;
154: }
155:
156: /*
157: * ========================================================================
158: *
159: * Getter / Setter
160: *
161: * ========================================================================
162: *
163: */
164:
165: /**
166: * Set the redirect Flag
167: *
168: * @param redirect The new value of the redirect flag.
169: */
170: public void setRedirect(boolean redirect) {
171: this .redirect = redirect;
172: }
173:
174: /**
175: * Returns the current value of the Redirect flag
176: *
177: * @return True if Redirect is allowed
178: *
179: */
180: public boolean isRedirect() {
181: return redirect;
182: }
183:
184: /**
185: * Gets the script name (/servlets/Turbine).
186: *
187: * @return A String with the script name.
188: */
189: public String getScriptName() {
190: return serverData.getScriptName();
191: }
192:
193: /**
194: * Sets the script name (/servlets/Turbine).
195: *
196: * @param scriptName A String with the script name.
197: */
198: public void setScriptName(String scriptName) {
199: serverData.setScriptName(scriptName);
200: }
201:
202: /**
203: * Gets the context path.
204: *
205: * @return A String with the context path.
206: */
207: public String getContextPath() {
208: return serverData.getContextPath();
209: }
210:
211: /**
212: * Sets the context path.
213: *
214: * @param contextPath A String with the context path
215: */
216: public void setContextPath(String contextPath) {
217: serverData.setContextPath(contextPath);
218: }
219:
220: /**
221: * Gets the server name.
222: *
223: * @return A String with the server name.
224: */
225: public String getServerName() {
226: return serverData.getServerName();
227: }
228:
229: /**
230: * Sets the server name.
231: *
232: * @param serverName A String with the server name.
233: */
234: public void setServerName(String serverName) {
235: serverData.setServerName(serverName);
236: }
237:
238: /**
239: * Gets the server port.
240: *
241: * @return A String with the server port.
242: */
243: public int getServerPort() {
244: int serverPort = serverData.getServerPort();
245:
246: if (serverPort == 0) {
247: if (getServerScheme().equals(HTTPS)) {
248: serverPort = HTTPS_PORT;
249: } else {
250: serverPort = HTTP_PORT;
251: }
252: }
253: return serverPort;
254: }
255:
256: /**
257: * Sets the server port.
258: *
259: * @param serverPort An int with the port.
260: */
261: public void setServerPort(int serverPort) {
262: serverData.setServerPort(serverPort);
263: }
264:
265: /**
266: * Method to specify that a URI should use SSL. The default port
267: * is used.
268: */
269: public void setSecure() {
270: setSecure(HTTPS_PORT);
271: }
272:
273: /**
274: * Method to specify that a URI should use SSL.
275: * Whether or not it does is determined from Turbine.properties.
276: * If use.ssl in the Turbine.properties is set to false, then
277: * http is used in any case. (Default of use.ssl is true).
278: *
279: * @param port An int with the port number.
280: */
281: public void setSecure(int port) {
282: boolean useSSL = Turbine.getConfiguration().getBoolean(
283: TurbineConstants.USE_SSL_KEY,
284: TurbineConstants.USE_SSL_DEFAULT);
285:
286: setServerScheme(useSSL ? HTTPS : HTTP);
287: setServerPort(port);
288: }
289:
290: /**
291: * Sets the scheme (HTTP or HTTPS).
292: *
293: * @param serverScheme A String with the scheme.
294: */
295: public void setServerScheme(String serverScheme) {
296: serverData
297: .setServerScheme(StringUtils.isNotEmpty(serverScheme) ? serverScheme
298: : "");
299: }
300:
301: /**
302: * Returns the current Server Scheme
303: *
304: * @return The current Server scheme
305: *
306: */
307: public String getServerScheme() {
308: String serverScheme = serverData.getServerScheme();
309:
310: return StringUtils.isNotEmpty(serverScheme) ? serverScheme
311: : HTTP;
312: }
313:
314: /**
315: * Sets a reference anchor (#ref).
316: *
317: * @param reference A String containing the reference.
318: */
319: public void setReference(String reference) {
320: this .reference = reference;
321: }
322:
323: /**
324: * Returns the current reference anchor.
325: *
326: * @return A String containing the reference.
327: */
328: public String getReference() {
329: return hasReference() ? reference : "";
330: }
331:
332: /**
333: * Does this URI contain an anchor? (#ref)
334: *
335: * @return True if this URI contains an anchor.
336: */
337: public boolean hasReference() {
338: return StringUtils.isNotEmpty(reference);
339: }
340:
341: /*
342: * ========================================================================
343: *
344: * Protected / Private Methods
345: *
346: * ========================================================================
347: *
348: */
349:
350: /**
351: * Set a Response Object to use when creating the
352: * response string.
353: *
354: */
355: protected void setResponse(HttpServletResponse response) {
356: this .response = response;
357: }
358:
359: /**
360: * Returns the Response Object from the Servlet Container.
361: *
362: * @return The Servlet Response object or null
363: *
364: */
365: protected HttpServletResponse getResponse() {
366: return response;
367: }
368:
369: /**
370: * Append the Context Path and Script Name to the passed
371: * String Buffer.
372: *
373: * <p>
374: * This is a convenience method to be
375: * used in the Link output routines of derived classes to
376: * easily append the correct path.
377: *
378: * @param sb The StringBuffer to store context path and script name.
379: */
380: protected void getContextAndScript(StringBuffer sb) {
381: String context = getContextPath();
382:
383: if (StringUtils.isNotEmpty(context)) {
384: if (context.charAt(0) != '/') {
385: sb.append('/');
386: }
387: sb.append(context);
388: }
389:
390: // /servlet/turbine
391: String script = getScriptName();
392:
393: if (StringUtils.isNotEmpty(script)) {
394: if (script.charAt(0) != '/') {
395: sb.append('/');
396: }
397: sb.append(script);
398: }
399: }
400:
401: /**
402: * Appends Scheme, Server and optionally the port to the
403: * supplied String Buffer.
404: *
405: * <p>
406: * This is a convenience method to be
407: * used in the Link output routines of derived classes to
408: * easily append the correct server scheme.
409: *
410: * @param sb The StringBuffer to store the scheme and port information.
411: */
412: protected void getSchemeAndPort(StringBuffer sb) {
413: // http(s)://<servername>
414: sb.append(getServerScheme());
415: sb.append(URIConstants.URI_SCHEME_SEPARATOR);
416: sb.append(getServerName());
417:
418: // (:<port>)
419: if ((getServerScheme().equals(HTTP) && getServerPort() != HTTP_PORT)
420: || (getServerScheme().equals(HTTPS) && getServerPort() != HTTPS_PORT)) {
421: sb.append(':');
422: sb.append(getServerPort());
423: }
424: }
425:
426: /**
427: * Encodes a Response Uri according to the Servlet Container.
428: * This might add a Java session identifier or do redirection.
429: * The resulting String can be used in a page or template.
430: *
431: * @param uri The Uri to encode
432: *
433: * @return An Uri encoded by the container.
434: */
435: protected String encodeResponse(String uri) {
436: String res = uri;
437:
438: HttpServletResponse response = getResponse();
439:
440: if (response == null) {
441: log.debug("No Response Object!");
442: } else {
443: try {
444: if (isRedirect()) {
445: log.debug("Should Redirect");
446: res = response.encodeRedirectURL(uri);
447: } else {
448: res = response.encodeURL(uri);
449: }
450: } catch (Exception e) {
451: log.error("response" + response + ", uri: " + uri);
452: log.error("While trying to encode the URI: ", e);
453: }
454: }
455:
456: log.debug("encodeResponse(): " + res);
457: return res;
458: }
459: }
|