Project

General

Profile

1 4080 daigle
/**
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$'
9
 *     '$Date$'
10
 * '$Revision$'
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 4481 daigle
import java.util.regex.Matcher;
30
import java.util.regex.Pattern;
31 4080 daigle
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 4421 leinfelder
		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 4080 daigle
		String httpPort = PropertyService.getProperty("server.httpSSLPort");
146
		if (!httpPort.equals("443")) {
147 4421 leinfelder
			server = server + ":" + httpPort;
148 4080 daigle
		}
149
150 4421 leinfelder
		return server;
151 4080 daigle
	}
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 4189 daigle
		return getContextURL()
161 4080 daigle
				+ 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 4141 daigle
	 * Get the context directory. This is made up of the deployment directory + file
221
	 * separator + context
222 4080 daigle
	 *
223
	 * @return string holding the context directory
224
	 */
225
	public static String getContextDir() throws PropertyNotFoundException {
226 4132 daigle
		return PropertyService.getProperty("application.deployDir") + FileUtil.getFS()
227 4080 daigle
				+ 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 4110 daigle
		return getContextDir() + FileUtil.getFS() + "WEB-INF" + FileUtil.getFS() + "sql";
271 4080 daigle
	}
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 4185 daigle
	 * Attempt to discover the deployment directory for this application. This is a
285 4080 daigle
	 * best guess scenario. It is used by configuration routines before the
286 4185 daigle
	 * deployment directory has been populated in metacat.properties.
287 4080 daigle
	 *
288
	 * @param request
289
	 *            the http servlet request we will use to find the tomcat directory
290
	 *
291 4481 daigle
	 * @return a string holding the web application directory
292 4080 daigle
	 */
293 4185 daigle
	public static String discoverDeployDir(HttpServletRequest request) {
294 4080 daigle
		ServletContext servletContext = request.getSession()
295
				.getServletContext();
296
		String realPath = servletContext.getRealPath(".");
297 4185 daigle
		String contextPath = request.getContextPath();
298
299
		logMetacat.debug("realPath: " + realPath);
300
		logMetacat.debug("contextPath: " + contextPath);
301 4481 daigle
302
		Pattern pattern = Pattern.compile(contextPath + "/\\.$");
303
		Matcher matcher = pattern.matcher(realPath);
304 4185 daigle
305 4481 daigle
		if (matcher.find()) {
306
			realPath = matcher.replaceFirst("");
307
		}
308
309 4185 daigle
		return realPath;
310 4080 daigle
	}
311
312
}