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