Project

General

Profile

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-11-24 11:51:33 -0800 (Mon, 24 Nov 2008) $'
10
 * '$Revision: 4619 $'
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
	 * The info is set in build.properties and then populated into metacat.properties
211
	 * at build time using Ant token replacement.
212
	 * 
213
	 * @return a MetaCatVersion object holding metacat version information
214
	 */
215
	public static MetaCatVersion getMetacatVersion() throws PropertyNotFoundException {
216
		String metacatVersionString = 
217
			PropertyService.getProperty("application.metacatVersion");
218
		return new MetaCatVersion(metacatVersionString);
219
	}
220
	
221
	/**
222
	 * Gets a string that holds some description about the release. Typically this is 
223
	 * used during release candidates for display purposes and might hold something
224
	 * like "Release Candidate 1".  Normally it is empty for final production release.
225
	 * The info is set in build.properties and then populated into metacat.properties
226
	 * at build time using Ant token replacement.
227
	 * 
228
	 * @return a MetaCatVersion object holding metacat version information
229
	 */
230
	public static String getMetacatReleaseInfo() throws PropertyNotFoundException {
231
		return PropertyService.getProperty("application.metacatReleaseInfo");
232
	}
233

    
234
	/**
235
	 * Get the context directory. This is made up of the deployment directory + file
236
	 * separator + context
237
	 * 
238
	 * @return string holding the context directory
239
	 */
240
	public static String getContextDir() throws PropertyNotFoundException {
241
		return PropertyService.getProperty("application.deployDir") + FileUtil.getFS()
242
				+ PropertyService.getProperty("application.context");
243
	}
244

    
245
	/**
246
	 * Attempt to discover the context for this application. This is a best
247
	 * guess scenario. It is used by configuration routines before the context
248
	 * has been populated in metacat.properties. You should always use
249
	 * getApplicationContext() instead of this method if possible.
250
	 * 
251
	 * @param request
252
	 *            the http servlet request we will use to find the context
253
	 * 
254
	 * @return a string holding the context
255
	 */
256
	public static String discoverApplicationContext(HttpServletRequest request) {
257
		String contextPath = request.getContextPath();
258

    
259
		if (contextPath.charAt(0) == '/') {
260
			contextPath = contextPath.substring(1);
261
		}
262
		logMetacat.debug("contextPath: " + contextPath);
263

    
264
		return contextPath;
265
	}
266

    
267
	/**
268
	 * Get the style skins directory. This is made up of the tomcat directory
269
	 * with context + file separator + "style" + file separator + "skins"
270
	 * 
271
	 * @return string holding the style skins directory
272
	 */
273
	public static String getStyleSkinsDir() throws PropertyNotFoundException {
274
		return getContextDir() + FileUtil.getFS() + "style" + FileUtil.getFS()
275
				+ "skins";
276
	}
277

    
278
	/**
279
	 * Get the SQL directory. This is made up of the context directory + file
280
	 * separator + sql
281
	 * 
282
	 * @return string holding the sql directory
283
	 */
284
	public static String getSQLDir() throws PropertyNotFoundException {
285
		return getContextDir() + FileUtil.getFS() + "WEB-INF" + FileUtil.getFS() + "sql";
286
	}
287

    
288
	/**
289
	 * Get the default style URL from metacat.properties.
290
	 * 
291
	 * @return string holding the default style URL
292
	 */
293
	public static String getDefaultStyleURL() throws PropertyNotFoundException {
294
		return getStyleCommonURL() + FileUtil.getFS()
295
				+ PropertyService.getProperty("application.default-style");
296
	}
297

    
298
	/**
299
	 * Attempt to discover the deployment directory for this application. This is a
300
	 * best guess scenario. It is used by configuration routines before the
301
	 * deployment directory has been populated in metacat.properties. 
302
	 * 
303
	 * @param request
304
	 *            the http servlet request we will use to find the tomcat directory
305
	 * 
306
	 * @return a string holding the web application directory
307
	 */
308
	public static String discoverDeployDir(HttpServletRequest request) {
309
		ServletContext servletContext = request.getSession()
310
				.getServletContext();
311
		String realPath = servletContext.getRealPath(".");
312
		String contextPath = request.getContextPath();
313
		
314
		logMetacat.debug("realPath: " + realPath);
315
		logMetacat.debug("contextPath: " + contextPath);
316

    
317
		Pattern pattern = Pattern.compile(contextPath + "/\\.$");
318
		Matcher matcher = pattern.matcher(realPath);
319
		
320
		if (matcher.find()) {
321
			realPath = matcher.replaceFirst("");
322
		}
323
		
324
		return realPath;
325
	}
326

    
327
}
(10-10/11)