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