1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.io.InputStream; |
---|
6 | import java.io.StringReader; |
---|
7 | import java.io.StringWriter; |
---|
8 | import java.net.MalformedURLException; |
---|
9 | import java.net.URISyntaxException; |
---|
10 | import java.net.URL; |
---|
11 | import java.util.HashMap; |
---|
12 | import java.io.ByteArrayInputStream; |
---|
13 | |
---|
14 | import javax.xml.parsers.DocumentBuilder; |
---|
15 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
16 | import javax.xml.parsers.ParserConfigurationException; |
---|
17 | import javax.xml.transform.OutputKeys; |
---|
18 | import javax.xml.transform.Transformer; |
---|
19 | import javax.xml.transform.TransformerException; |
---|
20 | import javax.xml.transform.TransformerFactory; |
---|
21 | import javax.xml.transform.dom.DOMSource; |
---|
22 | import javax.xml.transform.stream.StreamResult; |
---|
23 | import javax.xml.xpath.XPath; |
---|
24 | import javax.xml.xpath.XPathConstants; |
---|
25 | import javax.xml.xpath.XPathExpression; |
---|
26 | import javax.xml.xpath.XPathExpressionException; |
---|
27 | import javax.xml.xpath.XPathFactory; |
---|
28 | |
---|
29 | import org.w3c.dom.Document; |
---|
30 | import org.w3c.dom.Node; |
---|
31 | import org.w3c.dom.NodeList; |
---|
32 | import org.xml.sax.InputSource; |
---|
33 | import org.xml.sax.SAXException; |
---|
34 | |
---|
35 | import eu.clarin.cmdi.mdservice.model.Query; |
---|
36 | import eu.clarin.cmdi.mdservice.model.WorkspaceProfile; |
---|
37 | |
---|
38 | /** |
---|
39 | |
---|
40 | */ |
---|
41 | public class Pz2ProxyAction extends GenericProxyAction { |
---|
42 | |
---|
43 | private static final long serialVersionUID = 1L; |
---|
44 | |
---|
45 | private String proxy_key = "pazpar2"; |
---|
46 | |
---|
47 | private String command; |
---|
48 | private String query; |
---|
49 | private String sessionID; |
---|
50 | private String sort; |
---|
51 | private String block; |
---|
52 | |
---|
53 | public void setCommand(String command) { |
---|
54 | this.command = command; |
---|
55 | } |
---|
56 | public String getCommand() { |
---|
57 | return command; |
---|
58 | } |
---|
59 | public void setQuery(String query) { |
---|
60 | this.query = query; |
---|
61 | } |
---|
62 | public String getQuery() { |
---|
63 | return query; |
---|
64 | } |
---|
65 | public void setSessionID(String sessionID) { |
---|
66 | this.sessionID = sessionID; |
---|
67 | } |
---|
68 | public String getSessionID() { |
---|
69 | return sessionID; |
---|
70 | } |
---|
71 | |
---|
72 | public void setSort(String sort) { |
---|
73 | this.sort = sort; |
---|
74 | } |
---|
75 | public String getSort() { |
---|
76 | return sort; |
---|
77 | } |
---|
78 | public void setBlock(String block) { |
---|
79 | this.block = block; |
---|
80 | } |
---|
81 | public String getBlock() { |
---|
82 | return block; |
---|
83 | } |
---|
84 | |
---|
85 | @Override |
---|
86 | protected void initialize(){ |
---|
87 | super.initialize(); |
---|
88 | this.setActionkey("pazpar2"); |
---|
89 | } |
---|
90 | |
---|
91 | //TODO cache after analysis |
---|
92 | @Override |
---|
93 | public String getCache() { |
---|
94 | return Cache.SKIP; |
---|
95 | } |
---|
96 | |
---|
97 | public String createURLParams(){ |
---|
98 | String params = "?"; |
---|
99 | if (!(command == null)){ |
---|
100 | params = params + "command=" + command; |
---|
101 | } |
---|
102 | if (!(sessionID == null)){ |
---|
103 | params = params + "&session=" + sessionID; |
---|
104 | } |
---|
105 | if (!(query == null)){ |
---|
106 | params = params + "&query=" + query; |
---|
107 | } |
---|
108 | if (!(getStartItem() == null)){ |
---|
109 | params = params + "&start=" + getStartItem(); |
---|
110 | } |
---|
111 | if (!(getMaximumItems() == null)){ |
---|
112 | params = params + "&num=" + getMaximumItems(); |
---|
113 | } |
---|
114 | if (!(sort == null)){ |
---|
115 | params = params + "&sort=" + sort; |
---|
116 | if (!(block == null)){ |
---|
117 | params = params + ":" + block; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | return params; |
---|
122 | } |
---|
123 | @Override |
---|
124 | public String getBaseURI() { |
---|
125 | String uri = WorkspaceProfile.getRepositoryPath(getRepository()); |
---|
126 | return uri; |
---|
127 | } |
---|
128 | |
---|
129 | @Override |
---|
130 | public URL getTargetRequest() throws IOException { |
---|
131 | URL targetURL = null; |
---|
132 | |
---|
133 | targetURL =new URL( getBaseURL(), createURLParams() ); |
---|
134 | Admin.notifyUser("Pz2.targetURL:" + targetURL); |
---|
135 | return targetURL; |
---|
136 | } |
---|
137 | |
---|
138 | /* |
---|
139 | @Override |
---|
140 | public InputStream getSourceStream() throws IOException, NoStylesheetException { |
---|
141 | if (getCommand() == null){ |
---|
142 | try { |
---|
143 | return getSourcePz2(); |
---|
144 | } catch (Exception e) { |
---|
145 | // TODO Auto-generated catch block |
---|
146 | e.printStackTrace(); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | return super.getSourceStream(); |
---|
151 | } |
---|
152 | |
---|
153 | public Document getDocument(InputStream is){ |
---|
154 | Document doc = null; |
---|
155 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
156 | DocumentBuilder docBuilder; |
---|
157 | try { |
---|
158 | docBuilder = docFactory.newDocumentBuilder(); |
---|
159 | doc = docBuilder.parse(is); |
---|
160 | } catch (ParserConfigurationException e) { |
---|
161 | // TODO Auto-generated catch block |
---|
162 | e.printStackTrace(); |
---|
163 | } catch (SAXException e) { |
---|
164 | // TODO Auto-generated catch block |
---|
165 | e.printStackTrace(); |
---|
166 | } catch (IOException e) { |
---|
167 | // TODO Auto-generated catch block |
---|
168 | e.printStackTrace(); |
---|
169 | } |
---|
170 | return doc; |
---|
171 | } |
---|
172 | public String getDocumentData(InputStream is, String expression){ |
---|
173 | String sessionid = ""; |
---|
174 | |
---|
175 | XPathFactory factory = XPathFactory.newInstance(); |
---|
176 | XPath xpath = factory.newXPath(); |
---|
177 | XPathExpression expr; |
---|
178 | try { |
---|
179 | expr = xpath.compile(expression); |
---|
180 | sessionid = (String) expr.evaluate(getDocument(is), XPathConstants.STRING); |
---|
181 | } catch (XPathExpressionException e) { |
---|
182 | // TODO Auto-generated catch block |
---|
183 | e.printStackTrace(); |
---|
184 | } |
---|
185 | |
---|
186 | return sessionid; |
---|
187 | |
---|
188 | } |
---|
189 | */ |
---|
190 | /* |
---|
191 | public InputStream getSourcePz2() throws Exception { |
---|
192 | |
---|
193 | InputStream is = null; |
---|
194 | // TODO param settings |
---|
195 | //init |
---|
196 | setCommand("init"); |
---|
197 | String sessionID = getDocumentData(this.getSourceStream(), "//init/session"); |
---|
198 | // get result |
---|
199 | setCommand("search"); |
---|
200 | setSessionID(sessionID); |
---|
201 | setQuery(this.getSquery()); |
---|
202 | String ok = getDocumentData(this.getSourceStream(),"//search/status"); |
---|
203 | |
---|
204 | String activeclients = "1"; |
---|
205 | setCommand("show"); |
---|
206 | setSort("relevance"); |
---|
207 | setBlock("1"); |
---|
208 | //TODO timeout settings |
---|
209 | long startMillis = System.currentTimeMillis(); |
---|
210 | long timeout = 0; |
---|
211 | if (this.getWPOption("pazpartimeout") == null){ |
---|
212 | timeout = 1000; |
---|
213 | } else { |
---|
214 | timeout = Integer.parseInt(this.getWPOption("pazpartimeout")); |
---|
215 | } |
---|
216 | while (Integer.parseInt(activeclients) > 0){ |
---|
217 | //activeclients = getDocumentData(this.getSourceStream(), "//show/activeclients"); |
---|
218 | is = this.getSourceStream(); |
---|
219 | activeclients = getDocumentData(is, "//show/activeclients"); |
---|
220 | Thread.sleep(1000); |
---|
221 | Admin.notifyUser("ActiveClients: " + activeclients, Admin.DEBUG); |
---|
222 | if ((System.currentTimeMillis() - startMillis)/1000 > timeout) { |
---|
223 | break; |
---|
224 | } |
---|
225 | } |
---|
226 | is = this.getSourceStream(); |
---|
227 | return is; |
---|
228 | } |
---|
229 | */ |
---|
230 | |
---|
231 | } |
---|