comparison src/de/mpiwg/itgroup/eSciDoc/Tools/EScidocBasicHandler.java @ 0:c6929e63b0b8

first import
author dwinter
date Wed, 24 Nov 2010 16:52:07 +0100
parents
children fab8e78184fa
comparison
equal deleted inserted replaced
-1:000000000000 0:c6929e63b0b8
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;
14 import java.util.List;
15 import java.util.StringTokenizer;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;
20
21
22 import org.apache.http.HttpEntity;
23 import org.apache.http.HttpResponse;
24 import org.apache.http.client.ClientProtocolException;
25 import org.apache.http.client.HttpClient;
26 import org.apache.http.client.methods.HttpDelete;
27 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
28 import org.apache.http.client.methods.HttpGet;
29 import org.apache.http.client.methods.HttpPost;
30 import org.apache.http.client.methods.HttpPut;
31 import org.apache.http.client.methods.HttpRequestBase;
32 import org.apache.http.client.params.ClientPNames;
33 import org.apache.http.client.params.CookiePolicy;
34 import org.apache.http.entity.InputStreamEntity;
35 import org.apache.http.impl.client.DefaultHttpClient;
36 import org.apache.http.impl.conn.SingleClientConnManager;
37 import org.apache.http.protocol.BasicHttpContext;
38 import org.apache.http.protocol.HttpContext;
39 import org.apache.log4j.Logger;
40 import org.jdom.Attribute;
41 import org.jdom.Document;
42 import org.jdom.Element;
43 import org.jdom.JDOMException;
44 import org.jdom.Text;
45 import org.jdom.input.SAXBuilder;
46 import org.jdom.xpath.XPath;
47
48 import de.mpiwg.itgroup.eSciDoc.utils.eSciDocXmlObject;
49
50
51
52
53
54 /**
55 * Handler to create a connection with an eScidoc server
56 * @author dwinter
57 *
58 */
59 public class EScidocBasicHandler {
60
61 private Logger logger;
62 private String eScidocServer;
63 private int eScidocPort;
64 private String user;
65 private String password;
66 public String eScidocUrl;
67 private HttpClient httpclient=null;
68
69 /**
70 * @param eScidocServer
71 * @param eScidocPort
72 * @param user
73 * @param password
74 */
75 public EScidocBasicHandler(String eScidocServer, int eScidocPort,String user, String password){
76 this.eScidocServer=eScidocServer;
77 this.eScidocPort=eScidocPort;
78 this.user=user;
79 this.password=password;
80 this.eScidocUrl="http://"+eScidocServer+":"+String.valueOf(eScidocPort);
81
82 logger = Logger.getRootLogger();
83
84 }
85
86
87
88 /**
89 * Logs you into escidoc and sets the httpclient field to the current client for this session
90 * @return gives you the httpclient for further usage.
91 * @throws HttpException
92 * @throws ClientProtocolException
93 * @throws IOException
94 */
95 public HttpClient login() throws IOException {
96 httpclient = new DefaultHttpClient();
97
98 HttpContext localContext = new BasicHttpContext();
99
100 httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
101
102 HttpPost httppost = new HttpPost(eScidocUrl+"/aa/login?target=/");
103
104 logger.debug("executing request");
105
106 HttpResponse response = httpclient.execute(httppost);
107 //HttpEntity entity = httppost.getRes
108
109 System.out.println("----------------------------------------");
110 System.out.println(response.getStatusLine());
111
112 HttpEntity entity = response.getEntity();
113 if (entity != null) { entity.consumeContent();
114 }
115
116 HttpGet httpget = new HttpGet(eScidocUrl+"/aa/j_spring_security_check?j_username="+user+"&j_password="+password);
117
118 response = httpclient.execute(httpget);
119 //entity = response.getEntity();
120
121 System.out.println("----------------------------------------");
122 System.out.println(response.getStatusLine());
123
124 entity = response.getEntity();
125 if (entity != null) { entity.consumeContent();
126 }
127
128 //entity.consumeContent();
129 return httpclient;
130 }
131
132 /**
133 * Sends a PUT request to the escidoc client. performs a login if not done before.
134 * @param command
135 * @param body
136 * @return
137 * @throws ClientProtocolException
138 * @throws IOException
139 */
140 public HttpResponse eScidocPut(String command, InputStream body) throws IOException {
141 HttpPut httpput = new HttpPut(eScidocUrl+command);
142 return eScidocRequestBase(httpput,command,body);
143
144 }
145
146 /**
147 * Sends a PUT request to the escidoc client. performs a login if not done before.
148 * @param command
149 * @param url
150 * @return
151 * @throws ClientProtocolException
152 * @throws IOException
153 */
154 public HttpResponse eScidocPut(String command, URL url) throws IOException{
155
156 HttpPut httpput = new HttpPut(eScidocUrl+command);
157 return eScidocRequestBase(httpput,command,url.openStream());
158 }
159 /**
160 * Sends a POST request to the escidoc client. performs a login if not done before.
161 * @param command
162 * @param body
163 * @return
164 * @throws ClientProtocolException
165 * @throws IOException
166 */
167 public HttpResponse eScidocPost(String command, InputStream body) throws IOException{
168 HttpPost httppost = new HttpPost(eScidocUrl+command);
169 return eScidocRequestBase(httppost,command,body);
170 }
171
172 /** Sends a POST request to the escidoc client. performs a login if not done before.
173 * @param command
174 * @param url
175 * @return
176 * @throws ClientProtocolException
177 * @throws IOException
178 */
179 public HttpResponse eScidocPost(String command, URL url) throws IOException{
180 HttpPost httppost = new HttpPost(eScidocUrl+command);
181
182
183 return eScidocRequestBase(httppost,command,url.openStream());
184 }
185 /**
186 * Sends a GET request to the escidoc client. performs a login if not done before.
187 * @param command
188 * @return
189 * @throws ClientProtocolException
190 * @throws IOException
191 */
192 public HttpResponse eScidocGet(String command) throws IOException{
193 HttpGet httpget = new HttpGet(eScidocUrl+command);
194 return eScidocRequestBase(httpget,command,null);
195
196 }
197
198 /** Send a delete command to the escidoc client. performs a login if necessara
199 * @param command
200 * @return
201 * @throws ClientProtocolException
202 * @throws IOException
203 */
204 public HttpResponse eScidocDelete(String command) throws IOException{
205 HttpDelete httpdelete = new HttpDelete(eScidocUrl+command);
206 return eScidocRequestBase(httpdelete,command,null);
207
208 }
209 /**
210 * Generates and sends a general request to escidoc, used by escidocPUT, POST and GET.
211 * @param httpBase
212 * @param command
213 * @param body
214 * @return
215 * @throws ClientProtocolException
216 * @throws IOException
217 */
218 private HttpResponse eScidocRequestBase(HttpRequestBase httpBase, String command, InputStream body) throws IOException {
219
220
221
222 if (httpclient==null)
223 login();
224
225
226
227 if (HttpEntityEnclosingRequestBase.class.isInstance(httpBase)){
228
229
230 if (body!=null){
231
232
233
234 HttpEntity entity = new InputStreamEntity(body, -1);
235 ((HttpEntityEnclosingRequestBase)httpBase).setEntity(entity);
236 }
237 }
238
239 logger.debug("executing request:"+httpBase.getRequestLine());
240
241
242 HttpResponse status = httpclient.execute(httpBase);
243 //HttpEntity responseEntity = response.getEntity();
244
245 logger.debug("----------------------------------------");
246 logger.debug(status);
247
248
249 return status;
250 }
251
252 /**
253 *
254 * To convert the InputStream to String we use the BufferedReader.readLine()
255 * method. We iterate until the BufferedReader return null which means
256 * there's no more data to read. Each line will appended to a StringBuilder
257 * and returned as String.
258
259 * @param is
260 * @return
261 */
262 public static String convertStreamToString(InputStream is) {
263
264 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
265 StringBuilder sb = new StringBuilder();
266
267 String line = null;
268 try {
269 while ((line = reader.readLine()) != null) {
270 sb.append(line + "\n");
271 }
272 } catch (IOException e) {
273 e.printStackTrace();
274 } finally {
275 try {
276 is.close();
277 } catch (IOException e) {
278 e.printStackTrace();
279 }
280 }
281
282 return sb.toString();
283 }
284
285 /** converts a stream to a string
286 * @param string
287 * @return
288 * @throws UnsupportedEncodingException
289 */
290 public static InputStream convertStringToStream(String string) throws UnsupportedEncodingException{
291 return new ByteArrayInputStream(string.getBytes("utf-8"));
292 }
293
294 /**
295 * Find the last-modification-date from an escidoc xml-file (item/container/context)
296 * @param ret
297 * @return
298 */
299 public static String getDateStamp(String ret) {
300 Pattern p = Pattern.compile("last-modification-date=\"([^\"]*)\"");
301
302 Matcher m = p.matcher(ret);
303
304 m.find();
305
306 String txt;
307 try {
308 txt = m.group(1);
309 } catch (IllegalStateException e) {
310
311 e.printStackTrace();
312 System.out.println(ret);
313 throw new IllegalStateException();
314 }
315 return txt;
316 }
317
318 /**
319 * Find the content-modell (item)
320 * @param ret
321 * @return
322 * @throws IOException
323 * @throws JDOMException
324 * @throws UnsupportedEncodingException
325 */
326 public static String getContentModel(InputStream escidocstream) throws UnsupportedEncodingException, JDOMException, IOException {
327 SAXBuilder builder = new SAXBuilder();
328 Document doc = builder.build(escidocstream);
329
330 Element root= doc.getRootElement();
331 XPath xpath = EScidocTools.getESciDocXpath("escidocItem:properties/srel:content-model/@xlink:href");
332
333 Attribute node = (Attribute) xpath.selectSingleNode(root);
334
335 return node.getValue();
336
337 }
338
339
340 /**
341 * Find the escidoc:id from an escidoc xml (item/container/context)
342 * @param ret
343 * @return
344 */
345 public static String getId(String ret) {
346 Pattern p = Pattern.compile("xlink:href=\"([^\"]*)\"");
347
348 Matcher m = p.matcher(ret);
349
350 m.find();
351 String txt = m.group(1);
352 String[] splitted = txt.split("/");
353
354 String id = splitted[splitted.length-1];
355 return id;
356 }
357
358
359 /**
360 * Get the pid from an escidoc xml-file (item/container/context)
361 * @param retTxt
362 * @return
363 * @throws JDOMException
364 * @throws IOException
365 */
366 public static String getPID(String retTxt) throws JDOMException, IOException {
367 SAXBuilder builder = new SAXBuilder();
368
369
370 Document doc = builder.build(EScidocBasicHandler.convertStringToStream(retTxt));
371
372 XPath xpath = XPath.newInstance("//prop:pid");
373 xpath.addNamespace("prop", EScidocTools.prop);
374
375 Element node = (Element) xpath.selectSingleNode(doc);
376
377 return node.getTextTrim();
378
379 }
380
381
382 /**
383 * get the number of the last version from an escidoc xml-file (item/container/context)
384 * @param retTxt
385 * @return
386 * @throws JDOMException
387 * @throws IOException
388 */
389 public static String getLastVersionNumber(String retTxt) throws JDOMException, IOException {
390 SAXBuilder builder = new SAXBuilder();
391
392 Document doc = builder.build(EScidocBasicHandler.convertStringToStream(retTxt));
393
394
395 XPath xpath = XPath.newInstance("//prop:version[@xlink:title='This Version']/version:number");
396 xpath.addNamespace("prop", EScidocTools.prop);
397 xpath.addNamespace("xlink", EScidocTools.xlink);
398 xpath.addNamespace("version", EScidocTools.version);
399
400 Element node = (Element) xpath.selectSingleNode(doc);
401
402 return node.getText();
403 }
404
405 public static Object getXPath(Element node, String string,boolean single) throws JDOMException {
406 XPath xpath= XPath.newInstance(string);
407 xpath.addNamespace("dc",EScidocTools.DC);
408 xpath.addNamespace("escidocComponents",EScidocTools.escidocComponents);
409 xpath.addNamespace("prop",EScidocTools.prop);
410 xpath.addNamespace("xlink",EScidocTools.xlink);
411 xpath.addNamespace("mpiwg",EScidocTools.MPIWG);
412 xpath.addNamespace("version",EScidocTools.version);
413
414 if (single)
415 return xpath.selectSingleNode(node);
416 else
417 return xpath.selectNodes(node);
418 }
419
420
421
422 public Long writeItem(eSciDocXmlObject escidocItem) {
423 // TODO Auto-generated method stub
424 return null;
425 }
426
427
428
429 public Boolean createItem(eSciDocXmlObject escidocItem) {
430 String cmd="/ir/item";
431
432 try {
433 String retStr = escidocItem.printXML();
434 String newObj = createObject(cmd, retStr);
435 return escidocItem.upDateFromXML(newObj);
436 } catch (Exception e) {
437 // TODO Auto-generated catch block
438 e.printStackTrace();
439 return false;
440 }
441
442
443 }
444
445 public String createObject (String command,String xml) throws Exception {
446
447
448 InputStream stream = new ByteArrayInputStream(xml.getBytes("utf-8"));
449
450
451
452 //DefaultHttpClient httpclient = login();
453
454
455
456 HttpResponse eScidocPut = eScidocPut(command, stream);
457
458
459 //HttpEntity responseEntity = eScidocPut.getEntity();
460
461 System.out.println("----------------------------------------");
462 System.out.println(eScidocPut.getStatusLine());
463
464 int code = eScidocPut.getStatusLine().getStatusCode();
465
466 InputStream st = eScidocPut.getEntity().getContent();
467 //System.out.println(EScidocBasicHandler.convertStreamToString(st));
468 String xmlret = EScidocBasicHandler.convertStreamToString(st);
469 if (code !=200){
470 logger.error(xmlret);
471 throw (new Exception("CAN not DO error:"+code));
472
473 }
474 return xmlret;
475
476 }
477
478
479
480 public String getIDfromPID(String pid, String context) throws ClientProtocolException, IOException, IllegalStateException, JDOMException {
481
482
483 String filter = "<param><filter name=\"http://escidoc.de/core/01/properties/pid\">";
484
485 filter += pid;
486 filter += "</filter></param>";
487
488 String command = context
489 + "/resources/members/filter";
490 HttpResponse result =eScidocPost(command,
491 new ByteArrayInputStream(filter.getBytes()));
492
493 Document dom = new SAXBuilder().build(result.getEntity().getContent());
494
495 XPath xp = EScidocTools.getESciDocXpath("//escidocItem:item/@xlink:href");
496
497 Attribute attr = (Attribute)xp.selectSingleNode(dom);
498
499 if (attr!=null){
500 return attr.getValue();
501 }
502
503 return null;
504 //return convertStreamToString(result.getEntity().getContent());
505
506
507 }
508
509
510
511 public HttpResponse submitAnObject(eSciDocXmlObject obj, String comment) throws ClientProtocolException, IOException, JDOMException {
512 try {
513 addVersionPid(obj);
514 } catch (Exception e) {
515 // TODO: handle exception
516 }
517
518 String retTxt = obj.printXML();
519 String dateStamp = EScidocBasicHandler.getDateStamp(retTxt);
520
521 String param = "<param last-modification-date=\""+dateStamp+"\">";
522 param+="<comment>"+comment+"</comment>";
523 param+="</param>";
524
525 String command=obj.getESciDocId()+"/submit";
526 HttpResponse result = eScidocPost(command, EScidocBasicHandler.convertStringToStream(param));
527
528 return result;
529
530 }
531
532 public List<eSciDocXmlObject> getObjectListFromFilterResult(String command, String objectXpath) throws IOException, IllegalStateException, JDOMException {
533 //String filter = "<param><filter></filter></param>";
534 //
535 //String command = context
536 // + "/resources/members/filter";
537 //HttpResponse result =eScidocPost(command,
538 //new ByteArrayInputStream(filter.getBytes()));
539
540 //String command = context+"/resources/members";
541
542 HttpResponse result =eScidocGet(command);
543 Document dom = new SAXBuilder().build(result.getEntity().getContent());
544 XPath xp = EScidocTools.getESciDocXpath(objectXpath);
545
546 List<Element> attr = (List<Element>)xp.selectNodes(dom);
547 ArrayList<eSciDocXmlObject> ret = new ArrayList<eSciDocXmlObject>();
548 for (Element el: attr){
549 ret.add(new eSciDocXmlObject(el));
550 }
551 return ret;
552 }
553
554 public boolean addVersionPid(eSciDocXmlObject obj) throws ClientProtocolException, IOException, JDOMException{
555 //HttpResponse ret = eScidocGet(href);
556 //String retTxt = EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent());
557
558 String retTxt = obj.printXML();
559 String dateStamp = EScidocBasicHandler.getDateStamp(retTxt);
560 String pid=EScidocBasicHandler.getPID(retTxt);
561 String versionNumber = EScidocBasicHandler.getLastVersionNumber(retTxt);
562
563 String vpid=pid+":"+versionNumber;
564
565 String param = "<param last-modification-date=\""+dateStamp+"\">";
566 param+="<pid>"+vpid+"</pid>";
567 param+="</param>";
568 String command=obj.getESciDocId()+"/assign-version-pid";
569
570 HttpResponse result = eScidocPost(command, EScidocBasicHandler.convertStringToStream(param));
571
572 int code = result.getStatusLine().getStatusCode();
573 result.getEntity().consumeContent();
574 if (code!=200)
575 return false;
576 return true;
577
578
579 }
580
581
582
583 public HttpResponse releaseAnObject(eSciDocXmlObject obj, String comment) throws IOException, JDOMException {
584 //HttpResponse ret = getEsciDocHandler().eScidocGet(href);
585 //String retTxt = EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent());
586
587 String retTxt = obj.printXML();
588 String dateStamp = EScidocBasicHandler.getDateStamp(retTxt);
589
590 String param = "<param last-modification-date=\""+dateStamp+"\">";
591 param+="<comment>"+comment+"</comment>";
592 param+="</param>";
593
594 String command=obj.getESciDocId()+"/release";
595 HttpResponse result = eScidocPost(command, EScidocBasicHandler.convertStringToStream(param));
596
597 return result;
598
599
600
601
602 }
603
604
605
606 public boolean upDateObject(eSciDocXmlObject obj) throws Exception {
607 HttpResponse result = eScidocGet(obj.getESciDocId());
608 if (result.getStatusLine().getStatusCode()!=200){
609 logger.debug(result.getEntity().getContent());
610 return false;
611 }
612
613 String xml = convertStreamToString(result.getEntity().getContent());
614 obj.upDateFromXML(xml);
615 return true;
616 }
617
618
619
620 public boolean alreadyExists(String indexField, String testString, String context) throws Exception {
621
622 String[] ct = context.split("/"); // gebraucht wird hier nur die id, dh ohne /ir/...
623
624 String contextId=ct[ct.length-1];
625
626
627 String searchString = String.format("\"%s\"=\"%s\"",indexField,testString);
628 searchString += " and "+String.format("\"%s\"=\"%s\"","/properties/context/id",contextId);
629
630 searchString = URLEncoder.encode(searchString,"utf-8");
631 HttpResponse ret = eScidocGet("/ir/items?operation=searchRetrieve&version=1.1&query="+searchString);
632
633 if (ret.getStatusLine().getStatusCode()!=200)
634 {
635 logger.debug("alreadyExists: error searchstring:"+searchString);
636 HttpEntity ent = ret.getEntity();
637 if (ent!=null)
638 ent.consumeContent();
639 throw new Exception();
640 }
641 Document doc = new SAXBuilder().build(ret.getEntity().getContent());
642
643 XPath xp = EScidocTools.getESciDocXpath("//zs:numberOfRecords/text()");
644 String hitsStr = ((Text)xp.selectSingleNode(doc)).getText();
645 Integer hits = Integer.valueOf(hitsStr);
646 if (hits>0)
647 return true;
648 return false;
649 }
650
651
652
653 public ArrayList<String> getAllLinksOfContext(String string, String context) throws IOException, IllegalStateException, JDOMException {
654
655 HttpResponse result = eScidocGet(context+"/resources/members");
656 Document doc = new SAXBuilder().build(result.getEntity().getContent());
657 XPath xp = EScidocTools.getESciDocXpath("//escidocItem:item");
658 XPath id = EScidocTools.getESciDocXpath("./@xlink:href");
659
660 XPath url= EScidocTools.getESciDocXpath(".//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='"+string+"']]/escidocComponents:content/@xlink:href");
661
662 ArrayList<String> ret = new ArrayList<String>();
663 List<Element> items = xp.selectNodes(doc);
664
665 File fl = new File("/tmp/linksofcontext.out");
666 FileWriter fw = new FileWriter(fl);
667
668 for (Element item: items){
669
670 Attribute idAttribute = (Attribute)id.selectSingleNode(item);
671 String idStr = idAttribute.getValue();
672
673 Attribute urlAttribute = (Attribute)url.selectSingleNode(item);
674 String urlStr = urlAttribute.getValue();
675 ret.add(idStr+","+urlStr);
676 logger.debug("getALLLinksOfContex:"+idStr+","+urlStr);
677 fw.write(idStr+","+urlStr+"\n");
678 }
679 fw.close();
680 return ret;
681 }
682 }