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 RepoProxyAction { |
---|
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 | * Overrides the default implementation for the case, when the Service shall act as "collector", |
---|
140 | * waiting for the final result (implemented in getSourcePz2 |
---|
141 | * The distinction is based on the command parameter: if it is given, the Class(Service) acts as a basic proxy |
---|
142 | * transparently passing the communication between client and target repository. |
---|
143 | */ |
---|
144 | @Override |
---|
145 | public InputStream getSourceStream() throws IOException, NoStylesheetException { |
---|
146 | if (getCommand() == null){ |
---|
147 | try { |
---|
148 | return getSourcePz2(); |
---|
149 | } catch (Exception e) { |
---|
150 | // TODO Auto-generated catch block |
---|
151 | e.printStackTrace(); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | return super.getSourceStream(); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * TODO: move to Utils |
---|
160 | * @param is |
---|
161 | * @return |
---|
162 | */ |
---|
163 | public Document getDocument(InputStream is){ |
---|
164 | Document doc = null; |
---|
165 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
166 | DocumentBuilder docBuilder; |
---|
167 | try { |
---|
168 | docBuilder = docFactory.newDocumentBuilder(); |
---|
169 | doc = docBuilder.parse(is); |
---|
170 | } catch (ParserConfigurationException e) { |
---|
171 | // TODO Auto-generated catch block |
---|
172 | e.printStackTrace(); |
---|
173 | } catch (SAXException e) { |
---|
174 | // TODO Auto-generated catch block |
---|
175 | e.printStackTrace(); |
---|
176 | } catch (IOException e) { |
---|
177 | // TODO Auto-generated catch block |
---|
178 | e.printStackTrace(); |
---|
179 | } |
---|
180 | return doc; |
---|
181 | } |
---|
182 | |
---|
183 | /** |
---|
184 | * TODO: move to Utils |
---|
185 | * @param is |
---|
186 | * @param expression |
---|
187 | * @return |
---|
188 | */ |
---|
189 | public String getDocumentData(InputStream is, String expression){ |
---|
190 | String sessionid = ""; |
---|
191 | |
---|
192 | XPathFactory factory = XPathFactory.newInstance(); |
---|
193 | XPath xpath = factory.newXPath(); |
---|
194 | XPathExpression expr; |
---|
195 | try { |
---|
196 | expr = xpath.compile(expression); |
---|
197 | sessionid = (String) expr.evaluate(getDocument(is), XPathConstants.STRING); |
---|
198 | } catch (XPathExpressionException e) { |
---|
199 | // TODO Auto-generated catch block |
---|
200 | e.printStackTrace(); |
---|
201 | } |
---|
202 | |
---|
203 | return sessionid; |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * This function wraps the session-based communication with a Pazpar2-target, waiting until all the responses were returned (or timeout occured) |
---|
209 | * returning the full result at the end. |
---|
210 | * @return returns the collected final result as a stream |
---|
211 | * @throws Exception |
---|
212 | */ |
---|
213 | public InputStream getSourcePz2() throws Exception { |
---|
214 | |
---|
215 | InputStream is = null; |
---|
216 | // TODO param settings |
---|
217 | //init |
---|
218 | setCommand("init"); |
---|
219 | String sessionID = getDocumentData(this.getSourceStream(), "//init/session"); |
---|
220 | // get result |
---|
221 | setCommand("search"); |
---|
222 | setSessionID(sessionID); |
---|
223 | setQuery(this.getSquery()); |
---|
224 | String ok = getDocumentData(this.getSourceStream(),"//search/status"); |
---|
225 | |
---|
226 | String activeclients = "1"; |
---|
227 | setCommand("show"); |
---|
228 | setSort("relevance"); |
---|
229 | setBlock("1"); |
---|
230 | //TODO timeout settings |
---|
231 | long startMillis = System.currentTimeMillis(); |
---|
232 | long timeout = 0; |
---|
233 | if (this.getWPOption("pazpartimeout") == null){ |
---|
234 | timeout = 1000; |
---|
235 | } else { |
---|
236 | timeout = Integer.parseInt(this.getWPOption("pazpartimeout")); |
---|
237 | } |
---|
238 | while (Integer.parseInt(activeclients) > 0){ |
---|
239 | //activeclients = getDocumentData(this.getSourceStream(), "//show/activeclients"); |
---|
240 | is = this.getSourceStream(); |
---|
241 | activeclients = getDocumentData(is, "//show/activeclients"); |
---|
242 | Thread.sleep(1000); |
---|
243 | Admin.notifyUser("ActiveClients: " + activeclients, Admin.DEBUG); |
---|
244 | if ((System.currentTimeMillis() - startMillis)/1000 > timeout) { |
---|
245 | break; |
---|
246 | } |
---|
247 | } |
---|
248 | is = this.getSourceStream(); |
---|
249 | return is; |
---|
250 | } |
---|
251 | |
---|
252 | } |
---|