1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements system utility methods
|
4
|
* Copyright: 2008 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Michael Daigle
|
7
|
*
|
8
|
* '$Author: daigle $'
|
9
|
* '$Date: 2008-10-22 14:45:51 -0700 (Wed, 22 Oct 2008) $'
|
10
|
* '$Revision: 4481 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
package edu.ucsb.nceas.metacat.util;
|
28
|
|
29
|
import java.util.regex.Matcher;
|
30
|
import java.util.regex.Pattern;
|
31
|
import javax.servlet.ServletContext;
|
32
|
import javax.servlet.http.HttpServletRequest;
|
33
|
|
34
|
import org.apache.log4j.Logger;
|
35
|
|
36
|
import edu.ucsb.nceas.metacat.MetaCatVersion;
|
37
|
import edu.ucsb.nceas.metacat.service.PropertyService;
|
38
|
import edu.ucsb.nceas.utilities.FileUtil;
|
39
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
40
|
|
41
|
public class SystemUtil {
|
42
|
|
43
|
private static Logger logMetacat = Logger.getLogger(SystemUtil.class);
|
44
|
private static String METACAT_SERVLET = "metacat";
|
45
|
|
46
|
/**
|
47
|
* private constructor - all methods are static so there is no no need to
|
48
|
* instantiate.
|
49
|
*/
|
50
|
private SystemUtil() {}
|
51
|
|
52
|
/**
|
53
|
* Attempt to discover the server name. The name is retrieved from the http
|
54
|
* servlet request. This is used by configuration routines before the port
|
55
|
* has been populated in metacat.properties. it is possible the port that
|
56
|
* the user configures might be different than the name we get here. You
|
57
|
* should use getServerPort() instead of this method whenever possible.
|
58
|
*
|
59
|
* @param request
|
60
|
* the http servlet request we will use to find the server name
|
61
|
*
|
62
|
* @return a string holding the server name
|
63
|
*/
|
64
|
public static String discoverServerName(HttpServletRequest request) {
|
65
|
String serverName = request.getServerName();
|
66
|
|
67
|
return serverName;
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Attempt to discover the server port. The port is retrieved from the http
|
72
|
* servlet request. This is used by configuration routines before the port
|
73
|
* has been populated in metacat.properties. it is possible the port that
|
74
|
* the user configures might be different than the port we get here. You
|
75
|
* should use getServerPort() instead of this method whenever possible.
|
76
|
*
|
77
|
* @param request
|
78
|
* the http servlet request we will use to find the server port
|
79
|
*
|
80
|
* @return a string holding the server port
|
81
|
*/
|
82
|
public static String discoverServerPort(HttpServletRequest request) {
|
83
|
return Integer.toString(request.getServerPort());
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Attempt to discover the server ssl port. The ssl port is assumed using
|
88
|
* the standard port. This is used by configuration routines before the port
|
89
|
* has been populated in metacat.properties. it is possible the prot that
|
90
|
* the user configures might be different than the port we get here. You
|
91
|
* should use getServerSSLPort() instead of this method whenever possible.
|
92
|
*
|
93
|
* @param request
|
94
|
* the http servlet request we will use to find the server port
|
95
|
*
|
96
|
* @return a string holding the server ssl port
|
97
|
*/
|
98
|
public static String discoverServerSSLPort(HttpServletRequest request) {
|
99
|
String serverPort = discoverServerPort(request);
|
100
|
|
101
|
if (serverPort.length() == 4 && serverPort.charAt(0) == '8') {
|
102
|
return "8443";
|
103
|
}
|
104
|
|
105
|
return "443";
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Get the server URL which is made up of the server name + : + the http
|
110
|
* port number. Note that if the port is 80, it is left off.
|
111
|
*
|
112
|
* @return string holding the server URL
|
113
|
*/
|
114
|
public static String getServerURL() throws PropertyNotFoundException {
|
115
|
String ServerURL = "http://" + PropertyService.getProperty("server.name");
|
116
|
String httpPort = PropertyService.getProperty("server.httpPort");
|
117
|
if (!httpPort.equals("80")) {
|
118
|
ServerURL += ":" + httpPort;
|
119
|
}
|
120
|
|
121
|
return ServerURL;
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Get the secure server URL which is made up of the server name + : + the
|
126
|
* https port number. Note that if the port is 443, it is left off.
|
127
|
*
|
128
|
* @return string holding the server URL
|
129
|
*/
|
130
|
public static String getSecureServerURL() throws PropertyNotFoundException {
|
131
|
String ServerURL = "https://" + getSecureServer();
|
132
|
return ServerURL;
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* Get the secure server which is made up of the server name + : + the
|
137
|
* https port number. Note that if the port is 443, it is left off.
|
138
|
* NOTE: does NOT include "https://"
|
139
|
* @see getSecureServerURL()
|
140
|
*
|
141
|
* @return string holding the secure server
|
142
|
*/
|
143
|
public static String getSecureServer() throws PropertyNotFoundException {
|
144
|
String server = PropertyService.getProperty("server.name");
|
145
|
String httpPort = PropertyService.getProperty("server.httpSSLPort");
|
146
|
if (!httpPort.equals("443")) {
|
147
|
server = server + ":" + httpPort;
|
148
|
}
|
149
|
|
150
|
return server;
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* Get the CGI URL which is made up of the server URL + file separator + the
|
155
|
* CGI directory
|
156
|
*
|
157
|
* @return string holding the server URL
|
158
|
*/
|
159
|
public static String getCGI_URL() throws PropertyNotFoundException{
|
160
|
return getContextURL()
|
161
|
+ PropertyService.getProperty("application.cgiDir");
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Get the server URL with the context. This is made up of the server URL +
|
166
|
* file separator + the context
|
167
|
*
|
168
|
* @return string holding the server URL with context
|
169
|
*/
|
170
|
public static String getContextURL() throws PropertyNotFoundException {
|
171
|
return getServerURL() + FileUtil.getFS()
|
172
|
+ PropertyService.getProperty("application.context");
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* Get the servlet URL. This is made up of the server URL with context +
|
177
|
* file separator + the metacat servlet name
|
178
|
*
|
179
|
* @return string holding the servlet URL
|
180
|
*/
|
181
|
public static String getServletURL() throws PropertyNotFoundException {
|
182
|
return getContextURL() + FileUtil.getFS() + METACAT_SERVLET;
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* Get the style skins URL. This is made up of the server URL with context +
|
187
|
* file separator + "style" + file separator + "skins"
|
188
|
*
|
189
|
* @return string holding the style skins URL
|
190
|
*/
|
191
|
public static String getStyleSkinsURL() throws PropertyNotFoundException {
|
192
|
return getContextURL() + FileUtil.getFS() + "style" + FileUtil.getFS()
|
193
|
+ "skins";
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* Get the style common URL. This is made up of the server URL with context +
|
198
|
* file separator + "style" + file separator + "common"
|
199
|
*
|
200
|
* @return string holding the style common URL
|
201
|
*/
|
202
|
public static String getStyleCommonURL() throws PropertyNotFoundException {
|
203
|
return getContextURL() + FileUtil.getFS() + "style" + FileUtil.getFS()
|
204
|
+ "common";
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Get the metacat version by getting the string representation from
|
209
|
* metacat.properties and instantiating a MetaCatVersion object
|
210
|
*
|
211
|
* @return a MetaCatVersion object holding metacat version information
|
212
|
*/
|
213
|
public static MetaCatVersion getMetacatVersion() throws PropertyNotFoundException {
|
214
|
String metacatVersionString =
|
215
|
PropertyService.getProperty("application.metacatVersion");
|
216
|
return new MetaCatVersion(metacatVersionString);
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* Get the context directory. This is made up of the deployment directory + file
|
221
|
* separator + context
|
222
|
*
|
223
|
* @return string holding the context directory
|
224
|
*/
|
225
|
public static String getContextDir() throws PropertyNotFoundException {
|
226
|
return PropertyService.getProperty("application.deployDir") + FileUtil.getFS()
|
227
|
+ PropertyService.getProperty("application.context");
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Attempt to discover the context for this application. This is a best
|
232
|
* guess scenario. It is used by configuration routines before the context
|
233
|
* has been populated in metacat.properties. You should always use
|
234
|
* getApplicationContext() instead of this method if possible.
|
235
|
*
|
236
|
* @param request
|
237
|
* the http servlet request we will use to find the context
|
238
|
*
|
239
|
* @return a string holding the context
|
240
|
*/
|
241
|
public static String discoverApplicationContext(HttpServletRequest request) {
|
242
|
String contextPath = request.getContextPath();
|
243
|
|
244
|
if (contextPath.charAt(0) == '/') {
|
245
|
contextPath = contextPath.substring(1);
|
246
|
}
|
247
|
logMetacat.debug("contextPath: " + contextPath);
|
248
|
|
249
|
return contextPath;
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* Get the style skins directory. This is made up of the tomcat directory
|
254
|
* with context + file separator + "style" + file separator + "skins"
|
255
|
*
|
256
|
* @return string holding the style skins directory
|
257
|
*/
|
258
|
public static String getStyleSkinsDir() throws PropertyNotFoundException {
|
259
|
return getContextDir() + FileUtil.getFS() + "style" + FileUtil.getFS()
|
260
|
+ "skins";
|
261
|
}
|
262
|
|
263
|
/**
|
264
|
* Get the SQL directory. This is made up of the context directory + file
|
265
|
* separator + sql
|
266
|
*
|
267
|
* @return string holding the sql directory
|
268
|
*/
|
269
|
public static String getSQLDir() throws PropertyNotFoundException {
|
270
|
return getContextDir() + FileUtil.getFS() + "WEB-INF" + FileUtil.getFS() + "sql";
|
271
|
}
|
272
|
|
273
|
/**
|
274
|
* Get the default style URL from metacat.properties.
|
275
|
*
|
276
|
* @return string holding the default style URL
|
277
|
*/
|
278
|
public static String getDefaultStyleURL() throws PropertyNotFoundException {
|
279
|
return getStyleCommonURL() + FileUtil.getFS()
|
280
|
+ PropertyService.getProperty("application.default-style");
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* Attempt to discover the deployment directory for this application. This is a
|
285
|
* best guess scenario. It is used by configuration routines before the
|
286
|
* deployment directory has been populated in metacat.properties.
|
287
|
*
|
288
|
* @param request
|
289
|
* the http servlet request we will use to find the tomcat directory
|
290
|
*
|
291
|
* @return a string holding the web application directory
|
292
|
*/
|
293
|
public static String discoverDeployDir(HttpServletRequest request) {
|
294
|
ServletContext servletContext = request.getSession()
|
295
|
.getServletContext();
|
296
|
String realPath = servletContext.getRealPath(".");
|
297
|
String contextPath = request.getContextPath();
|
298
|
|
299
|
logMetacat.debug("realPath: " + realPath);
|
300
|
logMetacat.debug("contextPath: " + contextPath);
|
301
|
|
302
|
Pattern pattern = Pattern.compile(contextPath + "/\\.$");
|
303
|
Matcher matcher = pattern.matcher(realPath);
|
304
|
|
305
|
if (matcher.find()) {
|
306
|
realPath = matcher.replaceFirst("");
|
307
|
}
|
308
|
|
309
|
return realPath;
|
310
|
}
|
311
|
|
312
|
}
|