3
|
1 package de.mpiwg.itgroup.escidoc.MPIWGServices.Restlet.services.redirect;
|
|
2
|
|
3
|
|
4 import java.io.BufferedInputStream;
|
|
5 import java.io.BufferedReader;
|
|
6 import java.io.DataInputStream;
|
|
7 import java.io.IOException;
|
|
8 import java.io.InputStream;
|
|
9 import java.io.InputStreamReader;
|
|
10 import java.net.MalformedURLException;
|
|
11 import java.net.URL;
|
|
12 import java.util.ArrayList;
|
|
13 import java.util.HashMap;
|
|
14 import java.util.List;
|
|
15 import java.util.Map;
|
|
16 import java.util.Properties;
|
|
17
|
|
18 import org.apache.http.HttpResponse;
|
|
19 import org.jdom.Attribute;
|
|
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.xpath.XPath;
|
|
25 import org.restlet.data.Status;
|
|
26 import org.restlet.representation.Representation;
|
|
27 import org.restlet.representation.StringRepresentation;
|
|
28 import org.restlet.resource.Get;
|
|
29 import org.restlet.resource.ServerResource;
|
|
30
|
|
31 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler;
|
|
32 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools;
|
|
33 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHORessource;
|
|
34
|
|
35 //http://127.0.0.1:8280/eScidocMPIWGServicesRestlet/docuview/escidoc:35464:2
|
|
36
|
|
37 public class DocuviewerRedirector extends ServerResource{
|
|
38
|
|
39 private String eScidocServer;
|
|
40 private String eScidocPort;
|
|
41 private String eScidocUser;
|
|
42 private String eScidocUserPW;
|
|
43
|
|
44 private String viewerWithIndexMetaFormatString;//="http://echo.mpiwg-berlin.mpg.de/ECHOdocuViewfull?url=%s"; // Formatstring fuer URLS, die die Daten anzeigen, falls ein IndexMeta vorhanden ist.
|
|
45 private String viewerWithImagePathFormatString;//="http://echo.mpiwg-berlin.mpg.de/ECHOdocuViewfull?mode=imagepath&url=%s&viewMode=images"; // Formatstring fuer URLS, die die Daten anzeigen, falls ein ImagePath vorhanden ist.
|
|
46
|
|
47 private Map<String,List<String>> viewers;
|
|
48
|
|
49 public DocuviewerRedirector() throws IOException{
|
|
50 InputStream is = getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/config/eScidocServer.config");
|
|
51 Properties prop = new Properties();
|
|
52 prop.load(is);
|
|
53
|
|
54 eScidocServer= (String)prop.get("eScidocServer");
|
|
55 eScidocPort=(String)prop.get("eScidocPort");
|
|
56 eScidocUser = (String)prop.get("eScidocUser");
|
|
57 eScidocUserPW=(String)prop.get("eScidocUserPW");
|
|
58
|
|
59 InputStream viewerConf = getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/config/viewer.config");
|
|
60
|
|
61 BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(viewerConf)));
|
|
62
|
|
63 String strLine;
|
|
64 viewers = new HashMap<String, List<String>>();
|
|
65
|
|
66 while ((strLine = br.readLine()) != null) {
|
|
67 String[] splitted = strLine.split(",");
|
|
68
|
|
69 List<String> list =new ArrayList<String>();
|
|
70
|
|
71 if (splitted[1].equals(""))
|
|
72 list.add(null); // index.meta koennen nicht interpretiert werden.
|
|
73 else
|
|
74 list.add(splitted[1]); // add url for index.meta
|
|
75
|
|
76
|
|
77 if (splitted.length>1)
|
|
78 if (splitted[2].equals(""))
|
|
79 list.add(null);
|
|
80 else
|
|
81 list.add(splitted[2]); // add url for images
|
|
82 else
|
|
83 list.add(null); // null wenn keiner konfiguriert wird. TODO: handle this
|
|
84
|
|
85 viewers.put(splitted[0], list);
|
|
86 }
|
|
87
|
|
88
|
|
89 }
|
|
90 @Get("html")
|
|
91 public Representation getHtml()
|
|
92 {
|
|
93 String id = (String)getRequest().getAttributes().get("escidocId");
|
|
94 String flavour = (String)getRequest().getAttributes().get("flavour");
|
|
95
|
|
96 if (!viewers.containsKey(flavour)){
|
|
97 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
|
98 return new StringRepresentation("no viewer configured for flavour:"+flavour+"! Check /de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/config/viewer.config");
|
|
99 }
|
|
100
|
|
101 List<String> formats = viewers.get(flavour);
|
|
102
|
|
103 viewerWithIndexMetaFormatString = formats.get(0);
|
|
104 viewerWithImagePathFormatString = formats.get(1);
|
|
105
|
|
106 EScidocBasicHandler handler = null;
|
|
107 if (getContext().getAttributes().containsKey("escidocHandler")){
|
|
108 handler = (EScidocBasicHandler) getContext().getAttributes().get("escidocHandler");
|
|
109 }
|
|
110
|
|
111 if (handler==null){
|
|
112 handler = new EScidocBasicHandler(eScidocServer, Integer.valueOf(eScidocPort),eScidocUser,eScidocUserPW);
|
|
113 }
|
|
114
|
|
115
|
|
116 HttpResponse resp;
|
|
117 try {
|
|
118 resp = handler.eScidocGet("/ir/item/"+id);
|
|
119 } catch (IOException e) {
|
|
120 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
121 return new StringRepresentation("<html><body>error1</body></html>");
|
|
122 }
|
|
123 int code = resp.getStatusLine().getStatusCode();
|
|
124 if (code!=200){
|
|
125 setStatus(new Status(code));
|
|
126 return new StringRepresentation("<html><body>error2</body></html>");
|
|
127
|
|
128 }
|
|
129
|
|
130 Document doc;
|
|
131 String url;
|
|
132 try {
|
|
133 doc = new SAXBuilder().build(resp.getEntity().getContent());
|
|
134
|
|
135
|
|
136 //XPath xp= EScidocTools.getESciDocXpath("//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='index_meta']]/escidocComponents:content/@xlink:href");
|
|
137
|
|
138 XPath xp= EScidocTools.getESciDocXpath("//metadataRecords:md-record[@xlink:title='mpiwg-admin']/mpiwg:admin/mpiwg:orig-ref/@xlink:href");
|
|
139 Attribute item = (Attribute)xp.selectSingleNode(doc);
|
|
140 url = item.getValue();
|
|
141
|
|
142 } catch (Exception e) {
|
|
143 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
144 return new StringRepresentation("<html><body>error3a</body></html>");
|
|
145
|
|
146 }
|
|
147
|
|
148
|
|
149 if (!url.startsWith("http")) { // relative url
|
|
150 url="http://"+eScidocServer+":"+eScidocPort+url;
|
|
151 }
|
|
152
|
|
153
|
|
154
|
|
155 String viewerUrl;
|
|
156 if (checkIndexMeta(url) && (viewerWithIndexMetaFormatString!=null)){ // index meta enthaelt texttool und viewer kann index meta interpretieren.
|
|
157 viewerUrl=String.format(viewerWithIndexMetaFormatString, url);
|
|
158 } else { // andernfalls hole den pfad auf die bilder
|
|
159
|
|
160 try {
|
|
161 XPath xp= EScidocTools.getESciDocXpath("//metadataRecords:md-record[@xlink:title='mpiwg-admin']/mpiwg:admin/mpiwg:imageFolder");
|
|
162 Element item = (Element)xp.selectSingleNode(doc);
|
|
163 String path = item.getTextTrim();
|
|
164 path= ECHORessource.correct(path); // nur pfad nach online
|
|
165 viewerUrl=String.format(viewerWithImagePathFormatString, path);
|
|
166 } catch (Exception e) {
|
|
167 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
168 return new StringRepresentation("<html><body>error3</body></html>");
|
|
169 }
|
|
170
|
|
171
|
|
172
|
|
173 }
|
|
174 StringRepresentation repr = new StringRepresentation(viewerUrl);
|
|
175 getResponse().redirectPermanent(viewerUrl);
|
|
176 return repr;
|
|
177 }
|
|
178
|
|
179
|
|
180
|
|
181 private boolean checkIndexMeta(String url) {
|
|
182 try {
|
|
183 Document doc = new SAXBuilder().build(new URL(url));
|
|
184 XPath xp = XPath.newInstance("//texttool");
|
|
185
|
|
186 Object item = xp.selectSingleNode(doc);
|
|
187 if (item!=null)
|
|
188 return true;
|
|
189
|
|
190 } catch (MalformedURLException e) {
|
|
191 // TODO Auto-generated catch block
|
|
192 e.printStackTrace();
|
|
193 } catch (JDOMException e) {
|
|
194 // TODO Auto-generated catch block
|
|
195 e.printStackTrace();
|
|
196 } catch (IOException e) {
|
|
197 // TODO Auto-generated catch block
|
|
198 e.printStackTrace();
|
|
199 }
|
|
200
|
|
201 return false;
|
|
202 }
|
|
203
|
|
204
|
|
205
|
|
206 }
|