0
|
1 package de.mpiwg.itgroup.escidoc.MPIWGServices.Restlet.services.redirect;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.io.InputStream;
|
|
5 import java.util.Properties;
|
|
6
|
|
7 import javax.xml.transform.Transformer;
|
|
8 import javax.xml.transform.TransformerConfigurationException;
|
|
9 import javax.xml.transform.TransformerException;
|
|
10 import javax.xml.transform.TransformerFactory;
|
|
11 import javax.xml.transform.TransformerFactoryConfigurationError;
|
|
12 import javax.xml.transform.stream.StreamSource;
|
|
13
|
|
14 import org.apache.http.HttpResponse;
|
|
15 import org.jdom.Attribute;
|
|
16 import org.jdom.Document;
|
|
17 import org.jdom.Element;
|
|
18 import org.jdom.JDOMException;
|
|
19 import org.jdom.input.SAXBuilder;
|
|
20 import org.jdom.output.XMLOutputter;
|
|
21 import org.jdom.transform.JDOMResult;
|
|
22 import org.jdom.transform.JDOMSource;
|
|
23 import org.jdom.xpath.XPath;
|
|
24 import org.restlet.data.Form;
|
|
25 import org.restlet.data.MediaType;
|
|
26 import org.restlet.data.Status;
|
|
27 import org.restlet.representation.Representation;
|
|
28 import org.restlet.representation.StringRepresentation;
|
|
29 import org.restlet.resource.Get;
|
|
30 import org.restlet.resource.ServerResource;
|
|
31
|
|
32 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler;
|
|
33 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools;
|
|
34 import de.mpiwg.itgroup.eSciDoc.echoObjects.ECHORessource;
|
|
35
|
|
36
|
|
37 public class OrigRefRedirector extends ServerResource{
|
|
38
|
|
39 private String eScidocServer;
|
|
40 private String eScidocPort;
|
|
41
|
|
42 private String eScidocUser;
|
|
43 private String eScidocUserPW;
|
|
44 public OrigRefRedirector() throws IOException{
|
|
45 InputStream is = getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/config/eScidocServer.config");
|
|
46 Properties prop = new Properties();
|
|
47 prop.load(is);
|
|
48 eScidocServer= (String)prop.get("eScidocServer");
|
|
49 eScidocPort=(String)prop.get("eScidocPort");
|
|
50 eScidocUser=(String)prop.get("eScidocUser");
|
|
51 eScidocUserPW=(String)prop.get("eScidocUserPW");
|
|
52
|
|
53 }
|
|
54 @Get("html")
|
|
55 public Representation getHtml() throws JDOMException, IOException, TransformerFactoryConfigurationError, TransformerException
|
|
56 {
|
|
57
|
|
58 Form form = getRequest().getResourceRef().getQueryAsForm();
|
|
59
|
|
60 String mode=form.getValues("mode"); //xml or html default is html
|
2
|
61 if(mode==null || mode.equals(""))
|
0
|
62 mode="xml";
|
|
63
|
|
64 String id = (String)getRequest().getAttributes().get("escidocId");
|
|
65 EScidocBasicHandler handler = null;
|
|
66 if (getContext().getAttributes().containsKey("escidocHandler")){
|
|
67 handler = (EScidocBasicHandler) getContext().getAttributes().get("escidocHandler");
|
|
68 }
|
|
69
|
|
70 if (handler==null){
|
|
71 handler = new EScidocBasicHandler(eScidocServer, Integer.valueOf(eScidocPort),eScidocUser,eScidocUserPW);
|
|
72 }
|
|
73
|
|
74
|
|
75 HttpResponse resp;
|
|
76 try {
|
|
77 resp = handler.eScidocGet("/ir/item/"+id);
|
|
78 } catch (IOException e) {
|
|
79 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
80 return new StringRepresentation("<html><body>error1</body></html>");
|
|
81 }
|
|
82 int code = resp.getStatusLine().getStatusCode();
|
|
83 if (code!=200){
|
|
84 setStatus(new Status(code));
|
|
85 return new StringRepresentation("<html><body>error2</body></html>");
|
|
86
|
|
87 }
|
|
88
|
|
89 Document doc;
|
|
90 String path;
|
|
91 try {
|
|
92 doc = new SAXBuilder().build(resp.getEntity().getContent());
|
|
93
|
|
94
|
|
95 XPath xp= EScidocTools.getESciDocXpath("//metadataRecords:md-record[@xlink:title='mpiwg-admin']/mpiwg:admin/mpiwg:orig-ref/@xlink:href");
|
|
96 Attribute item = (Attribute)xp.selectSingleNode(doc);
|
|
97 path = item.getValue();
|
|
98 } catch (Exception e) {
|
|
99 setStatus(Status.SERVER_ERROR_INTERNAL);
|
|
100 return new StringRepresentation("<html><body>error3</body></html>");
|
|
101 }
|
|
102
|
|
103 if(mode.equals("xml"))
|
|
104 {
|
|
105 StringRepresentation repr = new StringRepresentation(path);
|
|
106 getResponse().redirectPermanent(path);
|
|
107 return repr;
|
|
108 } else if (mode.equals("html")){
|
|
109 Document indexMeta = new SAXBuilder().build(path);
|
|
110 InputStream xslt= getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/staticPages/formatIndexMeta.xsl");
|
|
111 //Document styleDoc = new SAXBuilder().build(xslt);
|
|
112
|
|
113 Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xslt));
|
|
114 JDOMSource inxml = new JDOMSource(indexMeta);
|
|
115 JDOMResult out = new JDOMResult();
|
|
116 transformer.transform(inxml, out);
|
|
117 //logger.debug(printXML(doc));
|
|
118
|
|
119 Document docresult = out.getDocument();
|
|
120 return new StringRepresentation(printXML(docresult),MediaType.TEXT_HTML);
|
|
121 } else {
|
|
122 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
123 return new StringRepresentation("<html><body>wrong value for parameter mode, only html or xml allowed.</body></html>");
|
|
124 }
|
|
125 }
|
|
126
|
|
127 public String printXML(Document dom) throws IOException{
|
|
128 XMLOutputter out = new XMLOutputter();
|
|
129
|
|
130 String string = out.outputString(dom);
|
|
131 return string;
|
|
132 }
|
|
133 }
|