Revision 5847
Added by ben leinfelder about 13 years ago
GeoserverUtil.java | ||
---|---|---|
26 | 26 |
|
27 | 27 |
package edu.ucsb.nceas.metacat.util; |
28 | 28 |
|
29 |
import org.apache.commons.httpclient.HttpClient; |
|
30 | 29 |
import org.apache.log4j.Logger; |
31 | 30 |
|
32 |
import java.io.IOException; |
|
33 |
import java.util.HashMap; |
|
31 |
import java.io.StringReader; |
|
34 | 32 |
|
35 | 33 |
import edu.ucsb.nceas.metacat.properties.PropertyService; |
36 | 34 |
import edu.ucsb.nceas.metacat.shared.MetacatUtilException; |
37 | 35 |
import edu.ucsb.nceas.utilities.FileUtil; |
36 |
import edu.ucsb.nceas.utilities.GeneralPropertyException; |
|
38 | 37 |
import edu.ucsb.nceas.utilities.PropertyNotFoundException; |
38 |
import edu.ucsb.nceas.utilities.SortedProperties; |
|
39 |
import edu.ucsb.nceas.utilities.UtilException; |
|
39 | 40 |
|
40 | 41 |
public class GeoserverUtil { |
41 | 42 |
|
... | ... | |
47 | 48 |
*/ |
48 | 49 |
private GeoserverUtil() {} |
49 | 50 |
|
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() + "/" |
|
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 | 51 |
|
112 | 52 |
/** |
113 | 53 |
* Change the password on the geoserver. The loginAdmin method must have |
... | ... | |
122 | 62 |
* @param password |
123 | 63 |
* the new password |
124 | 64 |
*/ |
125 |
public static void changePassword(HttpClient httpClient, String username, String password)
|
|
65 |
public static void changePassword(String username, String password) |
|
126 | 66 |
throws MetacatUtilException { |
127 | 67 |
try { |
128 | 68 |
|
129 |
HashMap<String, String> paramMap = new HashMap<String, String>(); |
|
130 |
paramMap.put("username", username); |
|
131 |
paramMap.put("password", password); |
|
69 |
// 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); |
|
132 | 84 |
|
133 |
// Create the post url from values in metacat.properties |
|
134 |
String passwordPostPage = |
|
135 |
PropertyService.getProperty("geoserver.passwordPostPage"); |
|
136 |
String passwordPostURL = SystemUtil.getContextURL() + "/" |
|
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() + "/" |
|
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 |
} |
|
85 |
} catch (Exception e) { |
|
86 |
throw new MetacatUtilException("Property error while changing default password: " |
|
87 |
+ e.getMessage()); |
|
88 |
} |
|
169 | 89 |
} |
170 | 90 |
|
171 | 91 |
/** |
... | ... | |
184 | 104 |
// geoserver is configured if not unconfigured |
185 | 105 |
return !geoserverConfiguredString.equals(PropertyService.UNCONFIGURED); |
186 | 106 |
} |
107 |
|
|
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 |
} |
|
187 | 152 |
} |
Also available in: Unified diff
rework geoserver configuration:
-geoserver context is set to 'geoserver' by default, but can be reconfigured
-data directory is set in the geoserver web.xml file (we have a template, set the value accordingly, then overwrite the deployed version in the geoserver webapp)
-the admin password is set directly in the data/security/users.properties file