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-08-04 17:14:28 -0700 (Mon, 04 Aug 2008) $'
10
 * '$Revision: 4202 $'
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 javax.servlet.ServletContext;
30
import javax.servlet.http.HttpServletRequest;
31

    
32
import org.apache.log4j.Logger;
33

    
34
import edu.ucsb.nceas.metacat.MetaCatVersion;
35
import edu.ucsb.nceas.metacat.service.PropertyService;
36
import edu.ucsb.nceas.utilities.FileUtil;
37
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
38

    
39
public class SystemUtil {
40

    
41
	private static Logger logMetacat = Logger.getLogger(SystemUtil.class);
42
	private static String METACAT_SERVLET = "metacat";
43

    
44
	/**
45
	 * private constructor - all methods are static so there is no no need to
46
	 * instantiate.
47
	 */
48
	private SystemUtil() {}
49

    
50
	/**
51
	 * Attempt to discover the server name. The name is retrieved from the http
52
	 * servlet request. This is used by configuration routines before the port
53
	 * has been populated in metacat.properties. it is possible the port that
54
	 * the user configures might be different than the name we get here. You
55
	 * should use getServerPort() instead of this method whenever possible.
56
	 * 
57
	 * @param request
58
	 *            the http servlet request we will use to find the server name
59
	 * 
60
	 * @return a string holding the server name
61
	 */
62
	public static String discoverServerName(HttpServletRequest request) {
63
		String serverName = request.getServerName();
64

    
65
		return serverName;
66
	}
67

    
68
	/**
69
	 * Attempt to discover the server port. The port is retrieved from the http
70
	 * servlet request. This is used by configuration routines before the port
71
	 * has been populated in metacat.properties. it is possible the port that
72
	 * the user configures might be different than the port we get here. You
73
	 * should use getServerPort() instead of this method whenever possible.
74
	 * 
75
	 * @param request
76
	 *            the http servlet request we will use to find the server port
77
	 * 
78
	 * @return a string holding the server port
79
	 */
80
	public static String discoverServerPort(HttpServletRequest request) {
81
		return Integer.toString(request.getServerPort());
82
	}
83
	
84
	/**
85
	 * Attempt to discover the server ssl port. The ssl port is assumed using
86
	 * the standard port. This is used by configuration routines before the port
87
	 * has been populated in metacat.properties. it is possible the prot that
88
	 * the user configures might be different than the port we get here. You
89
	 * should use getServerSSLPort() instead of this method whenever possible.
90
	 * 
91
	 * @param request
92
	 *            the http servlet request we will use to find the server port
93
	 * 
94
	 * @return a string holding the server ssl port
95
	 */
96
	public static String discoverServerSSLPort(HttpServletRequest request) {
97
		String serverPort = discoverServerPort(request);
98

    
99
		if (serverPort.length() == 4 && serverPort.charAt(0) == '8') {
100
			return "8443";
101
		}
102

    
103
		return "443";
104
	}
105

    
106
	/**
107
	 * Get the server URL which is made up of the server name + : + the http
108
	 * port number. Note that if the port is 80, it is left off.
109
	 * 
110
	 * @return string holding the server URL
111
	 */
112
	public static String getServerURL() throws PropertyNotFoundException {
113
		String ServerURL = "http://" + PropertyService.getProperty("server.name");
114
		String httpPort = PropertyService.getProperty("server.httpPort");
115
		if (!httpPort.equals("80")) {
116
			ServerURL += ":" + httpPort;
117
		}
118

    
119
		return ServerURL;
120
	}
121

    
122
	/**
123
	 * Get the secure server URL which is made up of the server name + : + the
124
	 * https port number. Note that if the port is 443, it is left off.
125
	 * 
126
	 * @return string holding the server URL
127
	 */
128
	public static String getSecureServerURL() throws PropertyNotFoundException {
129
		String ServerURL = "https://" + PropertyService.getProperty("server.name");
130
		String httpPort = PropertyService.getProperty("server.httpSSLPort");
131
		if (!httpPort.equals("443")) {
132
			ServerURL = PropertyService.getProperty("server.name") + ":" + httpPort;
133
		}
134

    
135
		return ServerURL;
136
	}
137

    
138
	/**
139
	 * Get the CGI URL which is made up of the server URL + file separator + the
140
	 * CGI directory
141
	 * 
142
	 * @return string holding the server URL
143
	 */
144
	public static String getCGI_URL() throws PropertyNotFoundException{
145
		return getContextURL() 
146
				+ PropertyService.getProperty("application.cgiDir");
147
	}
148

    
149
	/**
150
	 * Get the server URL with the context. This is made up of the server URL +
151
	 * file separator + the context
152
	 * 
153
	 * @return string holding the server URL with context
154
	 */
155
	public static String getContextURL() throws PropertyNotFoundException {
156
		return getServerURL() + FileUtil.getFS()
157
				+ PropertyService.getProperty("application.context");
158
	}
159

    
160
	/**
161
	 * Get the servlet URL. This is made up of the server URL with context +
162
	 * file separator + the metacat servlet name
163
	 * 
164
	 * @return string holding the servlet URL
165
	 */
166
	public static String getServletURL() throws PropertyNotFoundException {
167
		return getContextURL() + FileUtil.getFS() + METACAT_SERVLET;
168
	}
169

    
170
	/**
171
	 * Get the style skins URL. This is made up of the server URL with context +
172
	 * file separator + "style" + file separator + "skins"
173
	 * 
174
	 * @return string holding the style skins URL
175
	 */
176
	public static String getStyleSkinsURL() throws PropertyNotFoundException {
177
		return getContextURL() + FileUtil.getFS() + "style" + FileUtil.getFS()
178
				+ "skins";
179
	}
180

    
181
	/**
182
	 * Get the style common URL. This is made up of the server URL with context +
183
	 * file separator + "style" + file separator + "common"
184
	 * 
185
	 * @return string holding the style common URL
186
	 */
187
	public static String getStyleCommonURL() throws PropertyNotFoundException {
188
		return getContextURL() + FileUtil.getFS() + "style" + FileUtil.getFS()
189
				+ "common";
190
	}
191
	
192
	/**
193
	 * Get the metacat version by getting the string representation from
194
	 * metacat.properties and instantiating a MetaCatVersion object
195
	 * 
196
	 * @return a MetaCatVersion object holding metacat version information
197
	 */
198
	public static MetaCatVersion getMetacatVersion() throws PropertyNotFoundException {
199
		String metacatVersionString = 
200
			PropertyService.getProperty("application.metacatVersion");
201
		return new MetaCatVersion(metacatVersionString);
202
	}
203

    
204
	/**
205
	 * Get the context directory. This is made up of the deployment directory + file
206
	 * separator + context
207
	 * 
208
	 * @return string holding the context directory
209
	 */
210
	public static String getContextDir() throws PropertyNotFoundException {
211
		return PropertyService.getProperty("application.deployDir") + FileUtil.getFS()
212
				+ PropertyService.getProperty("application.context");
213
	}
214

    
215
	/**
216
	 * Attempt to discover the context for this application. This is a best
217
	 * guess scenario. It is used by configuration routines before the context
218
	 * has been populated in metacat.properties. You should always use
219
	 * getApplicationContext() instead of this method if possible.
220
	 * 
221
	 * @param request
222
	 *            the http servlet request we will use to find the context
223
	 * 
224
	 * @return a string holding the context
225
	 */
226
	public static String discoverApplicationContext(HttpServletRequest request) {
227
		String contextPath = request.getContextPath();
228

    
229
		if (contextPath.charAt(0) == '/') {
230
			contextPath = contextPath.substring(1);
231
		}
232
		logMetacat.debug("contextPath: " + contextPath);
233

    
234
		return contextPath;
235
	}
236

    
237
	/**
238
	 * Get the style skins directory. This is made up of the tomcat directory
239
	 * with context + file separator + "style" + file separator + "skins"
240
	 * 
241
	 * @return string holding the style skins directory
242
	 */
243
	public static String getStyleSkinsDir() throws PropertyNotFoundException {
244
		return getContextDir() + FileUtil.getFS() + "style" + FileUtil.getFS()
245
				+ "skins";
246
	}
247

    
248
	/**
249
	 * Get the SQL directory. This is made up of the context directory + file
250
	 * separator + sql
251
	 * 
252
	 * @return string holding the sql directory
253
	 */
254
	public static String getSQLDir() throws PropertyNotFoundException {
255
		return getContextDir() + FileUtil.getFS() + "WEB-INF" + FileUtil.getFS() + "sql";
256
	}
257

    
258
	/**
259
	 * Get the default style URL from metacat.properties.
260
	 * 
261
	 * @return string holding the default style URL
262
	 */
263
	public static String getDefaultStyleURL() throws PropertyNotFoundException {
264
		return getStyleCommonURL() + FileUtil.getFS()
265
				+ PropertyService.getProperty("application.default-style");
266
	}
267

    
268
	/**
269
	 * Attempt to discover the deployment directory for this application. This is a
270
	 * best guess scenario. It is used by configuration routines before the
271
	 * deployment directory has been populated in metacat.properties. 
272
	 * 
273
	 * @param request
274
	 *            the http servlet request we will use to find the tomcat directory
275
	 * 
276
	 * @return a string holding the tomcat directory
277
	 */
278
	public static String discoverDeployDir(HttpServletRequest request) {
279
		ServletContext servletContext = request.getSession()
280
				.getServletContext();
281
		String realPath = servletContext.getRealPath(".");
282
		String contextName = servletContext.getServletContextName();
283
		String serverInfo = servletContext.getServerInfo();
284
		String contextPath = request.getContextPath();
285
		String servletPath = request.getServletPath();
286
		
287
		logMetacat.debug("realPath: " + realPath);
288
		logMetacat.debug("contextName: " + contextName);
289
		logMetacat.debug("serverInfo: " + serverInfo);
290
		logMetacat.debug("contextPath: " + contextPath);
291
		logMetacat.debug("servletPath: " + servletPath);
292
		
293

    
294
		return realPath;
295
	}
296

    
297
}
(10-10/11)