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: berkley $'
|
7
|
* '$Date: 2006-04-17 14:59:20 -0700 (Mon, 17 Apr 2006) $'
|
8
|
* '$Revision: 2989 $'
|
9
|
*
|
10
|
* This program is free software; you can redistribute it and/or modify
|
11
|
* it under the terms of the GNU General Public License as published by
|
12
|
* the Free Software Foundation; either version 2 of the License, or
|
13
|
* (at your option) any later version.
|
14
|
*
|
15
|
* This program is distributed in the hope that it will be useful,
|
16
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
* GNU General Public License for more details.
|
19
|
*
|
20
|
* You should have received a copy of the GNU General Public License
|
21
|
* along with this program; if not, write to the Free Software
|
22
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
*/
|
24
|
|
25
|
package edu.ucsb.nceas.metacat.client;
|
26
|
|
27
|
import java.io.BufferedReader;
|
28
|
import java.io.InputStream;
|
29
|
import java.io.InputStreamReader;
|
30
|
import java.io.PushbackReader;
|
31
|
import java.io.StringReader;
|
32
|
import java.io.IOException;
|
33
|
import java.io.StringWriter;
|
34
|
import java.io.Reader;
|
35
|
import java.net.URL;
|
36
|
import java.util.Properties;
|
37
|
|
38
|
import org.w3c.dom.Node;
|
39
|
|
40
|
import edu.ucsb.nceas.utilities.HttpMessage;
|
41
|
import edu.ucsb.nceas.utilities.IOUtil;
|
42
|
import edu.ucsb.nceas.utilities.XMLUtilities;
|
43
|
import java.io.File;
|
44
|
|
45
|
|
46
|
/**
|
47
|
* This interface provides methods for initializing and logging in to a
|
48
|
* Metacat server, and then querying, reading, transforming, inserting,
|
49
|
* updating and deleting documents from that server.
|
50
|
*/
|
51
|
public class MetacatClient implements Metacat
|
52
|
{
|
53
|
/** The URL string for the metacat server */
|
54
|
private String metacatUrl;
|
55
|
|
56
|
/** The session identifier for the session */
|
57
|
private String sessionId;
|
58
|
|
59
|
/**
|
60
|
* Constructor to create a new instance. Protected because instances
|
61
|
* should only be created by the factory MetacatFactory.
|
62
|
*/
|
63
|
protected MetacatClient()
|
64
|
{
|
65
|
this.metacatUrl = null;
|
66
|
this.sessionId = null;
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Method used to log in to a metacat server. Implementations will need
|
71
|
* to cache a cookie value to make the session persistent. Each time a
|
72
|
* call is made to one of the other methods (e.g., read), the cookie will
|
73
|
* need to be passed back to the metacat server along with the request.
|
74
|
*
|
75
|
* @param username the username of the user, like an LDAP DN
|
76
|
* @param password the password for that user for authentication
|
77
|
* @return the response string from metacat in XML format
|
78
|
* @throws MetacatAuthException when the username/password could
|
79
|
* not be authenticated
|
80
|
*/
|
81
|
public String login(String username, String password)
|
82
|
throws MetacatAuthException, MetacatInaccessibleException
|
83
|
{
|
84
|
Properties prop = new Properties();
|
85
|
prop.put("action", "login");
|
86
|
prop.put("qformat", "xml");
|
87
|
prop.put("username", username);
|
88
|
prop.put("password", password);
|
89
|
|
90
|
String response = null;
|
91
|
try {
|
92
|
response = sendDataForString(prop, null, null, 0);
|
93
|
} catch (Exception e) {
|
94
|
throw new MetacatInaccessibleException(e.getMessage());
|
95
|
}
|
96
|
|
97
|
if (response.indexOf("<login>") == -1) {
|
98
|
setSessionId("");
|
99
|
throw new MetacatAuthException(response);
|
100
|
} else {
|
101
|
int start = response.indexOf("<sessionId>") + 11;
|
102
|
int end = response.indexOf("</sessionId>");
|
103
|
if ((start != -1) && (end != -1)) {
|
104
|
setSessionId(response.substring(start,end));
|
105
|
}
|
106
|
}
|
107
|
return response;
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Method used to log in to a metacat server. Implementations will need
|
112
|
* to cache a cookie value to make the session persistent. Each time a
|
113
|
* call is made to one of the other methods (e.g., read), the cookie will
|
114
|
* need to be passed back to the metacat server along with the request.
|
115
|
*
|
116
|
* @param username the username of the user, like an LDAP DN
|
117
|
* @param password the password for that user for authentication
|
118
|
* @return the response string from metacat in XML format
|
119
|
* @throws MetacatAuthException when the username/password could
|
120
|
* not be authenticated
|
121
|
*/
|
122
|
public String getloggedinuserinfo() throws MetacatInaccessibleException
|
123
|
{
|
124
|
Properties prop = new Properties();
|
125
|
prop.put("action", "getloggedinuserinfo");
|
126
|
prop.put("qformat", "xml");
|
127
|
|
128
|
String response = null;
|
129
|
try {
|
130
|
response = sendDataForString(prop, null, null, 0);
|
131
|
} catch (Exception e) {
|
132
|
throw new MetacatInaccessibleException(e.getMessage());
|
133
|
}
|
134
|
|
135
|
return response;
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Method used to log out a metacat server. The Metacat server will end
|
140
|
* the session when this call is invoked.
|
141
|
*
|
142
|
* @return the response string from metacat in XML format
|
143
|
* @throws MetacatInaccessibleException when the metacat server can not be
|
144
|
* reached or does not respond
|
145
|
*/
|
146
|
public String logout() throws MetacatInaccessibleException, MetacatException
|
147
|
{
|
148
|
Properties prop = new Properties();
|
149
|
prop.put("action", "logout");
|
150
|
prop.put("qformat", "xml");
|
151
|
|
152
|
String response = null;
|
153
|
try {
|
154
|
response = sendDataForString(prop, null, null, 0);
|
155
|
} catch (Exception e) {
|
156
|
throw new MetacatInaccessibleException(e.getMessage());
|
157
|
}
|
158
|
|
159
|
if (response.indexOf("<logout>") == -1) {
|
160
|
throw new MetacatException(response);
|
161
|
}
|
162
|
setSessionId("");
|
163
|
return response;
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Read an XML document from the metacat server session, accessed by docid,
|
168
|
* and returned as a Reader.
|
169
|
*
|
170
|
* @param docid the identifier of the document to be read
|
171
|
* @return a Reader for accessing the document
|
172
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
173
|
* for the operation
|
174
|
* @throws MetacatInaccessibleException when the metacat server can not be
|
175
|
* reached or does not respond
|
176
|
* @throws MetacatException when the metacat server generates another error
|
177
|
*/
|
178
|
public Reader read(String docid) throws InsufficientKarmaException,
|
179
|
MetacatInaccessibleException, MetacatException, DocumentNotFoundException
|
180
|
{
|
181
|
PushbackReader pbr = null;
|
182
|
|
183
|
Properties prop = new Properties();
|
184
|
prop.put("action", "read");
|
185
|
prop.put("qformat", "xml");
|
186
|
prop.put("docid", docid);
|
187
|
|
188
|
InputStream response = null;
|
189
|
try {
|
190
|
response = sendData(prop, null, null, 0);
|
191
|
} catch (Exception e) {
|
192
|
throw new MetacatInaccessibleException(e.getMessage());
|
193
|
}
|
194
|
|
195
|
pbr = new PushbackReader(new InputStreamReader(response), 512);
|
196
|
try {
|
197
|
char[] characters = new char[512];
|
198
|
int len = pbr.read(characters, 0, 512);
|
199
|
StringWriter sw = new StringWriter();
|
200
|
sw.write(characters, 0, len);
|
201
|
String message = sw.toString();
|
202
|
sw.close();
|
203
|
pbr.unread(characters, 0, len);
|
204
|
|
205
|
if (message.indexOf("<error>") != -1) {
|
206
|
if (message.indexOf("does not have permission") != -1) {
|
207
|
throw new InsufficientKarmaException(message);
|
208
|
} else if(message.indexOf("does not exist") != -1) {
|
209
|
throw new DocumentNotFoundException(message);
|
210
|
} else {
|
211
|
throw new MetacatException(message);
|
212
|
}
|
213
|
}
|
214
|
} catch (IOException ioe) {
|
215
|
throw new MetacatException(
|
216
|
"MetacatClient: Error converting Reader to String."
|
217
|
+ ioe.getMessage());
|
218
|
}
|
219
|
|
220
|
return pbr;
|
221
|
}
|
222
|
|
223
|
|
224
|
/**
|
225
|
* Read inline data from the metacat server session, accessed by
|
226
|
* inlinedataid and returned as a Reader.
|
227
|
*
|
228
|
* @param inlinedataid the identifier of the data to be read
|
229
|
* @return a Reader for accessing the document
|
230
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
231
|
* for the operation
|
232
|
* @throws MetacatInaccessibleException when the metacat server can not be
|
233
|
* reached or does not respond
|
234
|
* @throws MetacatException when the metacat server generates another error
|
235
|
*/
|
236
|
public Reader readInlineData(String inlinedataid)
|
237
|
throws InsufficientKarmaException,
|
238
|
MetacatInaccessibleException, MetacatException
|
239
|
{
|
240
|
PushbackReader pbr = null;
|
241
|
|
242
|
Properties prop = new Properties();
|
243
|
prop.put("action", "readinlinedata");
|
244
|
prop.put("inlinedataid", inlinedataid);
|
245
|
|
246
|
InputStream response = null;
|
247
|
try {
|
248
|
response = sendData(prop, null, null, 0);
|
249
|
} catch (Exception e) {
|
250
|
throw new MetacatInaccessibleException(e.getMessage());
|
251
|
}
|
252
|
|
253
|
pbr = new PushbackReader(new InputStreamReader(response), 512);
|
254
|
try {
|
255
|
char[] characters = new char[512];
|
256
|
int len = pbr.read(characters, 0, 512);
|
257
|
StringWriter sw = new StringWriter();
|
258
|
sw.write(characters, 0, len);
|
259
|
String message = sw.toString();
|
260
|
sw.close();
|
261
|
pbr.unread(characters, 0, len);
|
262
|
|
263
|
if (message.indexOf("<error>") != -1) {
|
264
|
if (message.indexOf("does not have permission") != -1) {
|
265
|
throw new InsufficientKarmaException(message);
|
266
|
} else {
|
267
|
throw new MetacatException(message);
|
268
|
}
|
269
|
}
|
270
|
} catch (IOException ioe) {
|
271
|
throw new MetacatException(
|
272
|
"MetacatClient: Error converting Reader to String."
|
273
|
+ ioe.getMessage());
|
274
|
}
|
275
|
|
276
|
return pbr;
|
277
|
}
|
278
|
|
279
|
/**
|
280
|
* Query the metacat document store with the given metacat-compatible
|
281
|
* query document, and return the result set as a Reader.
|
282
|
*
|
283
|
* @param xmlQuery a Reader for accessing the XML version of the query
|
284
|
* @return a Reader for accessing the result set
|
285
|
*/
|
286
|
public Reader query(Reader xmlQuery) throws MetacatInaccessibleException,
|
287
|
IOException
|
288
|
{
|
289
|
Reader reader = null;
|
290
|
String query = null;
|
291
|
try {
|
292
|
query = IOUtil.getAsString(xmlQuery, true);
|
293
|
} catch (IOException ioE) {
|
294
|
throw ioE;
|
295
|
}
|
296
|
|
297
|
//set up properties
|
298
|
Properties prop = new Properties();
|
299
|
prop.put("action", "squery");
|
300
|
prop.put("qformat", "xml");
|
301
|
prop.put("query", query);
|
302
|
|
303
|
InputStream response = null;
|
304
|
try {
|
305
|
response = sendData(prop, null, null, 0);
|
306
|
} catch (Exception e) {
|
307
|
throw new MetacatInaccessibleException(e.getMessage());
|
308
|
}
|
309
|
reader = new InputStreamReader(response);
|
310
|
return reader;
|
311
|
}
|
312
|
|
313
|
/**
|
314
|
* Insert an XML document into the repository.
|
315
|
*
|
316
|
* @param docid the docid to insert the document
|
317
|
* @param xmlDocument a Reader for accessing the XML document to be inserted
|
318
|
* @param schema a Reader for accessing the DTD or XML Schema for
|
319
|
* the document
|
320
|
* @return the metacat response message
|
321
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
322
|
* for the operation
|
323
|
* @throws MetacatInaccessibleException when the metacat server can not be
|
324
|
* reached or does not respond
|
325
|
* @throws MetacatException when the metacat server generates another error
|
326
|
* @throws IOException when there is an error reading the xml document
|
327
|
*/
|
328
|
public String insert(String docid, Reader xmlDocument, Reader schema)
|
329
|
throws InsufficientKarmaException, MetacatException, IOException,
|
330
|
MetacatInaccessibleException
|
331
|
{
|
332
|
Reader reader = null;
|
333
|
String doctext = null;
|
334
|
String schematext = null;
|
335
|
try {
|
336
|
doctext = IOUtil.getAsString(xmlDocument, true);
|
337
|
if (schema != null) {
|
338
|
schematext = IOUtil.getAsString(schema, true);
|
339
|
}
|
340
|
} catch (IOException ioE) {
|
341
|
throw ioE;
|
342
|
}
|
343
|
|
344
|
//set up properties
|
345
|
Properties prop = new Properties();
|
346
|
prop.put("action", "insert");
|
347
|
prop.put("docid", docid);
|
348
|
prop.put("doctext", doctext);
|
349
|
if (schematext != null) {
|
350
|
prop.put("dtdtext", schematext);
|
351
|
}
|
352
|
|
353
|
String response = null;
|
354
|
try {
|
355
|
response = sendDataForString(prop, null, null, 0);
|
356
|
} catch (Exception e) {
|
357
|
throw new MetacatInaccessibleException(e.getMessage());
|
358
|
}
|
359
|
|
360
|
// Check for an error condition
|
361
|
if (response.indexOf("<error>") != -1) {
|
362
|
if (response.indexOf("does not have permission") != -1) {
|
363
|
throw new InsufficientKarmaException(response);
|
364
|
} else {
|
365
|
throw new MetacatException(response);
|
366
|
}
|
367
|
}
|
368
|
|
369
|
return response;
|
370
|
}
|
371
|
|
372
|
/**
|
373
|
* Update an XML document in the repository.
|
374
|
*
|
375
|
* @param docid the docid to update
|
376
|
* @param xmlDocument a Reader for accessing the XML text to be updated
|
377
|
* @param schema a Reader for accessing the DTD or XML Schema for
|
378
|
* the document
|
379
|
* @return the metacat response message
|
380
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
381
|
* for the operation
|
382
|
* @throws MetacatInaccessibleException when the metacat server can not be
|
383
|
* reached or does not respond
|
384
|
* @throws MetacatException when the metacat server generates another error
|
385
|
* @throws IOException when there is an error reading the xml document
|
386
|
*/
|
387
|
public String update(String docid, Reader xmlDocument, Reader schema)
|
388
|
throws InsufficientKarmaException, MetacatException, IOException,
|
389
|
MetacatInaccessibleException
|
390
|
{
|
391
|
Reader reader = null;
|
392
|
String doctext = null;
|
393
|
String schematext = null;
|
394
|
try {
|
395
|
doctext = IOUtil.getAsString(xmlDocument, true);
|
396
|
if (schema != null) {
|
397
|
schematext = IOUtil.getAsString(schema, true);
|
398
|
}
|
399
|
} catch (IOException ioE) {
|
400
|
throw ioE;
|
401
|
}
|
402
|
|
403
|
//set up properties
|
404
|
Properties prop = new Properties();
|
405
|
prop.put("action", "update");
|
406
|
prop.put("docid", docid);
|
407
|
prop.put("doctext", doctext);
|
408
|
if (schematext != null) {
|
409
|
prop.put("dtdtext", schematext);
|
410
|
}
|
411
|
|
412
|
String response = null;
|
413
|
try {
|
414
|
response = sendDataForString(prop, null, null, 0);
|
415
|
} catch (Exception e) {
|
416
|
throw new MetacatInaccessibleException(e.getMessage());
|
417
|
}
|
418
|
|
419
|
// Check for an error condition
|
420
|
if (response.indexOf("<error>") != -1) {
|
421
|
if (response.indexOf("does not have permission") != -1) {
|
422
|
throw new InsufficientKarmaException(response);
|
423
|
} else {
|
424
|
throw new MetacatException(response);
|
425
|
}
|
426
|
}
|
427
|
|
428
|
return response;
|
429
|
}
|
430
|
|
431
|
/**
|
432
|
* Upload a data document into the repository.
|
433
|
*
|
434
|
* @param docid the docid to insert the document
|
435
|
* @param document a Reader for accessing the document to be uploaded
|
436
|
* @return the metacat response message
|
437
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
438
|
* for the operation
|
439
|
* @throws MetacatInaccessibleException when the metacat server can not be
|
440
|
* reached or does not respond
|
441
|
* @throws MetacatException when the metacat server generates another error
|
442
|
* @throws IOException when there is an error reading the xml document
|
443
|
*/
|
444
|
public String upload(String docid, File file)
|
445
|
throws InsufficientKarmaException, MetacatException, IOException,
|
446
|
MetacatInaccessibleException
|
447
|
{
|
448
|
|
449
|
URL url = new URL(metacatUrl.trim());
|
450
|
HttpMessage msg = new HttpMessage(url);
|
451
|
//set up properties
|
452
|
Properties arg = new Properties();
|
453
|
arg.put("action", "upload");
|
454
|
arg.put("docid", docid);
|
455
|
|
456
|
Properties filenames = new Properties();
|
457
|
String filename = file.getAbsolutePath();
|
458
|
filenames.put("datafile", filename);
|
459
|
|
460
|
String response = null;
|
461
|
try {
|
462
|
response = sendDataForString(arg, filenames, null, 0);
|
463
|
} catch (Exception e) {
|
464
|
throw new MetacatInaccessibleException(e.getMessage());
|
465
|
}
|
466
|
|
467
|
// Check for an error condition
|
468
|
if (response.indexOf("<error>") != -1) {
|
469
|
if (response.indexOf("does not have permission") != -1) {
|
470
|
throw new InsufficientKarmaException(response);
|
471
|
} else {
|
472
|
throw new MetacatException(response);
|
473
|
}
|
474
|
}
|
475
|
|
476
|
return response;
|
477
|
}
|
478
|
|
479
|
/**
|
480
|
* Upload a data document into the repository.
|
481
|
*
|
482
|
* @param docid the docid to insert the document
|
483
|
* @param document a Reader for accessing the document to be uploaded
|
484
|
* @return the metacat response message
|
485
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
486
|
* for the operation
|
487
|
* @throws MetacatInaccessibleException when the metacat server can not be
|
488
|
* reached or does not respond
|
489
|
* @throws MetacatException when the metacat server generates another error
|
490
|
* @throws IOException when there is an error reading the xml document
|
491
|
*/
|
492
|
|
493
|
|
494
|
public String upload(String docid, String filename, InputStream fileData,
|
495
|
int size)
|
496
|
throws InsufficientKarmaException, MetacatException, IOException,
|
497
|
MetacatInaccessibleException {
|
498
|
|
499
|
URL url = new URL(metacatUrl.trim());
|
500
|
HttpMessage msg = new HttpMessage(url);
|
501
|
//set up properties
|
502
|
Properties arg = new Properties();
|
503
|
arg.put("action", "upload");
|
504
|
arg.put("docid", docid);
|
505
|
|
506
|
Properties filenames = new Properties();
|
507
|
filenames.put("datafile", filename);
|
508
|
|
509
|
String response = null;
|
510
|
try {
|
511
|
response = sendDataForString(arg, filenames, fileData, size);
|
512
|
} catch (Exception e) {
|
513
|
throw new MetacatInaccessibleException(e.getMessage());
|
514
|
}
|
515
|
|
516
|
// Check for an error condition
|
517
|
if (response.indexOf("<error>") != -1) {
|
518
|
if (response.indexOf("does not have permission") != -1) {
|
519
|
throw new InsufficientKarmaException(response);
|
520
|
} else {
|
521
|
throw new MetacatException(response);
|
522
|
}
|
523
|
}
|
524
|
|
525
|
return response;
|
526
|
}
|
527
|
|
528
|
/**
|
529
|
* Delete an XML document in the repository.
|
530
|
*
|
531
|
* @param docid the docid to delete
|
532
|
* @return the metacat response message
|
533
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
534
|
* for the operation
|
535
|
* @throws MetacatInaccessibleException when the metacat server can not be
|
536
|
* reached or does not respond
|
537
|
* @throws MetacatException when the metacat server generates another error
|
538
|
*/
|
539
|
public String delete(String docid)
|
540
|
throws InsufficientKarmaException, MetacatException,
|
541
|
MetacatInaccessibleException
|
542
|
{
|
543
|
//set up properties
|
544
|
Properties prop = new Properties();
|
545
|
prop.put("action", "delete");
|
546
|
prop.put("docid", docid);
|
547
|
|
548
|
String response = null;
|
549
|
try {
|
550
|
response = sendDataForString(prop, null, null, 0);
|
551
|
} catch (Exception e) {
|
552
|
throw new MetacatInaccessibleException(e.getMessage());
|
553
|
}
|
554
|
|
555
|
// Check for an error condition
|
556
|
if (response.indexOf("<error>") != -1) {
|
557
|
if (response.indexOf("does not have permission") != -1) {
|
558
|
throw new InsufficientKarmaException(response);
|
559
|
} else {
|
560
|
throw new MetacatException(response);
|
561
|
}
|
562
|
}
|
563
|
return response;
|
564
|
}
|
565
|
|
566
|
|
567
|
/**
|
568
|
* set the access on an XML document in the repository.
|
569
|
*
|
570
|
* @param _docid the docid of the document for which the access should be applied.
|
571
|
*
|
572
|
* @param _principal the document's principal
|
573
|
*
|
574
|
* @param _permission the access permission to be applied to the docid
|
575
|
* {e.g. read,write,all}
|
576
|
*
|
577
|
* @param _permType the permission type to be applied to the document
|
578
|
* {e.g. allow or deny}
|
579
|
*
|
580
|
* @param _permOrder the order that the document's permissions should be
|
581
|
* processed {e.g. denyFirst or allowFirst}
|
582
|
*
|
583
|
*
|
584
|
* @return the metacat response message
|
585
|
*
|
586
|
* @throws InsufficientKarmaException when the user has insufficent rights
|
587
|
* for the operation
|
588
|
* @throws MetacatInaccessibleException when the metacat server can not be
|
589
|
* reached or does not respond
|
590
|
* @throws MetacatException when the metacat server generates another error
|
591
|
*/
|
592
|
public String setAccess(String _docid, String _principal, String
|
593
|
_permission, String _permType,
|
594
|
String _permOrder )
|
595
|
throws InsufficientKarmaException, MetacatException,
|
596
|
MetacatInaccessibleException
|
597
|
{
|
598
|
//set up properties
|
599
|
Properties prop = new Properties();
|
600
|
prop.put("action", "setaccess");
|
601
|
prop.put("docid", _docid);
|
602
|
prop.put("principal", _principal);
|
603
|
prop.put("permission", _permission);
|
604
|
prop.put("permType", _permType);
|
605
|
prop.put("permOrder", _permOrder);
|
606
|
|
607
|
String response = null;
|
608
|
try {
|
609
|
response = sendDataForString(prop, null, null, 0);
|
610
|
} catch (Exception e) {
|
611
|
throw new MetacatInaccessibleException(e.getMessage());
|
612
|
}
|
613
|
|
614
|
// Check for an error condition
|
615
|
if (response.indexOf("<error>") != -1) {
|
616
|
if (response.indexOf("does not have permission") != -1) {
|
617
|
throw new InsufficientKarmaException(response);
|
618
|
} else {
|
619
|
throw new MetacatException(response);
|
620
|
}
|
621
|
}
|
622
|
return response;
|
623
|
}
|
624
|
|
625
|
/**
|
626
|
* When the MetacatFactory creates an instance it needs to set the
|
627
|
* MetacatUrl to which connections should be made.
|
628
|
*
|
629
|
* @param metacatUrl the URL for the metacat server
|
630
|
*/
|
631
|
public void setMetacatUrl(String metacatUrl)
|
632
|
{
|
633
|
this.metacatUrl = metacatUrl;
|
634
|
}
|
635
|
|
636
|
/**
|
637
|
* Get the session identifier for this session. This is only valid if
|
638
|
* the login methods has been called successfully for this Metacat object
|
639
|
* beforehand.
|
640
|
*
|
641
|
* @returns the sessionId as a String, or null if the session is invalid
|
642
|
*/
|
643
|
public String getSessionId()
|
644
|
{
|
645
|
return this.sessionId;
|
646
|
}
|
647
|
|
648
|
/**
|
649
|
* Set the session identifier for this session. This identifier was
|
650
|
* previously established with a call to login. To continue to use the
|
651
|
* same session, set the session id before making a call to one of the
|
652
|
* metacat access methods (e.g., read, query, insert, etc.).
|
653
|
*
|
654
|
* @param String the sessionId from a previously established session
|
655
|
*/
|
656
|
public void setSessionId(String sessionId)
|
657
|
{
|
658
|
this.sessionId = sessionId;
|
659
|
}
|
660
|
|
661
|
/**
|
662
|
* The method will return the latest revision in metacat server
|
663
|
* for a given document id. If some error happens, this method will throw
|
664
|
* an exception.
|
665
|
* @param docId String the given docid you want to use. the docid it self
|
666
|
* can have or haven't revision number
|
667
|
* @throws MetacatException
|
668
|
*/
|
669
|
public int getNewestDocRevision(String docId) throws MetacatException
|
670
|
{
|
671
|
int rev = 0;
|
672
|
//set up properties
|
673
|
Properties prop = new Properties();
|
674
|
prop.put("action", "getrevisionanddoctype");
|
675
|
prop.put("docid", docId);
|
676
|
|
677
|
String response = null;
|
678
|
try {
|
679
|
response = sendDataForString(prop, null, null, 0);
|
680
|
String revStr = parserRevisionResponse(response);
|
681
|
Integer revObj = new Integer(revStr);
|
682
|
rev = revObj.intValue();
|
683
|
// Check for an error condition
|
684
|
if (response.indexOf("<error>") != -1) {
|
685
|
throw new MetacatException(response);
|
686
|
}
|
687
|
} catch (Exception e) {
|
688
|
throw new MetacatException(e.getMessage());
|
689
|
}
|
690
|
return rev;
|
691
|
}
|
692
|
|
693
|
/**
|
694
|
* Return the highest document id for a given scope. This is used by
|
695
|
* clients to make it easier to determine the next free identifier in a
|
696
|
* sequence for a given scope.
|
697
|
* @param scope String the scope to use for looking up the latest id
|
698
|
* @throws MetacatException when an error occurs
|
699
|
*/
|
700
|
public String getLastDocid(String scope) throws MetacatException {
|
701
|
String lastIdentifier = "";
|
702
|
//set up properties
|
703
|
Properties prop = new Properties();
|
704
|
prop.put("action", "getlastdocid");
|
705
|
prop.put("scope", scope);
|
706
|
|
707
|
String response = null;
|
708
|
try {
|
709
|
response = sendDataForString(prop, null, null, 0);
|
710
|
// Check for an error condition
|
711
|
if (response.indexOf("<error>") != -1) {
|
712
|
throw new MetacatException(response);
|
713
|
} else {
|
714
|
Reader responseReader = new StringReader(response);
|
715
|
Node root =
|
716
|
XMLUtilities.getXMLReaderAsDOMTreeRootNode(responseReader);
|
717
|
Node docidNode =
|
718
|
XMLUtilities.getNodeWithXPath(root, "/lastDocid/docid");
|
719
|
lastIdentifier = docidNode.getFirstChild().getNodeValue();
|
720
|
}
|
721
|
} catch (Exception e) {
|
722
|
throw new MetacatException(e.getMessage());
|
723
|
}
|
724
|
return lastIdentifier;
|
725
|
}
|
726
|
|
727
|
/************************************************************************
|
728
|
* PRIVATE METHODS
|
729
|
************************************************************************/
|
730
|
|
731
|
/**
|
732
|
* Send a request to metacat.
|
733
|
*
|
734
|
* @param prop the properties to be URL encoded and sent
|
735
|
* @param filename the properties to be sent to Metacat
|
736
|
* in case of upload, otherwise null
|
737
|
* @param fileData the inputStream for the file data to be sent to Metacat
|
738
|
* in case of upload, otherwise null
|
739
|
* @param size the size of the data being sent to Metacat
|
740
|
* in case of upload, otherwise 0
|
741
|
*/
|
742
|
synchronized private InputStream sendDataOnce(Properties args,
|
743
|
Properties filename,
|
744
|
InputStream fileData,
|
745
|
int size)
|
746
|
throws Exception
|
747
|
{
|
748
|
InputStream returnStream = null;
|
749
|
URL url = new URL(metacatUrl);
|
750
|
HttpMessage msg = new HttpMessage(url);
|
751
|
msg.setCookie("JSESSIONID="+this.sessionId);
|
752
|
if (filename == null){
|
753
|
returnStream = msg.sendPostData(args);
|
754
|
} else if (fileData == null){
|
755
|
returnStream = msg.sendPostData(args, filename);
|
756
|
} else if (size > 0) {
|
757
|
returnStream = msg.sendPostData(args, filename, fileData, size);
|
758
|
} else {
|
759
|
throw new MetacatException("Invalid size specified for " +
|
760
|
"the input stream being passed");
|
761
|
}
|
762
|
return returnStream;
|
763
|
}
|
764
|
|
765
|
/**
|
766
|
* Send a request to Metacat
|
767
|
*
|
768
|
* @param args the properties to be sent to Metacat
|
769
|
* @param filename the properties to be sent to Metacat
|
770
|
* in case of upload, otherwise null
|
771
|
* @param fileData the inputStream for the file data to be sent to Metacat
|
772
|
* in case of upload, otherwise null
|
773
|
* @param size the size of the data being sent to Metacat
|
774
|
* in case of upload, otherwise 0
|
775
|
* @return InputStream as returned by Metacat
|
776
|
*/
|
777
|
synchronized private InputStream sendData(Properties args,
|
778
|
Properties filename,
|
779
|
InputStream fileData,
|
780
|
int size)
|
781
|
throws Exception
|
782
|
{
|
783
|
InputStream returnStream = null;
|
784
|
|
785
|
/*
|
786
|
Note: The reason that there are three try statements all executing
|
787
|
the same code is that there is a problem with the initial connection
|
788
|
using the HTTPClient protocol handler. These try statements make
|
789
|
sure that a connection is made because it gives each connection a
|
790
|
2nd and 3rd chance to work before throwing an error.
|
791
|
THIS IS A TOTAL HACK. THIS NEEDS TO BE LOOKED INTO AFTER THE BETA1
|
792
|
RELEASE OF MORPHO!!! cwb (7/24/01)
|
793
|
*/
|
794
|
try {
|
795
|
return sendDataOnce(args, filename, fileData, size);
|
796
|
} catch (Exception e) {
|
797
|
try {
|
798
|
return sendDataOnce(args, filename, fileData, size);
|
799
|
} catch (Exception e2) {
|
800
|
try {
|
801
|
return sendDataOnce(args, filename, fileData, size);
|
802
|
} catch (Exception e3) {
|
803
|
System.err.println(
|
804
|
"Failed to send data to metacat 3 times.");
|
805
|
throw e3;
|
806
|
}
|
807
|
}
|
808
|
}
|
809
|
}
|
810
|
|
811
|
/**
|
812
|
* Send a request to Metacat
|
813
|
*
|
814
|
* @param args the properties to be sent to Metacat
|
815
|
* @param filename the properties to be sent to Metacat
|
816
|
* in case of upload, otherwise null
|
817
|
* @param fileData the inputStream for the file data to be sent to Metacat
|
818
|
* in case of upload, otherwise null
|
819
|
* @param size the size of the data being sent to Metacat
|
820
|
* in case of upload, otherwise 0
|
821
|
* @return a string as returned by Metacat
|
822
|
*/
|
823
|
synchronized private String sendDataForString(Properties args,
|
824
|
Properties filename,
|
825
|
InputStream fileData,
|
826
|
int size)
|
827
|
throws Exception
|
828
|
{
|
829
|
String response = null;
|
830
|
|
831
|
try {
|
832
|
InputStreamReader returnStream =
|
833
|
new InputStreamReader(sendData(args, filename,
|
834
|
fileData, size));
|
835
|
StringWriter sw = new StringWriter();
|
836
|
int len;
|
837
|
char[] characters = new char[512];
|
838
|
while ((len = returnStream.read(characters, 0, 512)) != -1) {
|
839
|
sw.write(characters, 0, len);
|
840
|
}
|
841
|
returnStream.close();
|
842
|
response = sw.toString();
|
843
|
sw.close();
|
844
|
} catch (Exception e) {
|
845
|
throw e;
|
846
|
}
|
847
|
return response;
|
848
|
}
|
849
|
|
850
|
/*
|
851
|
* "getversionanddoctype" action will return a string from metacat server.
|
852
|
* The string format is "revision;doctype"(This is bad idea, we should use xml)
|
853
|
* This method will get revision string from the response string
|
854
|
*/
|
855
|
private String parserRevisionResponse(String response) throws Exception
|
856
|
{
|
857
|
String revision = null;
|
858
|
if (response != null)
|
859
|
{
|
860
|
int firstSemiCol = response.indexOf(";");
|
861
|
revision = response.substring(0, firstSemiCol);
|
862
|
}
|
863
|
return revision;
|
864
|
}
|
865
|
}
|