Project

General

Profile

1
/**
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.commons.httpclient.HttpClient;
30
import org.apache.log4j.Logger;
31

    
32
import java.io.IOException;
33
import java.util.HashMap;
34

    
35
import edu.ucsb.nceas.metacat.properties.PropertyService;
36
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
37
import edu.ucsb.nceas.utilities.FileUtil;
38
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
39

    
40
public class GeoserverUtil {
41
	
42
	private static Logger logMetacat = Logger.getLogger(GeoserverUtil.class);
43
	
44
	/**
45
	 * private constructor - all methods are static so there is no
46
     * no need to instantiate.
47
	 */
48
	private GeoserverUtil() {}
49
	
50
	/**
51
	 * Log in to geoserver as the administrative user. If the geoserver.username
52
	 * and geoserver.password already exists in metacat.properties, we assume
53
	 * that the password was already changed at least once. We use these values.
54
	 * Otherwise we use the default values.
55
	 * 
56
	 * @param httpClient
57
	 *            the HttpClient we will use to post. This is passed in since it
58
	 *            may need to be used after login by other methods.
59
	 */
60
	public static void loginAdmin(HttpClient httpClient) throws MetacatUtilException {
61
		try {
62
			String username = 
63
				PropertyService.getProperty("geoserver.username");
64
			String password = 
65
				PropertyService.getProperty("geoserver.password");
66
			String defaultUserName = 
67
				PropertyService.getProperty("geoserver.defaultUsername");
68
			String defaultPassword = 
69
				PropertyService.getProperty("geoserver.defaultPassword");
70
			String loginSuccessString = 
71
				PropertyService.getProperty("geoserver.loginSuccessString");
72
			
73
			HashMap<String, String> paramMap = new HashMap<String, String>();
74
			// if the username and password exist, we assume the geoserver was 
75
			// already updated at least once.  Use the existing values as the 
76
			// current admin values.  Otherwise,use the default values.
77
			if (username != null && !username.equals("")
78
					&& password != null && !password.equals("")) {
79
				paramMap.put("username", username);
80
				paramMap.put("password", password);
81
			} else {
82
				paramMap.put("username", defaultUserName);
83
				paramMap.put("password", defaultPassword);
84
			}
85
			
86
			// Create the post url from values in metacat.properties
87
			String loginPostPage = 
88
				PropertyService.getProperty("geoserver.loginPostPage");
89
			String loginPostURL = SystemUtil.getContextURL() + FileUtil.getFS()
90
					+ loginPostPage;
91
			
92
			logMetacat.debug("loginPostURL: " + loginPostURL);
93
			
94
			// Do the post
95
			String postResult = RequestUtil.post(httpClient, loginPostURL, paramMap);
96
			
97
			// check to see if the success string is part of the result
98
			if(postResult.indexOf(loginSuccessString) == -1) {
99
				logMetacat.debug(postResult);
100
				throw new MetacatUtilException("Could not log in to geoserver.");
101
			}
102

    
103
		} catch (PropertyNotFoundException pnfe) {
104
			throw new MetacatUtilException("Property error while logging in with " 
105
					+ "default account: " + pnfe.getMessage());
106
		} catch (IOException ioe) {
107
			throw new MetacatUtilException("I/O error while logging in with " 
108
					+ "default account: " + ioe.getMessage());
109
		} 
110
	}
111
	
112
	/**
113
	 * Change the password on the geoserver. The loginAdmin method must have
114
	 * already been called using the same HttpClient that is passed to this
115
	 * method.
116
	 * 
117
	 * @param httpClient
118
	 *            the HttpClient we will use to post. This should have been used
119
	 *            in the login post.
120
	 * @param username
121
	 *            the new user name
122
	 * @param password
123
	 *            the new password
124
	 */
125
	public static void changePassword(HttpClient httpClient, String username, String password) 
126
		throws MetacatUtilException {
127
		try {	
128
			
129
			HashMap<String, String> paramMap = new HashMap<String, String>();
130
			paramMap.put("username", username);
131
			paramMap.put("password", password);
132
			
133
			// Create the post url from values in metacat.properties
134
			String passwordPostPage = 
135
				PropertyService.getProperty("geoserver.passwordPostPage");
136
			String passwordPostURL = SystemUtil.getContextURL() + FileUtil.getFS()
137
					+ passwordPostPage;
138
			
139
			String passwordSuccessString = 
140
				PropertyService.getProperty("geoserver.passwordSuccessString");
141

    
142
			logMetacat.debug("passwordPostURL: " + passwordPostURL);
143
			
144
			// Do the post
145
			String postResult = RequestUtil.post(httpClient, passwordPostURL, paramMap);
146
			
147
			// check to see if the success string is part of the result
148
			if(postResult.indexOf(passwordSuccessString) == -1) {
149
				logMetacat.debug(postResult);
150
				throw new MetacatUtilException("Could not change geoserver password.");
151
			}
152
			
153
			// send a post to apply the password changes.  Unfortunately, there really
154
			// isn't anything of the geoserver side saying if the post passed, so we have
155
			// to assume it did.
156
			String applyPostPage = 
157
				PropertyService.getProperty("geoserver.applyPostPage");
158
			String applyPostURL = SystemUtil.getContextURL() + FileUtil.getFS()
159
				+ applyPostPage;
160
			RequestUtil.post(httpClient, applyPostURL, null);		
161
			
162
		} catch (PropertyNotFoundException pnfe) {
163
			throw new MetacatUtilException("Property error while logging in with " 
164
					+ "default account: " + pnfe.getMessage());
165
		} catch (IOException ioe) {
166
			throw new MetacatUtilException("I/O error while logging in with " 
167
					+ "default account: " + ioe.getMessage());
168
		} 
169
	}
170
	
171
	/**
172
	 * Reports whether geoserver is configured.
173
	 * 
174
	 * @return a boolean that is true if geoserver is configured or bypassed
175
	 */
176
	public static boolean isGeoserverConfigured() throws MetacatUtilException {
177
		String geoserverConfiguredString = PropertyService.UNCONFIGURED;
178
		try {
179
			geoserverConfiguredString = PropertyService.getProperty("configutil.geoserverConfigured");
180
		} catch (PropertyNotFoundException pnfe) {
181
			throw new MetacatUtilException("Could not determine if geoservice are configured: "
182
					+ pnfe.getMessage());
183
		}
184
		// geoserver is configured if not unconfigured
185
		return !geoserverConfiguredString.equals(PropertyService.UNCONFIGURED);
186
	}
187
}
(7-7/14)