@@ -12,15 +12,18 @@ public class Utils {
12
12
13
13
14
14
//**************************************************************************
15
- //** Constructor
15
+ //** Private Constructor
16
16
//**************************************************************************
17
- /** Creates a new instance of Utils . */
17
+ /** Prevent users from instantiating this class . */
18
18
19
- private Utils () {
20
-
21
- }
19
+ private Utils () {}
22
20
23
21
22
+ //**************************************************************************
23
+ //** getPath
24
+ //**************************************************************************
25
+ /** Returns the path to the app root.
26
+ */
24
27
public static String getPath (javax .servlet .http .HttpServletRequest request ){
25
28
26
29
String Path = request .getContextPath ();
@@ -46,4 +49,60 @@ public static String getPath(javax.servlet.http.HttpServletRequest request){
46
49
47
50
}
48
51
52
+
53
+ //**************************************************************************
54
+ //** getURL
55
+ //**************************************************************************
56
+ /** Returns the url associated with a given request. Note that the standard
57
+ * request.getRequestURL() method returns the ContextPath even when the
58
+ * app is deployed as the default web application. For example, the "JavaXT"
59
+ * web app is deployed as the default web app. When clients request
60
+ * "http://www.javaxt.com" the request.getRequestURL() returns
61
+ * "http://www.javaxt.com/JavaXT". This method will return the correct url,
62
+ * namely "http://www.javaxt.com".
63
+ */
64
+ public static javaxt .utils .URL getURL (javax .servlet .http .HttpServletRequest request ){
65
+
66
+
67
+ String protocol = request .getProtocol ();
68
+ if (protocol ==null ) protocol = "http" ;
69
+ else {
70
+ if (protocol .contains ("/" )) protocol = protocol .substring (0 , protocol .indexOf ("/" ));
71
+ protocol = protocol .toLowerCase ();
72
+ }
73
+
74
+ //Get Host
75
+ String host = request .getServerName ();
76
+
77
+ Integer port = request .getServerPort ();
78
+ if (port !=null && port >0 && port !=80 ) host += ":" + port ;
79
+
80
+ //Get Path
81
+ String path = request .getRequestURI ();
82
+ if (path ==null ) path = "" ;
83
+
84
+ //Update Path: Special case when app is deployed as root
85
+ if (path .length ()>0 ){
86
+ if (Utils .getPath (request ).equals ("/" )){
87
+ String contextPath = request .getContextPath ();
88
+ if (path .startsWith (contextPath )){
89
+ path = path .substring (contextPath .length ());
90
+ }
91
+ }
92
+ }
93
+
94
+
95
+
96
+ //Get Query String
97
+ String query = request .getQueryString ();
98
+ if (query ==null ) query = "" ;
99
+ if (query .length ()>0 ) query = "?" + query ;
100
+
101
+
102
+ //Assemble URL
103
+ String url = protocol + "://" + host + path + query ;
104
+ //System.out.println("\r\nURL: " + url + "\r\nPath: " + Utils.getPath(request));
105
+ return new javaxt .utils .URL (url );
106
+ }
107
+
49
108
}
0 commit comments