1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2003 Regents of the University of California.
|
4
|
*
|
5
|
* '$Author: harris $'
|
6
|
* '$Date: 2006-03-02 19:32:34 -0800 (Thu, 02 Mar 2006) $'
|
7
|
* '$Revision: 2945 $'
|
8
|
*
|
9
|
* This program is free software; you can redistribute it and/or modify
|
10
|
* it under the terms of the GNU General Public License as published by
|
11
|
* the Free Software Foundation; either version 2 of the License, or
|
12
|
* (at your option) any later version.
|
13
|
*
|
14
|
* This program is distributed in the hope that it will be useful,
|
15
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
* GNU General Public License for more details.
|
18
|
*
|
19
|
* You should have received a copy of the GNU General Public License
|
20
|
* along with this program; if not, write to the Free Software
|
21
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
22
|
*/
|
23
|
package edu.ucsb.nceas.metacat.spatial;
|
24
|
|
25
|
import java.io.File;
|
26
|
import java.io.FileReader;
|
27
|
import java.io.PrintWriter;
|
28
|
import java.io.IOException;
|
29
|
import java.io.StringReader;
|
30
|
import java.io.BufferedReader;
|
31
|
import java.io.Reader;
|
32
|
import java.io.FileInputStream;
|
33
|
import java.io.BufferedInputStream;
|
34
|
import java.util.Enumeration;
|
35
|
import java.util.Hashtable;
|
36
|
import java.util.ResourceBundle;
|
37
|
import java.util.Random;
|
38
|
import java.util.StringTokenizer;
|
39
|
import java.util.Properties;
|
40
|
import java.util.PropertyResourceBundle;
|
41
|
import java.util.Vector;
|
42
|
import java.util.Map;
|
43
|
|
44
|
import java.net.MalformedURLException;
|
45
|
import java.sql.PreparedStatement;
|
46
|
import java.sql.ResultSet;
|
47
|
import java.sql.Connection;
|
48
|
import java.sql.SQLException;
|
49
|
import java.lang.reflect.*;
|
50
|
import java.net.*;
|
51
|
import java.util.zip.*;
|
52
|
|
53
|
import javax.servlet.ServletConfig;
|
54
|
import javax.servlet.ServletContext;
|
55
|
import javax.servlet.ServletException;
|
56
|
import javax.servlet.ServletInputStream;
|
57
|
import javax.servlet.http.HttpServlet;
|
58
|
import javax.servlet.http.HttpServletRequest;
|
59
|
import javax.servlet.http.HttpServletResponse;
|
60
|
import javax.servlet.http.HttpSession;
|
61
|
import javax.servlet.http.HttpUtils;
|
62
|
import javax.servlet.ServletOutputStream;
|
63
|
|
64
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
65
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
66
|
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
|
67
|
import edu.ucsb.nceas.metacat.client.MetacatException;
|
68
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
69
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
70
|
import edu.ucsb.nceas.utilities.IOUtil;
|
71
|
|
72
|
import org.apache.log4j.Logger;
|
73
|
import org.apache.log4j.BasicConfigurator;
|
74
|
import org.apache.log4j.PropertyConfigurator;
|
75
|
|
76
|
|
77
|
|
78
|
/**
|
79
|
* An application that manages the registration of ecological networks using the
|
80
|
* metacat technology
|
81
|
*/
|
82
|
public class SpatialQueryManager extends HttpServlet
|
83
|
{
|
84
|
|
85
|
private ServletConfig config = null;
|
86
|
private ServletContext context = null;
|
87
|
private Properties appConfig = null;
|
88
|
private static final String CONFIG_DIR = "WEB-INF";
|
89
|
private static final String CONFIG_NAME = "econet.properties";
|
90
|
private static final String STYLE_DIR = "WEB-INF/";
|
91
|
private String defaultStyle = null;
|
92
|
private String metacatUrl = null;
|
93
|
private String queryName = null;
|
94
|
private String invalidRegistrationErrorPage = null;
|
95
|
|
96
|
// private EcoNetwork network;
|
97
|
|
98
|
private String mcUser = "uid=jones,o=NCEAS,dc=ecoinformatics,dc=org";
|
99
|
private String mcPass = "@mcpass@";
|
100
|
|
101
|
// private EconetCache cache;
|
102
|
|
103
|
protected SpatialQueryProcessor queryProcessor;
|
104
|
// this is the key val for the econet summary to be in the cache and
|
105
|
// it must be the same value as the value in the manager
|
106
|
// private static String ECONET_SUMMARY_KEY = "econet_summary";
|
107
|
|
108
|
private static Logger log = Logger.getLogger(SpatialQueryManager.class.getName());
|
109
|
|
110
|
/**
|
111
|
* Initialize the servlet during container startup
|
112
|
*/
|
113
|
public void init(ServletConfig config) throws ServletException
|
114
|
{
|
115
|
try
|
116
|
{
|
117
|
super.init(config);
|
118
|
this.config = config;
|
119
|
this.context = config.getServletContext();
|
120
|
|
121
|
//BasicConfigurator.configure();
|
122
|
String prefix = getServletContext().getRealPath("/");
|
123
|
System.out.println("SpatialQueryManager servlet context path ####################### >>> " + prefix);
|
124
|
String file = getInitParameter("log4j-init-file");
|
125
|
System.out.println("SpatialQueryManager log4j-init-file ####################### >>> " + file);
|
126
|
System.out.println("SpatialQueryManager econet - metacat acct: ####################### >>> " + mcUser);
|
127
|
|
128
|
// if the log4j-init-file is not set, then no point in trying
|
129
|
if (file != null)
|
130
|
{
|
131
|
PropertyConfigurator.configure(prefix + file);
|
132
|
}
|
133
|
|
134
|
try
|
135
|
{
|
136
|
// locate, open, and read the properties
|
137
|
appConfig = new Properties();
|
138
|
String dirPath = context.getRealPath(CONFIG_DIR);
|
139
|
File propertyFile = new File(dirPath, CONFIG_NAME);
|
140
|
FileInputStream fis = new FileInputStream(propertyFile);
|
141
|
appConfig.load(fis);
|
142
|
fis.close();
|
143
|
defaultStyle = (String) appConfig.get("defaultStyle");
|
144
|
metacatUrl = (String) appConfig.get("metacatUrl");
|
145
|
System.out.println("SpatialQueryManager using metacat at url: ####################### >>> " + metacatUrl);
|
146
|
queryName = (String)appConfig.get("queryName");
|
147
|
System.out.println("SpatialQueryManager query name : ####################### >>> " + queryName);
|
148
|
invalidRegistrationErrorPage = (String) appConfig.get("invalidRegistrationErrorPage");
|
149
|
|
150
|
}
|
151
|
catch(Throwable t)
|
152
|
{
|
153
|
throw new ServletException(t.getMessage());
|
154
|
}
|
155
|
}
|
156
|
catch(ServletException ex) {
|
157
|
throw ex;
|
158
|
}
|
159
|
}
|
160
|
|
161
|
/** Handle "GET" method requests from HTTP clients */
|
162
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
163
|
throws ServletException, IOException
|
164
|
{
|
165
|
handleGetOrPost(request, response);
|
166
|
}
|
167
|
|
168
|
/** Handle "POST" method requests from HTTP clients */
|
169
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
170
|
throws ServletException, IOException
|
171
|
{
|
172
|
handleGetOrPost(request, response);
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* Control servlet response depending on the action parameter specified
|
177
|
*/
|
178
|
private void handleGetOrPost(HttpServletRequest request, HttpServletResponse response)
|
179
|
throws ServletException, IOException
|
180
|
{
|
181
|
Hashtable params = getParamHash(request);
|
182
|
String action = getAction(request);
|
183
|
log.debug(">> action " + action);
|
184
|
|
185
|
if (action.equals("list")) {
|
186
|
handleListAction(request, response);
|
187
|
} else if (action.equals("register")) {
|
188
|
//handleRegisterAction(request, response);
|
189
|
} else if (action.equals("delete")) {
|
190
|
handleDeleteAction(request, response);
|
191
|
} else if (action.equals("displaydoc")) {
|
192
|
handleDisplayDocAction(request, response);
|
193
|
} else {
|
194
|
handleListAction(request, response);
|
195
|
}
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* Reads a document from metacat and displays the information to the browser
|
200
|
* @param request -- the HTTP request object
|
201
|
* @param response -- the HTTP response object
|
202
|
*/
|
203
|
private void handleDisplayDocAction(HttpServletRequest request, HttpServletResponse response)
|
204
|
throws IOException
|
205
|
{
|
206
|
Hashtable params = getParamHash(request);
|
207
|
String contextPath = request.getContextPath();
|
208
|
String action = getAction(request);
|
209
|
String styleSystemId = getStyleSystemId(action, defaultStyle);
|
210
|
String docid = (String) getParamHash(request).get("docId");
|
211
|
PrintWriter out = response.getWriter();
|
212
|
ServletContext servletContext = getServletContext();
|
213
|
response.setContentType ("text/html");
|
214
|
if (docid != null && !docid.trim().equals(""))
|
215
|
{
|
216
|
try {
|
217
|
String doc = null;
|
218
|
// if ( cache.recover(docid) == null ) {
|
219
|
log.debug("docid in the cache is null ... going to the db");
|
220
|
Metacat m = MetacatFactory.createMetacatConnection(metacatUrl);
|
221
|
m.login(mcUser, mcPass);
|
222
|
Reader r = m.read(docid);
|
223
|
doc = IOUtil.getAsString(r, true);
|
224
|
r.close();
|
225
|
m.logout();
|
226
|
// adding this was giving a concurrent modification exception
|
227
|
//cache.admit(docid, doc);
|
228
|
// } else {
|
229
|
// log.debug("docid in the cache is valid");
|
230
|
// doc = (String)cache.recover(docid);
|
231
|
// }
|
232
|
String dirPath = context.getRealPath(CONFIG_DIR);
|
233
|
XSLTransform.transform(doc, styleSystemId, out, params);
|
234
|
out.close();
|
235
|
}
|
236
|
catch(Exception e) { e.printStackTrace(); }
|
237
|
} else {
|
238
|
out.println("no such docid!");
|
239
|
}
|
240
|
out.close();
|
241
|
}
|
242
|
|
243
|
/**
|
244
|
* Deletes a network from the registration system. This is called by the
|
245
|
* administrator to delete a registry from metacat. There is no response to
|
246
|
* the calling device - instead details are written to the logger
|
247
|
* @param request -- the HTTP request object
|
248
|
* @param response -- the HTTP response object
|
249
|
*/
|
250
|
private void handleDeleteAction(HttpServletRequest request, HttpServletResponse response) throws IOException
|
251
|
{
|
252
|
Hashtable params = getParamHash(request);
|
253
|
String docId = (String)params.get("docId");
|
254
|
// for the Metacat.delete request to be called the password needs to be the
|
255
|
// same as the mcpass var.
|
256
|
String pass = (String)params.get("password");
|
257
|
if ( params.containsKey("password") ) {
|
258
|
if ( pass.equals(mcPass) ) {
|
259
|
log.info(">> deleting docid: " + docId);
|
260
|
// add a lookup for an issued password.
|
261
|
try
|
262
|
{
|
263
|
Metacat m = MetacatFactory.createMetacatConnection(metacatUrl);
|
264
|
log.info(mcUser + " " + mcPass);
|
265
|
m.login(mcUser, mcPass);
|
266
|
String resp = m.delete(docId);
|
267
|
log.info(">>deletion success is: " + resp);
|
268
|
}
|
269
|
catch(Exception e) { e.printStackTrace(); }
|
270
|
}
|
271
|
else { log.fatal("attempt to delete a registry with invalid pass"); }
|
272
|
}
|
273
|
else { log.fatal("attempt to delete a registry with NO pass"); }
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* Display the registered networks. This is called when a list of
|
278
|
* regestered networks is requested for display and a transformation
|
279
|
* is needed. It queries the registered networks and then transforms the
|
280
|
* network docs using XSLT and displays the networks.
|
281
|
* @param request -- the HTTP request object
|
282
|
* @param response -- the HTTP response object
|
283
|
*/
|
284
|
private void handleListAction(HttpServletRequest request, HttpServletResponse response) throws IOException
|
285
|
{
|
286
|
Hashtable params = getParamHash(request);
|
287
|
String contextPath = request.getContextPath();
|
288
|
String action = getAction(request);
|
289
|
String styleSystemId = getStyleSystemId(action, defaultStyle);
|
290
|
|
291
|
//String style = (String)pathData.get(1);
|
292
|
PrintWriter out = response.getWriter();
|
293
|
ServletContext servletContext = getServletContext();
|
294
|
response.setContentType ("text/html");
|
295
|
|
296
|
// get the query document
|
297
|
String dirPath = context.getRealPath(CONFIG_DIR);
|
298
|
File queryFile = new File(dirPath, queryName);
|
299
|
// Query for the documents from the server
|
300
|
String queryResult = null;
|
301
|
try {
|
302
|
// if the cache doesnt have the summary then get it from the database
|
303
|
// if ( cache.recover(ECONET_SUMMARY_KEY) == null ) {
|
304
|
// log.debug("summary in the cache is null ... going to the db");
|
305
|
queryResult = queryProcessor.execute();
|
306
|
// cache.admit(ECONET_SUMMARY_KEY, queryResult);
|
307
|
// } else {
|
308
|
// log.debug("summary in the cache is valid");
|
309
|
// queryResult = (String)cache.recover(ECONET_SUMMARY_KEY);
|
310
|
// }
|
311
|
} catch(Exception e) { e.printStackTrace(); }
|
312
|
|
313
|
log.debug("query result >> " + queryResult);
|
314
|
XSLTransform.transform(queryResult, styleSystemId, out, params);
|
315
|
out.close();
|
316
|
}
|
317
|
|
318
|
|
319
|
|
320
|
/**
|
321
|
* Register the network. This is called when the attributes of a network
|
322
|
* are submitted to the system.
|
323
|
*
|
324
|
* @param request -- the HTTP request object
|
325
|
* @param response -- the HTTP response object
|
326
|
*/
|
327
|
/** private void handleRegisterAction(HttpServletRequest request, HttpServletResponse response) throws IOException
|
328
|
{
|
329
|
Map params = getParamHash(request);
|
330
|
String contextPath = request.getContextPath();
|
331
|
String action = getAction(request);
|
332
|
|
333
|
network = new EcoNetwork((Map) params);
|
334
|
// add a validation step here
|
335
|
if (network.isValid() == true)
|
336
|
{
|
337
|
PrintWriter out = response.getWriter();
|
338
|
response.setContentType ("text/html");
|
339
|
log.info("about to insert: " + network.toXML());
|
340
|
String metacatResponse = insertEcoNetwork(network.toXML());
|
341
|
log.info("metacat response: " + metacatResponse);
|
342
|
|
343
|
String styleSystemId = getStyleSystemId(action, defaultStyle);
|
344
|
log.info("registration system id " + styleSystemId);
|
345
|
XSLTransform.transform(metacatResponse, styleSystemId, out, (Hashtable) params);
|
346
|
|
347
|
// clear the summary from the cache which will be recovered either next
|
348
|
// time the show all docs function is called or the ttl in the cache is
|
349
|
// achived.
|
350
|
cache.clear();
|
351
|
|
352
|
out.close();
|
353
|
} else
|
354
|
{
|
355
|
//redirect somewhere
|
356
|
response.sendRedirect(invalidRegistrationErrorPage);
|
357
|
}
|
358
|
}
|
359
|
**/
|
360
|
|
361
|
|
362
|
/**
|
363
|
* Inserts the registration document to metacat. After the registration
|
364
|
* has been processed and validated the XML representation is submitted
|
365
|
* to the metacat system
|
366
|
* @param networkXML -- the xml document containing the econetwork
|
367
|
* data represented as a java string.
|
368
|
* @return metacatResponse -- the response from metacat
|
369
|
*/
|
370
|
/** private String insertEcoNetwork(String networkXML)
|
371
|
{
|
372
|
String error = null;
|
373
|
String response = null;
|
374
|
try {
|
375
|
Metacat m = MetacatFactory.createMetacatConnection(metacatUrl);
|
376
|
log.info(mcUser + " " + mcPass);
|
377
|
m.login(mcUser, mcPass);
|
378
|
int i = (int) (java.lang.Math.random() * 1000);
|
379
|
log.info(" inserting document: " + "econet." + i + ".1");
|
380
|
response = m.insert("econet." + i + ".1", new StringReader(networkXML), null);
|
381
|
log.info(" metacat response: " + response);
|
382
|
}
|
383
|
catch(Exception e) {
|
384
|
e.printStackTrace();
|
385
|
}
|
386
|
return response;
|
387
|
}
|
388
|
**/
|
389
|
|
390
|
|
391
|
/**
|
392
|
* Gets the "action" parameter from the http request object
|
393
|
* @param request -- the http request object
|
394
|
* @return action -- the action requested of this servlet
|
395
|
*/
|
396
|
private String getAction(HttpServletRequest request)
|
397
|
throws NullPointerException
|
398
|
{
|
399
|
String action = (String) getParamHash(request).get("action");
|
400
|
return action;
|
401
|
}
|
402
|
|
403
|
/**
|
404
|
* Get the stylesheet systemid from the properties file for a given
|
405
|
* action and style set.
|
406
|
*
|
407
|
* @param action the servlet request action
|
408
|
* @param style the name of the style to be used
|
409
|
* @return a String containing the system id of the stylesheet to be used
|
410
|
*/
|
411
|
private String getStyleSystemId(String action, String style)
|
412
|
throws IOException
|
413
|
{
|
414
|
String key = action + "." + style;
|
415
|
//log.info("key >>" + key);
|
416
|
String filename = (String) appConfig.get(key);
|
417
|
//log.info("filename >>" + filename);
|
418
|
String stylePath = context.getRealPath(STYLE_DIR);
|
419
|
//log.info("stylePath>>" + stylePath);
|
420
|
File xslFile = new File(stylePath, filename);
|
421
|
String systemId = xslFile.getCanonicalPath();
|
422
|
systemId = "file://" + systemId;
|
423
|
log.info("returning systemId >> " + systemId + " for action.style: " + key);
|
424
|
return systemId;
|
425
|
}
|
426
|
|
427
|
/**
|
428
|
* utility function to convert the request object into a hashtable
|
429
|
* @param request -- the http request object
|
430
|
* @return paramHash -- hashtable containing the name-value pairs
|
431
|
*/
|
432
|
private Hashtable getParamHash(HttpServletRequest request)
|
433
|
{
|
434
|
Hashtable params = new Hashtable();
|
435
|
try {
|
436
|
Enumeration _enum = request.getParameterNames();
|
437
|
while (_enum.hasMoreElements()) {
|
438
|
String name = (String) _enum.nextElement();
|
439
|
String values[] = request.getParameterValues(name);
|
440
|
if (values != null) {
|
441
|
for (int i = 0; i < values.length; i++) {
|
442
|
params.put(name, values[i]);
|
443
|
}
|
444
|
}
|
445
|
}
|
446
|
}
|
447
|
catch(Exception e) {
|
448
|
e.printStackTrace();
|
449
|
}
|
450
|
return (params);
|
451
|
}
|
452
|
|
453
|
}
|