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