0
|
1 package de.mpiwg.itgroup.eSciDoc.Tools;
|
|
2 import java.io.BufferedReader;
|
|
3 import java.io.ByteArrayInputStream;
|
|
4 import java.io.File;
|
|
5 import java.io.FileWriter;
|
|
6 import java.io.IOException;
|
|
7 import java.io.InputStream;
|
|
8 import java.io.InputStreamReader;
|
|
9 import java.io.UnsupportedEncodingException;
|
|
10 import java.net.URI;
|
|
11 import java.net.URL;
|
|
12 import java.net.URLEncoder;
|
|
13 import java.util.ArrayList;
|
3
|
14 import java.util.HashMap;
|
0
|
15 import java.util.List;
|
3
|
16 import java.util.Map;
|
0
|
17 import java.util.StringTokenizer;
|
|
18 import java.util.regex.Matcher;
|
|
19 import java.util.regex.Pattern;
|
|
20
|
|
21 import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
|
|
22
|
|
23
|
3
|
24 import org.apache.commons.codec.EncoderException;
|
15
|
25 import org.apache.http.Header;
|
0
|
26 import org.apache.http.HttpEntity;
|
15
|
27 import org.apache.http.HttpMessage;
|
0
|
28 import org.apache.http.HttpResponse;
|
15
|
29 import org.apache.http.NameValuePair;
|
8
|
30 import org.apache.http.StatusLine;
|
0
|
31 import org.apache.http.client.ClientProtocolException;
|
15
|
32 import org.apache.http.client.CookieStore;
|
0
|
33 import org.apache.http.client.HttpClient;
|
15
|
34 import org.apache.http.client.entity.UrlEncodedFormEntity;
|
0
|
35 import org.apache.http.client.methods.HttpDelete;
|
|
36 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
|
37 import org.apache.http.client.methods.HttpGet;
|
|
38 import org.apache.http.client.methods.HttpPost;
|
|
39 import org.apache.http.client.methods.HttpPut;
|
|
40 import org.apache.http.client.methods.HttpRequestBase;
|
|
41 import org.apache.http.client.params.ClientPNames;
|
|
42 import org.apache.http.client.params.CookiePolicy;
|
15
|
43 import org.apache.http.client.protocol.ClientContext;
|
|
44 import org.apache.http.cookie.Cookie;
|
0
|
45 import org.apache.http.entity.InputStreamEntity;
|
15
|
46 import org.apache.http.entity.StringEntity;
|
|
47 import org.apache.http.impl.client.BasicCookieStore;
|
0
|
48 import org.apache.http.impl.client.DefaultHttpClient;
|
|
49 import org.apache.http.impl.conn.SingleClientConnManager;
|
15
|
50 import org.apache.http.impl.cookie.BasicClientCookie;
|
|
51 import org.apache.http.message.BasicNameValuePair;
|
0
|
52 import org.apache.http.protocol.BasicHttpContext;
|
|
53 import org.apache.http.protocol.HttpContext;
|
|
54 import org.apache.log4j.Logger;
|
|
55 import org.jdom.Attribute;
|
|
56 import org.jdom.Document;
|
|
57 import org.jdom.Element;
|
|
58 import org.jdom.JDOMException;
|
|
59 import org.jdom.Text;
|
|
60 import org.jdom.input.SAXBuilder;
|
|
61 import org.jdom.xpath.XPath;
|
3
|
62 import org.w3c.dom.Node;
|
0
|
63
|
3
|
64 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHOObject;
|
|
65 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHORessource;
|
|
66 import de.mpiwg.itgroup.eSciDoc.exceptions.ConnectorException;
|
|
67 import de.mpiwg.itgroup.eSciDoc.exceptions.ESciDocXmlObjectException;
|
|
68 import de.mpiwg.itgroup.eSciDoc.exceptions.ObjectNotUniqueError;
|
0
|
69 import de.mpiwg.itgroup.eSciDoc.utils.eSciDocXmlObject;
|
|
70
|
|
71
|
|
72
|
15
|
73
|
0
|
74 /**
|
|
75 * Handler to create a connection with an eScidoc server
|
|
76 * @author dwinter
|
|
77 *
|
|
78 */
|
|
79 public class EScidocBasicHandler {
|
|
80
|
|
81 private Logger logger;
|
|
82 private String eScidocServer;
|
|
83 private int eScidocPort;
|
|
84 private String user;
|
|
85 private String password;
|
|
86 public String eScidocUrl;
|
|
87 private HttpClient httpclient=null;
|
|
88
|
|
89 /**
|
|
90 * @param eScidocServer
|
|
91 * @param eScidocPort
|
|
92 * @param user
|
|
93 * @param password
|
|
94 */
|
|
95 public EScidocBasicHandler(String eScidocServer, int eScidocPort,String user, String password){
|
|
96 this.eScidocServer=eScidocServer;
|
|
97 this.eScidocPort=eScidocPort;
|
|
98 this.user=user;
|
|
99 this.password=password;
|
|
100 this.eScidocUrl="http://"+eScidocServer+":"+String.valueOf(eScidocPort);
|
|
101
|
|
102 logger = Logger.getRootLogger();
|
|
103
|
|
104 }
|
|
105
|
15
|
106 private void addHandleToMethod(final HttpMessage method,
|
|
107 final String eSciDocUserHandle) {
|
|
108 method.setHeader("Cookie", "escidocCookie=" + eSciDocUserHandle);
|
|
109 }
|
0
|
110
|
|
111 /**
|
|
112 * Logs you into escidoc and sets the httpclient field to the current client for this session
|
|
113 * @return gives you the httpclient for further usage.
|
|
114 * @throws HttpException
|
|
115 * @throws ClientProtocolException
|
|
116 * @throws IOException
|
|
117 */
|
15
|
118
|
|
119
|
0
|
120 public HttpClient login() throws IOException {
|
|
121 httpclient = new DefaultHttpClient();
|
|
122
|
15
|
123
|
|
124 //httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
|
|
125 httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
|
|
126 httpclient.getParams().setParameter("http.protocol.single-cookie-header", true);
|
|
127
|
0
|
128 HttpPost httppost = new HttpPost(eScidocUrl+"/aa/login?target=/");
|
|
129
|
|
130 logger.debug("executing request");
|
|
131
|
|
132 HttpResponse response = httpclient.execute(httppost);
|
|
133 //HttpEntity entity = httppost.getRes
|
|
134
|
|
135 System.out.println("----------------------------------------");
|
|
136 System.out.println(response.getStatusLine());
|
|
137
|
|
138 HttpEntity entity = response.getEntity();
|
|
139 if (entity != null) { entity.consumeContent();
|
|
140 }
|
|
141
|
15
|
142
|
|
143
|
|
144 String params="j_username="+user+"&j_password="+password;
|
|
145
|
|
146
|
|
147 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
|
|
148 nameValuePairs.add(new BasicNameValuePair("j_username",
|
|
149 user));
|
|
150 nameValuePairs.add(new BasicNameValuePair("j_password",
|
|
151 password));
|
|
152 HttpPost httpget = new HttpPost(eScidocUrl+"/aa/j_spring_security_check");
|
|
153
|
|
154 httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
|
155 //httpget.setEntity(new StringEntity(params));
|
|
156 //
|
|
157 //
|
|
158 response = httpclient.execute(httpget);
|
|
159 // //entity = response.getEntity();
|
|
160 //
|
|
161 System.out.println("----------------------------------------");
|
|
162 System.out.println(response.getStatusLine());
|
|
163
|
|
164 entity = response.getEntity();
|
|
165 InputStream ct = entity.getContent();
|
|
166
|
|
167 String ret = EScidocBasicHandler.convertStreamToString(ct);
|
|
168 // if (entity != null) { entity.consumeContent();
|
|
169 // }
|
|
170 //
|
|
171 System.out.println(ret);
|
|
172
|
|
173
|
|
174 httppost = new HttpPost(eScidocUrl+"/aa/login?target=/");
|
|
175
|
|
176 logger.debug("executing request");
|
|
177
|
|
178 response = httpclient.execute(httppost);
|
|
179 //HttpEntity entity = httppost.getRes
|
|
180
|
0
|
181 System.out.println("----------------------------------------");
|
|
182 System.out.println(response.getStatusLine());
|
|
183
|
|
184 entity = response.getEntity();
|
|
185 if (entity != null) { entity.consumeContent();
|
|
186 }
|
15
|
187
|
0
|
188
|
|
189 return httpclient;
|
|
190 }
|
|
191
|
|
192 /**
|
|
193 * Sends a PUT request to the escidoc client. performs a login if not done before.
|
|
194 * @param command
|
|
195 * @param body
|
|
196 * @return
|
|
197 * @throws ClientProtocolException
|
|
198 * @throws IOException
|
|
199 */
|
|
200 public HttpResponse eScidocPut(String command, InputStream body) throws IOException {
|
|
201 HttpPut httpput = new HttpPut(eScidocUrl+command);
|
|
202 return eScidocRequestBase(httpput,command,body);
|
|
203
|
|
204 }
|
|
205
|
|
206 /**
|
|
207 * Sends a PUT request to the escidoc client. performs a login if not done before.
|
|
208 * @param command
|
|
209 * @param url
|
|
210 * @return
|
|
211 * @throws ClientProtocolException
|
|
212 * @throws IOException
|
|
213 */
|
|
214 public HttpResponse eScidocPut(String command, URL url) throws IOException{
|
|
215
|
|
216 HttpPut httpput = new HttpPut(eScidocUrl+command);
|
|
217 return eScidocRequestBase(httpput,command,url.openStream());
|
|
218 }
|
|
219 /**
|
|
220 * Sends a POST request to the escidoc client. performs a login if not done before.
|
|
221 * @param command
|
|
222 * @param body
|
|
223 * @return
|
|
224 * @throws ClientProtocolException
|
|
225 * @throws IOException
|
|
226 */
|
|
227 public HttpResponse eScidocPost(String command, InputStream body) throws IOException{
|
|
228 HttpPost httppost = new HttpPost(eScidocUrl+command);
|
|
229 return eScidocRequestBase(httppost,command,body);
|
|
230 }
|
|
231
|
|
232 /** Sends a POST request to the escidoc client. performs a login if not done before.
|
|
233 * @param command
|
|
234 * @param url
|
|
235 * @return
|
|
236 * @throws ClientProtocolException
|
|
237 * @throws IOException
|
|
238 */
|
|
239 public HttpResponse eScidocPost(String command, URL url) throws IOException{
|
|
240 HttpPost httppost = new HttpPost(eScidocUrl+command);
|
|
241
|
|
242
|
|
243 return eScidocRequestBase(httppost,command,url.openStream());
|
|
244 }
|
|
245 /**
|
|
246 * Sends a GET request to the escidoc client. performs a login if not done before.
|
|
247 * @param command
|
|
248 * @return
|
|
249 * @throws ClientProtocolException
|
|
250 * @throws IOException
|
|
251 */
|
|
252 public HttpResponse eScidocGet(String command) throws IOException{
|
|
253 HttpGet httpget = new HttpGet(eScidocUrl+command);
|
|
254 return eScidocRequestBase(httpget,command,null);
|
|
255
|
|
256 }
|
|
257
|
8
|
258
|
|
259
|
0
|
260 /** Send a delete command to the escidoc client. performs a login if necessara
|
|
261 * @param command
|
|
262 * @return
|
|
263 * @throws ClientProtocolException
|
|
264 * @throws IOException
|
|
265 */
|
|
266 public HttpResponse eScidocDelete(String command) throws IOException{
|
|
267 HttpDelete httpdelete = new HttpDelete(eScidocUrl+command);
|
|
268 return eScidocRequestBase(httpdelete,command,null);
|
|
269
|
|
270 }
|
|
271 /**
|
|
272 * Generates and sends a general request to escidoc, used by escidocPUT, POST and GET.
|
|
273 * @param httpBase
|
|
274 * @param command
|
|
275 * @param body
|
|
276 * @return
|
|
277 * @throws ClientProtocolException
|
|
278 * @throws IOException
|
|
279 */
|
|
280 private HttpResponse eScidocRequestBase(HttpRequestBase httpBase, String command, InputStream body) throws IOException {
|
|
281
|
|
282
|
|
283
|
|
284 if (httpclient==null)
|
|
285 login();
|
|
286
|
|
287
|
|
288
|
|
289 if (HttpEntityEnclosingRequestBase.class.isInstance(httpBase)){
|
|
290
|
|
291
|
|
292 if (body!=null){
|
|
293
|
|
294
|
|
295
|
|
296 HttpEntity entity = new InputStreamEntity(body, -1);
|
|
297 ((HttpEntityEnclosingRequestBase)httpBase).setEntity(entity);
|
|
298 }
|
|
299 }
|
|
300
|
2
|
301 //logger.debug("executing request:"+httpBase.getRequestLine());
|
0
|
302
|
|
303
|
|
304 HttpResponse status = httpclient.execute(httpBase);
|
|
305 //HttpEntity responseEntity = response.getEntity();
|
|
306
|
|
307 logger.debug("----------------------------------------");
|
|
308 logger.debug(status);
|
|
309
|
|
310
|
|
311 return status;
|
|
312 }
|
|
313
|
|
314 /**
|
|
315 *
|
|
316 * To convert the InputStream to String we use the BufferedReader.readLine()
|
|
317 * method. We iterate until the BufferedReader return null which means
|
|
318 * there's no more data to read. Each line will appended to a StringBuilder
|
|
319 * and returned as String.
|
|
320
|
|
321 * @param is
|
|
322 * @return
|
|
323 */
|
|
324 public static String convertStreamToString(InputStream is) {
|
|
325
|
|
326 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
|
327 StringBuilder sb = new StringBuilder();
|
|
328
|
|
329 String line = null;
|
|
330 try {
|
|
331 while ((line = reader.readLine()) != null) {
|
|
332 sb.append(line + "\n");
|
|
333 }
|
|
334 } catch (IOException e) {
|
|
335 e.printStackTrace();
|
|
336 } finally {
|
|
337 try {
|
|
338 is.close();
|
|
339 } catch (IOException e) {
|
|
340 e.printStackTrace();
|
|
341 }
|
|
342 }
|
|
343
|
|
344 return sb.toString();
|
|
345 }
|
|
346
|
|
347 /** converts a stream to a string
|
|
348 * @param string
|
|
349 * @return
|
|
350 * @throws UnsupportedEncodingException
|
|
351 */
|
|
352 public static InputStream convertStringToStream(String string) throws UnsupportedEncodingException{
|
|
353 return new ByteArrayInputStream(string.getBytes("utf-8"));
|
|
354 }
|
|
355
|
|
356 /**
|
|
357 * Find the last-modification-date from an escidoc xml-file (item/container/context)
|
|
358 * @param ret
|
|
359 * @return
|
|
360 */
|
|
361 public static String getDateStamp(String ret) {
|
|
362 Pattern p = Pattern.compile("last-modification-date=\"([^\"]*)\"");
|
|
363
|
|
364 Matcher m = p.matcher(ret);
|
|
365
|
|
366 m.find();
|
|
367
|
|
368 String txt;
|
|
369 try {
|
|
370 txt = m.group(1);
|
|
371 } catch (IllegalStateException e) {
|
|
372
|
|
373 e.printStackTrace();
|
|
374 System.out.println(ret);
|
|
375 throw new IllegalStateException();
|
|
376 }
|
|
377 return txt;
|
|
378 }
|
|
379
|
|
380 /**
|
8
|
381 * Find the content-model (item)
|
0
|
382 * @param ret
|
|
383 * @return
|
|
384 * @throws IOException
|
|
385 * @throws JDOMException
|
|
386 * @throws UnsupportedEncodingException
|
|
387 */
|
|
388 public static String getContentModel(InputStream escidocstream) throws UnsupportedEncodingException, JDOMException, IOException {
|
|
389 SAXBuilder builder = new SAXBuilder();
|
|
390 Document doc = builder.build(escidocstream);
|
|
391
|
|
392 Element root= doc.getRootElement();
|
|
393 XPath xpath = EScidocTools.getESciDocXpath("escidocItem:properties/srel:content-model/@xlink:href");
|
|
394
|
|
395 Attribute node = (Attribute) xpath.selectSingleNode(root);
|
|
396
|
|
397 return node.getValue();
|
|
398
|
|
399 }
|
|
400
|
|
401
|
|
402 /**
|
|
403 * Find the escidoc:id from an escidoc xml (item/container/context)
|
|
404 * @param ret
|
|
405 * @return
|
|
406 */
|
|
407 public static String getId(String ret) {
|
|
408 Pattern p = Pattern.compile("xlink:href=\"([^\"]*)\"");
|
|
409
|
|
410 Matcher m = p.matcher(ret);
|
|
411
|
|
412 m.find();
|
|
413 String txt = m.group(1);
|
|
414 String[] splitted = txt.split("/");
|
|
415
|
|
416 String id = splitted[splitted.length-1];
|
|
417 return id;
|
|
418 }
|
|
419
|
|
420
|
|
421 /**
|
|
422 * Get the pid from an escidoc xml-file (item/container/context)
|
|
423 * @param retTxt
|
|
424 * @return
|
|
425 * @throws JDOMException
|
|
426 * @throws IOException
|
|
427 */
|
|
428 public static String getPID(String retTxt) throws JDOMException, IOException {
|
|
429 SAXBuilder builder = new SAXBuilder();
|
|
430
|
|
431
|
|
432 Document doc = builder.build(EScidocBasicHandler.convertStringToStream(retTxt));
|
|
433
|
|
434 XPath xpath = XPath.newInstance("//prop:pid");
|
|
435 xpath.addNamespace("prop", EScidocTools.prop);
|
|
436
|
|
437 Element node = (Element) xpath.selectSingleNode(doc);
|
|
438
|
|
439 return node.getTextTrim();
|
|
440
|
|
441 }
|
|
442
|
|
443
|
|
444 /**
|
|
445 * get the number of the last version from an escidoc xml-file (item/container/context)
|
|
446 * @param retTxt
|
|
447 * @return
|
|
448 * @throws JDOMException
|
|
449 * @throws IOException
|
|
450 */
|
|
451 public static String getLastVersionNumber(String retTxt) throws JDOMException, IOException {
|
|
452 SAXBuilder builder = new SAXBuilder();
|
|
453
|
|
454 Document doc = builder.build(EScidocBasicHandler.convertStringToStream(retTxt));
|
|
455
|
|
456
|
|
457 XPath xpath = XPath.newInstance("//prop:version[@xlink:title='This Version']/version:number");
|
|
458 xpath.addNamespace("prop", EScidocTools.prop);
|
|
459 xpath.addNamespace("xlink", EScidocTools.xlink);
|
|
460 xpath.addNamespace("version", EScidocTools.version);
|
|
461
|
|
462 Element node = (Element) xpath.selectSingleNode(doc);
|
|
463
|
|
464 return node.getText();
|
|
465 }
|
|
466
|
8
|
467 public static Object getXPath(Element node, String xpathString,boolean single) throws JDOMException {
|
|
468 XPath xpath= XPath.newInstance(xpathString);
|
0
|
469 xpath.addNamespace("dc",EScidocTools.DC);
|
|
470 xpath.addNamespace("escidocComponents",EScidocTools.escidocComponents);
|
|
471 xpath.addNamespace("prop",EScidocTools.prop);
|
|
472 xpath.addNamespace("xlink",EScidocTools.xlink);
|
|
473 xpath.addNamespace("mpiwg",EScidocTools.MPIWG);
|
|
474 xpath.addNamespace("version",EScidocTools.version);
|
|
475
|
|
476 if (single)
|
|
477 return xpath.selectSingleNode(node);
|
|
478 else
|
|
479 return xpath.selectNodes(node);
|
|
480 }
|
|
481
|
|
482
|
|
483
|
|
484 public Long writeItem(eSciDocXmlObject escidocItem) {
|
|
485 // TODO Auto-generated method stub
|
|
486 return null;
|
|
487 }
|
|
488
|
|
489
|
|
490
|
8
|
491 /** Updates an item at the eScidocserver
|
|
492 * @param escidocItem
|
|
493 * @return
|
|
494 * @throws JDOMException
|
|
495 */
|
|
496 public Boolean updateItem(eSciDocXmlObject escidocItem) throws JDOMException {
|
|
497 String cmd=escidocItem.getESciDocId();
|
|
498
|
|
499 try {
|
|
500 String retStr = escidocItem.printXML();
|
|
501 logger.debug(retStr);
|
|
502 String newObj = createObject(cmd, retStr);
|
|
503 return escidocItem.upDateFromXML(newObj);
|
|
504 } catch (Exception e) {
|
|
505 // TODO Auto-generated catch block
|
|
506 e.printStackTrace();
|
|
507 return false;
|
|
508 }
|
|
509
|
|
510
|
|
511 }
|
|
512
|
|
513 /**
|
|
514 * Creates an item
|
|
515 * @param escidocItem
|
|
516 * @return
|
|
517 */
|
0
|
518 public Boolean createItem(eSciDocXmlObject escidocItem) {
|
|
519 String cmd="/ir/item";
|
|
520
|
|
521 try {
|
|
522 String retStr = escidocItem.printXML();
|
|
523 String newObj = createObject(cmd, retStr);
|
|
524 return escidocItem.upDateFromXML(newObj);
|
|
525 } catch (Exception e) {
|
|
526 // TODO Auto-generated catch block
|
|
527 e.printStackTrace();
|
|
528 return false;
|
|
529 }
|
|
530
|
|
531
|
|
532 }
|
|
533
|
8
|
534 /**
|
|
535 * Creates a new object, i.e. does a httPut on command
|
|
536 * @param command should accept an httPut for creating new object, e.g. /ir/irtem
|
|
537 * @param xml eScidocXML describing the new object
|
|
538 * @return
|
|
539 * @throws Exception
|
|
540 */
|
0
|
541 public String createObject (String command,String xml) throws Exception {
|
|
542
|
|
543
|
|
544 InputStream stream = new ByteArrayInputStream(xml.getBytes("utf-8"));
|
|
545
|
|
546
|
|
547
|
|
548 //DefaultHttpClient httpclient = login();
|
|
549
|
|
550
|
|
551
|
|
552 HttpResponse eScidocPut = eScidocPut(command, stream);
|
|
553
|
|
554
|
|
555 //HttpEntity responseEntity = eScidocPut.getEntity();
|
|
556
|
|
557 System.out.println("----------------------------------------");
|
|
558 System.out.println(eScidocPut.getStatusLine());
|
|
559
|
|
560 int code = eScidocPut.getStatusLine().getStatusCode();
|
|
561
|
|
562 InputStream st = eScidocPut.getEntity().getContent();
|
|
563 //System.out.println(EScidocBasicHandler.convertStreamToString(st));
|
|
564 String xmlret = EScidocBasicHandler.convertStreamToString(st);
|
|
565 if (code !=200){
|
|
566 logger.error(xmlret);
|
|
567 throw (new Exception("CAN not DO error:"+code));
|
|
568
|
|
569 }
|
|
570 return xmlret;
|
|
571
|
|
572 }
|
|
573
|
|
574
|
|
575
|
8
|
576 /**
|
|
577 * Searches in a given context for an object's pid and return its escidoc:id
|
|
578 * @param pid
|
|
579 * @param context
|
|
580 * @return
|
|
581 * @throws ConnectorException
|
|
582 */
|
3
|
583 public String getIDfromPID(String pid, String context) throws ConnectorException {
|
0
|
584
|
|
585
|
|
586 String filter = "<param><filter name=\"http://escidoc.de/core/01/properties/pid\">";
|
|
587
|
|
588 filter += pid;
|
|
589 filter += "</filter></param>";
|
|
590
|
|
591 String command = context
|
|
592 + "/resources/members/filter";
|
3
|
593 HttpResponse result;
|
|
594 try {
|
|
595 result = eScidocPost(command,
|
|
596 new ByteArrayInputStream(filter.getBytes()));
|
|
597 } catch (IOException e) {
|
|
598 // TODO Auto-generated catch block
|
|
599 e.printStackTrace();
|
|
600 throw new ConnectorException();
|
|
601 }
|
0
|
602
|
3
|
603 try {
|
|
604 Document dom = new SAXBuilder().build(result.getEntity().getContent());
|
|
605
|
|
606 XPath xp = EScidocTools.getESciDocXpath("//escidocItem:item/@xlink:href");
|
|
607
|
|
608 Attribute attr = (Attribute)xp.selectSingleNode(dom);
|
|
609
|
|
610 if (attr!=null){
|
|
611 return attr.getValue();
|
|
612 }
|
|
613
|
|
614 return null;
|
|
615 //return convertStreamToString(result.getEntity().getContent());
|
|
616 } catch (IllegalStateException e) {
|
|
617 // TODO Auto-generated catch block
|
|
618 e.printStackTrace();
|
|
619 throw new ConnectorException();
|
|
620 } catch (JDOMException e) {
|
|
621 // TODO Auto-generated catch block
|
|
622 e.printStackTrace();
|
|
623 throw new ConnectorException();
|
|
624 } catch (IOException e) {
|
|
625 // TODO Auto-generated catch block
|
|
626 e.printStackTrace();
|
|
627 throw new ConnectorException();
|
0
|
628 }
|
|
629
|
|
630
|
|
631 }
|
|
632
|
|
633
|
|
634
|
8
|
635 /**
|
|
636 * submits the object
|
|
637 * @param obj
|
|
638 * @param comment
|
|
639 * @return
|
|
640 * @throws ClientProtocolException
|
|
641 * @throws IOException
|
|
642 * @throws JDOMException
|
|
643 */
|
0
|
644 public HttpResponse submitAnObject(eSciDocXmlObject obj, String comment) throws ClientProtocolException, IOException, JDOMException {
|
|
645 try {
|
2
|
646 //addVersionPid(obj);
|
0
|
647 } catch (Exception e) {
|
|
648 // TODO: handle exception
|
|
649 }
|
|
650
|
|
651 String retTxt = obj.printXML();
|
|
652 String dateStamp = EScidocBasicHandler.getDateStamp(retTxt);
|
|
653
|
|
654 String param = "<param last-modification-date=\""+dateStamp+"\">";
|
|
655 param+="<comment>"+comment+"</comment>";
|
|
656 param+="</param>";
|
|
657
|
|
658 String command=obj.getESciDocId()+"/submit";
|
|
659 HttpResponse result = eScidocPost(command, EScidocBasicHandler.convertStringToStream(param));
|
|
660
|
|
661 return result;
|
|
662
|
|
663 }
|
|
664
|
8
|
665 /**
|
|
666 * Generates a list of eScidocObject from a search or filter
|
|
667 * @param command search or filter command (e.g. /resources/members/filter)
|
|
668 * @param objectXpath xpath to the element which separates the objects in the resultXML of the filering or search, e.g.
|
|
669 * "/zs:searchRetrieveResponse/zs:records/zs:record/zs:recordData/escidocItem:item" in a search
|
|
670 * @return
|
|
671 * @throws IOException
|
|
672 * @throws IllegalStateException
|
|
673 * @throws JDOMException
|
|
674 * @throws ESciDocXmlObjectException
|
|
675 */
|
4
|
676 public List<eSciDocXmlObject> getObjectsFromFilterResult(String command, String objectXpath) throws IOException, IllegalStateException, JDOMException,ESciDocXmlObjectException {
|
0
|
677 //String filter = "<param><filter></filter></param>";
|
|
678 //
|
|
679 //String command = context
|
|
680 // + "/resources/members/filter";
|
|
681 //HttpResponse result =eScidocPost(command,
|
|
682 //new ByteArrayInputStream(filter.getBytes()));
|
|
683
|
|
684 //String command = context+"/resources/members";
|
|
685
|
|
686 HttpResponse result =eScidocGet(command);
|
2
|
687 //InputStream text=result.getEntity().getContent();
|
|
688 //String tmtxt = convertStreamToString(text);
|
|
689 //System.out.println(tmtxt);
|
0
|
690 Document dom = new SAXBuilder().build(result.getEntity().getContent());
|
|
691 XPath xp = EScidocTools.getESciDocXpath(objectXpath);
|
|
692
|
2
|
693
|
0
|
694 List<Element> attr = (List<Element>)xp.selectNodes(dom);
|
|
695 ArrayList<eSciDocXmlObject> ret = new ArrayList<eSciDocXmlObject>();
|
|
696 for (Element el: attr){
|
|
697 ret.add(new eSciDocXmlObject(el));
|
|
698 }
|
|
699 return ret;
|
|
700 }
|
|
701
|
8
|
702 /**
|
|
703 * @param command search or filter command (e.g. /resources/members/filter)
|
|
704 * @param objectXpath xpath to the element which separates the objects in the resultXML of the filering or search, e.g.
|
|
705 * "/zs:searchRetrieveResponse/zs:records/zs:record/zs:recordData/escidocItem:item" in a search
|
|
706 * @param mode mode=0 or 2 for pending, mode=1 for submitted objects
|
|
707 * @return
|
|
708 * @throws IOException
|
|
709 * @throws IllegalStateException
|
|
710 * @throws JDOMException
|
|
711 */
|
2
|
712 public Integer getNumberOfHitsFromFilterResult(String command,
|
|
713 String objectXPath, int mode) throws IOException, IllegalStateException, JDOMException {
|
|
714 String query="";
|
|
715 if(mode==0 | mode==2){
|
|
716 query="query=%22/properties/version/status%22=pending";
|
|
717 } else {
|
|
718 query="query=%22/properties/version/status%22=submitted";
|
|
719 }
|
|
720
|
|
721 HttpResponse result =eScidocGet(command+"?maximumRecords=1&"+query);
|
|
722 //InputStream text=result.getEntity().getContent();
|
|
723 //String tmtxt = convertStreamToString(text);
|
|
724 //System.out.println(tmtxt);
|
|
725 Document dom = new SAXBuilder().build(result.getEntity().getContent());
|
|
726 XPath xp = EScidocTools.getESciDocXpath("//srw:numberOfRecords");
|
|
727
|
|
728 Element attr = (Element)xp.selectSingleNode(dom);
|
|
729
|
|
730 Integer tmpInt = Integer.valueOf(attr.getText());
|
|
731 return tmpInt;
|
|
732 }
|
|
733
|
8
|
734 /** adds a PID generated by the MPIWG PID generator to the object.
|
|
735 * @param obj
|
|
736 * @return
|
|
737 * @throws ClientProtocolException
|
|
738 * @throws IOException
|
|
739 * @throws JDOMException
|
|
740 */
|
0
|
741 public boolean addVersionPid(eSciDocXmlObject obj) throws ClientProtocolException, IOException, JDOMException{
|
|
742 //HttpResponse ret = eScidocGet(href);
|
|
743 //String retTxt = EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent());
|
|
744
|
|
745 String retTxt = obj.printXML();
|
|
746 String dateStamp = EScidocBasicHandler.getDateStamp(retTxt);
|
|
747 String pid=EScidocBasicHandler.getPID(retTxt);
|
|
748 String versionNumber = EScidocBasicHandler.getLastVersionNumber(retTxt);
|
|
749
|
|
750 String vpid=pid+":"+versionNumber;
|
|
751
|
|
752 String param = "<param last-modification-date=\""+dateStamp+"\">";
|
|
753 param+="<pid>"+vpid+"</pid>";
|
|
754 param+="</param>";
|
|
755 String command=obj.getESciDocId()+"/assign-version-pid";
|
|
756
|
|
757 HttpResponse result = eScidocPost(command, EScidocBasicHandler.convertStringToStream(param));
|
|
758
|
|
759 int code = result.getStatusLine().getStatusCode();
|
|
760 result.getEntity().consumeContent();
|
2
|
761 if (code!=200){
|
|
762 if(code==450)
|
|
763 return false;
|
0
|
764 return false;
|
2
|
765 }
|
0
|
766 return true;
|
|
767
|
|
768
|
|
769 }
|
|
770
|
|
771
|
|
772
|
8
|
773 /**
|
|
774 * @param obj
|
|
775 * @param comment
|
|
776 * @return
|
|
777 * @throws IOException
|
|
778 * @throws JDOMException
|
|
779 */
|
0
|
780 public HttpResponse releaseAnObject(eSciDocXmlObject obj, String comment) throws IOException, JDOMException {
|
|
781 //HttpResponse ret = getEsciDocHandler().eScidocGet(href);
|
|
782 //String retTxt = EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent());
|
|
783
|
|
784 String retTxt = obj.printXML();
|
|
785 String dateStamp = EScidocBasicHandler.getDateStamp(retTxt);
|
|
786
|
|
787 String param = "<param last-modification-date=\""+dateStamp+"\">";
|
|
788 param+="<comment>"+comment+"</comment>";
|
|
789 param+="</param>";
|
|
790
|
|
791 String command=obj.getESciDocId()+"/release";
|
|
792 HttpResponse result = eScidocPost(command, EScidocBasicHandler.convertStringToStream(param));
|
|
793
|
|
794 return result;
|
|
795
|
|
796
|
|
797
|
|
798
|
|
799 }
|
|
800
|
|
801
|
|
802
|
|
803 public boolean upDateObject(eSciDocXmlObject obj) throws Exception {
|
|
804 HttpResponse result = eScidocGet(obj.getESciDocId());
|
|
805 if (result.getStatusLine().getStatusCode()!=200){
|
|
806 logger.debug(result.getEntity().getContent());
|
|
807 return false;
|
|
808 }
|
|
809
|
|
810 String xml = convertStreamToString(result.getEntity().getContent());
|
|
811 obj.upDateFromXML(xml);
|
|
812 return true;
|
|
813 }
|
|
814
|
|
815
|
|
816
|
8
|
817 /**check if object already exists
|
|
818 * @param indexField field in the /ir/items object to identify the object, for a list see /ir/items?operation=explain
|
|
819 * @param testString test if this exist in field.
|
|
820 * @param context
|
|
821 * @return
|
|
822 * @throws ConnectorException
|
|
823 * @throws ObjectNotUniqueError
|
|
824 */
|
|
825 public ECHOObject alreadyExists(String indexField, String testString, String context, String comparator) throws ConnectorException, ObjectNotUniqueError {
|
0
|
826
|
|
827 String[] ct = context.split("/"); // gebraucht wird hier nur die id, dh ohne /ir/...
|
|
828
|
|
829 String contextId=ct[ct.length-1];
|
|
830
|
|
831
|
8
|
832 String searchString = String.format("\"%s\"%s\"%s\"",indexField,comparator,testString);
|
|
833 searchString += " and "+String.format("\"%s\"=\"%s\"","context.objid",contextId);
|
0
|
834
|
3
|
835 HttpResponse ret;
|
|
836 try{
|
0
|
837 searchString = URLEncoder.encode(searchString,"utf-8");
|
8
|
838 ret = eScidocGet("/srw/search/escidoc_all?operation=searchRetrieve&version=1.1&query="+searchString);
|
3
|
839 } catch (UnsupportedEncodingException e) {
|
|
840 throw new ConnectorException();
|
|
841 } catch (IOException e) {
|
|
842 // TODO Auto-generated catch block
|
|
843 e.printStackTrace();
|
|
844 throw new ConnectorException();
|
|
845 }
|
0
|
846
|
|
847 if (ret.getStatusLine().getStatusCode()!=200)
|
|
848 {
|
|
849 logger.debug("alreadyExists: error searchstring:"+searchString);
|
|
850 HttpEntity ent = ret.getEntity();
|
3
|
851 if (ent!=null) {
|
|
852 try {
|
|
853 ent.consumeContent();
|
|
854 } catch (IOException e) {
|
|
855 // TODO Auto-generated catch block
|
|
856 e.printStackTrace();
|
|
857 throw new ConnectorException();
|
|
858 }
|
|
859 throw new ConnectorException();
|
|
860 }
|
0
|
861 }
|
3
|
862
|
|
863 try{
|
0
|
864 Document doc = new SAXBuilder().build(ret.getEntity().getContent());
|
|
865
|
|
866 XPath xp = EScidocTools.getESciDocXpath("//zs:numberOfRecords/text()");
|
|
867 String hitsStr = ((Text)xp.selectSingleNode(doc)).getText();
|
|
868 Integer hits = Integer.valueOf(hitsStr);
|
3
|
869 if (hits>0){
|
|
870 if (hits>1)
|
|
871 throw new ObjectNotUniqueError();
|
|
872 return getOldObjectFromESciDoc(doc);
|
8
|
873 //XPath xpItem = EScidocTools.getESciDocXpath("//escidocItem:item");
|
|
874
|
|
875
|
|
876 //Element attr = (Element) xp.selectSingleNode(doc);
|
|
877
|
|
878 //return getOldObjectFromESciDoc(attr);
|
3
|
879 }
|
|
880 return null;
|
|
881 } catch (IOException e) {
|
|
882 e.printStackTrace();
|
|
883 throw new ConnectorException();
|
|
884 } catch (IllegalStateException e) {
|
|
885 // TODO Auto-generated catch block
|
|
886 e.printStackTrace();
|
|
887 throw new ConnectorException();
|
|
888 } catch (JDOMException e) {
|
|
889 // TODO Auto-generated catch block
|
|
890 e.printStackTrace();
|
|
891 throw new ConnectorException();
|
|
892 }
|
|
893 }
|
|
894
|
|
895
|
|
896
|
8
|
897 /**Nimmt ein u.U. modifiziertes eScidocObject (als jDom.Document) und sucht das dazugehšrige abgespeicherte Objekt.
|
|
898 * @param doc
|
|
899 * @return
|
|
900 */
|
3
|
901 private ECHOObject getOldObjectFromESciDoc(Document doc) {
|
|
902 Map<String,String>retMap = new HashMap<String,String>();
|
|
903 XPath md5Nodes;
|
|
904 XPath itemId;
|
|
905 XPath lastModificationDate;
|
8
|
906 XPath objId;
|
3
|
907 try {
|
|
908 md5Nodes= EScidocTools.getESciDocXpath(".//escidocComponents:component/escidocComponents:properties[prop:content-category[text()='index_meta']]/prop:checksum");
|
|
909 itemId= EScidocTools.getESciDocXpath(".//escidocItem:item/@xlink:href");
|
8
|
910 objId= EScidocTools.getESciDocXpath(".//escidocItem:item/@objid");
|
3
|
911 lastModificationDate = EScidocTools.getESciDocXpath(".//escidocItem:item/@last-modification-date");
|
|
912 } catch (JDOMException e) {
|
|
913 // TODO Auto-generated catch block
|
|
914 e.printStackTrace();
|
|
915 return null;
|
|
916 }
|
|
917 Element node;
|
|
918 Attribute idNode;
|
|
919 Attribute lastModificationDateNode;
|
|
920 try {
|
|
921 node = (Element)md5Nodes.selectSingleNode(doc);
|
|
922 idNode = (Attribute)itemId.selectSingleNode(doc);
|
8
|
923 if (idNode==null){
|
|
924 idNode = (Attribute)objId.selectSingleNode(doc);
|
|
925 }
|
3
|
926 lastModificationDateNode =(Attribute)lastModificationDate.selectSingleNode(doc);
|
|
927
|
|
928 } catch (JDOMException e) {
|
|
929 // TODO Auto-generated catch block
|
|
930 e.printStackTrace();
|
|
931 return null;
|
|
932 }
|
|
933 String md5= node.getTextTrim();
|
|
934 String escidocId= idNode.getValue();
|
|
935 ECHORessource er;
|
|
936 try {
|
|
937 er = new ECHORessource();
|
|
938 er.eScidocId=escidocId;
|
|
939 er.indexMetaMD5stored=md5;
|
|
940 er.lastModificationDate= lastModificationDateNode.getValue();
|
|
941 } catch (IOException e) {
|
|
942 // TODO Auto-generated catch block
|
|
943 e.printStackTrace();
|
|
944 return null;
|
|
945 }
|
|
946 return er;
|
0
|
947 }
|
|
948
|
|
949
|
|
950
|
8
|
951 /**
|
|
952 * Suche alle Links aus den Komponenten des Types type, d.h. es wird auf dem Context in allen Elementen nach:
|
|
953 * ".//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='"+type+"']]/escidocComponents:content/@xlink:href"
|
|
954 * gesucht.
|
|
955 *
|
|
956 * @param type
|
|
957 * @param context
|
|
958 * @return
|
|
959 * @throws IOException
|
|
960 * @throws IllegalStateException
|
|
961 * @throws JDOMException
|
|
962 */
|
|
963 public ArrayList<String> getAllLinksOfContext(String type, String context) throws IOException, IllegalStateException, JDOMException {
|
0
|
964
|
|
965 HttpResponse result = eScidocGet(context+"/resources/members");
|
|
966 Document doc = new SAXBuilder().build(result.getEntity().getContent());
|
|
967 XPath xp = EScidocTools.getESciDocXpath("//escidocItem:item");
|
|
968 XPath id = EScidocTools.getESciDocXpath("./@xlink:href");
|
|
969
|
8
|
970 XPath url= EScidocTools.getESciDocXpath(".//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='"+type+"']]/escidocComponents:content/@xlink:href");
|
0
|
971
|
|
972 ArrayList<String> ret = new ArrayList<String>();
|
|
973 List<Element> items = xp.selectNodes(doc);
|
|
974
|
|
975 File fl = new File("/tmp/linksofcontext.out");
|
|
976 FileWriter fw = new FileWriter(fl);
|
|
977
|
|
978 for (Element item: items){
|
|
979
|
|
980 Attribute idAttribute = (Attribute)id.selectSingleNode(item);
|
|
981 String idStr = idAttribute.getValue();
|
|
982
|
|
983 Attribute urlAttribute = (Attribute)url.selectSingleNode(item);
|
|
984 String urlStr = urlAttribute.getValue();
|
|
985 ret.add(idStr+","+urlStr);
|
|
986 logger.debug("getALLLinksOfContex:"+idStr+","+urlStr);
|
|
987 fw.write(idStr+","+urlStr+"\n");
|
|
988 }
|
|
989 fw.close();
|
|
990 return ret;
|
|
991 }
|
2
|
992
|
|
993
|
|
994
|
8
|
995 /** Suche alle Objekte mit field = value, die Felder ergeben sich aus /srw/search/escidoc_all?operation= explain
|
|
996 *
|
|
997 * @param field
|
|
998 * @param value
|
|
999 * @return
|
|
1000 * @throws ESciDocXmlObjectException
|
|
1001 */
|
4
|
1002 public List<eSciDocXmlObject> getObjectsFromSearch(String field, String value) throws ESciDocXmlObjectException {
|
|
1003 String query = "/srw/search/escidoc_all?operation=searchRetrieve&version=1.1&query=";
|
|
1004 query+=field+"%3d";
|
|
1005 try {
|
|
1006 query+=URLEncoder.encode(value, "utf-8");
|
|
1007 } catch (UnsupportedEncodingException e) {
|
|
1008 // TODO Auto-generated catch block
|
|
1009 e.printStackTrace();
|
|
1010 throw new ESciDocXmlObjectException();
|
|
1011 }
|
|
1012
|
|
1013 try {
|
|
1014 return getObjectsFromFilterResult(query, "//escidocItem:item");
|
|
1015 } catch (IllegalStateException e) {
|
|
1016 // TODO Auto-generated catch block
|
|
1017 e.printStackTrace();
|
|
1018 } catch (IOException e) {
|
|
1019 // TODO Auto-generated catch block
|
|
1020 e.printStackTrace();
|
|
1021 } catch (JDOMException e) {
|
|
1022 // TODO Auto-generated catch block
|
|
1023 e.printStackTrace();
|
|
1024 }
|
|
1025
|
|
1026 throw new ESciDocXmlObjectException(); // fehler wenn kein return
|
|
1027 }
|
|
1028
|
|
1029
|
|
1030
|
8
|
1031 /**
|
|
1032 * Suche alle Objekte mit field = value, die Felder ergeben sich aus /srw/search/escidoc_all?operation= explain
|
|
1033 *
|
|
1034 * @param field
|
|
1035 * @param value
|
|
1036 * @param context
|
|
1037 * @return
|
|
1038 * @throws ESciDocXmlObjectException
|
|
1039 */
|
6
|
1040 public List<eSciDocXmlObject> getObjectsFromSearch(String field,
|
|
1041 String value, String context) throws ESciDocXmlObjectException {
|
|
1042 String query = "/srw/search/escidoc_all?operation=searchRetrieve&version=1.1&query=";
|
|
1043 query+=field+"%3d";
|
|
1044 try {
|
|
1045 String[] contextSplitted = context.split("/");
|
|
1046 if(contextSplitted.length>0){
|
|
1047 query+=URLEncoder.encode(value, "utf-8")+"%20and%20escidoc.context.objid%3d"+contextSplitted[contextSplitted.length-1];
|
|
1048 }
|
|
1049 } catch (UnsupportedEncodingException e) {
|
|
1050 // TODO Auto-generated catch block
|
|
1051 e.printStackTrace();
|
|
1052 throw new ESciDocXmlObjectException();
|
|
1053 }
|
|
1054
|
|
1055 try {
|
|
1056 return getObjectsFromFilterResult(query, "//escidocItem:item");
|
|
1057 } catch (IllegalStateException e) {
|
|
1058 // TODO Auto-generated catch block
|
|
1059 e.printStackTrace();
|
|
1060 } catch (IOException e) {
|
|
1061 // TODO Auto-generated catch block
|
|
1062 e.printStackTrace();
|
|
1063 } catch (JDOMException e) {
|
|
1064 // TODO Auto-generated catch block
|
|
1065 e.printStackTrace();
|
|
1066 }
|
|
1067
|
|
1068 throw new ESciDocXmlObjectException(); // fehler wenn kein return
|
|
1069
|
|
1070
|
|
1071 }
|
|
1072
|
|
1073
|
|
1074
|
8
|
1075 /**
|
|
1076 * Teste ob das Item (itemString) mit Datestamp dateString versehen ist.
|
|
1077 * @param itemString
|
|
1078 * @param dateString
|
|
1079 * @return
|
|
1080 * @throws IOException
|
|
1081 */
|
|
1082 public boolean isCurrent(String itemString, String dateString) throws IOException {
|
|
1083 HttpResponse ret = eScidocGet(itemString);
|
|
1084 if (ret.getStatusLine().getStatusCode()!=200){
|
|
1085 return false;
|
|
1086 }
|
|
1087 String dateStamp = getDateStamp(EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent()));
|
|
1088 if(dateStamp.equals(dateString))
|
|
1089 return true;
|
|
1090 return false;
|
|
1091 }
|
|
1092
|
|
1093
|
|
1094
|
|
1095 /** holt alle ids von Objekten, die mit item Ÿber das Predicate Ÿber Relationen verbunden sind (item hat das Format /ir/item/XXX)
|
|
1096 * @param itemString
|
|
1097 * @param predicate
|
|
1098 * @return
|
|
1099 */
|
|
1100 public List<String> getIdsOfRelationFromObject(String item,
|
|
1101 String predicate) {
|
|
1102 String cmd=item+"/relations";
|
|
1103 ArrayList<String> ret = new ArrayList<String>();
|
|
1104
|
|
1105 HttpResponse result;
|
|
1106 try {
|
|
1107 result = eScidocGet(cmd);
|
|
1108 } catch (IOException e) {
|
|
1109 logger.error("Cannot deal with:"+cmd);
|
|
1110 return ret;
|
|
1111 }
|
|
1112
|
|
1113 InputStream resultString;
|
|
1114 try {
|
|
1115 resultString = result.getEntity().getContent();;
|
|
1116 } catch (IllegalStateException e) {
|
|
1117 logger.error("Cannot deal with:"+cmd);
|
|
1118 return ret;
|
|
1119 } catch (IOException e) {
|
|
1120 logger.error("Cannot deal with:"+cmd);
|
|
1121 return ret;
|
|
1122 }
|
|
1123
|
|
1124 try {
|
|
1125 Document doc = (new SAXBuilder()).build(resultString);
|
|
1126 List<Attribute> xp = (List<Attribute>) getXPath(doc.getRootElement(), "//relations:relation[@predicate='"+predicate+"']/@xlink:href", false);
|
|
1127
|
|
1128 for (Attribute attr: xp){
|
|
1129 ret.add(attr.getValue());
|
|
1130 }
|
|
1131 return ret;
|
|
1132 } catch (JDOMException e) {
|
|
1133 // TODO Auto-generated catch block
|
|
1134 e.printStackTrace();
|
|
1135 } catch (IOException e) {
|
|
1136 // TODO Auto-generated catch block
|
|
1137 e.printStackTrace();
|
|
1138 }
|
|
1139 return ret;
|
|
1140 }
|
|
1141
|
|
1142
|
|
1143
|
3
|
1144
|
|
1145
|
|
1146
|
2
|
1147
|
0
|
1148 }
|