1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2000 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
* Purpose: To test the MetaCatURL class by JUnit
|
6
|
* Authors: Jing Tao
|
7
|
*
|
8
|
* '$Author: leinfelder $'
|
9
|
* '$Date: 2012-10-15 16:41:47 -0700 (Mon, 15 Oct 2012) $'
|
10
|
* '$Revision: 7402 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
package edu.ucsb.nceas.metacatnettest;
|
28
|
|
29
|
import java.io.File;
|
30
|
import java.io.InputStream;
|
31
|
import java.io.InputStreamReader;
|
32
|
import java.io.StringWriter;
|
33
|
import java.nio.charset.Charset;
|
34
|
import java.util.ArrayList;
|
35
|
import java.util.Enumeration;
|
36
|
import java.util.List;
|
37
|
import java.util.Properties;
|
38
|
|
39
|
import junit.framework.Test;
|
40
|
import junit.framework.TestSuite;
|
41
|
|
42
|
import org.apache.http.HttpResponse;
|
43
|
import org.apache.http.HttpVersion;
|
44
|
import org.apache.http.NameValuePair;
|
45
|
import org.apache.http.client.HttpClient;
|
46
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
47
|
import org.apache.http.client.methods.HttpPost;
|
48
|
import org.apache.http.entity.mime.HttpMultipartMode;
|
49
|
import org.apache.http.entity.mime.MultipartEntity;
|
50
|
import org.apache.http.entity.mime.content.FileBody;
|
51
|
import org.apache.http.entity.mime.content.StringBody;
|
52
|
import org.apache.http.impl.client.DefaultHttpClient;
|
53
|
import org.apache.http.message.BasicNameValuePair;
|
54
|
import org.apache.http.params.CoreProtocolPNames;
|
55
|
import org.apache.http.util.EntityUtils;
|
56
|
|
57
|
import edu.ucsb.nceas.MCTestCase;
|
58
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
59
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
60
|
|
61
|
/**
|
62
|
* A JUnit test for testing Step class processing
|
63
|
*/
|
64
|
public class MetaCatServletNetTest extends MCTestCase {
|
65
|
static {
|
66
|
try {
|
67
|
metacatUrl = PropertyService.getProperty("test.metacatUrl");
|
68
|
} catch (PropertyNotFoundException pnfe) {
|
69
|
System.err.println("could not find metacat URL in MetacatServletNetTest: "
|
70
|
+ pnfe.getMessage());
|
71
|
} catch (Exception e) {
|
72
|
System.err.println("Exception in initialize option in MetacatServletNetTest: "
|
73
|
+ e.getMessage());
|
74
|
}
|
75
|
}
|
76
|
|
77
|
private String serialNumber;
|
78
|
private static String sessionId;
|
79
|
|
80
|
/**
|
81
|
* Constructor to build the test
|
82
|
*
|
83
|
* @param name the name of the test method
|
84
|
*/
|
85
|
public MetaCatServletNetTest(String name) {
|
86
|
super(name);
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Constructor to build the test
|
91
|
*
|
92
|
* @param name the name of the test method
|
93
|
*/
|
94
|
public MetaCatServletNetTest(String name, String serial) {
|
95
|
super(name);
|
96
|
serialNumber = serial;
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* Establish a testing framework by initializing appropriate objects
|
101
|
*/
|
102
|
public void setUp() {
|
103
|
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Release any objects after tests are complete
|
108
|
*/
|
109
|
public void tearDown() {
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Create a suite of tests to be run together
|
114
|
*/
|
115
|
public static Test suite() {
|
116
|
double number = 0;
|
117
|
String serial = null;
|
118
|
|
119
|
TestSuite suite = new TestSuite();
|
120
|
suite.addTest(new MetaCatServletNetTest("initialize"));
|
121
|
suite.addTest(new MetaCatServletNetTest("testNCEASLoginFail"));
|
122
|
//Should put a login successfully at the end of login test
|
123
|
//So insert or update can have cookie.
|
124
|
suite.addTest(new MetaCatServletNetTest("testNCEASLogin"));
|
125
|
|
126
|
//create random number for docid, so it can void repeat
|
127
|
number = Math.random() * 100000;
|
128
|
serial = Integer.toString(((new Double(number)).intValue()));
|
129
|
debug("serial: " + serial);
|
130
|
suite.addTest(new MetaCatServletNetTest("testInsertXMLDocument", serial));
|
131
|
suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentXMLFormat", serial));
|
132
|
suite.addTest(new MetaCatServletNetTest("testUpdateXMLDocument", serial));
|
133
|
suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentHTMLFormat", serial));
|
134
|
suite.addTest(new MetaCatServletNetTest("testReadXMLDocumentZipFormat", serial));
|
135
|
|
136
|
suite.addTest(new MetaCatServletNetTest("testDeleteXMLDocument", serial));
|
137
|
|
138
|
//insert invalid xml document
|
139
|
number = Math.random() * 100000;
|
140
|
serial = Integer.toString(((new Double(number)).intValue()));
|
141
|
suite.addTest(new MetaCatServletNetTest("testInsertInvalidateXMLDocument",
|
142
|
serial));
|
143
|
//insert non well formed document
|
144
|
number = Math.random() * 100000;
|
145
|
serial = Integer.toString(((new Double(number)).intValue()));
|
146
|
suite.addTest(new MetaCatServletNetTest("testInsertNonWellFormedXMLDocument",
|
147
|
serial));
|
148
|
//insert data file
|
149
|
number = Math.random() * 100000;
|
150
|
serial = Integer.toString(((new Double(number)).intValue()));
|
151
|
suite.addTest(new MetaCatServletNetTest("testLogOut"));
|
152
|
|
153
|
return suite;
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* Run an initial test that always passes to check that the test
|
158
|
* harness is working.
|
159
|
*/
|
160
|
public void initialize() {
|
161
|
assertTrue(1 == 1);
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Test the login to nceas succesfully
|
166
|
*/
|
167
|
public void testNCEASLogin() {
|
168
|
System.out.println("Testing nceas login");
|
169
|
debug("\nRunning: testNCEASLogin test");
|
170
|
try {
|
171
|
String user = PropertyService.getProperty("test.mcUser");
|
172
|
String passwd = PropertyService.getProperty("test.mcPassword");
|
173
|
assertTrue(logIn(user, passwd));
|
174
|
//assertTrue( withProtocol.getProtocol().equals("http"));
|
175
|
} catch (PropertyNotFoundException e) {
|
176
|
fail(e.getMessage());
|
177
|
}
|
178
|
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* Test the login to nceas failed
|
183
|
*/
|
184
|
public void testNCEASLoginFail() {
|
185
|
debug("\nRunning: testNCEASLoginFail test");
|
186
|
try {
|
187
|
String user = PropertyService.getProperty("test.mcUser");
|
188
|
System.out.println("Testing nceas login fail");
|
189
|
String passwd = "thisPasswordIsInvalidAndShouldFail";
|
190
|
assertTrue(!logIn(user, passwd));
|
191
|
|
192
|
} catch (PropertyNotFoundException e) {
|
193
|
fail(e.getMessage());
|
194
|
}
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* Test the login to lter failed
|
199
|
*/
|
200
|
public void testLterReferralLoginFail() {
|
201
|
debug("\nRunning: testLterReferralLoginFail test");
|
202
|
try {
|
203
|
String user = PropertyService.getProperty("test.lterUser");
|
204
|
String passwd = "thisPasswordIsInvalidAndShouldFail";
|
205
|
assertTrue(!logIn(user, passwd));
|
206
|
//assertTrue( withProtocol.getProtocol().equals("http"));
|
207
|
} catch (PropertyNotFoundException e) {
|
208
|
fail(e.getMessage());
|
209
|
}
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* Test insert a xml document successfully
|
214
|
*/
|
215
|
public void testInsertXMLDocument() throws PropertyNotFoundException {
|
216
|
debug("\nRunning: testInsertXMLDocument test");
|
217
|
String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
|
218
|
+ PropertyService.getProperty("document.accNumSeparator") + "1";
|
219
|
debug("insert docid: " + name);
|
220
|
String content = "<?xml version=\"1.0\"?>"
|
221
|
+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
|
222
|
+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
|
223
|
+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
|
224
|
+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
|
225
|
+ "</identifier>" + "<allow>"
|
226
|
+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
|
227
|
+ "<permission>all</permission>" + "</allow>" + "<allow>"
|
228
|
+ "<principal>public</principal>" + "<permission>read</permission>"
|
229
|
+ "</allow>" + "</acl>";
|
230
|
debug("xml document: " + content);
|
231
|
assertTrue(handleXMLDocument(content, name, "insert"));
|
232
|
|
233
|
}
|
234
|
|
235
|
/**
|
236
|
* Test insert a invalidate xml document successfully
|
237
|
* In the String, there is no <!Doctype ... Public/System/>
|
238
|
*/
|
239
|
public void testInsertInvalidateXMLDocument() throws PropertyNotFoundException {
|
240
|
debug("\nRunning: testInsertInvalidateXMLDocument test");
|
241
|
String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
|
242
|
+ PropertyService.getProperty("document.accNumSeparator") + "1";
|
243
|
debug("insert docid: " + name);
|
244
|
String content = "<?xml version=\"1.0\"?>"
|
245
|
+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
|
246
|
+ "</identifier>" + "<allow>"
|
247
|
+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
|
248
|
+ "<permission>all</permission>" + "</allow>" + "<allow>"
|
249
|
+ "<principal>public</principal>" + "<permission>read</permission>"
|
250
|
+ "</allow>" + "</acl>";
|
251
|
debug("xml document: " + content);
|
252
|
assertTrue(handleXMLDocument(content, name, "insert"));
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Test insert a non well-formed xml document successfully
|
257
|
* There is no </acl> in this string
|
258
|
*/
|
259
|
public void testInsertNonWellFormedXMLDocument() throws PropertyNotFoundException {
|
260
|
debug("\nRunning: testInsertNonWellFormedXMLDocument test");
|
261
|
String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
|
262
|
+ PropertyService.getProperty("document.accNumSeparator") + "1";
|
263
|
debug("insert non well-formed docid: " + name);
|
264
|
String content = "<?xml version=\"1.0\"?>"
|
265
|
+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
|
266
|
+ "</identifier>" + "<allow>"
|
267
|
+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
|
268
|
+ "<permission>all</permission>" + "</allow>" + "<allow>"
|
269
|
+ "<principal>public</principal>" + "<permission>read</permission>"
|
270
|
+ "</allow>";
|
271
|
|
272
|
debug("xml document: " + content);
|
273
|
assertTrue(!handleXMLDocument(content, name, "insert"));
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* Test read a xml document in xml format successfully
|
278
|
*/
|
279
|
public void testReadXMLDocumentXMLFormat() throws PropertyNotFoundException {
|
280
|
debug("\nRunning: testReadXMLDocumentXMLFormat test");
|
281
|
String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
|
282
|
+ PropertyService.getProperty("document.accNumSeparator") + "1";
|
283
|
assertTrue(handleReadAction(name, "xml"));
|
284
|
|
285
|
}
|
286
|
|
287
|
/**
|
288
|
* Test read a xml document in html format successfully
|
289
|
*/
|
290
|
public void testReadXMLDocumentHTMLFormat() throws PropertyNotFoundException {
|
291
|
debug("\nRunning: testReadXMLDocumentHTMLFormat test");
|
292
|
String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
|
293
|
+ PropertyService.getProperty("document.accNumSeparator") + "1";
|
294
|
assertTrue(handleReadAction(name, "html"));
|
295
|
|
296
|
}
|
297
|
|
298
|
/**
|
299
|
* Test read a xml document in zip format successfully
|
300
|
*/
|
301
|
public void testReadXMLDocumentZipFormat() throws PropertyNotFoundException {
|
302
|
debug("\nRunning: testReadXMLDocumentZipFormat test");
|
303
|
String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
|
304
|
+ PropertyService.getProperty("document.accNumSeparator") + "1";
|
305
|
assertTrue(handleReadAction(name, "zip"));
|
306
|
|
307
|
}
|
308
|
|
309
|
/**
|
310
|
* Test insert a xml document successfully
|
311
|
*/
|
312
|
public void testUpdateXMLDocument() throws PropertyNotFoundException {
|
313
|
debug("\nRunning: testUpdateXMLDocument test");
|
314
|
String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
|
315
|
+ PropertyService.getProperty("document.accNumSeparator") + "2";
|
316
|
debug("update docid: " + name);
|
317
|
String content = "<?xml version=\"1.0\"?>"
|
318
|
+ "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
|
319
|
+ "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb."
|
320
|
+ "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
|
321
|
+ "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name
|
322
|
+ "</identifier>" + "<allow>"
|
323
|
+ "<principal>uid=john,o=NCEAS,dc=ecoinformatics,dc=org</principal>"
|
324
|
+ "<permission>all</permission>" + "</allow>" + "<allow>"
|
325
|
+ "<principal>public</principal>" + "<permission>read</permission>"
|
326
|
+ "</allow>" + "</acl>";
|
327
|
debug("xml document: " + content);
|
328
|
|
329
|
//sleep to make sure the original document was indexed
|
330
|
try {
|
331
|
String indexDelay = PropertyService.getProperty("database.maximumIndexDelay");
|
332
|
Thread.sleep(Integer.parseInt(indexDelay));
|
333
|
} catch (Exception e) {
|
334
|
fail(e.getMessage());
|
335
|
}
|
336
|
assertTrue(handleXMLDocument(content, name, "update"));
|
337
|
|
338
|
}
|
339
|
|
340
|
/**
|
341
|
* Test insert a data file successfully
|
342
|
*/
|
343
|
public void testInertDataFile() throws PropertyNotFoundException {
|
344
|
debug("\nRunning: testInertDataFile test");
|
345
|
String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
|
346
|
+ PropertyService.getProperty("document.accNumSeparator") + "1";
|
347
|
debug("insert data file docid: " + name);
|
348
|
debug("insert data file ");
|
349
|
File hello = new File("test/jones.204.22.xml");
|
350
|
|
351
|
assertTrue(insertDataFile(name, hello));
|
352
|
}
|
353
|
|
354
|
/**
|
355
|
* Test delete a xml document successfully
|
356
|
*/
|
357
|
public void testDeleteXMLDocument() throws PropertyNotFoundException {
|
358
|
debug("\nRunning: testDeleteXMLDocument test");
|
359
|
String name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber
|
360
|
+ PropertyService.getProperty("document.accNumSeparator") + "2";
|
361
|
debug("delete docid: " + name);
|
362
|
//sleep to make sure the original document was indexed
|
363
|
try {
|
364
|
String indexDelay = PropertyService.getProperty("database.maximumIndexDelay");
|
365
|
Thread.sleep(Integer.parseInt(indexDelay));
|
366
|
} catch (Exception e) {
|
367
|
fail(e.getMessage());
|
368
|
}
|
369
|
assertTrue(handleDeleteFile(name));
|
370
|
}
|
371
|
|
372
|
/**
|
373
|
* Test logout action
|
374
|
*/
|
375
|
public void testLogOut() {
|
376
|
debug("\nRunning: testLogOut test");
|
377
|
assertTrue(handleLogOut());
|
378
|
|
379
|
}
|
380
|
|
381
|
/**
|
382
|
* Method to hanld login action
|
383
|
*
|
384
|
* @param usrerName, the DN name of the test method
|
385
|
* @param passWord, the passwd of the user
|
386
|
*/
|
387
|
|
388
|
public boolean logIn(String userName, String passWord) {
|
389
|
Properties prop = new Properties();
|
390
|
prop.put("action", "login");
|
391
|
prop.put("qformat", "xml");
|
392
|
prop.put("username", userName);
|
393
|
prop.put("password", passWord);
|
394
|
|
395
|
// Now contact metacat
|
396
|
String response = getMetacatString(prop);
|
397
|
debug("Login Message: " + response);
|
398
|
boolean connected = false;
|
399
|
if (response.indexOf("<login>") != -1) {
|
400
|
connected = true;
|
401
|
// set the session id
|
402
|
int start = response.indexOf("<sessionId>") + 11;
|
403
|
int end = response.indexOf("</sessionId>");
|
404
|
if ((start != -1) && (end != -1)) {
|
405
|
sessionId = response.substring(start,end);
|
406
|
}
|
407
|
} else {
|
408
|
sessionId = "";
|
409
|
connected = false;
|
410
|
}
|
411
|
|
412
|
debug("sessionId: " + this.sessionId);
|
413
|
|
414
|
return connected;
|
415
|
}
|
416
|
|
417
|
/**
|
418
|
* Method to hanld logout action
|
419
|
*
|
420
|
* @param usrerName, the DN name of the test method
|
421
|
* @param passWord, the passwd of the user
|
422
|
*/
|
423
|
|
424
|
public boolean handleLogOut() {
|
425
|
boolean disConnected = false;
|
426
|
Properties prop = new Properties();
|
427
|
prop.put("action", "logout");
|
428
|
prop.put("qformat", "xml");
|
429
|
|
430
|
String response = getMetacatString(prop);
|
431
|
debug("Logout Message: " + response);
|
432
|
|
433
|
if (response.indexOf("<logout>") != -1) {
|
434
|
disConnected = true;
|
435
|
} else {
|
436
|
disConnected = false;
|
437
|
}
|
438
|
|
439
|
return disConnected;
|
440
|
}
|
441
|
|
442
|
/**
|
443
|
* Method to hanld read both xml and data file
|
444
|
*
|
445
|
* @param docid, the docid of the document want to read
|
446
|
* @param qformat, the format of document user want to get
|
447
|
*/
|
448
|
public boolean handleReadAction(String docid, String qformat) {
|
449
|
Properties prop = new Properties();
|
450
|
String message = "";
|
451
|
prop.put("action", "read");
|
452
|
prop.put("qformat", qformat);
|
453
|
prop.put("docid", docid);
|
454
|
|
455
|
message = getMetacatString(prop);
|
456
|
message = message.trim();
|
457
|
//MetacatUtil.debugMessage("Read Message: "+message, 30);
|
458
|
if (message == null || message.equals("") || message.indexOf("<error>") != -1) {//there was an error
|
459
|
|
460
|
return false;
|
461
|
} else {//successfully
|
462
|
return true;
|
463
|
}
|
464
|
|
465
|
}
|
466
|
|
467
|
/**
|
468
|
* Method to hanld inset or update xml document
|
469
|
*
|
470
|
* @param xmlDocument, the content of xml qformat
|
471
|
* @param docid, the docid of the document
|
472
|
* @param action, insert or update
|
473
|
*/
|
474
|
public boolean handleXMLDocument(String xmlDocument, String docid, String action)
|
475
|
|
476
|
{ //-attempt to write file to metacat
|
477
|
String access = "no";
|
478
|
StringBuffer fileText = new StringBuffer();
|
479
|
StringBuffer messageBuf = new StringBuffer();
|
480
|
String accessFileId = null;
|
481
|
Properties prop = new Properties();
|
482
|
prop.put("action", action);
|
483
|
prop.put("public", access); //This is the old way of controlling access
|
484
|
prop.put("doctext", xmlDocument);
|
485
|
prop.put("docid", docid);
|
486
|
|
487
|
String message = getMetacatString(prop);
|
488
|
debug("Insert or Update Message: " + message);
|
489
|
if (message.indexOf("<error>") != -1) {//there was an error
|
490
|
|
491
|
return false;
|
492
|
} else if (message.indexOf("<success>") != -1) {//the operation worked
|
493
|
//write the file to the cache and return the file object
|
494
|
return true;
|
495
|
|
496
|
} else {//something weird happened.
|
497
|
return false;
|
498
|
}
|
499
|
|
500
|
}
|
501
|
|
502
|
public boolean handleDeleteFile(String name) {
|
503
|
|
504
|
Properties prop = new Properties();
|
505
|
prop.put("action", "delete");
|
506
|
prop.put("docid", name);
|
507
|
|
508
|
String message = getMetacatString(prop);
|
509
|
debug("Delete Message: " + message);
|
510
|
if (message.indexOf("<error>") != -1) {//there was an error
|
511
|
|
512
|
return false;
|
513
|
} else if (message.indexOf("<success>") != -1) {//the operation worked
|
514
|
//write the file to the cache and return the file object
|
515
|
return true;
|
516
|
|
517
|
} else {//something weird happened.
|
518
|
return false;
|
519
|
}
|
520
|
}
|
521
|
|
522
|
/**
|
523
|
* sends a data file to the metacat using "multipart/form-data" encoding
|
524
|
*
|
525
|
* @param id the id to assign to the file on metacat (e.g., knb.1.1)
|
526
|
* @param file the file to send
|
527
|
*/
|
528
|
public boolean insertDataFile(String id, File file) {
|
529
|
String response = null;
|
530
|
//Get response for calling sendDataFile function
|
531
|
response = sendDataFile(id, file);
|
532
|
|
533
|
if (response.indexOf("success") != -1) {
|
534
|
//insert successfully
|
535
|
return true;
|
536
|
} else {
|
537
|
return false;
|
538
|
}
|
539
|
}
|
540
|
|
541
|
/**
|
542
|
* sends a data file to the metacat using "multipart/form-data" encoding
|
543
|
*
|
544
|
* @param id the id to assign to the file on metacat (e.g., knb.1.1)
|
545
|
* @param file the file to send
|
546
|
*/
|
547
|
public String sendDataFile(String docid, File file) {
|
548
|
|
549
|
String response = null;
|
550
|
|
551
|
try {
|
552
|
HttpClient httpclient = new DefaultHttpClient();
|
553
|
httpclient.getParams().setParameter(
|
554
|
CoreProtocolPNames.PROTOCOL_VERSION,
|
555
|
HttpVersion.HTTP_1_1);
|
556
|
httpclient.getParams().setParameter(
|
557
|
CoreProtocolPNames.HTTP_CONTENT_CHARSET,
|
558
|
"UTF-8");
|
559
|
|
560
|
HttpPost post = new HttpPost(metacatUrl);
|
561
|
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
|
562
|
|
563
|
// For File parameters
|
564
|
entity.addPart("datafile", new FileBody(file));
|
565
|
|
566
|
//set up properties
|
567
|
Properties prop = new Properties();
|
568
|
prop.put("action", "upload");
|
569
|
prop.put("docid", docid);
|
570
|
|
571
|
// For usual String parameters
|
572
|
Enumeration<Object> keys = prop.keys();
|
573
|
while (keys.hasMoreElements()) {
|
574
|
String key = (String) keys.nextElement();
|
575
|
String value = prop.getProperty(key);
|
576
|
entity.addPart(key, new StringBody(value, Charset.forName("UTF-8")));
|
577
|
}
|
578
|
|
579
|
post.setHeader("Cookie", "JSESSIONID=" + sessionId);
|
580
|
post.setEntity(entity);
|
581
|
|
582
|
response = EntityUtils.toString(httpclient.execute(post).getEntity(), "UTF-8");
|
583
|
httpclient.getConnectionManager().shutdown();
|
584
|
} catch (Exception e) {
|
585
|
e.printStackTrace();
|
586
|
}
|
587
|
|
588
|
return response;
|
589
|
}
|
590
|
|
591
|
public String getMetacatString(Properties prop) {
|
592
|
String response = null;
|
593
|
|
594
|
// Now contact metacat and send the request
|
595
|
try {
|
596
|
InputStreamReader returnStream = new InputStreamReader(
|
597
|
getMetacatInputStream(prop));
|
598
|
StringWriter sw = new StringWriter();
|
599
|
int len;
|
600
|
char[] characters = new char[512];
|
601
|
while ((len = returnStream.read(characters, 0, 512)) != -1) {
|
602
|
sw.write(characters, 0, len);
|
603
|
}
|
604
|
returnStream.close();
|
605
|
response = sw.toString();
|
606
|
sw.close();
|
607
|
} catch (Exception e) {
|
608
|
return null;
|
609
|
}
|
610
|
|
611
|
return response;
|
612
|
}
|
613
|
|
614
|
/**
|
615
|
* Send a request to Metacat
|
616
|
*
|
617
|
* @param prop the properties to be sent to Metacat
|
618
|
* @return InputStream as returned by Metacat
|
619
|
*/
|
620
|
public InputStream getMetacatInputStream(Properties prop) throws Exception {
|
621
|
|
622
|
InputStream result = null;
|
623
|
HttpClient httpclient = new DefaultHttpClient();
|
624
|
httpclient.getParams().setParameter(
|
625
|
CoreProtocolPNames.PROTOCOL_VERSION,
|
626
|
HttpVersion.HTTP_1_1);
|
627
|
httpclient.getParams().setParameter(
|
628
|
CoreProtocolPNames.HTTP_CONTENT_CHARSET,
|
629
|
"UTF-8");
|
630
|
HttpPost post = new HttpPost(metacatUrl);
|
631
|
//set the params
|
632
|
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
|
633
|
Enumeration<Object> keys = prop.keys();
|
634
|
while (keys.hasMoreElements()) {
|
635
|
String key = (String) keys.nextElement();
|
636
|
String value = prop.getProperty(key);
|
637
|
NameValuePair nvp = new BasicNameValuePair(key, value);
|
638
|
nameValuePairs.add(nvp);
|
639
|
}
|
640
|
|
641
|
debug("getMetacatInputStream.sessionId: " + sessionId);
|
642
|
|
643
|
post.setHeader("Cookie", "JSESSIONID=" + sessionId);
|
644
|
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
|
645
|
|
646
|
HttpResponse httpResponse = httpclient.execute(post);
|
647
|
result = httpResponse.getEntity().getContent();
|
648
|
|
649
|
return result;
|
650
|
|
651
|
|
652
|
}
|
653
|
|
654
|
}
|