0
|
1 package de.mpiwg.itgroup.eSciDoc.echoObjects;
|
|
2
|
8
|
3 import java.io.File;
|
|
4 import java.io.FileOutputStream;
|
0
|
5 import java.io.IOException;
|
3
|
6 import java.io.InputStream;
|
|
7 import java.net.MalformedURLException;
|
0
|
8 import java.net.URI;
|
3
|
9 import java.net.URL;
|
|
10 import java.security.MessageDigest;
|
|
11 import java.security.NoSuchAlgorithmException;
|
0
|
12 import java.util.Properties;
|
|
13 import java.util.regex.Matcher;
|
|
14 import java.util.regex.Pattern;
|
|
15
|
|
16
|
3
|
17 import org.apache.commons.codec.binary.Hex;
|
0
|
18 import org.apache.http.Header;
|
|
19 import org.apache.http.HttpException;
|
|
20 import org.apache.http.HttpHost;
|
|
21 import org.apache.http.HttpResponse;
|
|
22 import org.apache.http.client.ClientProtocolException;
|
|
23 import org.apache.http.client.HttpClient;
|
|
24 import org.apache.http.client.methods.HttpGet;
|
|
25 import org.apache.http.client.methods.HttpUriRequest;
|
|
26 import org.apache.http.impl.client.DefaultHttpClient;
|
|
27 import org.apache.http.protocol.BasicHttpContext;
|
|
28 import org.apache.http.protocol.ExecutionContext;
|
|
29 import org.apache.http.protocol.HttpContext;
|
8
|
30 import org.apache.log4j.Level;
|
|
31 import org.apache.log4j.Logger;
|
|
32 import org.jdom.Attribute;
|
|
33 import org.jdom.Document;
|
|
34 import org.jdom.Element;
|
|
35 import org.jdom.JDOMException;
|
|
36 import org.jdom.input.SAXBuilder;
|
|
37 import org.jdom.output.XMLOutputter;
|
|
38 import org.jdom.xpath.XPath;
|
|
39
|
|
40
|
|
41 import com.sun.org.apache.xalan.internal.xsltc.dom.DOMWSFilter;
|
|
42 import com.sun.org.apache.xerces.internal.parsers.SAXParser;
|
0
|
43
|
|
44 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler;
|
|
45 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools;
|
8
|
46 import de.mpiwg.itgroup.eSciDoc.utils.eSciDocXmlObject;
|
0
|
47 import de.mpiwg.itgroup.metadataManager.pid.DCTransformer;
|
|
48
|
|
49 public class ECHORessource extends ECHOObject{
|
|
50
|
|
51
|
|
52 public String archivePath;
|
|
53 public String metaData ="";
|
|
54 public String fullText ="";
|
|
55 private String textServletUrl;
|
|
56 private String dirInfoUrl;
|
|
57 public String link;
|
|
58
|
3
|
59 public String indexMetaMD5stored;
|
|
60
|
0
|
61
|
|
62 public ECHORessource() throws IOException{
|
|
63 super();
|
|
64 CMM ="/cmm/content-model/escidoc:11003";
|
|
65 Properties echoProperties = new Properties();
|
|
66 echoProperties.load(getClass().getResourceAsStream("/de/mpiwg/itgroup/eSciDoc/config/echo.properties"));
|
|
67
|
|
68 textServletUrl = (String) echoProperties.get("textServletUrl");
|
|
69 dirInfoUrl = (String) echoProperties.get("dirInfoUrl");
|
|
70
|
|
71 }
|
|
72
|
|
73
|
|
74 public ECHORessource(String name, String archivePath, String echoUrl) throws IOException {
|
|
75 this();
|
|
76 CMM ="/cmm/content-model/escidoc:11003";
|
|
77 this.name = name;
|
|
78 this.archivePath = archivePath;
|
|
79 this.echoUrl = echoUrl;
|
|
80 this.link = getLinkFromUrl(echoUrl);
|
|
81
|
|
82
|
|
83 }
|
|
84
|
|
85 /** Holt die URL auf die das ECHO Objekt redirected
|
|
86 * @param echoUrl
|
|
87 * @return
|
|
88 */
|
|
89 private String getLinkFromUrl(String echoUrl) {
|
|
90 if (echoUrl==null)
|
|
91 return null;
|
|
92 HttpContext localContext = new BasicHttpContext();
|
|
93 HttpClient hc = new DefaultHttpClient();
|
|
94 HttpGet get = new HttpGet(echoUrl);
|
|
95 HttpResponse response;
|
|
96 HttpUriRequest req;
|
|
97 HttpHost target;
|
|
98
|
|
99 try {
|
|
100 response = hc.execute(get,localContext);
|
|
101 req = (HttpUriRequest) localContext.getAttribute(
|
|
102 ExecutionContext.HTTP_REQUEST);
|
|
103 target = (HttpHost) localContext.getAttribute(
|
|
104 ExecutionContext.HTTP_TARGET_HOST);
|
|
105
|
|
106
|
|
107 } catch (ClientProtocolException e) {
|
|
108 // TODO Auto-generated catch block
|
|
109 e.printStackTrace();
|
|
110 return "";
|
|
111 } catch (IOException e) {
|
|
112 // TODO Auto-generated catch block
|
|
113 e.printStackTrace();
|
|
114 return "";
|
|
115 }
|
|
116 URI ret = req.getURI();
|
|
117
|
|
118 return target.toString()+ret.toString();
|
|
119 }
|
|
120
|
|
121
|
|
122 public String toString(){
|
|
123 String format="NAME: %s; archivePAth: %s; metaData: %s; fullText %s; url %s";
|
|
124
|
|
125 return String.format(format, name,archivePath,metaData,fullText,echoUrl);
|
|
126
|
|
127 }
|
|
128
|
|
129
|
|
130 /**
|
|
131 * Baue Metadatalink um, dass er auf das Servlet zeigt.
|
|
132 * @param ml
|
|
133 * @return
|
|
134 */
|
|
135 public String correctML(String ml) {
|
|
136 Pattern p = Pattern.compile("experimental/(.*)");
|
|
137 Matcher m = p.matcher(ml);
|
|
138 String pf;
|
|
139 if (m.find())
|
|
140 pf = "experimental/" + m.group(1);
|
|
141 else {
|
|
142 p = Pattern.compile("permanent/(.*)");
|
|
143 m = p.matcher(ml);
|
|
144 if (m.find())
|
|
145 pf = "permanent/" + m.group(1);
|
|
146 else
|
|
147 return ml;
|
|
148 }
|
|
149 return textServletUrl + pf;
|
|
150 }
|
|
151
|
|
152 static public String correct(String ml){
|
|
153 Pattern p = Pattern.compile("experimental/(.*)");
|
|
154 Matcher m = p.matcher(ml);
|
|
155 String pf;
|
|
156 if (m.find())
|
|
157 pf = "experimental/" + m.group(1);
|
|
158 else {
|
|
159 p = Pattern.compile("permanent/(.*)");
|
|
160 m = p.matcher(ml);
|
|
161 if (m.find())
|
|
162 pf = "permanent/" + m.group(1);
|
|
163 else
|
|
164 return ml;
|
|
165 }
|
|
166 return pf;
|
|
167 }
|
|
168
|
|
169
|
|
170 public String getImageFolderPath() {
|
|
171 DCTransformer trans = new DCTransformer(metaData);
|
|
172 String path = trans.getImagePathFromIndexMeta();
|
|
173 if (path==null || path.equals("")){
|
|
174 path=archivePath+"/pageimg";
|
|
175 } else {
|
|
176 path=archivePath+"/"+path;
|
|
177 }
|
|
178
|
|
179 String testPath=correct(path); // get rid of everything before eperimental or permanent
|
|
180 if(testPath(testPath))
|
|
181 return path;
|
|
182
|
|
183 return null;
|
|
184 }
|
|
185
|
|
186
|
|
187 private boolean testPath(String path) {
|
|
188 HttpClient client = new DefaultHttpClient();
|
|
189 HttpGet get;
|
|
190 try {
|
|
191 get = new HttpGet(dirInfoUrl+path);
|
|
192 } catch (RuntimeException e){
|
|
193 e.printStackTrace();
|
|
194 return false;
|
|
195 }
|
|
196
|
|
197 try {
|
|
198 HttpResponse response = client.execute(get);
|
|
199 String body = EScidocBasicHandler.convertStreamToString(response.getEntity().getContent());;
|
|
200
|
|
201 Pattern p = Pattern.compile("<dir>(.*)</dir>",Pattern.DOTALL);
|
|
202 Matcher m = p.matcher(body);
|
|
203 if (m.find()){ // dir body leer
|
|
204 if (m.group(1).equals(""))
|
|
205 return false;
|
|
206 else
|
|
207 return true;
|
|
208 }
|
|
209
|
|
210
|
|
211 } catch (IOException e) {
|
|
212 // TODO Auto-generated catch block
|
|
213 e.printStackTrace();
|
|
214 }
|
|
215 return false;
|
|
216 }
|
3
|
217
|
|
218
|
|
219 public String getIndexMetaMD5onServer() {
|
|
220 String digest="";
|
|
221 try {
|
|
222 URL url = new URL(metaData);
|
|
223 InputStream is = url.openStream();
|
|
224 MessageDigest md = MessageDigest.getInstance("MD5");
|
|
225 digest = getDigest(is, md, 2048);
|
|
226 } catch (MalformedURLException e) {
|
|
227 // TODO Auto-generated catch block
|
|
228 e.printStackTrace();
|
|
229 } catch (NoSuchAlgorithmException e) {
|
|
230 // TODO Auto-generated catch block
|
|
231 e.printStackTrace();
|
|
232 } catch (IOException e) {
|
|
233 // TODO Auto-generated catch block
|
|
234 e.printStackTrace();
|
|
235 }
|
|
236 return digest;
|
|
237 }
|
|
238
|
|
239 public static String getDigest(InputStream is, MessageDigest md, int byteArraySize)
|
|
240 throws NoSuchAlgorithmException, IOException {
|
|
241
|
|
242 md.reset();
|
|
243 byte[] bytes = new byte[byteArraySize];
|
|
244 int numBytes;
|
|
245 while ((numBytes = is.read(bytes)) != -1) {
|
|
246 md.update(bytes, 0, numBytes);
|
|
247 }
|
|
248 byte[] digest = md.digest();
|
|
249 String result = new String(Hex.encodeHex(digest));
|
|
250 return result;
|
|
251 }
|
|
252
|
|
253
|
|
254 public String getIndexMetaMD5stored() {
|
|
255 return indexMetaMD5stored;
|
|
256 }
|
|
257
|
|
258 public String getEScidocId() {
|
|
259 return eScidocId;
|
|
260 }
|
|
261
|
|
262
|
8
|
263 // schreibe die escidoc:id in die index.meta
|
|
264
|
|
265 public void writeEsciDocIDToIndexMeta(eSciDocXmlObject eSciDocXmlObject) {
|
|
266 String txt;
|
|
267 try {
|
|
268 txt = eSciDocXmlObject.getESciDocObjId();
|
|
269 writeEsciDocIDToIndexMeta(txt);
|
|
270 } catch (JDOMException e) {
|
|
271 // TODO Auto-generated catch block
|
|
272 e.printStackTrace();
|
|
273 }
|
|
274
|
3
|
275 }
|
8
|
276 public void writeEsciDocIDToIndexMeta(String objid) {
|
|
277 String driType="escidoc-test";
|
|
278 String txt;
|
|
279 Element result;
|
|
280 try {
|
|
281 txt = objid;
|
|
282
|
|
283 URL url = new URL(metaData);
|
|
284 InputStream is = url.openStream();
|
|
285 Document doc = new SAXBuilder().build(is);
|
|
286 XPath xp = XPath.newInstance("//meta/dri[@type=\""+driType+"\"]");
|
|
287 result = (Element)xp.selectSingleNode(doc);
|
|
288
|
|
289 if (result!=null){
|
|
290 result.setText(txt);
|
|
291 } else {
|
|
292 XPath xpMeta = XPath.newInstance("//meta");
|
|
293 Element metaTag = (Element)xpMeta.selectSingleNode(doc);
|
|
294
|
|
295 if (metaTag==null){
|
|
296 return;
|
|
297 } else {
|
|
298
|
|
299 Element dri = new Element("dri");
|
|
300 dri.setAttribute("type",driType);
|
|
301 dri.setText(txt);
|
|
302 metaTag.addContent(dri);
|
|
303 }
|
|
304 }
|
|
305
|
|
306 XMLOutputter xo = new XMLOutputter();
|
|
307
|
|
308
|
|
309
|
|
310 //String outpath ="/tmp/out/"+archivePath;
|
|
311 String outpath =archivePath;
|
|
312 //File parent = new File(outpath);
|
|
313 //parent.mkdirs();
|
|
314
|
|
315 File oldFile = new File(outpath+"/index.meta");
|
|
316 boolean f = oldFile.renameTo(new File(outpath+"/index.meta.old"));
|
|
317 if (!f){
|
|
318 Logger lg = Logger.getLogger("transformerLogger");
|
|
319 lg.error("cannot: write "+outpath+"/index.meta.old");
|
|
320 lg.error("cannot: but will proceed to write new index.meta");
|
|
321
|
|
322 }
|
|
323 FileOutputStream out = new FileOutputStream(outpath+"/index.meta");
|
|
324 xo.output(doc, out);
|
|
325
|
|
326 Logger lg = Logger.getLogger("transformerLogger");
|
|
327 lg.info("changed:"+outpath+" -- added escidoc: "+txt);
|
|
328 out.close();
|
|
329
|
|
330 } catch (MalformedURLException e) {
|
|
331 // TODO Auto-generated catch block
|
|
332 e.printStackTrace();
|
|
333 } catch (JDOMException e) {
|
|
334 // TODO Auto-generated catch block
|
|
335 e.printStackTrace();
|
|
336 } catch (IOException e) {
|
|
337 // TODO Auto-generated catch block
|
|
338 e.printStackTrace();
|
|
339 }
|
|
340
|
|
341
|
|
342 }
|
|
343
|
|
344 }
|