Project

General

Profile

1 4207 daigle
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements administrative 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-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
10
 * '$Revision: 4080 $'
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 org.apache.log4j.Logger;
30
31 5847 leinfelder
import java.io.StringReader;
32 4207 daigle
33 5030 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
34 5015 daigle
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
35 4207 daigle
import edu.ucsb.nceas.utilities.FileUtil;
36 5847 leinfelder
import edu.ucsb.nceas.utilities.GeneralPropertyException;
37 4207 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
38 5847 leinfelder
import edu.ucsb.nceas.utilities.SortedProperties;
39
import edu.ucsb.nceas.utilities.UtilException;
40 4207 daigle
41
public class GeoserverUtil {
42
43
	private static Logger logMetacat = Logger.getLogger(GeoserverUtil.class);
44
45
	/**
46
	 * private constructor - all methods are static so there is no
47
     * no need to instantiate.
48
	 */
49
	private GeoserverUtil() {}
50
51
52
	/**
53
	 * Change the password on the geoserver. The loginAdmin method must have
54
	 * already been called using the same HttpClient that is passed to this
55
	 * method.
56
	 *
57
	 * @param httpClient
58
	 *            the HttpClient we will use to post. This should have been used
59
	 *            in the login post.
60
	 * @param username
61
	 *            the new user name
62
	 * @param password
63
	 *            the new password
64
	 */
65 5847 leinfelder
	public static void changePassword(String username, String password)
66 4854 daigle
		throws MetacatUtilException {
67 4207 daigle
		try {
68
69 5847 leinfelder
			// the users file
70
			String usersFile =
71
			 PropertyService.getProperty("geoserver.GEOSERVER_DATA_DIR")
72
				+ FileUtil.getFS()
73
				+ "security"
74
				+ FileUtil.getFS()
75
				+ "users.properties";
76
			SortedProperties userProperties = new SortedProperties(usersFile);
77
			userProperties.load();
78
			/* looks like this:
79
				#Wed Jan 26 12:13:09 PST 2011
80
				admin=geoserver,ROLE_ADMINISTRATOR,enabled
81
			*/
82
			String value = password + ",ROLE_ADMINISTRATOR,enabled";
83
			userProperties.setProperty(username, value);
84 4207 daigle
85 5847 leinfelder
		} catch (Exception e) {
86
			throw new MetacatUtilException("Property error while changing default password: "
87
				 + e.getMessage());
88
		}
89 4207 daigle
	}
90
91
	/**
92
	 * Reports whether geoserver is configured.
93
	 *
94
	 * @return a boolean that is true if geoserver is configured or bypassed
95
	 */
96 4854 daigle
	public static boolean isGeoserverConfigured() throws MetacatUtilException {
97 4207 daigle
		String geoserverConfiguredString = PropertyService.UNCONFIGURED;
98
		try {
99
			geoserverConfiguredString = PropertyService.getProperty("configutil.geoserverConfigured");
100
		} catch (PropertyNotFoundException pnfe) {
101 4854 daigle
			throw new MetacatUtilException("Could not determine if geoservice are configured: "
102 4207 daigle
					+ pnfe.getMessage());
103
		}
104
		// geoserver is configured if not unconfigured
105
		return !geoserverConfiguredString.equals(PropertyService.UNCONFIGURED);
106
	}
107 5847 leinfelder
108
	public static void writeConfig() throws MetacatUtilException {
109
		try {
110
			// the template geoserver web.xml file
111
			String configFileTemplate =
112
				SystemUtil.getContextDir()
113
				+ FileUtil.getFS()
114
				+ "web.xml.geoserver";
115
116
			// the destination for the template
117
			String configFileDestination =
118
				PropertyService.getProperty("application.deployDir")
119
				+ FileUtil.getFS()
120
				+ PropertyService.getProperty("geoserver.context")
121
				+ FileUtil.getFS()
122
				+ "WEB-INF"
123
				+ FileUtil.getFS()
124
				+ "web.xml";
125
126
			// look up the configured value for the data dir, write it to the file
127
			String dataDir = PropertyService.getProperty("geoserver.GEOSERVER_DATA_DIR");
128
			String configContents = FileUtil.readFileToString(configFileTemplate, "UTF-8");
129
			configContents = configContents.replace("_GEOSERVER_DATA_DIR_VALUE_", dataDir);
130
			FileUtil.writeFile(configFileDestination, new StringReader(configContents), "UTF-8");
131
		} catch (PropertyNotFoundException pnfe) {
132
			throw new MetacatUtilException(
133
					"Property error while setting geoserver config: " + pnfe.getMessage());
134
		}
135
		catch (UtilException ue) {
136
			throw new MetacatUtilException(
137
					"Util error while setting geoserver config: " + ue.getMessage());
138
		}
139
140
	}
141
142
	/**
143
	 * Get the server URL with the Geoserver context. This is made up of the server URL +
144
	 * file separator + the Geoserver context
145
	 *
146
	 * @return string holding the server URL with context
147
	 */
148
	public static String getGeoserverContextURL() throws PropertyNotFoundException {
149
		return SystemUtil.getServerURL() + "/"
150
				+ PropertyService.getProperty("geoserver.context");
151
	}
152 4207 daigle
}