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;
|
|
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
|
2
|
239 //logger.debug("executing request:"+httpBase.getRequestLine());
|
0
|
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 {
|
2
|
513 //addVersionPid(obj);
|
0
|
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);
|
2
|
543 //InputStream text=result.getEntity().getContent();
|
|
544 //String tmtxt = convertStreamToString(text);
|
|
545 //System.out.println(tmtxt);
|
0
|
546 Document dom = new SAXBuilder().build(result.getEntity().getContent());
|
|
547 XPath xp = EScidocTools.getESciDocXpath(objectXpath);
|
|
548
|
2
|
549
|
0
|
550 List<Element> attr = (List<Element>)xp.selectNodes(dom);
|
|
551 ArrayList<eSciDocXmlObject> ret = new ArrayList<eSciDocXmlObject>();
|
|
552 for (Element el: attr){
|
|
553 ret.add(new eSciDocXmlObject(el));
|
|
554 }
|
|
555 return ret;
|
|
556 }
|
|
557
|
2
|
558 public Integer getNumberOfHitsFromFilterResult(String command,
|
|
559 String objectXPath, int mode) throws IOException, IllegalStateException, JDOMException {
|
|
560 String query="";
|
|
561 if(mode==0 | mode==2){
|
|
562 query="query=%22/properties/version/status%22=pending";
|
|
563 } else {
|
|
564 query="query=%22/properties/version/status%22=submitted";
|
|
565 }
|
|
566
|
|
567 HttpResponse result =eScidocGet(command+"?maximumRecords=1&"+query);
|
|
568 //InputStream text=result.getEntity().getContent();
|
|
569 //String tmtxt = convertStreamToString(text);
|
|
570 //System.out.println(tmtxt);
|
|
571 Document dom = new SAXBuilder().build(result.getEntity().getContent());
|
|
572 XPath xp = EScidocTools.getESciDocXpath("//srw:numberOfRecords");
|
|
573
|
|
574 Element attr = (Element)xp.selectSingleNode(dom);
|
|
575
|
|
576 Integer tmpInt = Integer.valueOf(attr.getText());
|
|
577 return tmpInt;
|
|
578 }
|
|
579
|
0
|
580 public boolean addVersionPid(eSciDocXmlObject obj) throws ClientProtocolException, IOException, JDOMException{
|
|
581 //HttpResponse ret = eScidocGet(href);
|
|
582 //String retTxt = EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent());
|
|
583
|
|
584 String retTxt = obj.printXML();
|
|
585 String dateStamp = EScidocBasicHandler.getDateStamp(retTxt);
|
|
586 String pid=EScidocBasicHandler.getPID(retTxt);
|
|
587 String versionNumber = EScidocBasicHandler.getLastVersionNumber(retTxt);
|
|
588
|
|
589 String vpid=pid+":"+versionNumber;
|
|
590
|
|
591 String param = "<param last-modification-date=\""+dateStamp+"\">";
|
|
592 param+="<pid>"+vpid+"</pid>";
|
|
593 param+="</param>";
|
|
594 String command=obj.getESciDocId()+"/assign-version-pid";
|
|
595
|
|
596 HttpResponse result = eScidocPost(command, EScidocBasicHandler.convertStringToStream(param));
|
|
597
|
|
598 int code = result.getStatusLine().getStatusCode();
|
|
599 result.getEntity().consumeContent();
|
2
|
600 if (code!=200){
|
|
601 if(code==450)
|
|
602 return false;
|
0
|
603 return false;
|
2
|
604 }
|
0
|
605 return true;
|
|
606
|
|
607
|
|
608 }
|
|
609
|
|
610
|
|
611
|
|
612 public HttpResponse releaseAnObject(eSciDocXmlObject obj, String comment) throws IOException, JDOMException {
|
|
613 //HttpResponse ret = getEsciDocHandler().eScidocGet(href);
|
|
614 //String retTxt = EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent());
|
|
615
|
|
616 String retTxt = obj.printXML();
|
|
617 String dateStamp = EScidocBasicHandler.getDateStamp(retTxt);
|
|
618
|
|
619 String param = "<param last-modification-date=\""+dateStamp+"\">";
|
|
620 param+="<comment>"+comment+"</comment>";
|
|
621 param+="</param>";
|
|
622
|
|
623 String command=obj.getESciDocId()+"/release";
|
|
624 HttpResponse result = eScidocPost(command, EScidocBasicHandler.convertStringToStream(param));
|
|
625
|
|
626 return result;
|
|
627
|
|
628
|
|
629
|
|
630
|
|
631 }
|
|
632
|
|
633
|
|
634
|
|
635 public boolean upDateObject(eSciDocXmlObject obj) throws Exception {
|
|
636 HttpResponse result = eScidocGet(obj.getESciDocId());
|
|
637 if (result.getStatusLine().getStatusCode()!=200){
|
|
638 logger.debug(result.getEntity().getContent());
|
|
639 return false;
|
|
640 }
|
|
641
|
|
642 String xml = convertStreamToString(result.getEntity().getContent());
|
|
643 obj.upDateFromXML(xml);
|
|
644 return true;
|
|
645 }
|
|
646
|
|
647
|
|
648
|
|
649 public boolean alreadyExists(String indexField, String testString, String context) throws Exception {
|
|
650
|
|
651 String[] ct = context.split("/"); // gebraucht wird hier nur die id, dh ohne /ir/...
|
|
652
|
|
653 String contextId=ct[ct.length-1];
|
|
654
|
|
655
|
|
656 String searchString = String.format("\"%s\"=\"%s\"",indexField,testString);
|
|
657 searchString += " and "+String.format("\"%s\"=\"%s\"","/properties/context/id",contextId);
|
|
658
|
|
659 searchString = URLEncoder.encode(searchString,"utf-8");
|
|
660 HttpResponse ret = eScidocGet("/ir/items?operation=searchRetrieve&version=1.1&query="+searchString);
|
|
661
|
|
662 if (ret.getStatusLine().getStatusCode()!=200)
|
|
663 {
|
|
664 logger.debug("alreadyExists: error searchstring:"+searchString);
|
|
665 HttpEntity ent = ret.getEntity();
|
|
666 if (ent!=null)
|
|
667 ent.consumeContent();
|
|
668 throw new Exception();
|
|
669 }
|
|
670 Document doc = new SAXBuilder().build(ret.getEntity().getContent());
|
|
671
|
|
672 XPath xp = EScidocTools.getESciDocXpath("//zs:numberOfRecords/text()");
|
|
673 String hitsStr = ((Text)xp.selectSingleNode(doc)).getText();
|
|
674 Integer hits = Integer.valueOf(hitsStr);
|
|
675 if (hits>0)
|
|
676 return true;
|
|
677 return false;
|
|
678 }
|
|
679
|
|
680
|
|
681
|
|
682 public ArrayList<String> getAllLinksOfContext(String string, String context) throws IOException, IllegalStateException, JDOMException {
|
|
683
|
|
684 HttpResponse result = eScidocGet(context+"/resources/members");
|
|
685 Document doc = new SAXBuilder().build(result.getEntity().getContent());
|
|
686 XPath xp = EScidocTools.getESciDocXpath("//escidocItem:item");
|
|
687 XPath id = EScidocTools.getESciDocXpath("./@xlink:href");
|
|
688
|
|
689 XPath url= EScidocTools.getESciDocXpath(".//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='"+string+"']]/escidocComponents:content/@xlink:href");
|
|
690
|
|
691 ArrayList<String> ret = new ArrayList<String>();
|
|
692 List<Element> items = xp.selectNodes(doc);
|
|
693
|
|
694 File fl = new File("/tmp/linksofcontext.out");
|
|
695 FileWriter fw = new FileWriter(fl);
|
|
696
|
|
697 for (Element item: items){
|
|
698
|
|
699 Attribute idAttribute = (Attribute)id.selectSingleNode(item);
|
|
700 String idStr = idAttribute.getValue();
|
|
701
|
|
702 Attribute urlAttribute = (Attribute)url.selectSingleNode(item);
|
|
703 String urlStr = urlAttribute.getValue();
|
|
704 ret.add(idStr+","+urlStr);
|
|
705 logger.debug("getALLLinksOfContex:"+idStr+","+urlStr);
|
|
706 fw.write(idStr+","+urlStr+"\n");
|
|
707 }
|
|
708 fw.close();
|
|
709 return ret;
|
|
710 }
|
2
|
711
|
|
712
|
|
713
|
|
714
|
0
|
715 }
|