1
|
1 package de.mpiwg.itgroup.metadataManager.client;
|
|
2
|
|
3 import java.io.ByteArrayInputStream;
|
|
4 import java.io.IOException;
|
|
5 import java.io.InputStream;
|
|
6 import java.io.StringReader;
|
|
7 import java.io.UnsupportedEncodingException;
|
|
8 import java.net.URI;
|
|
9 import java.net.URISyntaxException;
|
|
10
|
|
11 import javax.xml.parsers.SAXParser;
|
|
12
|
|
13 import org.apache.http.HttpEntity;
|
|
14 import org.apache.http.HttpResponse;
|
|
15 import org.apache.http.client.ClientProtocolException;
|
|
16 import org.apache.http.client.HttpClient;
|
|
17 import org.apache.http.client.methods.HttpGet;
|
|
18 import org.apache.http.client.utils.URIUtils;
|
|
19 import org.apache.http.impl.client.DefaultHttpClient;
|
|
20 import org.jdom.Document;
|
|
21 import org.jdom.Element;
|
|
22 import org.jdom.JDOMException;
|
|
23 import org.jdom.input.SAXBuilder;
|
|
24 import org.jdom.output.XMLOutputter;
|
|
25 import org.jdom.xpath.XPath;
|
|
26 import org.json.JSONObject;
|
|
27 import org.json.JSONTokener;
|
|
28
|
|
29 import org.restlet.data.MediaType;
|
|
30 import org.restlet.representation.Representation;
|
|
31 import org.restlet.resource.ClientResource;
|
|
32
|
|
33 public class MetadataClient {
|
|
34
|
|
35 private String serverUrl;
|
|
36
|
|
37 public MetadataClient(String string) {
|
|
38 serverUrl = string;
|
|
39
|
|
40 }
|
|
41
|
|
42 public String replaceBIB(String idOfObject, String newBibTag, boolean save) throws URISyntaxException, ClientProtocolException, IOException {
|
|
43
|
|
44 String newIM=null;
|
|
45 SAXBuilder sb = new SAXBuilder();
|
|
46
|
|
47 String url = serverUrl+"indexMeta/permanent/library/"+idOfObject;
|
|
48
|
|
49 HttpClient httpclient = new DefaultHttpClient();
|
|
50
|
|
51 HttpGet httpget = new HttpGet(new URI(url));
|
|
52
|
|
53 HttpResponse response = httpclient.execute(httpget);
|
|
54 HttpEntity entity = response.getEntity();
|
|
55
|
|
56 try {
|
|
57 if (entity != null) {
|
|
58 InputStream instream = (InputStream) entity.getContent();
|
|
59 Document dom = sb.build(instream);
|
|
60
|
|
61 XPath xp =XPath.newInstance("//bib");
|
|
62 Element bibNode = (Element)xp.selectSingleNode(dom);
|
|
63
|
|
64 Document doc2 = sb.build(new StringReader(newBibTag));
|
|
65 Element newBibRoot=doc2.getRootElement();
|
|
66
|
|
67 Element bibPar=(Element) bibNode.getParent();
|
|
68 bibPar.removeContent(bibNode);
|
|
69 bibPar.addContent((Element) newBibRoot.clone());
|
|
70
|
|
71 XMLOutputter op = new XMLOutputter();
|
|
72 newIM = op.outputString(dom);
|
|
73 if (save){
|
|
74 //TODO save the new XML
|
|
75 }
|
|
76 }
|
|
77 } catch (IllegalStateException e) {
|
|
78 // TODO Auto-generated catch block
|
|
79 e.printStackTrace();
|
|
80 } catch (JDOMException e) {
|
|
81
|
|
82 System.err.println("Can't handle:"+ idOfObject);
|
|
83 e.printStackTrace();
|
|
84
|
|
85 }
|
|
86 return newIM;
|
|
87 }
|
|
88
|
|
89 public String addContext(String indexMeta, String link, String name, Boolean save) throws IOException {
|
|
90 SAXBuilder sb = new SAXBuilder();
|
|
91 InputStream im = new ByteArrayInputStream(indexMeta.getBytes("utf-8"));
|
|
92 try {
|
|
93 Document dom =sb.build(im);
|
|
94
|
|
95 XPath xp = XPath.newInstance("//meta");
|
|
96 Element metaNode=(Element) xp.selectSingleNode(dom);
|
|
97
|
|
98 Element contextElement = new Element("context");
|
|
99
|
|
100 Element linkElement = new Element("link");
|
|
101 linkElement.setText(link);
|
|
102 Element nameElement = new Element("name");
|
|
103 nameElement.setText(name);
|
|
104 contextElement.addContent(linkElement);
|
|
105 contextElement.addContent(nameElement);
|
|
106
|
|
107 metaNode.addContent(contextElement);
|
|
108
|
|
109 XMLOutputter op = new XMLOutputter();
|
|
110 String newIM = op.outputString(dom);
|
|
111
|
|
112 if (save){
|
|
113 //TODO save the new XML
|
|
114 }
|
|
115 return newIM;
|
|
116 } catch (JDOMException e) {
|
|
117 // TODO Auto-generated catch block
|
|
118 e.printStackTrace();
|
|
119 return null;
|
|
120 }
|
|
121 }
|
|
122
|
|
123 }
|