Mercurial > hg > eSciDocMPIWGServicesRestlet
comparison src/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/services/redirect/ComponentURLRedirector.java @ 0:1aac45997235
first ingest
author | dwinter |
---|---|
date | Wed, 01 Dec 2010 17:35:51 +0100 |
parents | |
children | 498b68667ff3 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:1aac45997235 |
---|---|
1 package de.mpiwg.itgroup.escidoc.MPIWGServices.Restlet.services.redirect; | |
2 | |
3 | |
4 import java.io.IOException; | |
5 import java.io.InputStream; | |
6 import java.util.Properties; | |
7 | |
8 import org.apache.http.HttpResponse; | |
9 import org.jdom.Attribute; | |
10 import org.jdom.Document; | |
11 import org.jdom.JDOMException; | |
12 import org.jdom.input.SAXBuilder; | |
13 import org.jdom.xpath.XPath; | |
14 import org.restlet.data.Status; | |
15 import org.restlet.representation.Representation; | |
16 import org.restlet.representation.StringRepresentation; | |
17 import org.restlet.resource.Get; | |
18 import org.restlet.resource.ServerResource; | |
19 | |
20 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler; | |
21 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools; | |
22 | |
23 | |
24 public class ComponentURLRedirector extends ServerResource{ | |
25 | |
26 private String eScidocServer; | |
27 private String eScidocPort; | |
28 private String content_cat; | |
29 private String eScidocUser; | |
30 private String eScidocUserPW; | |
31 | |
32 public ComponentURLRedirector(String content_cat) throws IOException{ | |
33 this.content_cat = content_cat; | |
34 InputStream is = getClass().getResourceAsStream("/de/mpiwg/itgroup/escidoc/MPIWGServices/Restlet/config/eScidocServer.config"); | |
35 Properties prop = new Properties(); | |
36 prop.load(is); | |
37 | |
38 eScidocServer= (String)prop.get("eScidocServer"); | |
39 eScidocPort=(String)prop.get("eScidocPort"); | |
40 eScidocUser = (String)prop.get("eScidocUser"); | |
41 eScidocUserPW=(String)prop.get("eScidocUserPW"); | |
42 | |
43 } | |
44 @Get("html") | |
45 public Representation getHtml() | |
46 { | |
47 String id = (String)getRequest().getAttributes().get("escidocId"); | |
48 EScidocBasicHandler handler = null; | |
49 if (getContext().getAttributes().containsKey("escidocHandler")){ | |
50 handler = (EScidocBasicHandler) getContext().getAttributes().get("escidocHandler"); | |
51 } | |
52 | |
53 if (handler==null){ | |
54 handler = new EScidocBasicHandler(eScidocServer, Integer.valueOf(eScidocPort),eScidocUser,eScidocUserPW); | |
55 } | |
56 | |
57 | |
58 HttpResponse resp; | |
59 try { | |
60 resp = handler.eScidocGet("/ir/item/"+id); | |
61 } catch (IOException e) { | |
62 setStatus(Status.SERVER_ERROR_INTERNAL); | |
63 return new StringRepresentation("<html><body>error1</body></html>"); | |
64 } | |
65 int code = resp.getStatusLine().getStatusCode(); | |
66 if (code!=200){ | |
67 setStatus(new Status(code)); | |
68 return new StringRepresentation("<html><body>error2</body></html>"); | |
69 | |
70 } | |
71 | |
72 Document doc; | |
73 String url; | |
74 try { | |
75 doc = new SAXBuilder().build(resp.getEntity().getContent()); | |
76 | |
77 | |
78 XPath xp= EScidocTools.getESciDocXpath("//escidocComponents:component[escidocComponents:properties/prop:content-category[text()='"+content_cat+"']]/escidocComponents:content/@xlink:href"); | |
79 Attribute item = (Attribute)xp.selectSingleNode(doc); | |
80 url = item.getValue(); | |
81 | |
82 } catch (Exception e) { | |
83 | |
84 if (content_cat.equals("display_url")) // wemm display und error dann versuche genrische anzeige der bilder | |
85 { | |
86 url="./images"; | |
87 StringRepresentation repr = new StringRepresentation(url); | |
88 getResponse().redirectPermanent(url); | |
89 return repr; | |
90 | |
91 } else { | |
92 setStatus(Status.SERVER_ERROR_INTERNAL); | |
93 | |
94 return new StringRepresentation("<html><body>error3a</body></html>"); | |
95 } | |
96 } | |
97 | |
98 | |
99 if (!url.startsWith("http")) { // relative url | |
100 url="http://"+eScidocServer+":"+eScidocPort+url; | |
101 } | |
102 | |
103 StringRepresentation repr = new StringRepresentation(url); | |
104 getResponse().redirectPermanent(url); | |
105 return repr; | |
106 } | |
107 } |