1 |
5394
|
berkley
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: A Class that implements administrative methods
|
4 |
|
|
* Copyright: 2010 Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* Authors: Michael Daigle
|
7 |
|
|
*
|
8 |
|
|
* '$Author: berkley $'
|
9 |
|
|
* '$Date: 2010-06-08 12:34:30 -0700 (Tue, 08 Jun 2010) $'
|
10 |
|
|
* '$Revision: 5374 $'
|
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 |
|
|
package edu.ucsb.nceas.metacat.util;
|
27 |
|
|
|
28 |
6092
|
jones
|
import java.io.ByteArrayInputStream;
|
29 |
|
|
import java.io.InputStream;
|
30 |
|
|
import java.io.OutputStream;
|
31 |
6386
|
cjones
|
import java.math.BigInteger;
|
32 |
6092
|
jones
|
import java.net.HttpURLConnection;
|
33 |
|
|
import java.net.URL;
|
34 |
5394
|
berkley
|
import java.security.MessageDigest;
|
35 |
6092
|
jones
|
import java.util.Calendar;
|
36 |
|
|
import java.util.Date;
|
37 |
|
|
import java.util.Vector;
|
38 |
5394
|
berkley
|
|
39 |
|
|
import javax.activation.DataHandler;
|
40 |
|
|
import javax.activation.DataSource;
|
41 |
|
|
import javax.mail.internet.MimeBodyPart;
|
42 |
|
|
import javax.mail.internet.MimeMultipart;
|
43 |
|
|
|
44 |
|
|
import org.apache.commons.io.IOUtils;
|
45 |
6092
|
jones
|
import org.dataone.client.D1Client;
|
46 |
|
|
import org.dataone.client.MNode;
|
47 |
6124
|
cjones
|
import org.dataone.client.ObjectFormatCache;
|
48 |
6152
|
leinfelder
|
import org.dataone.client.auth.CertificateManager;
|
49 |
6359
|
leinfelder
|
import org.dataone.service.exceptions.NotFound;
|
50 |
6366
|
leinfelder
|
import org.dataone.service.types.v1.AccessPolicy;
|
51 |
|
|
import org.dataone.service.types.v1.AccessRule;
|
52 |
|
|
import org.dataone.service.types.v1.Checksum;
|
53 |
|
|
import org.dataone.service.types.v1.Identifier;
|
54 |
|
|
import org.dataone.service.types.v1.NodeReference;
|
55 |
|
|
import org.dataone.service.types.v1.ObjectFormat;
|
56 |
|
|
import org.dataone.service.types.v1.Permission;
|
57 |
|
|
import org.dataone.service.types.v1.Session;
|
58 |
|
|
import org.dataone.service.types.v1.Subject;
|
59 |
|
|
import org.dataone.service.types.v1.SystemMetadata;
|
60 |
6134
|
leinfelder
|
import org.ecoinformatics.datamanager.DataManager;
|
61 |
|
|
import org.ecoinformatics.datamanager.database.DatabaseConnectionPoolInterface;
|
62 |
|
|
import org.ecoinformatics.datamanager.parser.DataPackage;
|
63 |
5394
|
berkley
|
|
64 |
6092
|
jones
|
import edu.ucsb.nceas.metacat.MetaCatServlet;
|
65 |
6134
|
leinfelder
|
import edu.ucsb.nceas.metacat.dataquery.MetacatDatabaseConnectionPoolFactory;
|
66 |
6092
|
jones
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
67 |
|
|
import edu.ucsb.nceas.metacat.restservice.InputStreamDataSource;
|
68 |
5394
|
berkley
|
|
69 |
|
|
/**
|
70 |
|
|
* @author berkley
|
71 |
|
|
* A class to populate a metacat instance based on documents returned from a query
|
72 |
|
|
*/
|
73 |
|
|
public class MetacatPopulator
|
74 |
|
|
{
|
75 |
|
|
private String sourceUrl = null;
|
76 |
|
|
private String destinationUrl = null;
|
77 |
|
|
private String query = null;
|
78 |
|
|
private String username = null;
|
79 |
|
|
private String password = null;
|
80 |
6152
|
leinfelder
|
private Session session = null;
|
81 |
5394
|
berkley
|
|
82 |
|
|
/**
|
83 |
|
|
* create a new MetacatPopulator with given source and destination urls.
|
84 |
|
|
* These should be
|
85 |
|
|
* of the form "http://<url>/<metacat_instance>"
|
86 |
|
|
* If username and/or password is null, the query will be run as public
|
87 |
|
|
* @param sourceUrl
|
88 |
|
|
* @param destUrl
|
89 |
|
|
* @param query
|
90 |
|
|
* @param username
|
91 |
|
|
* @param password
|
92 |
|
|
*/
|
93 |
|
|
public MetacatPopulator(String sourceUrl, String destUrl, String query, String username, String password)
|
94 |
|
|
{
|
95 |
|
|
this.sourceUrl = sourceUrl;
|
96 |
|
|
this.query = query;
|
97 |
|
|
this.username = username;
|
98 |
|
|
this.password = password;
|
99 |
|
|
this.destinationUrl = destUrl;
|
100 |
6152
|
leinfelder
|
// TODO: set up certificate for D1 interaction
|
101 |
|
|
CertificateManager.getInstance();
|
102 |
|
|
this.session = new Session();
|
103 |
|
|
Subject subject = new Subject();
|
104 |
|
|
subject.setValue(username);
|
105 |
5394
|
berkley
|
}
|
106 |
|
|
|
107 |
|
|
/**
|
108 |
|
|
* populate from the source
|
109 |
|
|
*/
|
110 |
|
|
public void populate()
|
111 |
|
|
throws Exception
|
112 |
|
|
{
|
113 |
5397
|
berkley
|
printHeader("Source login");
|
114 |
5394
|
berkley
|
String sourceSessionid = loginSource();
|
115 |
5397
|
berkley
|
|
116 |
5394
|
berkley
|
//do a query
|
117 |
5568
|
berkley
|
String params = "returndoctype=eml://ecoinformatics.org/eml-2.1.0&" +
|
118 |
|
|
"returndoctype=eml://ecoinformatics.org/eml-2.0.1&" +
|
119 |
|
|
"returndoctype=eml://ecoinformatics.org/eml-2.0.0&";
|
120 |
5394
|
berkley
|
params += "action=query&";
|
121 |
|
|
params += "qformat=xml&";
|
122 |
|
|
params += "anyfield=" + query;
|
123 |
|
|
|
124 |
5397
|
berkley
|
printHeader("Searching source");
|
125 |
5394
|
berkley
|
System.out.println("searching '" + sourceUrl + "' for '" + query + "' with sessionid '" + sourceSessionid + "'");
|
126 |
|
|
InputStream is = getResponse(sourceUrl, "/metacat",
|
127 |
|
|
params, "POST");
|
128 |
|
|
String response = streamToString(is);
|
129 |
|
|
//System.out.println("response: " + response);
|
130 |
|
|
Vector<Document> docs = parseResponse(response);
|
131 |
5441
|
berkley
|
|
132 |
|
|
|
133 |
5397
|
berkley
|
printHeader("Parsing source results");
|
134 |
5874
|
berkley
|
System.out.println("creating MN with url: " + destinationUrl + "/");
|
135 |
5860
|
jones
|
MNode mn = D1Client.getMN(destinationUrl + "/");
|
136 |
5568
|
berkley
|
|
137 |
5441
|
berkley
|
printHeader("Processing " + docs.size() + " results.");
|
138 |
5397
|
berkley
|
printHeader("logging in to the destination " + destinationUrl);
|
139 |
6152
|
leinfelder
|
|
140 |
6151
|
leinfelder
|
System.out.println("session: " + session.getSubject());
|
141 |
5394
|
berkley
|
for(int i=0; i<docs.size(); i++)
|
142 |
|
|
{
|
143 |
|
|
//for each document in the query
|
144 |
|
|
Document doc = docs.get(i);
|
145 |
|
|
String docid = doc.docid;
|
146 |
|
|
//get the doc from source
|
147 |
5397
|
berkley
|
printHeader("Getting document " + doc.docid + " from source " + sourceUrl);
|
148 |
5394
|
berkley
|
params = "action=read&qformat=xml&docid=" + docid;
|
149 |
|
|
is = getResponse(sourceUrl, "/metacat", params, "POST");
|
150 |
|
|
String doctext = streamToString(is);
|
151 |
5568
|
berkley
|
System.out.println("doctext: " + doctext);
|
152 |
5394
|
berkley
|
is = stringToStream(doctext);
|
153 |
5568
|
berkley
|
//parse the document
|
154 |
6134
|
leinfelder
|
DatabaseConnectionPoolInterface connectionPool = MetacatDatabaseConnectionPoolFactory.getDatabaseConnectionPoolInterface();
|
155 |
|
|
DataManager dataManager = DataManager.getInstance(connectionPool, connectionPool.getDBAdapterName());
|
156 |
|
|
DataPackage dataPackage = dataManager.parseMetadata(is);
|
157 |
|
|
|
158 |
|
|
if(dataPackage == null)
|
159 |
5577
|
berkley
|
{
|
160 |
|
|
continue;
|
161 |
|
|
}
|
162 |
5568
|
berkley
|
//go through the DistributionMetadata and download any described data
|
163 |
|
|
|
164 |
|
|
is = stringToStream(doctext);
|
165 |
5394
|
berkley
|
doc.doctext = doctext;
|
166 |
5397
|
berkley
|
|
167 |
|
|
printHeader("creating document on destination " + destinationUrl);
|
168 |
5394
|
berkley
|
SystemMetadata sysmeta = generateSystemMetadata(doc);
|
169 |
6359
|
leinfelder
|
if (dataPackage.getEntityList() != null) {
|
170 |
|
|
for(int j=0; j < dataPackage.getEntityList().length; j++)
|
171 |
|
|
{
|
172 |
|
|
String dataDocUrl = dataPackage.getEntityList()[j].getURL();
|
173 |
|
|
String dataDocMimeType =
|
174 |
|
|
dataPackage.getEntityList()[j].getDataFormat();
|
175 |
|
|
if (dataDocMimeType == null) {
|
176 |
|
|
dataDocMimeType =
|
177 |
|
|
ObjectFormatCache.getInstance().getFormat("application/octet-stream").getFmtid().getValue();
|
178 |
|
|
}
|
179 |
|
|
String dataDocLocalId = "";
|
180 |
|
|
if(dataDocUrl.trim().startsWith("ecogrid://knb/"))
|
181 |
|
|
{ //we only handle ecogrid urls right now
|
182 |
|
|
dataDocLocalId = dataDocUrl.substring(dataDocUrl.indexOf("ecogrid://knb/") +
|
183 |
|
|
"ecogrid://knb/".length(), dataDocUrl.length());
|
184 |
|
|
//get the file
|
185 |
|
|
params = "action=read&qformat=xml&docid=" + dataDocLocalId;
|
186 |
|
|
InputStream dataDocIs = getResponse(sourceUrl, "/metacat", params, "POST");
|
187 |
|
|
String dataDocText = streamToString(dataDocIs);
|
188 |
|
|
|
189 |
|
|
//set the id
|
190 |
|
|
Identifier did = new Identifier();
|
191 |
|
|
did.setValue(dataDocLocalId);
|
192 |
|
|
|
193 |
|
|
//add the desribeby to the eml's sysmeta
|
194 |
6366
|
leinfelder
|
// TODO Use ORE
|
195 |
|
|
// System.out.println("adding describe for doc " +
|
196 |
|
|
// sysmeta.getIdentifier().getValue() + " :" + did.getValue());
|
197 |
|
|
// sysmeta.addDescribe(did);
|
198 |
6359
|
leinfelder
|
|
199 |
|
|
//create sysmeta for the data doc
|
200 |
|
|
SystemMetadata dataDocSysMeta = generateSystemMetadata(doc);
|
201 |
|
|
//overwrite the bogus values from the last call
|
202 |
|
|
dataDocSysMeta.setIdentifier(did);
|
203 |
|
|
ObjectFormat format = null;
|
204 |
|
|
try {
|
205 |
|
|
format = ObjectFormatCache.getInstance().getFormat(dataDocMimeType);
|
206 |
|
|
} catch (NotFound e) {
|
207 |
|
|
System.out.println(e.getMessage());
|
208 |
|
|
}
|
209 |
6386
|
cjones
|
dataDocSysMeta.setFmtid(format.getFmtid());
|
210 |
6359
|
leinfelder
|
Checksum checksum = new Checksum();
|
211 |
|
|
dataDocIs = stringToStream(dataDocText);
|
212 |
6397
|
leinfelder
|
String ca = "MD5";
|
213 |
6359
|
leinfelder
|
checksum.setAlgorithm(ca);
|
214 |
|
|
checksum.setValue(checksum(dataDocIs));
|
215 |
|
|
dataDocSysMeta.setChecksum(checksum);
|
216 |
6386
|
cjones
|
String sizeStr =
|
217 |
|
|
Long.toString(dataDocText.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
|
218 |
|
|
dataDocSysMeta.setSize(new BigInteger(sizeStr));
|
219 |
6366
|
leinfelder
|
// TODO use ORE map
|
220 |
|
|
//dataDocSysMeta.addDescribedBy(sysmeta.getIdentifier());
|
221 |
6359
|
leinfelder
|
boolean error = false;
|
222 |
|
|
|
223 |
|
|
// create access policy
|
224 |
|
|
//"public", "read", "allow", "allowFirst"
|
225 |
|
|
AccessPolicy accessPolicy = new AccessPolicy();
|
226 |
|
|
AccessRule accessRule = new AccessRule();
|
227 |
|
|
accessRule.addPermission(Permission.READ);
|
228 |
|
|
Subject subject = new Subject();
|
229 |
|
|
subject.setValue("public");
|
230 |
|
|
accessRule.addSubject(subject );
|
231 |
|
|
accessPolicy.addAllow(accessRule );
|
232 |
|
|
//create the data doc on d1
|
233 |
|
|
try
|
234 |
|
|
{
|
235 |
|
|
mn.create(session, dataDocSysMeta.getIdentifier(), IOUtils.toInputStream(dataDocText), dataDocSysMeta);
|
236 |
|
|
mn.setAccessPolicy(session, dataDocSysMeta.getIdentifier(), accessPolicy);
|
237 |
|
|
}
|
238 |
|
|
catch(Exception e)
|
239 |
|
|
{
|
240 |
|
|
error = true;
|
241 |
|
|
System.out.println("ERROR: Could not create data document with id " +
|
242 |
|
|
dataDocSysMeta.getIdentifier().getValue() + " : " + e.getMessage());
|
243 |
|
|
}
|
244 |
|
|
finally
|
245 |
|
|
{
|
246 |
|
|
if(error)
|
247 |
|
|
{
|
248 |
|
|
printHeader("Insertion of document " + dataDocSysMeta.getIdentifier().getValue() +
|
249 |
|
|
"FAILED.");
|
250 |
|
|
}
|
251 |
|
|
else
|
252 |
|
|
{
|
253 |
|
|
printHeader("Done inserting document " + dataDocSysMeta.getIdentifier().getValue() +
|
254 |
|
|
" which is described by " + sysmeta.getIdentifier().getValue());
|
255 |
|
|
}
|
256 |
|
|
}
|
257 |
|
|
}
|
258 |
|
|
else
|
259 |
|
|
{
|
260 |
|
|
System.out.println("WARNING: Could not process describes url " +
|
261 |
|
|
dataDocUrl + " for document " + doc.docid +
|
262 |
|
|
". Only ecogrid://knb/ urls are currently supported.");
|
263 |
|
|
}
|
264 |
|
|
}
|
265 |
5568
|
berkley
|
}
|
266 |
|
|
|
267 |
5397
|
berkley
|
try
|
268 |
|
|
{
|
269 |
6151
|
leinfelder
|
Identifier id = mn.create(session, sysmeta.getIdentifier(),
|
270 |
5397
|
berkley
|
IOUtils.toInputStream(doc.doctext), sysmeta);
|
271 |
|
|
System.out.println("Success inserting document " + id.getValue());
|
272 |
5568
|
berkley
|
|
273 |
5397
|
berkley
|
}
|
274 |
|
|
catch(Exception e)
|
275 |
|
|
{
|
276 |
5568
|
berkley
|
e.printStackTrace();
|
277 |
5397
|
berkley
|
System.out.println("Could not create document with id " +
|
278 |
|
|
sysmeta.getIdentifier().getValue() + " : " + e.getMessage());
|
279 |
5568
|
berkley
|
|
280 |
5397
|
berkley
|
}
|
281 |
|
|
finally
|
282 |
|
|
{
|
283 |
|
|
printHeader("Done inserting document " + sysmeta.getIdentifier().getValue());
|
284 |
|
|
}
|
285 |
5394
|
berkley
|
}
|
286 |
|
|
|
287 |
|
|
logout();
|
288 |
|
|
}
|
289 |
|
|
|
290 |
6134
|
leinfelder
|
|
291 |
5394
|
berkley
|
|
292 |
|
|
/**
|
293 |
|
|
* @param doc
|
294 |
|
|
* @return
|
295 |
|
|
*/
|
296 |
|
|
private SystemMetadata generateSystemMetadata(Document doc)
|
297 |
|
|
throws Exception
|
298 |
|
|
{
|
299 |
|
|
SystemMetadata sm = new SystemMetadata();
|
300 |
|
|
//set the id
|
301 |
|
|
Identifier id = new Identifier();
|
302 |
5452
|
berkley
|
id.setValue(doc.docid.trim());
|
303 |
5394
|
berkley
|
sm.setIdentifier(id);
|
304 |
|
|
|
305 |
|
|
//set the object format
|
306 |
6144
|
cjones
|
ObjectFormat format = ObjectFormatCache.getInstance().getFormat(doc.doctype);
|
307 |
5394
|
berkley
|
if(format == null)
|
308 |
|
|
{
|
309 |
|
|
if(doc.doctype.trim().equals("BIN"))
|
310 |
|
|
{
|
311 |
6144
|
cjones
|
format = ObjectFormatCache.getInstance().getFormat("application/octet-stream");
|
312 |
5394
|
berkley
|
}
|
313 |
|
|
else
|
314 |
|
|
{
|
315 |
6144
|
cjones
|
format = ObjectFormatCache.getInstance().getFormat("text/plain");
|
316 |
5394
|
berkley
|
}
|
317 |
|
|
}
|
318 |
6386
|
cjones
|
sm.setFmtid(format.getFmtid());
|
319 |
5394
|
berkley
|
|
320 |
|
|
//create the checksum
|
321 |
5760
|
leinfelder
|
ByteArrayInputStream bais = new ByteArrayInputStream(doc.doctext.getBytes(MetaCatServlet.DEFAULT_ENCODING));
|
322 |
5394
|
berkley
|
String checksumS = checksum(bais);
|
323 |
6397
|
leinfelder
|
String ca = "MD5";
|
324 |
5394
|
berkley
|
Checksum checksum = new Checksum();
|
325 |
|
|
checksum.setValue(checksumS);
|
326 |
|
|
checksum.setAlgorithm(ca);
|
327 |
|
|
sm.setChecksum(checksum);
|
328 |
|
|
|
329 |
|
|
//set the size
|
330 |
6386
|
cjones
|
String sizeStr = Long.toString(doc.doctext.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
|
331 |
|
|
sm.setSize(new BigInteger(sizeStr));
|
332 |
5394
|
berkley
|
|
333 |
|
|
//submitter
|
334 |
6092
|
jones
|
Subject p = new Subject();
|
335 |
5441
|
berkley
|
p.setValue("unknown");
|
336 |
5394
|
berkley
|
sm.setSubmitter(p);
|
337 |
|
|
sm.setRightsHolder(p);
|
338 |
|
|
try
|
339 |
|
|
{
|
340 |
|
|
Date dateCreated = parseMetacatDate(doc.createDate);
|
341 |
|
|
sm.setDateUploaded(dateCreated);
|
342 |
|
|
Date dateUpdated = parseMetacatDate(doc.updateDate);
|
343 |
|
|
sm.setDateSysMetadataModified(dateUpdated);
|
344 |
|
|
}
|
345 |
|
|
catch(Exception e)
|
346 |
|
|
{
|
347 |
|
|
System.out.println("couldn't parse a date: " + e.getMessage());
|
348 |
|
|
Date dateCreated = new Date();
|
349 |
|
|
sm.setDateUploaded(dateCreated);
|
350 |
|
|
Date dateUpdated = new Date();
|
351 |
|
|
sm.setDateSysMetadataModified(dateUpdated);
|
352 |
|
|
}
|
353 |
|
|
NodeReference nr = new NodeReference();
|
354 |
5916
|
jones
|
nr.setValue(PropertyService.getProperty("dataone.memberNodeId"));
|
355 |
5394
|
berkley
|
sm.setOriginMemberNode(nr);
|
356 |
|
|
sm.setAuthoritativeMemberNode(nr);
|
357 |
5568
|
berkley
|
|
358 |
5394
|
berkley
|
return sm;
|
359 |
|
|
}
|
360 |
|
|
|
361 |
5568
|
berkley
|
private void printHeader(String s)
|
362 |
|
|
{
|
363 |
|
|
System.out.println("****** " + s + " *******");
|
364 |
|
|
}
|
365 |
|
|
|
366 |
5394
|
berkley
|
/**
|
367 |
5568
|
berkley
|
* produce an md5 checksum for item
|
368 |
|
|
*/
|
369 |
|
|
private String checksum(InputStream is)
|
370 |
|
|
throws Exception
|
371 |
|
|
{
|
372 |
|
|
byte[] buffer = new byte[1024];
|
373 |
|
|
MessageDigest complete = MessageDigest.getInstance("MD5");
|
374 |
|
|
int numRead;
|
375 |
|
|
|
376 |
|
|
do
|
377 |
|
|
{
|
378 |
|
|
numRead = is.read(buffer);
|
379 |
|
|
if (numRead > 0)
|
380 |
|
|
{
|
381 |
|
|
complete.update(buffer, 0, numRead);
|
382 |
|
|
}
|
383 |
|
|
} while (numRead != -1);
|
384 |
|
|
|
385 |
|
|
|
386 |
|
|
return getHex(complete.digest());
|
387 |
|
|
}
|
388 |
|
|
|
389 |
|
|
/**
|
390 |
|
|
* convert a byte array to a hex string
|
391 |
|
|
*/
|
392 |
|
|
private static String getHex( byte [] raw )
|
393 |
|
|
{
|
394 |
|
|
final String HEXES = "0123456789ABCDEF";
|
395 |
|
|
if ( raw == null ) {
|
396 |
|
|
return null;
|
397 |
|
|
}
|
398 |
|
|
final StringBuilder hex = new StringBuilder( 2 * raw.length );
|
399 |
|
|
for ( final byte b : raw ) {
|
400 |
|
|
hex.append(HEXES.charAt((b & 0xF0) >> 4))
|
401 |
|
|
.append(HEXES.charAt((b & 0x0F)));
|
402 |
|
|
}
|
403 |
|
|
return hex.toString();
|
404 |
|
|
}
|
405 |
|
|
|
406 |
|
|
/**
|
407 |
5394
|
berkley
|
* parse the metacat date which looks like 2010-06-08 (YYYY-MM-DD) into
|
408 |
|
|
* a proper date object
|
409 |
|
|
* @param date
|
410 |
|
|
* @return
|
411 |
|
|
*/
|
412 |
|
|
private Date parseMetacatDate(String date)
|
413 |
|
|
{
|
414 |
|
|
String year = date.substring(0, 4);
|
415 |
|
|
String month = date.substring(5, 7);
|
416 |
|
|
String day = date.substring(8, 10);
|
417 |
|
|
Calendar c = Calendar.getInstance();
|
418 |
|
|
c.set(new Integer(year).intValue(),
|
419 |
|
|
new Integer(month).intValue(),
|
420 |
|
|
new Integer(day).intValue());
|
421 |
|
|
return c.getTime();
|
422 |
|
|
}
|
423 |
|
|
|
424 |
|
|
/**
|
425 |
|
|
* send a request to the resource
|
426 |
|
|
*/
|
427 |
|
|
private InputStream sendRequest(String contextRootUrl, String resource,
|
428 |
|
|
String sessionid, String method, String urlParamaters,
|
429 |
|
|
String contentType, InputStream dataStream)
|
430 |
|
|
throws Exception
|
431 |
|
|
{
|
432 |
|
|
|
433 |
|
|
HttpURLConnection connection = null ;
|
434 |
|
|
String restURL = contextRootUrl + resource;
|
435 |
|
|
|
436 |
|
|
if (urlParamaters != null) {
|
437 |
|
|
if (restURL.indexOf("?") == -1)
|
438 |
|
|
restURL += "?";
|
439 |
|
|
restURL += urlParamaters;
|
440 |
|
|
if(restURL.indexOf(" ") != -1)
|
441 |
|
|
{
|
442 |
|
|
restURL = restURL.replaceAll("\\s", "%20");
|
443 |
|
|
}
|
444 |
|
|
}
|
445 |
|
|
|
446 |
|
|
if(sessionid != null)
|
447 |
|
|
{
|
448 |
|
|
if(restURL.indexOf("?") == -1)
|
449 |
|
|
{
|
450 |
|
|
restURL += "?sessionid=" + sessionid;
|
451 |
|
|
}
|
452 |
|
|
else
|
453 |
|
|
{
|
454 |
|
|
restURL += "&sessionid=" + sessionid;
|
455 |
|
|
}
|
456 |
|
|
}
|
457 |
|
|
|
458 |
|
|
URL u = null;
|
459 |
|
|
InputStream content = null;
|
460 |
|
|
System.out.println("url: " + restURL);
|
461 |
|
|
System.out.println("method: " + method);
|
462 |
|
|
u = new URL(restURL);
|
463 |
|
|
connection = (HttpURLConnection) u.openConnection();
|
464 |
|
|
if (contentType!=null) {
|
465 |
|
|
connection.setRequestProperty("Content-Type",contentType);
|
466 |
|
|
}
|
467 |
|
|
|
468 |
|
|
connection.setDoOutput(true);
|
469 |
|
|
connection.setDoInput(true);
|
470 |
|
|
connection.setRequestMethod(method);
|
471 |
|
|
|
472 |
|
|
if (!method.equals("GET")) {
|
473 |
|
|
if (dataStream != null) {
|
474 |
|
|
OutputStream out = connection.getOutputStream();
|
475 |
|
|
IOUtils.copy(dataStream, out);
|
476 |
|
|
}
|
477 |
|
|
}
|
478 |
|
|
|
479 |
|
|
return connection.getInputStream();
|
480 |
|
|
}
|
481 |
|
|
|
482 |
|
|
/**
|
483 |
|
|
* create a mime multipart message from object and sysmeta
|
484 |
|
|
*/
|
485 |
|
|
private MimeMultipart createMimeMultipart(InputStream object)
|
486 |
|
|
throws Exception
|
487 |
|
|
{
|
488 |
|
|
final MimeMultipart mmp = new MimeMultipart();
|
489 |
|
|
MimeBodyPart objectPart = new MimeBodyPart();
|
490 |
|
|
objectPart.addHeaderLine("Content-Transfer-Encoding: base64");
|
491 |
|
|
objectPart.setFileName("doctext");
|
492 |
|
|
DataSource ds = new InputStreamDataSource("doctext", object);
|
493 |
|
|
DataHandler dh = new DataHandler(ds);
|
494 |
|
|
objectPart.setDataHandler(dh);
|
495 |
|
|
mmp.addBodyPart(objectPart);
|
496 |
|
|
return mmp;
|
497 |
|
|
}
|
498 |
|
|
|
499 |
|
|
/**
|
500 |
|
|
* parse a metacat query response and return a vector of docids
|
501 |
|
|
* @param response
|
502 |
|
|
* @return
|
503 |
|
|
*/
|
504 |
|
|
private Vector<Document> parseResponse(String response)
|
505 |
|
|
{
|
506 |
|
|
Vector<Document> v = new Vector<Document>();
|
507 |
|
|
int dstart = response.indexOf("<document>");
|
508 |
|
|
int dend = response.indexOf("</document>", dstart);
|
509 |
|
|
while(dstart != -1)
|
510 |
|
|
{
|
511 |
|
|
String doc = response.substring(dstart + "<document>".length(), dend);
|
512 |
|
|
//System.out.println("adding " + docid);
|
513 |
|
|
Document d = new Document(getFieldFromDoc(doc, "docid"),
|
514 |
|
|
getFieldFromDoc(doc, "doctype"),
|
515 |
|
|
getFieldFromDoc(doc, "createdate"),
|
516 |
|
|
getFieldFromDoc(doc, "updatedate"));
|
517 |
|
|
v.add(d);
|
518 |
|
|
dstart = response.indexOf("<document>", dend);
|
519 |
|
|
dend = response.indexOf("</document>", dstart);
|
520 |
|
|
}
|
521 |
|
|
|
522 |
|
|
return v;
|
523 |
|
|
}
|
524 |
|
|
|
525 |
|
|
private String getFieldFromDoc(String doc, String fieldname)
|
526 |
|
|
{
|
527 |
|
|
String field = "<" + fieldname + ">";
|
528 |
|
|
String fieldend = "</" + fieldname + ">";
|
529 |
|
|
int start = doc.indexOf(field);
|
530 |
|
|
int end = doc.indexOf(fieldend);
|
531 |
|
|
String s = doc.substring(start + field.length(), end);
|
532 |
5441
|
berkley
|
//System.out.println("field: " + fieldname + " : " + s);
|
533 |
5394
|
berkley
|
return s;
|
534 |
|
|
}
|
535 |
|
|
|
536 |
|
|
/**
|
537 |
|
|
* login the source
|
538 |
|
|
* @return
|
539 |
|
|
* @throws Exception
|
540 |
|
|
*/
|
541 |
|
|
private String loginSource()
|
542 |
|
|
throws Exception
|
543 |
|
|
{
|
544 |
|
|
return login(sourceUrl);
|
545 |
|
|
}
|
546 |
|
|
|
547 |
|
|
|
548 |
|
|
/**
|
549 |
|
|
* returns a sessionid
|
550 |
|
|
* @return
|
551 |
|
|
*/
|
552 |
|
|
private String login(String sourceUrl)
|
553 |
|
|
throws Exception
|
554 |
|
|
{
|
555 |
|
|
InputStream is = getResponse(sourceUrl, "/metacat",
|
556 |
6152
|
leinfelder
|
"action=login&username=" + username + "&password=" + password + "&qformat=xml", "POST");
|
557 |
5394
|
berkley
|
String response = streamToString(is);
|
558 |
|
|
//System.out.println("response: " + response);
|
559 |
5397
|
berkley
|
if(response.indexOf("sessionId") == -1)
|
560 |
|
|
{
|
561 |
|
|
throw new Exception("Error logging into " + sourceUrl);
|
562 |
|
|
}
|
563 |
|
|
|
564 |
5394
|
berkley
|
String sessionid = response.substring(
|
565 |
|
|
response.indexOf("<sessionId>") + "<sessionId>".length(),
|
566 |
|
|
response.indexOf("</sessionId>"));
|
567 |
|
|
System.out.println("sessionid: " + sessionid);
|
568 |
|
|
return sessionid;
|
569 |
|
|
}
|
570 |
|
|
|
571 |
|
|
/**
|
572 |
|
|
* logout both the source and destination
|
573 |
|
|
* @throws Exception
|
574 |
|
|
*/
|
575 |
|
|
private void logout()
|
576 |
|
|
throws Exception
|
577 |
|
|
{
|
578 |
|
|
getResponse(sourceUrl, "/metacat", "action=logout&username=" + username, "POST");
|
579 |
|
|
getResponse(destinationUrl, "/metacat", "action=logout&username=" + username, "POST");
|
580 |
|
|
}
|
581 |
|
|
|
582 |
|
|
/**
|
583 |
|
|
* get an http response
|
584 |
|
|
* @param contextRootUrl
|
585 |
|
|
* @param resource
|
586 |
|
|
* @param urlParameters
|
587 |
|
|
* @param method
|
588 |
|
|
* @return
|
589 |
|
|
* @throws Exception
|
590 |
|
|
*/
|
591 |
|
|
private InputStream getResponse(String contextRootUrl, String resource,
|
592 |
|
|
String urlParameters, String method)
|
593 |
|
|
throws Exception
|
594 |
|
|
{
|
595 |
|
|
HttpURLConnection connection = null ;
|
596 |
|
|
|
597 |
|
|
String restURL = contextRootUrl+resource;
|
598 |
|
|
|
599 |
|
|
if (urlParameters != null) {
|
600 |
|
|
if (restURL.indexOf("?") == -1)
|
601 |
|
|
restURL += "?";
|
602 |
|
|
restURL += urlParameters;
|
603 |
|
|
if(restURL.indexOf(" ") != -1)
|
604 |
|
|
{
|
605 |
|
|
restURL = restURL.replaceAll("\\s", "%20");
|
606 |
|
|
}
|
607 |
|
|
}
|
608 |
|
|
|
609 |
|
|
URL u = null;
|
610 |
|
|
InputStream content = null;
|
611 |
|
|
System.out.println("url: " + restURL);
|
612 |
|
|
System.out.println("method: " + method);
|
613 |
|
|
u = new URL(restURL);
|
614 |
|
|
connection = (HttpURLConnection) u.openConnection();
|
615 |
|
|
connection.setDoOutput(true);
|
616 |
|
|
connection.setDoInput(true);
|
617 |
|
|
connection.setRequestMethod(method);
|
618 |
|
|
content = connection.getInputStream();
|
619 |
|
|
return content;
|
620 |
|
|
}
|
621 |
|
|
|
622 |
|
|
private String streamToString(InputStream is)
|
623 |
|
|
throws Exception
|
624 |
|
|
{
|
625 |
|
|
byte b[] = new byte[1024];
|
626 |
|
|
int numread = is.read(b, 0, 1024);
|
627 |
|
|
String response = new String();
|
628 |
|
|
while(numread != -1)
|
629 |
|
|
{
|
630 |
|
|
response += new String(b, 0, numread);
|
631 |
|
|
numread = is.read(b, 0, 1024);
|
632 |
|
|
}
|
633 |
|
|
return response;
|
634 |
|
|
}
|
635 |
|
|
|
636 |
|
|
private InputStream stringToStream(String s)
|
637 |
|
|
throws Exception
|
638 |
|
|
{
|
639 |
5760
|
leinfelder
|
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes(MetaCatServlet.DEFAULT_ENCODING));
|
640 |
5394
|
berkley
|
return bais;
|
641 |
|
|
}
|
642 |
|
|
|
643 |
|
|
private class Document
|
644 |
|
|
{
|
645 |
|
|
public String docid;
|
646 |
|
|
public String doctype;
|
647 |
|
|
public String createDate;
|
648 |
|
|
public String updateDate;
|
649 |
|
|
public String doctext;
|
650 |
|
|
|
651 |
|
|
public Document(String docid, String doctype, String createDate, String updateDate)
|
652 |
|
|
{
|
653 |
5452
|
berkley
|
this.docid = docid.trim();
|
654 |
|
|
this.doctype = doctype.trim();
|
655 |
|
|
this.createDate = createDate.trim();
|
656 |
|
|
this.updateDate = updateDate.trim();
|
657 |
5394
|
berkley
|
}
|
658 |
|
|
}
|
659 |
|
|
}
|