Project

General

Profile

1 1112 tao
/**
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 3080 jones
 *    Authors: Jing Tao
7 1112 tao
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
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 1118 tao
package edu.ucsb.nceas.metacattest;
28 1112 tao
29 4301 daigle
import edu.ucsb.nceas.MCTestCase;
30 7843 leinfelder
import edu.ucsb.nceas.metacat.IdentifierManager;
31 5035 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
32 1822 jones
import edu.ucsb.nceas.utilities.HttpMessage;
33 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
34 6004 cjones
35 1112 tao
import junit.framework.Test;
36
import junit.framework.TestSuite;
37
38
import java.io.*;
39
import java.net.*;
40
import java.util.*;
41
42 6004 cjones
43 1112 tao
/**
44
 * A JUnit test for testing Step class processing
45
 */
46 4301 daigle
public class MetaCatServletTest extends MCTestCase {
47 4080 daigle
	private static String metacatURL;
48
	private String serialNumber;
49 4301 daigle
50
	/* Initialize properties */
51 4080 daigle
	static {
52
		try {
53 4231 daigle
			metacatURL = PropertyService.getProperty("test.metacatUrl");
54 4301 daigle
		} catch (PropertyNotFoundException pnfe) {
55
			System.err.println("Could not get property in static block: "
56
					+ pnfe.getMessage());
57 4080 daigle
		}
58
	}
59 1112 tao
60 4080 daigle
	/**
61
	 * Constructor to build the test
62 4301 daigle
	 *
63
	 * @param name
64
	 *            the name of the test method
65 4080 daigle
	 */
66
	public MetaCatServletTest(String name) {
67
		super(name);
68
	}
69 1112 tao
70 4080 daigle
	/**
71
	 * Constructor to build the test
72 4301 daigle
	 *
73
	 * @param name
74
	 *            the name of the test method
75 4080 daigle
	 */
76
	public MetaCatServletTest(String name, String serial) {
77
		super(name);
78
		serialNumber = serial;
79
	}
80 1112 tao
81 4080 daigle
	/**
82
	 * Establish a testing framework by initializing appropriate objects
83
	 */
84
	public void setUp() {
85 1112 tao
86 4080 daigle
	}
87 1112 tao
88 4080 daigle
	/**
89
	 * Release any objects after tests are complete
90
	 */
91
	public void tearDown() {
92
	}
93 1112 tao
94 4080 daigle
	/**
95
	 * Create a suite of tests to be run together
96
	 */
97
	public static Test suite() {
98
		double number = 0;
99
		String serial = null;
100 1112 tao
101 4080 daigle
		TestSuite suite = new TestSuite();
102
		suite.addTest(new MetaCatServletTest("initialize"));
103
		suite.addTest(new MetaCatServletTest("testLterReferralLogin"));
104
		suite.addTest(new MetaCatServletTest("testLterReferralLoginFail"));
105 6211 leinfelder
		suite.addTest(new MetaCatServletTest("testOtherReferralLogin"));
106
		suite.addTest(new MetaCatServletTest("testOtherReferralLoginFail"));
107 4080 daigle
		suite.addTest(new MetaCatServletTest("testNCEASLoginFail"));
108 4301 daigle
		// Should put a login successfully at the end of login test
109
		// So insert or update can have cookie.
110 4080 daigle
		suite.addTest(new MetaCatServletTest("testNCEASLogin"));
111 1112 tao
112 4301 daigle
		// create random number for docid, so it can void repeat
113 4080 daigle
		number = Math.random() * 100000;
114
		serial = Integer.toString(((new Double(number)).intValue()));
115 4301 daigle
		debug("serial: " + serial);
116 4080 daigle
		suite.addTest(new MetaCatServletTest("testInsertXMLDocument", serial));
117
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentXMLFormat", serial));
118
		suite.addTest(new MetaCatServletTest("testUpdateXMLDocument", serial));
119 6211 leinfelder
120 4080 daigle
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentHTMLFormat", serial));
121
		suite.addTest(new MetaCatServletTest("testReadXMLDocumentZipFormat", serial));
122 6211 leinfelder
123 4080 daigle
		suite.addTest(new MetaCatServletTest("testDeleteXMLDocument", serial));
124 6360 leinfelder
125 4301 daigle
		// insert invalid xml document
126 4080 daigle
		number = Math.random() * 100000;
127
		serial = Integer.toString(((new Double(number)).intValue()));
128
		suite.addTest(new MetaCatServletTest("testInsertInvalidateXMLDocument", serial));
129 4301 daigle
		// insert non well formed document
130 4080 daigle
		number = Math.random() * 100000;
131
		serial = Integer.toString(((new Double(number)).intValue()));
132
		suite
133
				.addTest(new MetaCatServletTest("testInsertNonWellFormedXMLDocument",
134
						serial));
135 6211 leinfelder
136 4080 daigle
		suite.addTest(new MetaCatServletTest("testLogOut"));
137 7843 leinfelder
138
		suite.addTest(new MetaCatServletTest("testReindexFail"));
139 8695 tao
140
141 4080 daigle
		return suite;
142
	}
143 1112 tao
144 4080 daigle
	/**
145 4301 daigle
	 * Run an initial test that always passes to check that the test harness is
146
	 * working.
147 4080 daigle
	 */
148
	public void initialize() {
149
		assertTrue(1 == 1);
150
	}
151 1112 tao
152 4080 daigle
	/**
153
	 * Test the login to neceas succesfully
154
	 */
155 6094 jones
    public void testNCEASLogin() {
156
        debug("\nRunning: testNCEASLogin test");
157
        try {
158
            String user = PropertyService.getProperty("test.mcUser");
159
            String passwd = PropertyService.getProperty("test.mcPassword");
160
            assertTrue(logIn(user, passwd));
161
        } catch (PropertyNotFoundException pnfe) {
162
            fail("Could not find property: " + pnfe.getMessage());
163
        }
164
    }
165 4080 daigle
166
	/**
167
	 * Test the login to neceas failed
168
	 */
169
	public void testNCEASLoginFail() {
170 4301 daigle
		debug("\nRunning: testNCEASLoginFail test");
171 6094 jones
        try {
172
            String user = PropertyService.getProperty("test.mcUser");
173
            String passwd = "BogusPasswordShouldFail";
174
            assertTrue(!logIn(user, passwd));
175
        } catch (PropertyNotFoundException pnfe) {
176
            fail("Could not find property: " + pnfe.getMessage());
177
        }
178 4080 daigle
	}
179
180
	/**
181
	 * Test the login to lter succesfully
182
	 */
183
	public void testLterReferralLogin() {
184 4301 daigle
		debug("\nRunning: testLterReferralLogin test");
185 4080 daigle
		String user = null;
186
		String passwd = null;
187
		try {
188 4231 daigle
			user = PropertyService.getProperty("test.lterUser");
189
			passwd = PropertyService.getProperty("test.lterPassword");
190 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
191
			fail("Could not find property: " + pnfe.getMessage());
192
		}
193 4301 daigle
194
		debug("Logging into lter: " + user + " : " + passwd);
195 4080 daigle
		assertTrue(logIn(user, passwd));
196
197
	}
198
199
	/**
200
	 * Test the login to lter failed
201
	 */
202
	public void testLterReferralLoginFail() {
203 4301 daigle
		debug("\nRunning: testLterReferralLoginFail test");
204 6211 leinfelder
		String user = null;
205
    String passwd = "wrong";
206
    try {
207
      user = PropertyService.getProperty("test.lterUser");
208
    } catch (PropertyNotFoundException pnfe) {
209
      fail("Could not find property: " + pnfe.getMessage());
210
    }
211 4080 daigle
		assertTrue(!logIn(user, passwd));
212 4301 daigle
		// assertTrue( withProtocol.getProtocol().equals("http"));
213 4080 daigle
	}
214
215
	/**
216 6211 leinfelder
	 * Test the login to Other succesfully
217 4080 daigle
	 */
218 6211 leinfelder
	public void testOtherReferralLogin() {
219
		debug("\nRunning: testOtherReferralLogin test");
220 4080 daigle
		String user = null;
221
		String passwd = null;
222
		try {
223 6211 leinfelder
			user = PropertyService.getProperty("test.referralUser");
224
			passwd = PropertyService.getProperty("test.referralPassword");
225 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
226
			fail("Could not find property: " + pnfe.getMessage());
227
		}
228 6211 leinfelder
		debug("logging in Other user: " + user + ":" + passwd);
229 4080 daigle
		assertTrue(logIn(user, passwd));
230
		// assertTrue( withProtocol.getProtocol().equals("http"));
231
	}
232
233
	/**
234 6211 leinfelder
	 * Test the login to Other failed
235 4080 daigle
	 */
236 6211 leinfelder
	public void testOtherReferralLoginFail() {
237
		debug("\nRunning: testOtherReferralLoginFail test");
238
		String user = null;
239
    String passwd = "wrong";
240
    try {
241
      user = PropertyService.getProperty("test.referralUser");
242
    } catch (PropertyNotFoundException pnfe) {
243
      fail("Could not find property: " + pnfe.getMessage());
244
    }
245 4080 daigle
		assertTrue(!logIn(user, passwd));
246 4301 daigle
		// assertTrue( withProtocol.getProtocol().equals("http"));
247 4080 daigle
	}
248 8695 tao
249 4080 daigle
250 8695 tao
251 4080 daigle
	/**
252
	 * Test insert a xml document successfully
253
	 */
254 6094 jones
    public void testInsertXMLDocument() {
255
        debug("\nRunning: testInsertXMLDocument test");
256
        String name = null;
257
        try {
258
            String user = PropertyService.getProperty("test.mcUser");
259
            String passwd = PropertyService.getProperty("test.mcPassword");
260 4080 daigle
261 6094 jones
            name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber + PropertyService.getProperty("document.accNumSeparator")
262
                    + "1";
263
            debug("insert docid: " + name);
264
            String content = "<?xml version=\"1.0\"?>" + "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
265
                    + "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb.edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
266
                    + "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name + "</identifier>" + "<allow>" + "<principal>" + user
267
                    + "</principal>" + "<permission>all</permission>" + "</allow>" + "<allow>" + "<principal>public</principal>"
268
                    + "<permission>read</permission>" + "</allow>" + "</acl>";
269
            debug("xml document: " + content);
270
            assertTrue(handleXMLDocument(content, name, "insert"));
271
        } catch (PropertyNotFoundException pnfe) {
272
            fail("Could not find property: " + pnfe.getMessage());
273
        }
274
    }
275 4080 daigle
276
	/**
277 4301 daigle
	 * Test insert a invalidate xml document successfully In the String, there
278
	 * is no <!Doctype ... Public/System/>
279 4080 daigle
	 */
280 6094 jones
    public void testInsertInvalidateXMLDocument() {
281
        debug("\nRunning: testInsertInvalidateXMLDocument test");
282
        String name = null;
283
        try {
284
            String user = PropertyService.getProperty("test.mcUser");
285
            String passwd = PropertyService.getProperty("test.mcPassword");
286 4080 daigle
287 6094 jones
            name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber + PropertyService.getProperty("document.accNumSeparator")
288
                    + "1";
289
            debug("insert docid: " + name);
290
            String content = "<?xml version=\"1.0\"?>" + "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name + "</identifier>" + "<allow>"
291
                    + "<principal>" + user + "</principal>" + "<permission>all</permission>" + "</allow>" + "<allow>" + "<principal>public</principal>"
292
                    + "<permission>read</permission>" + "</allow>" + "</acl>";
293
            debug("xml document: " + content);
294
            assertTrue(handleXMLDocument(content, name, "insert"));
295
        } catch (PropertyNotFoundException pnfe) {
296
            fail("Could not find property: " + pnfe.getMessage());
297
        }
298
    }
299
300 4080 daigle
	/**
301 4301 daigle
	 * Test insert a non well-formed xml document successfully There is no
302
	 * </acl> in this string
303 4080 daigle
	 */
304 6094 jones
    public void testInsertNonWellFormedXMLDocument() {
305
        debug("\nRunning: testInsertNonWellFormedXMLDocument test");
306
        String name = null;
307
        try {
308
            String user = PropertyService.getProperty("test.mcUser");
309
            String passwd = PropertyService.getProperty("test.mcPassword");
310 4080 daigle
311 6094 jones
            name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber + PropertyService.getProperty("document.accNumSeparator")
312
                    + "1";
313
            debug("insert non well-formed docid: " + name);
314
            String content = "<?xml version=\"1.0\"?>" + "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name + "</identifier>" + "<allow>"
315
                    + "<principal>" + user + "</principal>" + "<permission>all</permission>" + "</allow>" + "<allow>" + "<principal>public</principal>"
316
                    + "<permission>read</permission>" + "</allow>";
317 4080 daigle
318 6094 jones
            debug("xml document: " + content);
319
            assertTrue(!handleXMLDocument(content, name, "insert"));
320
        } catch (PropertyNotFoundException pnfe) {
321
            fail("Could not find property: " + pnfe.getMessage());
322
        }
323
    }
324
325 4080 daigle
	/**
326 4301 daigle
	 * Test read a xml document in xml format successfully
327 4080 daigle
	 */
328
	public void testReadXMLDocumentXMLFormat() {
329 4301 daigle
		debug("\nRunning: testReadXMLDocumentXMLFormat test");
330 4080 daigle
		String name = null;
331
		try {
332 4301 daigle
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
333
					+ serialNumber
334
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
335 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
336
			fail("Could not find property: " + pnfe.getMessage());
337
		}
338
		assertTrue(handleReadAction(name, "xml"));
339
340
	}
341
342
	/**
343 4301 daigle
	 * Test read a xml document in html format successfully
344 4080 daigle
	 */
345
	public void testReadXMLDocumentHTMLFormat() {
346 4301 daigle
		debug("\nRunning: testReadXMLDocumentHTMLFormat test");
347 4080 daigle
		String name = null;
348
		try {
349 4301 daigle
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
350
					+ serialNumber
351
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
352 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
353
			fail("Could not find property: " + pnfe.getMessage());
354 4301 daigle
		}
355 4080 daigle
		assertTrue(handleReadAction(name, "html"));
356
357
	}
358
359
	/**
360 4301 daigle
	 * Test read a xml document in zip format successfully
361 4080 daigle
	 */
362
	public void testReadXMLDocumentZipFormat() {
363 4301 daigle
		debug("\nRunning: testReadXMLDocumentZipFormat test");
364 4080 daigle
		String name = null;
365
		try {
366 4301 daigle
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
367
					+ serialNumber
368
					+ PropertyService.getProperty("document.accNumSeparator") + "1";
369 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
370
			fail("Could not find property: " + pnfe.getMessage());
371 4301 daigle
		}
372 4080 daigle
		assertTrue(handleReadAction(name, "zip"));
373
374
	}
375
376
	/**
377
	 * Test insert a xml document successfully
378
	 */
379 6094 jones
    public void testUpdateXMLDocument() {
380
        debug("\nRunning: testUpdateXMLDocument test");
381
        String name = null;
382
        try {
383
            String user = PropertyService.getProperty("test.mcUser");
384
            String passwd = PropertyService.getProperty("test.mcPassword");
385 4080 daigle
386 6094 jones
            name = "john" + PropertyService.getProperty("document.accNumSeparator") + serialNumber + PropertyService.getProperty("document.accNumSeparator")
387
                    + "2";
388
            debug("update docid: " + name);
389
            String content = "<?xml version=\"1.0\"?>" + "<!DOCTYPE acl PUBLIC \"-//ecoinformatics.org//"
390
                    + "eml-access-2.0.0beta6//EN\" \"http://pine.nceas.ucsb." + "edu:8080/tao/dtd/eml-access-2.0.0beta6.dtd\">"
391
                    + "<acl authSystem=\"knb\" order=\"allowFirst\">" + "<identifier>" + name + "</identifier>" + "<allow>" + "<principal>" + user
392
                    + "</principal>" + "<permission>all</permission>" + "</allow>" + "<allow>" + "<principal>public</principal>"
393
                    + "<permission>read</permission>" + "</allow>" + "</acl>";
394
            debug("xml document: " + content);
395
            assertTrue(handleXMLDocument(content, name, "update"));
396
        } catch (PropertyNotFoundException pnfe) {
397
            fail("Could not find property: " + pnfe.getMessage());
398
        }
399
    }
400 4080 daigle
401
	/**
402
	 * Test delete a xml document successfully
403
	 */
404
	public void testDeleteXMLDocument() {
405 4301 daigle
		debug("\nRunning: testDeleteXMLDocument test");
406 4080 daigle
		String name = null;
407
		try {
408 4301 daigle
			name = "john" + PropertyService.getProperty("document.accNumSeparator")
409
					+ serialNumber
410
					+ PropertyService.getProperty("document.accNumSeparator") + "2";
411 4080 daigle
		} catch (PropertyNotFoundException pnfe) {
412
			fail("Could not find property: " + pnfe.getMessage());
413
		}
414 4301 daigle
		debug("delete docid: " + name);
415 4080 daigle
		assertTrue(handleDeleteFile(name));
416
417
	}
418
419 6004 cjones
420 4080 daigle
	/**
421
	 * Test logout action
422
	 */
423
	public void testLogOut() {
424 4301 daigle
		debug("\nRunning: testLogOut test");
425 4080 daigle
		assertTrue(handleLogOut());
426
427
	}
428 7843 leinfelder
429
	public void testReindexFail() {
430 4080 daigle
431 7843 leinfelder
		// find a pid to reindex
432
		String identifier = null;
433
		List<String> ids = IdentifierManager.getInstance().getAllSystemMetadataGUIDs();
434
		if (ids != null && !ids.isEmpty()) {
435
			identifier = ids.get(0);
436
		}
437
		Properties prop = new Properties();
438
		prop.put("action", "reindex");
439
		prop.put("pid", identifier);
440
441
		String message = getMetacatString(prop);
442
		debug("Reindex Message: " + message);
443
		if (message.indexOf("<error>") != -1) {// there was an error
444
			assertTrue(true);
445
		} else if (message.indexOf("<success>") != -1) {
446
			fail("Unauthenticated user should not be able to invoke this action: " + message);
447
		} else {// something weird happened.
448
			fail("There was an unexpected error reindexing pid: " + message);
449
		}
450
	}
451
452 4080 daigle
	/**
453
	 * Method to hanld login action
454 4301 daigle
	 *
455
	 * @param usrerName,
456
	 *            the DN name of the test method
457
	 * @param passWord,
458
	 *            the passwd of the user
459 4080 daigle
	 */
460
461
	public boolean logIn(String userName, String passWord) {
462
		Properties prop = new Properties();
463
		prop.put("action", "login");
464
		prop.put("qformat", "xml");
465
		prop.put("username", userName);
466
		prop.put("password", passWord);
467
468
		// Now contact metacat
469
		String response = getMetacatString(prop);
470 4301 daigle
		debug("Login Message: " + response);
471 4080 daigle
		boolean connected = false;
472 6211 leinfelder
		if (response != null && response.indexOf("<login>") != -1) {
473 4080 daigle
			connected = true;
474
		} else {
475
476
			connected = false;
477
		}
478
479
		return connected;
480
	}
481
482
	/**
483
	 * Method to hanld logout action
484 4301 daigle
	 *
485
	 * @param usrerName,
486
	 *            the DN name of the test method
487
	 * @param passWord,
488
	 *            the passwd of the user
489 4080 daigle
	 */
490
491
	public boolean handleLogOut() {
492
		boolean disConnected = false;
493
		Properties prop = new Properties();
494
		prop.put("action", "logout");
495
		prop.put("qformat", "xml");
496
497
		String response = getMetacatString(prop);
498 4301 daigle
		debug("Logout Message: " + response);
499 4080 daigle
		HttpMessage.setCookie(null);
500
501
		if (response.indexOf("<logout>") != -1) {
502
			disConnected = true;
503
		} else {
504
			disConnected = false;
505
		}
506
507
		return disConnected;
508
	}
509
510
	/**
511
	 * Method to hanld read both xml and data file
512 4301 daigle
	 *
513
	 * @param docid,
514
	 *            the docid of the document want to read
515
	 * @param qformat,
516
	 *            the format of document user want to get
517 4080 daigle
	 */
518
	public boolean handleReadAction(String docid, String qformat) {
519
		Properties prop = new Properties();
520
		String message = "";
521
		prop.put("action", "read");
522
		prop.put("qformat", qformat);
523
		prop.put("docid", docid);
524
525
		message = getMetacatString(prop);
526
		message = message.trim();
527 4301 daigle
		if (message == null || message.equals("") || message.indexOf("<error>") != -1) {// there
528
																						// was
529
																						// an
530
																						// error
531 4080 daigle
532
			return false;
533 4301 daigle
		} else {// successfully
534 4080 daigle
			return true;
535
		}
536
537
	}
538
539
	/**
540
	 * Method to hanld inset or update xml document
541 4301 daigle
	 *
542
	 * @param xmlDocument,
543
	 *            the content of xml qformat
544
	 * @param docid,
545
	 *            the docid of the document
546
	 * @param action,
547
	 *            insert or update
548 4080 daigle
	 */
549
	public boolean handleXMLDocument(String xmlDocument, String docid, String action)
550
551 4301 daigle
	{ // -attempt to write file to metacat
552 4080 daigle
		String access = "no";
553
		StringBuffer fileText = new StringBuffer();
554
		StringBuffer messageBuf = new StringBuffer();
555
		String accessFileId = null;
556
		Properties prop = new Properties();
557
		prop.put("action", action);
558 4301 daigle
		prop.put("public", access); // This is the old way of controlling access
559 4080 daigle
		prop.put("doctext", xmlDocument);
560
		prop.put("docid", docid);
561
562
		String message = getMetacatString(prop);
563 4301 daigle
		debug("Insert or Update Message: " + message);
564
		if (message.indexOf("<error>") != -1) {// there was an error
565 4080 daigle
566
			return false;
567 4301 daigle
		} else if (message.indexOf("<success>") != -1) {// the operation worked
568
			// write the file to the cache and return the file object
569 4080 daigle
			return true;
570
571 4301 daigle
		} else {// something weird happened.
572 4080 daigle
			return false;
573
		}
574
575
	}
576
577
	public boolean handleDeleteFile(String name) {
578
579
		Properties prop = new Properties();
580
		prop.put("action", "delete");
581
		prop.put("docid", name);
582
583
		String message = getMetacatString(prop);
584 4301 daigle
		debug("Delete Message: " + message);
585
		if (message.indexOf("<error>") != -1) {// there was an error
586 4080 daigle
587
			return false;
588 4301 daigle
		} else if (message.indexOf("<success>") != -1) {// the operation worked
589
			// write the file to the cache and return the file object
590 4080 daigle
			return true;
591
592 4301 daigle
		} else {// something weird happened.
593 4080 daigle
			return false;
594
		}
595
	}
596
597
	public String getMetacatString(Properties prop) {
598
		String response = null;
599
600
		// Now contact metacat and send the request
601
		try {
602
			InputStreamReader returnStream = new InputStreamReader(
603
					getMetacatInputStream(prop));
604
			StringWriter sw = new StringWriter();
605
			int len;
606
			char[] characters = new char[512];
607
			while ((len = returnStream.read(characters, 0, 512)) != -1) {
608
				sw.write(characters, 0, len);
609
			}
610
			returnStream.close();
611
			response = sw.toString();
612
			sw.close();
613
		} catch (Exception e) {
614
			return null;
615
		}
616
617
		return response;
618
	}
619
620
	/**
621
	 * Send a request to Metacat
622 4301 daigle
	 *
623
	 * @param prop
624
	 *            the properties to be sent to Metacat
625 4080 daigle
	 * @return InputStream as returned by Metacat
626
	 */
627
	public InputStream getMetacatInputStream(Properties prop) {
628
		InputStream returnStream = null;
629
		// Now contact metacat and send the request
630
		try {
631
632
			URL url = new URL(metacatURL);
633
			HttpMessage msg = new HttpMessage(url);
634
			returnStream = msg.sendPostMessage(prop);
635
			return returnStream;
636
		} catch (Exception e) {
637
			e.printStackTrace(System.err);
638
639
		}
640
		return returnStream;
641
642
	}
643
644 1112 tao
}