Mercurial > hg > eSciDocMPIWGServicesRestlet
view src/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/services/redirect/DocuviewerRedirector.java @ 3:498b68667ff3
Neuer Mechanismus zum umleiten der Quellen.
author | dwinter |
---|---|
date | Fri, 20 Apr 2012 15:55:51 +0200 |
parents | |
children | 938e45fabd7c |
line wrap: on
line source
package de.mpiwg.itgroup.escidoc.MPIWGServices.Restlet.services.redirect; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.http.HttpResponse; import org.jdom.Attribute; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.xpath.XPath; import org.restlet.data.Status; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.resource.Get; import org.restlet.resource.ServerResource; import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler; import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools; import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHORessource; //http://127.0.0.1:8280/eScidocMPIWGServicesRestlet/docuview/escidoc:35464:2 public class DocuviewerRedirector extends ServerResource{ private String eScidocServer; private String eScidocPort; private String eScidocUser; private String eScidocUserPW; private String viewerWithIndexMetaFormatString;//="http://echo.mpiwg-berlin.mpg.de/ECHOdocuViewfull?url=%s"; // Formatstring fuer URLS, die die Daten anzeigen, falls ein IndexMeta vorhanden ist. 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. private Map<String,List<String>> viewers; public DocuviewerRedirector() throws IOException{ InputStream is = getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/config/eScidocServer.config"); Properties prop = new Properties(); prop.load(is); eScidocServer= (String)prop.get("eScidocServer"); eScidocPort=(String)prop.get("eScidocPort"); eScidocUser = (String)prop.get("eScidocUser"); eScidocUserPW=(String)prop.get("eScidocUserPW"); InputStream viewerConf = getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/config/viewer.config"); BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(viewerConf))); String strLine; viewers = new HashMap<String, List<String>>(); while ((strLine = br.readLine()) != null) { String[] splitted = strLine.split(","); List<String> list =new ArrayList<String>(); if (splitted[1].equals("")) list.add(null); // index.meta koennen nicht interpretiert werden. else list.add(splitted[1]); // add url for index.meta if (splitted.length>1) if (splitted[2].equals("")) list.add(null); else list.add(splitted[2]); // add url for images else list.add(null); // null wenn keiner konfiguriert wird. TODO: handle this viewers.put(splitted[0], list); } } @Get("html") public Representation getHtml() { String id = (String)getRequest().getAttributes().get("escidocId"); String flavour = (String)getRequest().getAttributes().get("flavour"); if (!viewers.containsKey(flavour)){ setStatus(Status.CLIENT_ERROR_NOT_FOUND); return new StringRepresentation("no viewer configured for flavour:"+flavour+"! Check /de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/config/viewer.config"); } List<String> formats = viewers.get(flavour); viewerWithIndexMetaFormatString = formats.get(0); viewerWithImagePathFormatString = formats.get(1); EScidocBasicHandler handler = null; if (getContext().getAttributes().containsKey("escidocHandler")){ handler = (EScidocBasicHandler) getContext().getAttributes().get("escidocHandler"); } if (handler==null){ handler = new EScidocBasicHandler(eScidocServer, Integer.valueOf(eScidocPort),eScidocUser,eScidocUserPW); } HttpResponse resp; try { resp = handler.eScidocGet("/ir/item/"+id); } catch (IOException e) { setStatus(Status.SERVER_ERROR_INTERNAL); return new StringRepresentation("<html><body>error1</body></html>"); } int code = resp.getStatusLine().getStatusCode(); if (code!=200){ setStatus(new Status(code)); return new StringRepresentation("<html><body>error2</body></html>"); } Document doc; String url; try { doc = new SAXBuilder().build(resp.getEntity().getContent()); //XPath xp= EScidocTools.getESciDocXpath("//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='index_meta']]/escidocComponents:content/@xlink:href"); XPath xp= EScidocTools.getESciDocXpath("//metadataRecords:md-record[@xlink:title='mpiwg-admin']/mpiwg:admin/mpiwg:orig-ref/@xlink:href"); Attribute item = (Attribute)xp.selectSingleNode(doc); url = item.getValue(); } catch (Exception e) { setStatus(Status.SERVER_ERROR_INTERNAL); return new StringRepresentation("<html><body>error3a</body></html>"); } if (!url.startsWith("http")) { // relative url url="http://"+eScidocServer+":"+eScidocPort+url; } String viewerUrl; if (checkIndexMeta(url) && (viewerWithIndexMetaFormatString!=null)){ // index meta enthaelt texttool und viewer kann index meta interpretieren. viewerUrl=String.format(viewerWithIndexMetaFormatString, url); } else { // andernfalls hole den pfad auf die bilder try { XPath xp= EScidocTools.getESciDocXpath("//metadataRecords:md-record[@xlink:title='mpiwg-admin']/mpiwg:admin/mpiwg:imageFolder"); Element item = (Element)xp.selectSingleNode(doc); String path = item.getTextTrim(); path= ECHORessource.correct(path); // nur pfad nach online viewerUrl=String.format(viewerWithImagePathFormatString, path); } catch (Exception e) { setStatus(Status.SERVER_ERROR_INTERNAL); return new StringRepresentation("<html><body>error3</body></html>"); } } StringRepresentation repr = new StringRepresentation(viewerUrl); getResponse().redirectPermanent(viewerUrl); return repr; } private boolean checkIndexMeta(String url) { try { Document doc = new SAXBuilder().build(new URL(url)); XPath xp = XPath.newInstance("//texttool"); Object item = xp.selectSingleNode(doc); if (item!=null) return true; } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } }