Project

General

Profile

« Previous | Next » 

Revision 4207

Added by daigle about 16 years ago

Intial addition of geoserver utility file

View differences:

src/edu/ucsb/nceas/metacat/util/GeoserverUtil.java
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.service.PropertyService;
36
import edu.ucsb.nceas.utilities.FileUtil;
37
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
38

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

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

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

  

Also available in: Unified diff