KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > MVCURLTools


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc;
8
9
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11
12 import com.inversoft.util.StringTools;
13 import com.inversoft.verge.mvc.controller.ControllerMVCInfo;
14
15
16 /**
17  * <p>
18  * This class is used to assist the default Verge MVC
19  * implementations with URL related processing. The main
20  * feature is the ability to change the URL beginning used
21  * when generating URLs for the MVC. This value defaults to
22  * the String:
23  * </p>
24  *
25  * <code>/mvc</code>
26  *
27  * <p>
28  * This can be changed using the MVC configuration element
29  * in a base verge configuration file or at runtime by calling
30  * this class.
31  * </p>
32  *
33  * @author Brian Pontarelli
34  */

35 public class MVCURLTools {
36
37     private static volatile String JavaDoc urlBeginning = "/mvc";
38
39
40     /**
41      * Retrieves the URL beginning.
42      *
43      * @return The URL beginning and never null
44      */

45     public static String JavaDoc getURLBeginning() {
46         return urlBeginning;
47     }
48
49     /**
50      * Sets a new URL beginning. The variable is volatile and therefore changes
51      * are immediate to main memory.
52      *
53      * @param urlBeginning The new URL beginning
54      * @throws NullPointerException If the value is empty or null
55      */

56     public static void setURLEnding(String JavaDoc urlBeginning) {
57         if (StringTools.isTrimmedEmpty(urlBeginning)) {
58             throw new NullPointerException JavaDoc("[MVCURLTools] URL beginnings can not be" +
59                 " empty or null");
60         }
61         MVCURLTools.urlBeginning = urlBeginning;
62     }
63
64     /**
65      * Parses out the given path String into a ControllerMVCInfo object.
66      *
67      * @param request The HttpServletRequest
68      * @return A ControllerMVCInfo and never null
69      */

70     public static ControllerMVCInfo decodeURL(HttpServletRequest JavaDoc request)
71     throws MVCException {
72         // Strip off the MVC URL beginning
73
String JavaDoc path = request.getPathInfo();
74         String JavaDoc begin = MVCURLTools.getURLBeginning();
75         if (path != null && path.startsWith(begin)) {
76             path = path.substring(begin.length());
77         } else if (path == null) {
78             throw new MVCException("[MVCURLTools] Encountered an invalid " +
79                 "MVC URL [" + request.getRequestURI() + "].");
80         }
81
82         // Allow the implementation to set attributes
83
return new ControllerMVCInfo(path);
84     }
85 }
Popular Tags