Revision 6700
Added by ben leinfelder about 13 years ago
src/edu/ucsb/nceas/metacat/util/MetacatPopulator.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
import java.io.ByteArrayInputStream; |
29 | 29 |
import java.io.InputStream; |
30 |
import java.io.OutputStream; |
|
31 | 30 |
import java.math.BigInteger; |
32 | 31 |
import java.net.HttpURLConnection; |
33 | 32 |
import java.net.URL; |
... | ... | |
39 | 38 |
import java.util.Map; |
40 | 39 |
import java.util.Vector; |
41 | 40 |
|
42 |
import javax.activation.DataHandler; |
|
43 |
import javax.activation.DataSource; |
|
44 |
import javax.mail.internet.MimeBodyPart; |
|
45 |
import javax.mail.internet.MimeMultipart; |
|
46 |
|
|
47 | 41 |
import org.apache.commons.io.IOUtils; |
48 | 42 |
import org.dataone.client.D1Client; |
49 | 43 |
import org.dataone.client.MNode; |
... | ... | |
71 | 65 |
import edu.ucsb.nceas.metacat.MetaCatServlet; |
72 | 66 |
import edu.ucsb.nceas.metacat.dataquery.MetacatDatabaseConnectionPoolFactory; |
73 | 67 |
import edu.ucsb.nceas.metacat.properties.PropertyService; |
74 |
import edu.ucsb.nceas.metacat.restservice.InputStreamDataSource; |
|
75 | 68 |
|
76 | 69 |
/** |
77 | 70 |
* @author berkley |
... | ... | |
116 | 109 |
public void populate() |
117 | 110 |
throws Exception |
118 | 111 |
{ |
119 |
//String sourceSessionid = loginSource();
|
|
112 |
//String sourceSessionid = login(); |
|
120 | 113 |
|
121 | 114 |
//do a query |
122 | 115 |
String params = "returndoctype=eml://ecoinformatics.org/eml-2.1.0&" + |
... | ... | |
129 | 122 |
printHeader("Searching source"); |
130 | 123 |
System.out.println("searching '" + sourceUrl + "' for '" + query + "'"); |
131 | 124 |
InputStream is = getResponse(sourceUrl, "/metacat", params, "POST"); |
132 |
String response = streamToString(is);
|
|
125 |
String response = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
|
|
133 | 126 |
//System.out.println("response: " + response); |
134 | 127 |
Vector<Document> docs = parseResponse(response); |
135 | 128 |
|
... | ... | |
151 | 144 |
printHeader("Getting document " + doc.docid + " from source " + sourceUrl); |
152 | 145 |
params = "action=read&qformat=xml&docid=" + docid; |
153 | 146 |
is = getResponse(sourceUrl, "/metacat", params, "POST"); |
154 |
String doctext = streamToString(is);
|
|
147 |
String doctext = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
|
|
155 | 148 |
System.out.println("doctext: " + doctext); |
156 |
is = stringToStream(doctext);
|
|
149 |
is = IOUtils.toInputStream(doctext, MetaCatServlet.DEFAULT_ENCODING);
|
|
157 | 150 |
//parse the document |
158 | 151 |
DatabaseConnectionPoolInterface connectionPool = MetacatDatabaseConnectionPoolFactory.getDatabaseConnectionPoolInterface(); |
159 | 152 |
DataManager dataManager = DataManager.getInstance(connectionPool, connectionPool.getDBAdapterName()); |
... | ... | |
164 | 157 |
} |
165 | 158 |
|
166 | 159 |
//go through the DistributionMetadata and download any described data |
167 |
is = stringToStream(doctext);
|
|
160 |
is = IOUtils.toInputStream(doctext, MetaCatServlet.DEFAULT_ENCODING);
|
|
168 | 161 |
doc.doctext = doctext; |
169 | 162 |
|
170 | 163 |
printHeader("creating document on destination " + destinationUrl); |
... | ... | |
186 | 179 |
//get the file |
187 | 180 |
params = "action=read&qformat=xml&docid=" + dataDocLocalId; |
188 | 181 |
InputStream dataDocIs = getResponse(sourceUrl, "/metacat", params, "POST"); |
189 |
String dataDocText = streamToString(dataDocIs);
|
|
182 |
String dataDocText = IOUtils.toString(dataDocIs, MetaCatServlet.DEFAULT_ENCODING);
|
|
190 | 183 |
|
191 | 184 |
//set the id |
192 | 185 |
Identifier did = new Identifier(); |
... | ... | |
206 | 199 |
} catch (NotFound e) { |
207 | 200 |
System.out.println(e.getMessage()); |
208 | 201 |
} |
209 |
dataDocIs = stringToStream(dataDocText);
|
|
202 |
dataDocIs = IOUtils.toInputStream(dataDocText, MetaCatServlet.DEFAULT_ENCODING);
|
|
210 | 203 |
Checksum checksum = ChecksumUtil.checksum(dataDocIs, "MD5"); |
211 | 204 |
dataDocSysMeta.setChecksum(checksum); |
212 | 205 |
String sizeStr = |
... | ... | |
217 | 210 |
|
218 | 211 |
//create the data doc on d1 |
219 | 212 |
try { |
220 |
mn.create(session, dataDocSysMeta.getIdentifier(), IOUtils.toInputStream(dataDocText), dataDocSysMeta); |
|
213 |
mn.create(session, dataDocSysMeta.getIdentifier(), IOUtils.toInputStream(dataDocText, MetaCatServlet.DEFAULT_ENCODING), dataDocSysMeta);
|
|
221 | 214 |
} |
222 | 215 |
catch(Exception e) { |
223 | 216 |
error = true; |
... | ... | |
245 | 238 |
|
246 | 239 |
try { |
247 | 240 |
Identifier id = |
248 |
mn.create(session, sysmeta.getIdentifier(), IOUtils.toInputStream(doc.doctext), sysmeta); |
|
241 |
mn.create(session, sysmeta.getIdentifier(), IOUtils.toInputStream(doc.doctext, MetaCatServlet.DEFAULT_ENCODING), sysmeta);
|
|
249 | 242 |
System.out.println("Success inserting document " + id.getValue()); |
250 | 243 |
|
251 | 244 |
// no need for an ORE map if there's no data |
... | ... | |
259 | 252 |
Document rmDoc = new Document(resourceMapId.getValue(), "http://www.openarchives.org/ore/terms", "", ""); |
260 | 253 |
rmDoc.doctext = resourceMapXML; |
261 | 254 |
SystemMetadata resourceMapSysMeta = generateSystemMetadata(rmDoc); |
262 |
mn.create(session, resourceMapId, IOUtils.toInputStream(resourceMapXML), resourceMapSysMeta); |
|
255 |
mn.create(session, resourceMapId, IOUtils.toInputStream(resourceMapXML, MetaCatServlet.DEFAULT_ENCODING), resourceMapSysMeta);
|
|
263 | 256 |
|
264 | 257 |
// clean up the permissions (FORCE public read) |
265 | 258 |
for (Identifier dataId: dataIds) { |
... | ... | |
376 | 369 |
new Integer(day).intValue()); |
377 | 370 |
return c.getTime(); |
378 | 371 |
} |
379 |
|
|
380 |
/** |
|
381 |
* send a request to the resource |
|
382 |
*/ |
|
383 |
private InputStream sendRequest(String contextRootUrl, String resource, |
|
384 |
String sessionid, String method, String urlParamaters, |
|
385 |
String contentType, InputStream dataStream) |
|
386 |
throws Exception |
|
387 |
{ |
|
388 |
|
|
389 |
HttpURLConnection connection = null ; |
|
390 |
String restURL = contextRootUrl + resource; |
|
391 |
|
|
392 |
if (urlParamaters != null) { |
|
393 |
if (restURL.indexOf("?") == -1) |
|
394 |
restURL += "?"; |
|
395 |
restURL += urlParamaters; |
|
396 |
if(restURL.indexOf(" ") != -1) |
|
397 |
{ |
|
398 |
restURL = restURL.replaceAll("\\s", "%20"); |
|
399 |
} |
|
400 |
} |
|
401 |
|
|
402 |
if(sessionid != null) |
|
403 |
{ |
|
404 |
if(restURL.indexOf("?") == -1) |
|
405 |
{ |
|
406 |
restURL += "?sessionid=" + sessionid; |
|
407 |
} |
|
408 |
else |
|
409 |
{ |
|
410 |
restURL += "&sessionid=" + sessionid; |
|
411 |
} |
|
412 |
} |
|
413 |
|
|
414 |
URL u = null; |
|
415 |
InputStream content = null; |
|
416 |
System.out.println("url: " + restURL); |
|
417 |
System.out.println("method: " + method); |
|
418 |
u = new URL(restURL); |
|
419 |
connection = (HttpURLConnection) u.openConnection(); |
|
420 |
if (contentType!=null) { |
|
421 |
connection.setRequestProperty("Content-Type",contentType); |
|
422 |
} |
|
423 |
|
|
424 |
connection.setDoOutput(true); |
|
425 |
connection.setDoInput(true); |
|
426 |
connection.setRequestMethod(method); |
|
427 |
|
|
428 |
if (!method.equals("GET")) { |
|
429 |
if (dataStream != null) { |
|
430 |
OutputStream out = connection.getOutputStream(); |
|
431 |
IOUtils.copy(dataStream, out); |
|
432 |
} |
|
433 |
} |
|
434 |
|
|
435 |
return connection.getInputStream(); |
|
436 |
} |
|
437 | 372 |
|
438 | 373 |
/** |
439 |
* create a mime multipart message from object and sysmeta |
|
440 |
*/ |
|
441 |
private MimeMultipart createMimeMultipart(InputStream object) |
|
442 |
throws Exception |
|
443 |
{ |
|
444 |
final MimeMultipart mmp = new MimeMultipart(); |
|
445 |
MimeBodyPart objectPart = new MimeBodyPart(); |
|
446 |
objectPart.addHeaderLine("Content-Transfer-Encoding: base64"); |
|
447 |
objectPart.setFileName("doctext"); |
|
448 |
DataSource ds = new InputStreamDataSource("doctext", object); |
|
449 |
DataHandler dh = new DataHandler(ds); |
|
450 |
objectPart.setDataHandler(dh); |
|
451 |
mmp.addBodyPart(objectPart); |
|
452 |
return mmp; |
|
453 |
} |
|
454 |
|
|
455 |
/** |
|
456 | 374 |
* parse a metacat query response and return a vector of docids |
457 | 375 |
* @param response |
458 | 376 |
* @return |
... | ... | |
489 | 407 |
return s; |
490 | 408 |
} |
491 | 409 |
|
492 |
/** |
|
493 |
* login the source |
|
494 |
* @return |
|
495 |
* @throws Exception |
|
496 |
*/ |
|
497 |
private String loginSource() |
|
498 |
throws Exception |
|
499 |
{ |
|
500 |
return login(sourceUrl); |
|
501 |
} |
|
502 | 410 |
|
503 |
|
|
504 | 411 |
/** |
505 | 412 |
* returns a sessionid |
506 | 413 |
* @return |
507 | 414 |
*/ |
508 |
private String login(String sourceUrl)
|
|
415 |
private String login() |
|
509 | 416 |
throws Exception |
510 | 417 |
{ |
511 | 418 |
InputStream is = getResponse(sourceUrl, "/metacat", |
512 | 419 |
"action=login&username=" + username + "&password=" + password + "&qformat=xml", "POST"); |
513 |
String response = streamToString(is);
|
|
420 |
String response = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
|
|
514 | 421 |
//System.out.println("response: " + response); |
515 | 422 |
if(response.indexOf("sessionId") == -1) |
516 | 423 |
{ |
... | ... | |
574 | 481 |
return content; |
575 | 482 |
} |
576 | 483 |
|
577 |
private String streamToString(InputStream is) |
|
578 |
throws Exception |
|
579 |
{ |
|
580 |
byte b[] = new byte[1024]; |
|
581 |
int numread = is.read(b, 0, 1024); |
|
582 |
String response = new String(); |
|
583 |
while(numread != -1) |
|
584 |
{ |
|
585 |
response += new String(b, 0, numread); |
|
586 |
numread = is.read(b, 0, 1024); |
|
587 |
} |
|
588 |
return response; |
|
589 |
} |
|
590 |
|
|
591 |
private InputStream stringToStream(String s) |
|
592 |
throws Exception |
|
593 |
{ |
|
594 |
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes(MetaCatServlet.DEFAULT_ENCODING)); |
|
595 |
return bais; |
|
596 |
} |
|
597 |
|
|
598 | 484 |
private class Document |
599 | 485 |
{ |
600 | 486 |
public String docid; |
Also available in: Unified diff
clean up populator; use IOUtils library to do string<->stream conversions