Revision 6669
Added by ben leinfelder over 12 years ago
ReplicationServlet.java | ||
---|---|---|
43 | 43 |
import javax.servlet.http.HttpServlet; |
44 | 44 |
import javax.servlet.http.HttpServletRequest; |
45 | 45 |
import javax.servlet.http.HttpServletResponse; |
46 |
import javax.servlet.http.HttpSession; |
|
47 | 46 |
|
48 | 47 |
import org.apache.log4j.Logger; |
49 | 48 |
import org.dataone.client.auth.CertificateManager; |
50 | 49 |
|
51 | 50 |
import edu.ucsb.nceas.metacat.service.ServiceService; |
52 |
import edu.ucsb.nceas.metacat.service.SessionService; |
|
53 |
import edu.ucsb.nceas.metacat.shared.MetacatUtilException; |
|
54 | 51 |
import edu.ucsb.nceas.metacat.shared.ServiceException; |
55 |
import edu.ucsb.nceas.metacat.util.AuthUtil; |
|
56 |
import edu.ucsb.nceas.metacat.util.SessionData; |
|
57 | 52 |
|
58 | 53 |
public class ReplicationServlet extends HttpServlet { |
59 | 54 |
|
... | ... | |
117 | 112 |
|
118 | 113 |
try { |
119 | 114 |
// check if the server is included in the list of replicated servers |
120 |
if (!action.equals("servercontrol") && !action.equals("stop") |
|
121 |
&& !action.equals("start") && !action.equals("getall")) { |
|
115 |
server = ((String[]) params.get("server"))[0]; |
|
122 | 116 |
|
123 |
server = ((String[]) params.get("server"))[0]; |
|
124 |
|
|
125 |
// verify the client certificate on the request |
|
126 |
boolean isValid = false; |
|
127 |
String msg = "Client certificate is invalid"; |
|
128 |
try { |
|
129 |
isValid = hasValidCertificate(request, server); |
|
130 |
} catch (Exception e) { |
|
131 |
msg = "Could not verify client certificate: " + e.getMessage(); |
|
132 |
logMetacat.error(msg, e); |
|
133 |
logReplication.error(msg, e); |
|
134 |
} |
|
135 |
if (!isValid) { |
|
136 |
// send message to response |
|
137 |
out = response.getWriter(); |
|
138 |
out.print("<error>"); |
|
139 |
out.print(msg); |
|
140 |
out.print("</error>"); |
|
141 |
out.close(); |
|
142 |
return; |
|
143 |
} |
|
144 |
|
|
145 |
if (ReplicationService.getServerCodeForServerName(server) == 0) { |
|
146 |
logReplication.debug("ReplicationServlet.handleGetOrPost - Action \"" + action + "\" rejected for server: " |
|
147 |
+ server); |
|
148 |
return; |
|
149 |
} else { |
|
150 |
logReplication.debug("ReplicationServlet.handleGetOrPost - Action \"" + action + "\" accepted for server: " |
|
151 |
+ server); |
|
152 |
} |
|
153 |
|
|
117 |
// verify the client certificate on the request |
|
118 |
boolean isValid = false; |
|
119 |
String msg = "Client certificate is invalid"; |
|
120 |
try { |
|
121 |
isValid = hasValidCertificate(request, server); |
|
122 |
} catch (Exception e) { |
|
123 |
msg = "Could not verify client certificate: " + e.getMessage(); |
|
124 |
logMetacat.error(msg, e); |
|
125 |
logReplication.error(msg, e); |
|
126 |
} |
|
127 |
if (!isValid) { |
|
128 |
// send message to response |
|
129 |
out = response.getWriter(); |
|
130 |
out.print("<error>"); |
|
131 |
out.print(msg); |
|
132 |
out.print("</error>"); |
|
133 |
out.close(); |
|
134 |
return; |
|
135 |
} |
|
136 |
|
|
137 |
// we passed the test, now continue |
|
138 |
if (ReplicationService.getServerCodeForServerName(server) == 0) { |
|
139 |
logReplication.debug("ReplicationServlet.handleGetOrPost - Action \"" + action + "\" rejected for server: " + server); |
|
140 |
return; |
|
154 | 141 |
} else { |
155 |
// start, stop, getall and servercontrol need to check if user is administor |
|
156 |
HttpSession sess = request.getSession(true); |
|
157 |
SessionData sessionData = null; |
|
158 |
String sess_id = ""; |
|
159 |
String username = ""; |
|
160 |
String[] groupnames = { "" }; |
|
161 |
|
|
162 |
if (params.containsKey("sessionid")) { |
|
163 |
sess_id = ((String[]) params.get("sessionid"))[0]; |
|
164 |
logReplication.info("ReplicationServlet.handleGetOrPost - in has sessionid " + sess_id); |
|
165 |
if (SessionService.getInstance().isSessionRegistered(sess_id)) { |
|
166 |
logReplication.info("ReplicationServlet.handleGetOrPost - find the id " + sess_id + " in hash table"); |
|
167 |
sessionData = SessionService.getInstance().getRegisteredSession(sess_id); |
|
168 |
} |
|
169 |
} |
|
170 |
if (sessionData == null) { |
|
171 |
sessionData = new SessionData(sess.getId(), |
|
172 |
(String) sess.getAttribute("username"), |
|
173 |
(String[]) sess.getAttribute("groups"), |
|
174 |
(String) sess.getAttribute("password"), |
|
175 |
(String) sess.getAttribute("name")); |
|
176 |
} |
|
177 |
|
|
178 |
username = sessionData.getUserName(); |
|
179 |
logReplication.warn("ReplicationServlet.handleGetOrPost - The user name from session is: " + username); |
|
180 |
groupnames = sessionData.getGroupNames(); |
|
181 |
if (!AuthUtil.isAdministrator(username, groupnames)) { |
|
182 |
out = response.getWriter(); |
|
183 |
out.print("<error>"); |
|
184 |
out.print("The user \"" + username |
|
185 |
+ "\" is not authorized for this action."); |
|
186 |
out.print("</error>"); |
|
187 |
out.close(); |
|
188 |
logReplication.warn("ReplicationServlet.handleGetOrPost - The user \"" + username |
|
189 |
+ "\" is not authorized for this action: " + action); |
|
190 |
return; |
|
191 |
} |
|
192 |
|
|
193 |
}// else |
|
194 |
|
|
142 |
logReplication.debug("ReplicationServlet.handleGetOrPost - Action \"" + action + "\" accepted for server: " + server); |
|
143 |
} |
|
144 |
|
|
145 |
// perform the correct action |
|
195 | 146 |
if (action.equals("readdata")) { |
196 | 147 |
OutputStream outStream = response.getOutputStream(); |
197 | 148 |
//to get the data file. |
... | ... | |
200 | 151 |
} else if (action.equals("forcereplicatedatafile")) { |
201 | 152 |
//read a specific docid from remote host, and store it into local host |
202 | 153 |
ReplicationService.handleForceReplicateDataFileRequest(params, request); |
203 |
} else if (action.equals("stop")) { |
|
204 |
// stop the replication server |
|
205 |
ReplicationService.getInstance().stopReplication(); |
|
206 |
out = response.getWriter(); |
|
207 |
out.println("Replication Handler Stopped"); |
|
208 |
} else if (action.equals("start")) { |
|
209 |
ReplicationService.getInstance().startReplication(params); |
|
210 |
out = response.getWriter(); |
|
211 |
out.println("Replication Handler Started"); |
|
212 |
} else if (action.equals("getall")) { |
|
213 |
ReplicationService.getInstance().runOnce(); |
|
214 |
response.setContentType("text/html"); |
|
215 |
out = response.getWriter(); |
|
216 |
out.println("<html><body>\"Get All\" Done</body></html>"); |
|
217 | 154 |
} else if (action.equals("forcereplicate")) { |
218 |
// read a specific docid from remote host, and store it into |
|
219 |
// local host |
|
220 |
ReplicationService.handleForceReplicateRequest(params, response, |
|
221 |
request); |
|
155 |
// read a specific docid from remote host, and store it into local host |
|
156 |
ReplicationService.handleForceReplicateRequest(params, response, request); |
|
222 | 157 |
} else if (action.equals("forcereplicatesystemmetadata")) { |
223 |
ReplicationService.handleForceReplicateSystemMetadataRequest(params, response, |
|
224 |
request); |
|
158 |
ReplicationService.handleForceReplicateSystemMetadataRequest(params, response, request); |
|
225 | 159 |
} else if (action.equals("forcereplicatedelete")) { |
226 |
// read a specific docid from remote host, and store it into |
|
227 |
// local host |
|
228 |
ReplicationService.handleForceReplicateDeleteRequest(params, |
|
229 |
response, request); |
|
160 |
// read a specific docid from remote host, and store it into local host |
|
161 |
ReplicationService.handleForceReplicateDeleteRequest(params, response, request); |
|
230 | 162 |
} else if (action.equals("update")) { |
231 | 163 |
// request an update list from the server |
232 | 164 |
ReplicationService.handleUpdateRequest(params, response); |
... | ... | |
245 | 177 |
ReplicationService.handleGetTimeRequest(params, response); |
246 | 178 |
} else if (action.equals("getcatalog")) { |
247 | 179 |
ReplicationService.handleGetCatalogRequest(params, response, true); |
248 |
} else if (action.equals("servercontrol")) { |
|
249 |
ReplicationService.handleServerControlRequest(params, response); |
|
250 | 180 |
} else if (action.equals("test")) { |
251 | 181 |
response.setContentType("text/html"); |
252 | 182 |
out = response.getWriter(); |
... | ... | |
256 | 186 |
} catch (ServiceException e) { |
257 | 187 |
logMetacat.error("ReplicationServlet.handleGetOrPost - " + ReplicationService.METACAT_REPL_ERROR_MSG); |
258 | 188 |
logReplication.error("ReplicationServlet.handleGetOrPost - Error in ReplicationServlet.handleGetOrPost: " + e.getMessage()); |
259 |
} catch (MetacatUtilException mue) { |
|
260 |
logMetacat.error("ReplicationServlet.handleGetOrPost - " + ReplicationService.METACAT_REPL_ERROR_MSG); |
|
261 |
logReplication.error("ReplicationServlet.handleGetOrPost - Metacat utility error in ReplicationServlet.handleGetOrPost: " |
|
262 |
+ mue.getMessage()); |
|
263 | 189 |
} finally { |
264 | 190 |
if (out != null) { |
265 | 191 |
out.close(); |
Also available in: Unified diff
move replication configuration actions to the admin servlet and out of the replication servlet
http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5528