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