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