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