1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.BufferedInputStream; |
---|
4 | import java.io.BufferedReader; |
---|
5 | import java.io.ByteArrayInputStream; |
---|
6 | import java.io.File; |
---|
7 | import java.io.FileInputStream; |
---|
8 | import java.io.IOException; |
---|
9 | import java.io.InputStream; |
---|
10 | import java.io.InputStreamReader; |
---|
11 | import java.io.StringWriter; |
---|
12 | import java.io.UnsupportedEncodingException; |
---|
13 | import java.net.MalformedURLException; |
---|
14 | import java.net.URISyntaxException; |
---|
15 | import java.net.URL; |
---|
16 | import java.util.HashMap; |
---|
17 | |
---|
18 | import javax.servlet.http.HttpServletRequest; |
---|
19 | import javax.servlet.http.HttpSession; |
---|
20 | import javax.xml.parsers.DocumentBuilder; |
---|
21 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
22 | import javax.xml.parsers.ParserConfigurationException; |
---|
23 | import javax.xml.transform.TransformerConfigurationException; |
---|
24 | import javax.xml.transform.TransformerException; |
---|
25 | import javax.xml.transform.TransformerFactory; |
---|
26 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
27 | import javax.xml.transform.dom.DOMSource; |
---|
28 | import javax.xml.transform.stream.StreamResult; |
---|
29 | |
---|
30 | import net.sf.json.JSON; |
---|
31 | import net.sf.json.JSONArray; |
---|
32 | import net.sf.json.JSONObject; |
---|
33 | import net.sf.json.JSONSerializer; |
---|
34 | |
---|
35 | |
---|
36 | import org.apache.commons.io.IOUtils; |
---|
37 | import org.apache.commons.lang.StringEscapeUtils; |
---|
38 | import org.apache.struts2.interceptor.ServletRequestAware; |
---|
39 | import org.w3c.dom.Document; |
---|
40 | import org.w3c.dom.Element; |
---|
41 | import org.w3c.dom.Node; |
---|
42 | import org.xml.sax.InputSource; |
---|
43 | import org.xml.sax.SAXException; |
---|
44 | |
---|
45 | import com.opensymphony.xwork2.ActionSupport; |
---|
46 | |
---|
47 | import eu.clarin.cmdi.mdservice.model.Diagnostic; |
---|
48 | import eu.clarin.cmdi.mdservice.model.Diagnostics; |
---|
49 | |
---|
50 | |
---|
51 | public class SRUProxyAction extends ActionSupport |
---|
52 | implements ServletRequestAware |
---|
53 | { |
---|
54 | |
---|
55 | private static final long serialVersionUID = 1L; |
---|
56 | |
---|
57 | private Diagnostics diagnostics; |
---|
58 | |
---|
59 | /** |
---|
60 | * Properties to be filled by Struts with according request-parameters |
---|
61 | */ |
---|
62 | private String q; |
---|
63 | private String version; |
---|
64 | private String operation; |
---|
65 | private String query; |
---|
66 | private int startRecord; |
---|
67 | private int maximumRecords; |
---|
68 | private String recordPacking; |
---|
69 | private String recordSchema; |
---|
70 | private String recordXPath; |
---|
71 | private int resultSetTTL; |
---|
72 | private String sortKeys; |
---|
73 | private String stylesheet; |
---|
74 | private String x_cmd_repository; |
---|
75 | private String x_cmd_collections; |
---|
76 | |
---|
77 | private MDRepoProxyAction mdrepoproxyaction; |
---|
78 | private InputStream resultStream; |
---|
79 | private HttpServletRequest request; |
---|
80 | |
---|
81 | private final static HashMap<String,String> hash_operations = new HashMap<String,String>(); |
---|
82 | static |
---|
83 | { |
---|
84 | hash_operations.put("searchRetrieve", "recordset"); |
---|
85 | } |
---|
86 | |
---|
87 | public SRUProxyAction(){ |
---|
88 | super(); |
---|
89 | initialize(); |
---|
90 | } |
---|
91 | |
---|
92 | protected void initialize(){ |
---|
93 | //repository = 1; |
---|
94 | } |
---|
95 | |
---|
96 | @Override |
---|
97 | public void setServletRequest(HttpServletRequest arg0) { |
---|
98 | request = arg0; |
---|
99 | } |
---|
100 | |
---|
101 | public HttpServletRequest getServletRequest() { |
---|
102 | return request; |
---|
103 | } |
---|
104 | |
---|
105 | public Diagnostics getDiagnostics(){ |
---|
106 | return this.diagnostics; |
---|
107 | } |
---|
108 | |
---|
109 | public void setDiagnostics(Diagnostics diagnostics){ |
---|
110 | this.diagnostics = diagnostics; |
---|
111 | } |
---|
112 | |
---|
113 | |
---|
114 | public String getQ() { |
---|
115 | return q; |
---|
116 | } |
---|
117 | |
---|
118 | public void setQ(String q) { |
---|
119 | this.q = q; |
---|
120 | } |
---|
121 | |
---|
122 | public InputStream getResultStream() { |
---|
123 | return resultStream; |
---|
124 | } |
---|
125 | public void setResultStream(InputStream _resultStream){ |
---|
126 | resultStream = _resultStream; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | public String getVersion() { |
---|
131 | return version; |
---|
132 | } |
---|
133 | |
---|
134 | public void setVersion(String version) { |
---|
135 | this.version = version; |
---|
136 | } |
---|
137 | |
---|
138 | public String getOperation() { |
---|
139 | return operation; |
---|
140 | } |
---|
141 | |
---|
142 | public void setOperation(String operation) { |
---|
143 | this.operation = operation; |
---|
144 | } |
---|
145 | |
---|
146 | public String getQuery() { |
---|
147 | return query; |
---|
148 | } |
---|
149 | |
---|
150 | public void setQuery(String query) { |
---|
151 | this.query = query; |
---|
152 | } |
---|
153 | |
---|
154 | public int getStartRecord() { |
---|
155 | return startRecord; |
---|
156 | } |
---|
157 | |
---|
158 | public void setStartRecord(int startRecord) { |
---|
159 | this.startRecord = startRecord; |
---|
160 | } |
---|
161 | public int getMaximumRecords() { |
---|
162 | return maximumRecords; |
---|
163 | } |
---|
164 | |
---|
165 | public void setMaximumRecords(int maximumRecords) { |
---|
166 | this.maximumRecords = maximumRecords; |
---|
167 | } |
---|
168 | |
---|
169 | public String getRecordPacking() { |
---|
170 | return recordPacking; |
---|
171 | } |
---|
172 | |
---|
173 | public void setRecordPacking(String recordPacking) { |
---|
174 | this.recordPacking = recordPacking; |
---|
175 | } |
---|
176 | |
---|
177 | public String getRecordSchema() { |
---|
178 | return recordSchema; |
---|
179 | } |
---|
180 | |
---|
181 | public void setRecordSchema(String recordSchema) { |
---|
182 | this.recordSchema = recordSchema; |
---|
183 | } |
---|
184 | |
---|
185 | public String getRecordXPath() { |
---|
186 | return recordXPath; |
---|
187 | } |
---|
188 | |
---|
189 | public void setRecordXPath(String recordXPath) { |
---|
190 | this.recordXPath = recordXPath; |
---|
191 | } |
---|
192 | |
---|
193 | public int getResultSetTTL() { |
---|
194 | return resultSetTTL; |
---|
195 | } |
---|
196 | |
---|
197 | public void setResultSetTTL(int resultSetTTL) { |
---|
198 | this.resultSetTTL = resultSetTTL; |
---|
199 | } |
---|
200 | |
---|
201 | public String getSortKeys() { |
---|
202 | return sortKeys; |
---|
203 | } |
---|
204 | |
---|
205 | public void setSortKeys(String sortKeys) { |
---|
206 | this.sortKeys = sortKeys; |
---|
207 | } |
---|
208 | |
---|
209 | public String getStylesheet() { |
---|
210 | return stylesheet; |
---|
211 | } |
---|
212 | |
---|
213 | public void setStylesheet(String stylesheet) { |
---|
214 | this.stylesheet = stylesheet; |
---|
215 | } |
---|
216 | |
---|
217 | public String getX_cmd_repository(){ |
---|
218 | return x_cmd_repository; |
---|
219 | } |
---|
220 | |
---|
221 | public void setX_cmd_repository(String x_cmd_repository){ |
---|
222 | this.x_cmd_repository = x_cmd_repository; |
---|
223 | } |
---|
224 | |
---|
225 | public String getX_cmd_collections(){ |
---|
226 | return x_cmd_collections; |
---|
227 | } |
---|
228 | |
---|
229 | public void setX_cmd_collections(String x_cmd_collections){ |
---|
230 | this.x_cmd_collections = x_cmd_collections; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | public String getDiagnosticStreamName() throws TransformerConfigurationException, UnsupportedEncodingException, ParserConfigurationException, TransformerException, TransformerFactoryConfigurationError{ |
---|
235 | resultStream = this.Diagnostics().buildXMLStream(); |
---|
236 | return "resultStream"; |
---|
237 | } |
---|
238 | /** |
---|
239 | * primitive identification of the target-proxy |
---|
240 | * base for finding the right base_url in the props |
---|
241 | * subclass has to override with its specific proxykey |
---|
242 | * @return |
---|
243 | */ |
---|
244 | public String getProxyKey() { |
---|
245 | return ""; |
---|
246 | } |
---|
247 | |
---|
248 | public Diagnostics Diagnostics(){ |
---|
249 | if (this.diagnostics == null){ |
---|
250 | this.diagnostics = new Diagnostics(); |
---|
251 | } |
---|
252 | return this.diagnostics; |
---|
253 | } |
---|
254 | |
---|
255 | public void prepare(){ |
---|
256 | // special handling of parameter setting, x-cmd-... not alowed in java |
---|
257 | this.setX_cmd_repository(getServletRequest().getParameter("x-cmd-repository")); |
---|
258 | this.setX_cmd_collections(getServletRequest().getParameter("x-cmd-collections")); |
---|
259 | |
---|
260 | mdrepoproxyaction = new MDRepoProxyAction(); |
---|
261 | mdrepoproxyaction.setDiagnostics(diagnostics); |
---|
262 | |
---|
263 | //operation |
---|
264 | if (this.operation == null) { |
---|
265 | this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "operation"); |
---|
266 | //return -1; |
---|
267 | } |
---|
268 | if (!operation.equals("searchRetrieve")) { |
---|
269 | this.Diagnostics().Add(Diagnostic.UNSUPPOERTED_OPERATION, "Only searchRetrieve supported."); |
---|
270 | //return -1; |
---|
271 | } |
---|
272 | mdrepoproxyaction.setActionkey(hash_operations.get(this.getOperation())); |
---|
273 | //version |
---|
274 | if (this.version == null) { |
---|
275 | this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "version"); |
---|
276 | //return -1; |
---|
277 | } |
---|
278 | if (!version.equals("1.2")) { |
---|
279 | this.Diagnostics().Add(Diagnostic.UNSUPPORTED_VERSION, "Version 1.2 supported."); |
---|
280 | //return -1; |
---|
281 | } |
---|
282 | //query |
---|
283 | mdrepoproxyaction.setSquery(""); |
---|
284 | if (this.query == null) { |
---|
285 | this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "query"); |
---|
286 | //return -1; |
---|
287 | } |
---|
288 | mdrepoproxyaction.setQ(getQuery()); |
---|
289 | |
---|
290 | mdrepoproxyaction.setFormat("xml"); |
---|
291 | |
---|
292 | if ((this.startRecord < 0 ) ||(getServletRequest().getParameter("startRecord").equals("0"))) { |
---|
293 | this.Diagnostics().Add(new Diagnostic(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "startRecord must by grater than 0."));//,Diagnostic.NONFATALNONSURORGATE)); |
---|
294 | //mdrepoproxyaction.setStartItem("1"); |
---|
295 | } else { |
---|
296 | mdrepoproxyaction.setStartItem(Integer.toString(getStartRecord())); |
---|
297 | } |
---|
298 | |
---|
299 | if ((this.maximumRecords < 0 ) ||(getServletRequest().getParameter("maximumRecords").equals("0"))){ |
---|
300 | this.Diagnostics().Add(new Diagnostic(Diagnostic.UNSUPPORTED_PARAMETERVALUE, |
---|
301 | "maximumRecords must by grater than 0.")); |
---|
302 | } else { |
---|
303 | mdrepoproxyaction.setMaximumItems(Integer.toString(this.getMaximumRecords())); |
---|
304 | } |
---|
305 | |
---|
306 | if (this.getX_cmd_repository() != null ) { |
---|
307 | mdrepoproxyaction.setRepository(this.getX_cmd_repository()); |
---|
308 | //TODO\ |
---|
309 | if (mdrepoproxyaction.getBaseURI() == null) this.Diagnostics().Add(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "Unknown repository." + getX_cmd_repository()); |
---|
310 | } |
---|
311 | if (this.getX_cmd_collections() != null ) { |
---|
312 | mdrepoproxyaction.setCollection(this.getX_cmd_collections()); |
---|
313 | } |
---|
314 | |
---|
315 | //return 1; |
---|
316 | } |
---|
317 | public static String inputStreamAsString(InputStream stream) |
---|
318 | throws IOException { |
---|
319 | BufferedReader br = new BufferedReader(new InputStreamReader(stream)); |
---|
320 | StringBuilder sb = new StringBuilder(); |
---|
321 | String line = null; |
---|
322 | |
---|
323 | while ((line = br.readLine()) != null) { |
---|
324 | sb.append(line + "\n"); |
---|
325 | } |
---|
326 | |
---|
327 | br.close(); |
---|
328 | return sb.toString(); |
---|
329 | } |
---|
330 | |
---|
331 | public static Document newDocumentFromInputStream(InputStream in) { |
---|
332 | DocumentBuilderFactory factory = null; |
---|
333 | DocumentBuilder builder = null; |
---|
334 | Document ret = null; |
---|
335 | |
---|
336 | try { |
---|
337 | factory = DocumentBuilderFactory.newInstance(); |
---|
338 | builder = factory.newDocumentBuilder(); |
---|
339 | } catch (ParserConfigurationException e) { |
---|
340 | e.printStackTrace(); |
---|
341 | } |
---|
342 | |
---|
343 | try { |
---|
344 | ret = builder.parse(new InputSource(in)); |
---|
345 | } catch (SAXException e) { |
---|
346 | e.printStackTrace(); |
---|
347 | } catch (IOException e) { |
---|
348 | e.printStackTrace(); |
---|
349 | } |
---|
350 | return ret; |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | public void postprocess() throws TransformerConfigurationException, UnsupportedEncodingException, ParserConfigurationException, TransformerException, TransformerFactoryConfigurationError{ |
---|
355 | //recordPacking |
---|
356 | if (this.getRecordPacking() != null){ |
---|
357 | if (this.getRecordPacking().toLowerCase().equals("string")){ |
---|
358 | try { |
---|
359 | resultStream = IOUtils.toInputStream(StringEscapeUtils.escapeXml(inputStreamAsString(resultStream))); |
---|
360 | } catch (IOException e) { |
---|
361 | // TODO Auto-generated catch block |
---|
362 | e.printStackTrace(); |
---|
363 | } |
---|
364 | |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
368 | // add diagnostics |
---|
369 | if (Diagnostics().Exists()){ |
---|
370 | Document doc = newDocumentFromInputStream(resultStream); |
---|
371 | Node r_element= doc.getElementsByTagName("searchRetrieveResponse").item(0); |
---|
372 | Document ddoc = Diagnostics().buildXMLDocument(); |
---|
373 | Node d_element= ddoc.getElementsByTagName("diagnostics").item(0); |
---|
374 | doc.adoptNode(d_element); |
---|
375 | r_element.appendChild(d_element); |
---|
376 | DOMSource source = new DOMSource(doc); |
---|
377 | StringWriter xmlAsWriter = new StringWriter(); |
---|
378 | StreamResult result = new StreamResult(xmlAsWriter); |
---|
379 | TransformerFactory.newInstance().newTransformer().transform(source, result); |
---|
380 | resultStream = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8")); |
---|
381 | } |
---|
382 | } |
---|
383 | |
---|
384 | /** |
---|
385 | * default Action method |
---|
386 | */ |
---|
387 | public String execute() throws Exception { |
---|
388 | prepare(); |
---|
389 | if (Diagnostics().Accepted()){ |
---|
390 | //TODO diagnostic |
---|
391 | if (this.mdrepoproxyaction.getTargetRequest() == null) { |
---|
392 | Diagnostics().Add(Diagnostic.QUERYSYNTAXERROR); |
---|
393 | return ERROR; |
---|
394 | } |
---|
395 | if (this.mdrepoproxyaction.execute() == SUCCESS){ |
---|
396 | resultStream = this.mdrepoproxyaction.getResultStream(); |
---|
397 | postprocess(); |
---|
398 | return SUCCESS; |
---|
399 | } |
---|
400 | } |
---|
401 | return ERROR; |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | } |
---|