001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * ---------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * These programs are distributed in the hope that they will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
017: *
018: * See the GNU General Public License in the file LICENSE.txt for more
019: * details.
020: *
021: * If you would like a copy of the GNU General Public License write to
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place - Suite 330
025: * Boston, MA 02111-1307, USA.
026: *
027: *
028: * To arrange commercial support and licensing, contact ivata at
029: * http://www.ivata.com/contact.jsp
030: * ---------------------------------------------------------
031: * $Log: RewriteHandling.java,v $
032: * Revision 1.5 2005/10/08 09:19:56 colinmacleod
033: * Changed cached values to store empty string when null.
034: *
035: * Revision 1.4 2005/10/08 09:10:56 colinmacleod
036: * Added variables to cache the env entries.
037: *
038: * Revision 1.3 2005/10/02 14:06:34 colinmacleod
039: * Added/improved log4j logging.
040: *
041: * Revision 1.2 2005/04/29 09:22:05 colinmacleod
042: * Changed warning messages from info to debug
043: * when the env entry is not set.
044: *
045: * Revision 1.1 2005/04/28 18:23:35 colinmacleod
046: * Added server/contextPath rewriting.
047: *
048: * ---------------------------------------------------------
049: */
050: package com.ivata.mask.web;
051:
052: import javax.naming.Context;
053: import javax.naming.InitialContext;
054: import javax.naming.NamingException;
055: import javax.servlet.http.HttpServletRequest;
056:
057: import org.apache.log4j.Logger;
058:
059: import com.ivata.mask.util.StringHandling;
060:
061: /**
062: * This class contains methods to convert the URL and context path for use
063: * with URL rewriting features of a web server such as <strong>Apache</strong>.
064: *
065: * @since ivata groupware 0.6.1 (2005-04-28)
066: * @author Colin MacLeod
067: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
068: * @version $Revision: 1.5 $
069: */
070:
071: public final class RewriteHandling {
072: /**
073: * Logger for this class.
074: */
075: private static final Logger logger = Logger
076: .getLogger(RewriteHandling.class);
077: /**
078: * Stores the rewrite context path - see {@link #getRewriteContextPath()}.
079: */
080: private static String rewriteContextPath = null;
081:
082: /**
083: * Stores the rewrite server - see {@link #getRewriteServer()}.
084: */
085: private static String rewriteServer = null;
086:
087: static {
088: // if either of the cached contexts are empty, that means null
089: rewriteContextPath = getRewriteContextPath();
090: if (rewriteContextPath == null) {
091: rewriteContextPath = "";
092: }
093: rewriteServer = getRewriteServer();
094: if (rewriteServer == null) {
095: rewriteServer = "";
096: }
097: }
098:
099: /**
100: * Helper. Tries to get the context path using {@link #getRewriteContextPath
101: * getRewriteContextPath()}, but uses the path from
102: * <code>request.getContextPath()</code>, if it is <code>null</code>.
103: *
104: * @param requestParam servlet request we are processing.
105: * @return context path, either from {@link #getRewriteContextPath
106: * getRewriteContextPath()}, or <code>request.getContextPath()</code>.
107: */
108: public static String getContextPath(
109: final HttpServletRequest requestParam) {
110: if (logger.isDebugEnabled()) {
111: logger
112: .debug("getContextPath(HttpServletRequest requestParam = "
113: + requestParam + ") - start");
114: }
115:
116: String contextPath = getRewriteContextPath();
117: if (contextPath == null) {
118: contextPath = requestParam.getContextPath();
119: }
120:
121: if (logger.isDebugEnabled()) {
122: logger.debug("getContextPath - end - return value = "
123: + contextPath);
124: }
125: return contextPath;
126: }
127:
128: /**
129: * Helper to get the current environment context.
130: * @return environment context.
131: */
132: public static Context getEnvironmentContext() {
133: if (logger.isDebugEnabled()) {
134: logger.debug("getEnvironmentContext() - start");
135: }
136:
137: InitialContext initialContext;
138: try {
139: initialContext = new InitialContext();
140: } catch (NamingException e) {
141: logger.error("getEnvironmentContext()", e);
142:
143: logger
144: .error(
145: "Exception in getEnvironmentContext. Returning null.",
146: e);
147: return null;
148: }
149: try {
150: Context returnContext = (Context) initialContext
151: .lookup("java:comp/env");
152: if (logger.isDebugEnabled()) {
153: logger
154: .debug("getEnvironmentContext() - end - return value = "
155: + returnContext);
156: }
157: return returnContext;
158: } catch (NamingException e) {
159: logger.error("getEnvironmentContext()", e);
160:
161: logger
162: .error(
163: "Exception in getEnvironmentContext. Returning null.",
164: e);
165: return null;
166: }
167: }
168:
169: /**
170: * Get the new request context path.
171: * @return the request context to replace the current one with.
172: */
173: public static String getRewriteContextPath() {
174: if (logger.isDebugEnabled()) {
175: logger.debug("getRewriteContextPath() - start");
176: }
177: // see if the 'cached' version exists
178: if (rewriteContextPath != null) {
179: if ("".equals(rewriteContextPath)) {
180: return null;
181: }
182: return rewriteContextPath;
183: }
184:
185: Context environmentContext = RewriteHandling
186: .getEnvironmentContext();
187: if (environmentContext == null) {
188: // assume the error was already trapped
189: if (logger.isDebugEnabled()) {
190: logger
191: .debug("getRewriteContextPath: environment context is "
192: + "null.");
193: }
194: return null;
195: }
196: try {
197: String returnString = (String) environmentContext
198: .lookup("rewriteContextPath");
199: if (logger.isDebugEnabled()) {
200: logger
201: .debug("getRewriteContextPath() - end - return value = "
202: + returnString);
203: }
204: return returnString;
205: } catch (Exception e) {
206: logger.error("getRewriteContextPath()", e);
207:
208: if (logger.isDebugEnabled()) {
209: logger.debug("Exception in getRewriteServer. "
210: + "Returning null.", e);
211: }
212: return null;
213: }
214: }
215:
216: /**
217: * Get the server to use in the base tag.
218: * @return the server to write out to the base tag.
219: */
220: public static String getRewriteServer() {
221: if (logger.isDebugEnabled()) {
222: logger.debug("getRewriteServer() - start");
223: }
224: // see if the 'cached' version exists
225: if (rewriteServer != null) {
226: if ("".equals(rewriteServer)) {
227: return null;
228: }
229: return rewriteServer;
230: }
231:
232: Context environmentContext = RewriteHandling
233: .getEnvironmentContext();
234: if (environmentContext == null) {
235: // assume the error was already trapped
236: if (logger.isDebugEnabled()) {
237: logger
238: .debug("getRewriteServer: environment context is null.");
239: }
240: return null;
241: }
242: try {
243: String returnString = (String) environmentContext
244: .lookup("rewriteServer");
245: if (logger.isDebugEnabled()) {
246: logger
247: .debug("getRewriteServer() - end - return value = "
248: + returnString);
249: }
250: return returnString;
251: } catch (Exception e) {
252: logger.error("getRewriteServer()", e);
253:
254: if (logger.isDebugEnabled()) {
255: logger.debug("Exception in getRewriteServer. "
256: + "Returning null.", e);
257: }
258: return null;
259: }
260: }
261:
262: /**
263: * Helper to avoid repetition in rewrite and link tags.
264: * @param oldURL the URL generted by <code>TagUTils</code>.
265: * @param oldContextPath the old context path.
266: * @return new url with the server and context path replaced.
267: */
268: public static String rewriteURL(final String oldURL,
269: final String oldContextPath) {
270: if (logger.isDebugEnabled()) {
271: logger.debug("rewriteURL(String oldURL = " + oldURL
272: + ", String oldContextPath = " + oldContextPath
273: + ") - start");
274: }
275:
276: if (StringHandling.isNullOrEmpty(rewriteServer)) {
277: if (logger.isDebugEnabled()) {
278: logger
279: .debug("Rewrite server null or empty so returning '"
280: + oldURL + "' in rewriteURL.");
281: }
282: return oldURL;
283: }
284: if (StringHandling.isNullOrEmpty(rewriteContextPath)) {
285: if (logger.isDebugEnabled()) {
286: logger
287: .debug("Rewrite context path null or empty so returning '"
288: + oldURL + "' in rewriteURL.");
289: }
290: return oldURL;
291: }
292: int posOldContextPath = oldURL.indexOf(oldContextPath);
293: if (posOldContextPath == -1) {
294: if (logger.isDebugEnabled()) {
295: logger
296: .debug("Old URL '"
297: + oldURL
298: + "' does not contain "
299: + "the current context path. Returning it unchanged "
300: + "in rewriteURL.");
301: }
302: return oldURL;
303: }
304: // don't want two directory separators
305: if ("/".equals(rewriteContextPath)) {
306: rewriteContextPath = "";
307: }
308: StringBuffer newURL = new StringBuffer();
309:
310: // if the old path was relative, don't start with the server
311: if (!oldURL.startsWith("/")) {
312: newURL.append(rewriteServer);
313: }
314: newURL.append(rewriteContextPath);
315: newURL.append(oldURL.substring(posOldContextPath
316: + oldContextPath.length()));
317: if (logger.isDebugEnabled()) {
318: logger.debug("Returning '" + newURL + "'");
319: }
320: return newURL.toString();
321: }
322:
323: /**
324: * Private default constructor enforces utility class behavior.
325: */
326: private RewriteHandling() {
327: }
328: }
|