1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2000 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* '$Author: Serhan AKIN $'
|
7
|
* '$Date: 2009-06-13 15:28:13 +0300 $'
|
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.restservice;
|
24
|
|
25
|
import java.io.BufferedReader;
|
26
|
import java.io.IOException;
|
27
|
import java.io.InputStream;
|
28
|
import java.io.PrintWriter;
|
29
|
import java.util.Enumeration;
|
30
|
import java.util.Hashtable;
|
31
|
import java.util.Timer;
|
32
|
|
33
|
import javax.servlet.ServletContext;
|
34
|
import javax.servlet.http.HttpServletRequest;
|
35
|
import javax.servlet.http.HttpServletResponse;
|
36
|
|
37
|
import org.apache.commons.io.IOUtils;
|
38
|
import org.apache.log4j.Logger;
|
39
|
import org.dataone.service.exceptions.BaseException;
|
40
|
import org.dataone.service.exceptions.ServiceFailure;
|
41
|
import org.dataone.service.types.AuthToken;
|
42
|
import org.dataone.service.types.IdentifierType;
|
43
|
|
44
|
import edu.ucsb.nceas.metacat.DBUtil;
|
45
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
46
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
47
|
import edu.ucsb.nceas.metacat.MetacatHandler;
|
48
|
import edu.ucsb.nceas.metacat.dataone.CrudService;
|
49
|
import edu.ucsb.nceas.metacat.util.RequestUtil;
|
50
|
import edu.ucsb.nceas.metacat.util.SessionData;
|
51
|
|
52
|
/**
|
53
|
*
|
54
|
* Implements Earthgrid REST API to Metacat <br/><br/>
|
55
|
*
|
56
|
* <ul>
|
57
|
* <li>
|
58
|
* <h3> EarthGrid Query Service</h3>
|
59
|
* <ul><li>
|
60
|
* <h3>Get & Authenticated Get:</h3>
|
61
|
* is equal to Metacat's read action and returns a data file having
|
62
|
* the specified <doc-id> in the resource path. For authenticated Get service, a session id must be provided
|
63
|
* in the query string. <br/><br/>
|
64
|
*
|
65
|
* <b>REST URL:</b> <code>GET, [context-root]/object/[doc-id]?sessionid=[sessionid] </code><br/>
|
66
|
* <b>Returns:</b> data file <br/><br/>
|
67
|
* </li>
|
68
|
*
|
69
|
* <li>
|
70
|
* <h3>Query & Authenticated Query:</h3>
|
71
|
* Metacat's equivalent is squery action but this function
|
72
|
* receives a Earthgrid query document and returns Earthgrid resultset document by transforming those documents
|
73
|
* to Metacat's equivalents by the means of Metacat Implementation in Earthgrid library. For authenticated Query service
|
74
|
* a session id must be provided in the query string. See Earthgrid (a.k.a. Ecogrid) project for XSD files of
|
75
|
* query and resultset documents<br/><br/>
|
76
|
*
|
77
|
* <b>REST URL:</b> <code>POST, [context-root]/object?sessionid=[sessionid]</code> <br/>
|
78
|
* <b>POST Data:</b> Earthgrid query document , Content-type: <code>text/xml</code><br/>
|
79
|
* <b>Returns:</b> Earthgrid resultset document<br/><br/>
|
80
|
*
|
81
|
* </li>
|
82
|
* </ul>
|
83
|
*
|
84
|
* </li>
|
85
|
*
|
86
|
* <li>
|
87
|
* <h3> EarthGrid Authentication Service</h3>
|
88
|
* <ul><li>
|
89
|
* <h3>Login: </h3>
|
90
|
* Receives username and password parameters in POST data and
|
91
|
* returns SessionId (in XML format) or failure message and uses MetacatHandler's handleLoginAction function<br/><br/>
|
92
|
*
|
93
|
* <b>REST URL:</b> <code>POST, [context-root]/session?op=login</code> <br/>
|
94
|
* <b>POST Data:</b> username=[username]&password=[password], Content-type: <code>application/x-www-form-urlencoded</code>
|
95
|
* <b>Returns:</b> sessionId in XML format<br/><br/>
|
96
|
* </li>
|
97
|
*
|
98
|
* <li>
|
99
|
* <h3>Logout: </h3>
|
100
|
* Receives session Id parameters in querystring and returns xml message, calls
|
101
|
* MetacatHandler's handleLogoutAction function<br/><br/>
|
102
|
*
|
103
|
* <b>REST URL:</b> <code>GET, [context-root]/session?op=logout&sessionid=[sessionid]</code> <br/>
|
104
|
* <b>Returns:</b> message in XML format<br/><br/>
|
105
|
* </li>
|
106
|
* </ul>
|
107
|
*
|
108
|
* <li>
|
109
|
* <h3>EarthGrid Put Service</h3>
|
110
|
*
|
111
|
* <ul>
|
112
|
* <li><h3>Update/Insert: </h3>
|
113
|
* <br/>
|
114
|
* <b>REST URL:</b> <code>PUT, [context-root]/object/[doc-id]?op={update|insert}&sessionid=[sessionid]</code> <br/>
|
115
|
* <b>POST Data:</b> document object, Content-type: <code>text/xml</code><br/>
|
116
|
* <b>Returns:</b> message in XML format<br/><br/>
|
117
|
* </li>
|
118
|
*
|
119
|
* <li><h3>Delete: </h3>
|
120
|
* <br/>
|
121
|
* <b>REST URL:</b> <code>DELETE, [context-root]/object/[doc-id]?sessionid=[sessionid]</code> <br/>
|
122
|
* <b>Returns:</b> message in XML format<br/><br/>
|
123
|
* </li>
|
124
|
|
125
|
* </ul>
|
126
|
* </li>
|
127
|
*
|
128
|
* <li>
|
129
|
* <h3>EarthGrid Identifier Service</h3><br/>
|
130
|
*
|
131
|
* <ul>
|
132
|
* <li><h3>isRegistered: </h3> <br/>
|
133
|
* <b>REST URL:</b> <code>GET, [context-root]/identifier/[doc-id]?op=isregistered</code> <br/>
|
134
|
* <b>Returns:</b> message in XML format<br/><br/>
|
135
|
* </li>
|
136
|
|
137
|
* <li><h3>getAllDocIds:</h3> <br/>
|
138
|
* <b>REST URL:</b> <code>GET, [context-root]/identifier?op=getalldocids</code> <br/>
|
139
|
* <b>Returns:</b> document id list in XML format<br/><br/>
|
140
|
* </li>
|
141
|
*
|
142
|
* <li><h3>addLSID Function:</h3>
|
143
|
* Metacat does not support this function <br/>
|
144
|
* <b>REST URL:</b> <code>PUT, [context-root]/identifier/[doc-id]</code> <br/>
|
145
|
* <b>Returns:</b> error message in XML format<br/><br/>
|
146
|
* </li>
|
147
|
*
|
148
|
* <li><h3>getNextRevision:</h3> <br/>
|
149
|
* <b>REST URL:</b> <code>GET, [context-root]/identifier/[doc-id]?op=getnextrevision</code> <br/>
|
150
|
* <b>Returns:</b> message in XML format<br/><br/>
|
151
|
* </li>
|
152
|
*
|
153
|
* <li><h3>getNextObject:</h3> <br/>
|
154
|
* <b>REST URL:</b> <code>GET, [context-root]/identifier?op=getnextobject&scope=[scope]</code> <br/>
|
155
|
* <b>Returns:</b> message in XML format<br/><br/>
|
156
|
* </li>
|
157
|
*
|
158
|
* </li>
|
159
|
* </ul>
|
160
|
*
|
161
|
*/
|
162
|
public class ResourceHandler {
|
163
|
|
164
|
/**HTTP Verb GET*/
|
165
|
public static final byte GET = 1;
|
166
|
/**HTTP Verb POST*/
|
167
|
public static final byte POST = 2;
|
168
|
/**HTTP Verb PUT*/
|
169
|
public static final byte PUT = 3;
|
170
|
/**HTTP Verb DELETE*/
|
171
|
public static final byte DELETE = 4;
|
172
|
|
173
|
/*
|
174
|
* API Resources
|
175
|
*/
|
176
|
private static final String RESOURCE_OBJECTS = "object";
|
177
|
private static final String RESOURCE_SESSION = "session";
|
178
|
private static final String RESOURCE_IDENTIFIER = "identifier";
|
179
|
|
180
|
/*
|
181
|
* API Functions used as URL parameters
|
182
|
*/
|
183
|
private static final String FUNCTION_KEYWORD = "op";
|
184
|
private static final String FUNCTION_NAME_LOGIN = "login";
|
185
|
private static final String FUNCTION_NAME_LOGOUT = "logout";
|
186
|
private static final String FUNCTION_NAME_ISREGISTERED = "isregistered";
|
187
|
private static final String FUNCTION_NAME_GETALLDOCS = "getalldocids";
|
188
|
private static final String FUNCTION_NAME_GETNEXTREV = "getnextrevision";
|
189
|
private static final String FUNCTION_NAME_GETNEXTOBJ = "getnextobject";
|
190
|
private static final String FUNCTION_NAME_INSERT = "insert";
|
191
|
private static final String FUNCTION_NAME_UPDATE = "update";
|
192
|
|
193
|
private ServletContext servletContext;
|
194
|
private Logger logMetacat;
|
195
|
private MetacatHandler handler;
|
196
|
private HttpServletRequest request;
|
197
|
private HttpServletResponse response;
|
198
|
private String username;
|
199
|
private String password;
|
200
|
private String sessionId;
|
201
|
private String[] groupNames;
|
202
|
|
203
|
private Hashtable<String, String[]> params;
|
204
|
|
205
|
/**Initializes new instance by setting servlet context,request and response*/
|
206
|
public ResourceHandler(ServletContext servletContext,
|
207
|
HttpServletRequest request, HttpServletResponse response) {
|
208
|
this.servletContext = servletContext;
|
209
|
this.request = request;
|
210
|
this.response = response;
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* copies request parameters to a hashtable which is given as argument to native metacathandler functions
|
215
|
*/
|
216
|
private void initParams() {
|
217
|
|
218
|
String name = null;
|
219
|
String[] value = null;
|
220
|
Enumeration paramlist = request.getParameterNames();
|
221
|
while (paramlist.hasMoreElements()) {
|
222
|
name = (String) paramlist.nextElement();
|
223
|
value = request.getParameterValues(name);
|
224
|
params.put(name, value);
|
225
|
}
|
226
|
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
*
|
231
|
* Load user details of metacat session from the request
|
232
|
*
|
233
|
*/
|
234
|
private void loadSessionData() {
|
235
|
SessionData sessionData = RequestUtil.getSessionData(request);
|
236
|
|
237
|
// TODO: validate the session before allowing these values to be set
|
238
|
username = sessionData.getUserName();
|
239
|
password = sessionData.getPassword();
|
240
|
groupNames = sessionData.getGroupNames();
|
241
|
sessionId = sessionData.getId();
|
242
|
|
243
|
if (username == null) {
|
244
|
username = "public";
|
245
|
}
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* This function is called from REST APU servlet and handles each request to the servlet
|
250
|
*
|
251
|
* @param httpVerb (GET, POST, PUT or DELETE)
|
252
|
*/
|
253
|
public void handle(byte httpVerb) {
|
254
|
|
255
|
logMetacat = Logger.getLogger(ResourceHandler.class);
|
256
|
try {
|
257
|
String resource = request.getServletPath();
|
258
|
|
259
|
boolean status = false;
|
260
|
|
261
|
if (resource != null) {
|
262
|
resource = request.getServletPath().substring(1);
|
263
|
System.out.println("accessing resource: " + resource);
|
264
|
|
265
|
params = new Hashtable<String, String[]>();
|
266
|
initParams();
|
267
|
|
268
|
Timer timer = new Timer();
|
269
|
handler = new MetacatHandler(servletContext, timer);
|
270
|
|
271
|
if (resource.equals(RESOURCE_SESSION) && httpVerb == POST
|
272
|
&& params.get(FUNCTION_KEYWORD) != null) {
|
273
|
if (params.get(FUNCTION_KEYWORD)[0]
|
274
|
.equals(FUNCTION_NAME_LOGIN)) {
|
275
|
login();
|
276
|
status = true;
|
277
|
} else if (params.get(FUNCTION_KEYWORD)[0]
|
278
|
.equals(FUNCTION_NAME_LOGOUT)) {
|
279
|
logout();
|
280
|
status = true;
|
281
|
}
|
282
|
} else if (resource.equals(RESOURCE_OBJECTS)) {
|
283
|
|
284
|
loadSessionData();
|
285
|
|
286
|
String objectId = request.getPathInfo();
|
287
|
if (objectId != null && objectId.length() > 1)
|
288
|
objectId = request.getPathInfo().substring(1); //trim the slash
|
289
|
|
290
|
System.out.println("verb:" + httpVerb);
|
291
|
|
292
|
System.out.println("objectId:" + objectId);
|
293
|
|
294
|
if (httpVerb == GET) {
|
295
|
getObject(objectId);
|
296
|
status = true;
|
297
|
} else if (httpVerb == POST) {
|
298
|
query();
|
299
|
status = true;
|
300
|
} else if (httpVerb == PUT) {
|
301
|
putObject(objectId);
|
302
|
status = true;
|
303
|
} else if (httpVerb == DELETE) {
|
304
|
deleteObject(objectId);
|
305
|
status = true;
|
306
|
}
|
307
|
|
308
|
} else if (resource.equals(RESOURCE_IDENTIFIER)) {
|
309
|
|
310
|
String identifierId = request.getPathInfo();
|
311
|
if (identifierId != null && identifierId.length() > 1)
|
312
|
identifierId = request.getPathInfo().substring(1); //trim the slash
|
313
|
|
314
|
System.out.println("identifierId:" + identifierId);
|
315
|
|
316
|
if (httpVerb == GET) {
|
317
|
String op = params.get(FUNCTION_KEYWORD)[0];
|
318
|
System.out.println("op:" + op);
|
319
|
if (op.equals(FUNCTION_NAME_ISREGISTERED)) {
|
320
|
isRegistered(identifierId);
|
321
|
status = true;
|
322
|
} else if (op.equals(FUNCTION_NAME_GETALLDOCS)) {
|
323
|
getAllDocIds();
|
324
|
status = true;
|
325
|
} else if (op.equals(FUNCTION_NAME_GETNEXTREV)) {
|
326
|
getNextRevision(identifierId);
|
327
|
status = true;
|
328
|
} else if (op.equals(FUNCTION_NAME_GETNEXTOBJ)) {
|
329
|
getNextObject();
|
330
|
status = true;
|
331
|
}
|
332
|
|
333
|
} else if (httpVerb == PUT) {
|
334
|
//Earthgrid API > Identifier Service > addLSID Function
|
335
|
printError(
|
336
|
"This method is not supported by metacat. To "
|
337
|
+ "add a new LSID, add a document to metacat.",
|
338
|
response);
|
339
|
status = true;
|
340
|
}
|
341
|
|
342
|
}
|
343
|
if (!status)
|
344
|
printError("Incorrect parameters!", response);
|
345
|
} else {
|
346
|
printError("Incorrect resource!", response);
|
347
|
}
|
348
|
} catch (Exception e) {
|
349
|
logMetacat.error(e.getMessage());
|
350
|
e.printStackTrace();
|
351
|
}
|
352
|
}
|
353
|
|
354
|
/**
|
355
|
* Earthgrid API > Identifier Service > isRegistered Function : calls MetacatHandler > handleIdIsRegisteredAction
|
356
|
* @param guid
|
357
|
* @throws IOException
|
358
|
*/
|
359
|
private void isRegistered(String guid) throws IOException
|
360
|
{
|
361
|
|
362
|
// Look up the localId for this guid
|
363
|
IdentifierManager im = IdentifierManager.getInstance();
|
364
|
String localId = "";
|
365
|
try {
|
366
|
localId = im.getLocalId(guid);
|
367
|
} catch (McdbDocNotFoundException e) {
|
368
|
// TODO: Need to return the proper DataONE exception
|
369
|
}
|
370
|
|
371
|
params.put("docid", new String[] { localId });
|
372
|
PrintWriter out = response.getWriter();
|
373
|
handler.handleIdIsRegisteredAction(out, params, response);
|
374
|
out.close();
|
375
|
}
|
376
|
|
377
|
/**
|
378
|
* Earthgrid API > Identifier Service > getAllDocIds Function : calls MetacatHandler > handleGetAllDocidsAction
|
379
|
* @throws IOException
|
380
|
*/
|
381
|
private void getAllDocIds() throws IOException {
|
382
|
PrintWriter out = response.getWriter();
|
383
|
handler.handleGetAllDocidsAction(out, params, response);
|
384
|
out.close();
|
385
|
}
|
386
|
|
387
|
/**
|
388
|
* Earthgrid API > Identifier Service > getNextRevision Function : calls MetacatHandler > handleGetRevisionAndDocTypeAction
|
389
|
* @param guid
|
390
|
* @throws IOException
|
391
|
*/
|
392
|
private void getNextRevision(String guid) throws IOException
|
393
|
{
|
394
|
params.put("docid", new String[] { guid });
|
395
|
PrintWriter out = response.getWriter();
|
396
|
//handler.handleGetRevisionAndDocTypeAction(out, params);
|
397
|
|
398
|
try {
|
399
|
// Make sure there is a docid
|
400
|
if (guid == null || guid.equals("")) {
|
401
|
throw new Exception("User didn't specify docid!");
|
402
|
}
|
403
|
|
404
|
// Look up the localId for this guid
|
405
|
IdentifierManager im = IdentifierManager.getInstance();
|
406
|
String localId = "";
|
407
|
try {
|
408
|
localId = im.getLocalId(guid);
|
409
|
} catch (McdbDocNotFoundException e) {
|
410
|
// TODO: Need to return the proper DataONE exception
|
411
|
}
|
412
|
|
413
|
// Create a DBUtil object
|
414
|
DBUtil dbutil = new DBUtil();
|
415
|
// Get a rev and doctype
|
416
|
String revAndDocType = dbutil
|
417
|
.getCurrentRevisionAndDocTypeForGivenDocument(localId);
|
418
|
int revision = Integer.parseInt(revAndDocType.split(";")[0]) + 1;
|
419
|
|
420
|
out.println("<?xml version=\"1.0\"?>");
|
421
|
out.print("<next-revision>");
|
422
|
out.print(revision);
|
423
|
out.print("</next-revision>");
|
424
|
|
425
|
} catch (Exception e) {
|
426
|
// Handle exception
|
427
|
out.println("<?xml version=\"1.0\"?>");
|
428
|
out.println("<error>");
|
429
|
out.println(e.getMessage());
|
430
|
out.println("</error>");
|
431
|
}
|
432
|
|
433
|
out.close();
|
434
|
}
|
435
|
|
436
|
/**
|
437
|
* Earthgrid API > Identifier Service > getNextObject Function : calls MetacatHandler > handleGetMaxDocidAction
|
438
|
* @throws IOException
|
439
|
*/
|
440
|
private void getNextObject() throws IOException {
|
441
|
PrintWriter out = response.getWriter();
|
442
|
handler.handleGetMaxDocidAction(out, params, response);
|
443
|
out.close();
|
444
|
}
|
445
|
|
446
|
/**
|
447
|
* Implements REST version of DataONE CRUD API --> get
|
448
|
* @param guid ID of data object to be read
|
449
|
*/
|
450
|
private void getObject(String guid) {
|
451
|
CrudService cs = new CrudService(servletContext, request, response);
|
452
|
AuthToken token = null;
|
453
|
try {
|
454
|
InputStream data = cs.get(token, new IdentifierType(guid));
|
455
|
IOUtils.copyLarge(data, response.getOutputStream());
|
456
|
} catch (BaseException e) {
|
457
|
response.setContentType("text/xml");
|
458
|
response.setStatus(e.getCode());
|
459
|
// TODO: Use content negotiation to determine which return format to use
|
460
|
try {
|
461
|
IOUtils.write(e.serialize(BaseException.FMT_XML),
|
462
|
response.getOutputStream());
|
463
|
} catch (IOException e1) {
|
464
|
logMetacat.error("Error writing exception to stream. "
|
465
|
+ e1.getMessage());
|
466
|
}
|
467
|
} catch (IOException e) {
|
468
|
ServiceFailure sf = new ServiceFailure(1030, e.getMessage());
|
469
|
response.setContentType("text/xml");
|
470
|
response.setStatus(sf.getCode());
|
471
|
try {
|
472
|
IOUtils.write(sf.serialize(BaseException.FMT_XML),
|
473
|
response.getOutputStream());
|
474
|
} catch (IOException e1) {
|
475
|
logMetacat.error("Error writing service exception to stream. "
|
476
|
+ e1.getMessage());
|
477
|
}
|
478
|
}
|
479
|
}
|
480
|
|
481
|
/**
|
482
|
* Earthgrid API > Query Service > Query Function : translates ecogrid query document to metacat query
|
483
|
* then calls DBQuery > createResultDocument function and then again translate resultset to ecogrid resultset
|
484
|
*
|
485
|
* NOTE:
|
486
|
* This is the only method that uses EcoGrid classes for its implementation.
|
487
|
* It does so because it takes an EcoGrid Query as input, and outputs an
|
488
|
* EcoGrid ResultSet document. These documents are parsed by the auto-generated
|
489
|
* EcoGrid classes from axis, and so we link to them here rather than re-inventing them.
|
490
|
* This creates a circular dependency, because the Metacat classes are needed
|
491
|
* to build the EcoGrid implementation, and the EcoGrid jars are needed to build this query()
|
492
|
* method. This circularity could be resolved by moving the EcoGrid classes
|
493
|
* to Metacat directly. As we transition away from EcoGrid SOAP methods in
|
494
|
* favor of these REST interfaces, this circular dependency can be eliminated.
|
495
|
*
|
496
|
* @throws Exception
|
497
|
*/
|
498
|
private void query() throws Exception {
|
499
|
/* This block commented out because of the EcoGrid circular dependency.
|
500
|
* For now, query will not be supported until the circularity can be
|
501
|
* resolved, probably by moving the ecogrid query syntax transformers
|
502
|
* directly into the Metacat codebase. MBJ 2010-02-03
|
503
|
|
504
|
try {
|
505
|
EcogridQueryParser parser = new EcogridQueryParser(request
|
506
|
.getReader());
|
507
|
parser.parseXML();
|
508
|
QueryType queryType = parser.getEcogridQuery();
|
509
|
EcogridJavaToMetacatJavaQueryTransformer queryTransformer =
|
510
|
new EcogridJavaToMetacatJavaQueryTransformer();
|
511
|
QuerySpecification metacatQuery = queryTransformer
|
512
|
.transform(queryType);
|
513
|
|
514
|
DBQuery metacat = new DBQuery();
|
515
|
|
516
|
boolean useXMLIndex = (new Boolean(PropertyService
|
517
|
.getProperty("database.usexmlindex"))).booleanValue();
|
518
|
String xmlquery = "query"; // we don't care the query in resultset,
|
519
|
// the query can be anything
|
520
|
PrintWriter out = null; // we don't want metacat result, so set out null
|
521
|
|
522
|
// parameter: queryspecification, user, group, usingIndexOrNot
|
523
|
StringBuffer result = metacat.createResultDocument(xmlquery,
|
524
|
metacatQuery, out, username, groupNames, useXMLIndex);
|
525
|
|
526
|
// create result set transfer
|
527
|
String saxparser = PropertyService.getProperty("xml.saxparser");
|
528
|
MetacatResultsetParser metacatResultsetParser = new MetacatResultsetParser(
|
529
|
new StringReader(result.toString()), saxparser, queryType
|
530
|
.getNamespace().get_value());
|
531
|
ResultsetType records = metacatResultsetParser.getEcogridResult();
|
532
|
|
533
|
System.out
|
534
|
.println(EcogridResultsetTransformer.toXMLString(records));
|
535
|
response.setContentType("text/xml");
|
536
|
out = response.getWriter();
|
537
|
out.print(EcogridResultsetTransformer.toXMLString(records));
|
538
|
|
539
|
} catch (Exception e) {
|
540
|
e.printStackTrace();
|
541
|
}*/
|
542
|
response.setContentType("text/xml");
|
543
|
PrintWriter out = response.getWriter();
|
544
|
out.print("<error>Query operation not yet supported by Metacat.</error>");
|
545
|
out.close();
|
546
|
}
|
547
|
|
548
|
/**
|
549
|
* Earthgrid API > Put Service >Put Function : calls MetacatHandler > handleInsertOrUpdateAction
|
550
|
*
|
551
|
* @param objectId ID of data object to be inserted or updated
|
552
|
* @throws IOException
|
553
|
*/
|
554
|
private void putObject(String objectId) throws IOException {
|
555
|
|
556
|
// TODO: This function lacks proper handling of authz and authn, so it
|
557
|
// seems that anyone can insert or update; interacts with
|
558
|
// loadSessinData(), which doesn't validate the session
|
559
|
|
560
|
String action = request.getParameter(FUNCTION_KEYWORD);
|
561
|
|
562
|
if (action.equals(FUNCTION_NAME_UPDATE)
|
563
|
|| action.equals(FUNCTION_NAME_INSERT)) {
|
564
|
|
565
|
// Check if the objectId exists
|
566
|
IdentifierManager im = IdentifierManager.getInstance();
|
567
|
if (im.identifierExists(objectId)) {
|
568
|
// TODO: return IdentifierNotUnique exception
|
569
|
}
|
570
|
|
571
|
// TODO: For updates, need to check if the old id exists, and if not throw an exception
|
572
|
|
573
|
// WARNING: This should not be a Reader if we are inserting data
|
574
|
// Nor should it be read into a String
|
575
|
// so this seems like a latent bug to me (MBJ; 16Mar2010)
|
576
|
BufferedReader reader = request.getReader();
|
577
|
StringBuffer buffer = new StringBuffer();
|
578
|
String line = null;
|
579
|
while ((line = reader.readLine()) != null) {
|
580
|
buffer.append(line);
|
581
|
}
|
582
|
|
583
|
String localId = im.generateLocalId(objectId, 1);
|
584
|
params.put("docid", new String[] { localId });
|
585
|
params.put("doctext", new String[] { buffer.toString().trim() });
|
586
|
params.put("action", new String[] { action });
|
587
|
|
588
|
if (username != null && !username.equals("public")) {
|
589
|
PrintWriter out = response.getWriter();
|
590
|
handler.handleInsertOrUpdateAction(request, response, out,
|
591
|
params, username, groupNames);
|
592
|
out.close();
|
593
|
} else {
|
594
|
// TODO: throw exception to show lack of credentials
|
595
|
printError("Permission denied for user " + username, response);
|
596
|
|
597
|
}
|
598
|
} else {
|
599
|
// TODO: throw the proper exception to indicate an invalid request
|
600
|
printError("Specifiy the operation type.(update or insert)",
|
601
|
response);
|
602
|
}
|
603
|
|
604
|
}
|
605
|
|
606
|
/**
|
607
|
* Earthgrid API > Put Service > Delete Function : calls MetacatHandler > handleDeleteAction
|
608
|
*
|
609
|
* @param guid ID of data object to be deleted
|
610
|
* @throws IOException
|
611
|
*/
|
612
|
private void deleteObject(String guid) throws IOException
|
613
|
{
|
614
|
// Look up the localId for this global identifier
|
615
|
IdentifierManager im = IdentifierManager.getInstance();
|
616
|
String localId = "";
|
617
|
try {
|
618
|
localId = im.getLocalId(guid);
|
619
|
} catch (McdbDocNotFoundException e) {
|
620
|
// TODO: Need to return the proper DataONE exception
|
621
|
}
|
622
|
|
623
|
params.put("docid", new String[] { localId });
|
624
|
PrintWriter out = response.getWriter();
|
625
|
handler.handleDeleteAction(out, params, request, response, username,
|
626
|
groupNames);
|
627
|
out.close();
|
628
|
}
|
629
|
|
630
|
/**
|
631
|
* Earthgrid API > Authentication Service > Login Function : calls MetacatHandler > handleLoginAction
|
632
|
*
|
633
|
* @throws IOException
|
634
|
*/
|
635
|
private void login() throws IOException {
|
636
|
PrintWriter out = response.getWriter();
|
637
|
handler.handleLoginAction(out, params, request, response);
|
638
|
out.close();
|
639
|
}
|
640
|
|
641
|
/**
|
642
|
* Earthgrid API > Authentication Service > Logout Function : calls MetacatHandler > handleLogoutAction
|
643
|
*
|
644
|
* @throws IOException
|
645
|
*/
|
646
|
private void logout() throws IOException {
|
647
|
PrintWriter out = response.getWriter();
|
648
|
handler.handleLogoutAction(out, params, request, response);
|
649
|
out.close();
|
650
|
}
|
651
|
|
652
|
/**
|
653
|
* Prints xml response
|
654
|
* @param message Message to be displayed
|
655
|
* @param response Servlet response that xml message will be printed
|
656
|
* */
|
657
|
private void printError(String message, HttpServletResponse response) {
|
658
|
try {
|
659
|
PrintWriter out = response.getWriter();
|
660
|
response.setContentType("text/xml");
|
661
|
out.println("<?xml version=\"1.0\"?>");
|
662
|
out.println("<error>");
|
663
|
out.println(message);
|
664
|
out.println("</error>");
|
665
|
out.close();
|
666
|
} catch (IOException e) {
|
667
|
e.printStackTrace();
|
668
|
}
|
669
|
}
|
670
|
|
671
|
}
|