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