1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.BufferedReader; |
---|
4 | import java.io.ByteArrayInputStream; |
---|
5 | import java.io.IOException; |
---|
6 | import java.io.InputStream; |
---|
7 | import java.io.InputStreamReader; |
---|
8 | import java.io.StringWriter; |
---|
9 | import java.io.UnsupportedEncodingException; |
---|
10 | import java.util.HashMap; |
---|
11 | import javax.xml.parsers.ParserConfigurationException; |
---|
12 | import javax.xml.transform.TransformerConfigurationException; |
---|
13 | import javax.xml.transform.TransformerException; |
---|
14 | import javax.xml.transform.TransformerFactory; |
---|
15 | import javax.xml.transform.TransformerFactoryConfigurationError; |
---|
16 | import javax.xml.transform.dom.DOMSource; |
---|
17 | import javax.xml.transform.stream.StreamResult; |
---|
18 | |
---|
19 | import org.w3c.dom.Document; |
---|
20 | import org.w3c.dom.Node; |
---|
21 | |
---|
22 | import eu.clarin.cmdi.mdservice.internal.Cache; |
---|
23 | import eu.clarin.cmdi.mdservice.internal.Utils; |
---|
24 | import eu.clarin.cmdi.mdservice.model.Diagnostic; |
---|
25 | import eu.clarin.cmdi.mdservice.model.Query; |
---|
26 | import eu.clarin.cmdi.mdservice.model.WorkspaceProfile; |
---|
27 | import eu.clarin.cmdi.mdservice.proxy.MDRepoProxy; |
---|
28 | import eu.clarin.cmdi.mdservice.proxy.Pz2Proxy; |
---|
29 | import eu.clarin.cmdi.mdservice.proxy.SRUProxy; |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | public class SRUAction extends RepoAction |
---|
35 | { |
---|
36 | |
---|
37 | private static final long serialVersionUID = 1L; |
---|
38 | |
---|
39 | /** |
---|
40 | * Properties to be filled by Struts with according request-parameters |
---|
41 | */ |
---|
42 | /* |
---|
43 | private String q; |
---|
44 | private String version; |
---|
45 | private String operation; |
---|
46 | private String query; |
---|
47 | private String scanClause; |
---|
48 | private int startRecord; |
---|
49 | private int maximumRecords; |
---|
50 | private String recordPacking; |
---|
51 | private String recordSchema; |
---|
52 | private String recordXPath; |
---|
53 | private int resultSetTTL; |
---|
54 | private String sortKeys; |
---|
55 | private String stylesheet; |
---|
56 | private String x_cmd_repository; |
---|
57 | private String x_cmd_collections; |
---|
58 | private Set paramSet; |
---|
59 | */ |
---|
60 | |
---|
61 | private static int OP_SEARCHRETRIEVE = 1; |
---|
62 | private static int OP_EXPLAIN = 2; |
---|
63 | private static int OP_SCAN = 3; |
---|
64 | |
---|
65 | |
---|
66 | //TODO cache after analysis ? |
---|
67 | @Override |
---|
68 | public String getCache() { |
---|
69 | return Cache.SKIP; |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | public String getRepository() { |
---|
74 | if (getParams().get("repository") == null){ |
---|
75 | return getParam("x-cmd-repository"); |
---|
76 | } |
---|
77 | return getParam("repository"); |
---|
78 | } |
---|
79 | |
---|
80 | @Override |
---|
81 | public String getFormat() { |
---|
82 | if (getParams().get("format") == null){ |
---|
83 | if (getParams().get("x-cmd-format") == null){ |
---|
84 | return "xml"; |
---|
85 | } |
---|
86 | return getParam("x-cmd-format"); |
---|
87 | } |
---|
88 | return getParam("format"); |
---|
89 | } |
---|
90 | |
---|
91 | private final static HashMap<String,Integer> operation_code = new HashMap<String,Integer>(); |
---|
92 | static |
---|
93 | { |
---|
94 | operation_code.put("searchRetrieve", OP_SEARCHRETRIEVE); |
---|
95 | operation_code.put("explain", OP_EXPLAIN); |
---|
96 | operation_code.put("scan", OP_SCAN); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * primitive identification of the target-proxy |
---|
101 | * base for finding the right base_url in the props |
---|
102 | * subclass has to override with its specific proxykey |
---|
103 | * @return |
---|
104 | */ |
---|
105 | public String getProxyKey() { |
---|
106 | return ""; |
---|
107 | } |
---|
108 | |
---|
109 | @Override |
---|
110 | protected void setDefaultParams() { |
---|
111 | // set defaults |
---|
112 | /* if ( params.get("version") == null){ |
---|
113 | addParam("version","1.2"); |
---|
114 | } |
---|
115 | if ( params.get("operation") == null){ |
---|
116 | addParam("operation","searchRetrieve"); |
---|
117 | } |
---|
118 | //q=query |
---|
119 | if (getQ() != null) { |
---|
120 | addParam("q",getQ()); |
---|
121 | addParam("query",getQ()); |
---|
122 | } else { |
---|
123 | addParam("q",this.getParam("query")); |
---|
124 | } |
---|
125 | |
---|
126 | if ( params.get("startRecord") == null){ |
---|
127 | addParam("startRecord","1"); |
---|
128 | } |
---|
129 | if ( params.get("maximumRecords") == null){ |
---|
130 | addParam("maximumRecords","50"); |
---|
131 | } |
---|
132 | |
---|
133 | if ( params.get("x-cmd-maxdepth") == null){ |
---|
134 | if (params.get("maxdepth") == null){ |
---|
135 | addParam("maxdepth","2"); |
---|
136 | } |
---|
137 | } else { |
---|
138 | addParam("maxdepth",params.get("x-cmd-maxdepth")[0]); |
---|
139 | } |
---|
140 | |
---|
141 | if ( params.get("x-cmd-repository") == null){ |
---|
142 | if (params.get("repository") == null){ |
---|
143 | addParam("repository",WorkspaceProfile.getRepositoryByIndex(0)); |
---|
144 | } |
---|
145 | } else { |
---|
146 | addParam("repository",params.get("x-cmd-repository")[0]); |
---|
147 | } |
---|
148 | |
---|
149 | if ( params.get("x-cmd-cache") == null){ |
---|
150 | if (params.get("cache") == null){ |
---|
151 | addParam("cache",Cache.SKIP); |
---|
152 | } |
---|
153 | } else { |
---|
154 | addParam("cache",params.get("x-cmd-cache")[0]); |
---|
155 | } |
---|
156 | |
---|
157 | if ( params.get("x-cmd-lang") == null){ |
---|
158 | if (params.get("lang") == null){ |
---|
159 | addParam("lang","en"); |
---|
160 | } |
---|
161 | } else { |
---|
162 | addParam("lang",params.get("x-cmd-lang")[0]); |
---|
163 | } |
---|
164 | |
---|
165 | if ( params.get("x-cmd-format") == null){ |
---|
166 | if (params.get("format") == null){ |
---|
167 | if (this.format == null) { |
---|
168 | addParam("format","xml"); |
---|
169 | } else if (this.format.equals("")){ |
---|
170 | addParam("format","xml"); |
---|
171 | } else { |
---|
172 | addParam("format",this.format); |
---|
173 | } |
---|
174 | } |
---|
175 | } else { |
---|
176 | addParam("format",params.get("x-cmd-format")[0]); |
---|
177 | } |
---|
178 | |
---|
179 | addParam("fullformat",getFullFormat()); |
---|
180 | */ |
---|
181 | } |
---|
182 | /* |
---|
183 | public void prepareOperation() throws UnsupportedEncodingException{ |
---|
184 | |
---|
185 | mdrepoproxyaction = new MDRepoProxyAction(); |
---|
186 | mdrepoproxyaction.setDiagnostics(diagnostics); |
---|
187 | mdrepoproxyaction.setActionkey(hash_operations.get(this.getOperation())); |
---|
188 | |
---|
189 | // set the SRU extern property in proxyaction |
---|
190 | if (this.getX_cmd_repository() != null ) { |
---|
191 | mdrepoproxyaction.setRepository(this.getX_cmd_repository()); |
---|
192 | //TODO\ |
---|
193 | if (mdrepoproxyaction.getBaseURI() == null) this.Diagnostics().Add(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "Unknown repository." + getX_cmd_repository()); |
---|
194 | } |
---|
195 | |
---|
196 | if (operation.equals("searchRetrieve")){ |
---|
197 | |
---|
198 | //query |
---|
199 | mdrepoproxyaction.setSquery(""); |
---|
200 | if (this.query == null) { |
---|
201 | this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "query"); |
---|
202 | //return -1; |
---|
203 | } |
---|
204 | mdrepoproxyaction.setQ(getQuery()); |
---|
205 | mdrepoproxyaction.setFormat("xml"); |
---|
206 | |
---|
207 | if ((this.startRecord < 0 ) ||((getServletRequest().getParameter("startRecord") != null)&&(getServletRequest().getParameter("startRecord").equals("0")))) { |
---|
208 | this.Diagnostics().Add(new Diagnostic(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "startRecord must by grater than 0."));//,Diagnostic.NONFATALNONSURORGATE)); |
---|
209 | //mdrepoproxyaction.setStartItem("1"); |
---|
210 | } else { |
---|
211 | mdrepoproxyaction.setStartItem(Integer.toString(getStartRecord())); |
---|
212 | } |
---|
213 | |
---|
214 | if ((this.maximumRecords < 0 ) ||((getServletRequest().getParameter("maximumRecords") != null) && (getServletRequest().getParameter("maximumRecords").equals("0")))){ |
---|
215 | this.Diagnostics().Add(new Diagnostic(Diagnostic.UNSUPPORTED_PARAMETERVALUE, |
---|
216 | "maximumRecords must by greater than 0.")); |
---|
217 | } else { |
---|
218 | mdrepoproxyaction.setMaximumItems(Integer.toString(this.getMaximumRecords())); |
---|
219 | } |
---|
220 | |
---|
221 | if (this.getX_cmd_repository() != null ) { |
---|
222 | mdrepoproxyaction.setRepository(this.getX_cmd_repository()); |
---|
223 | //TODO\ |
---|
224 | if (mdrepoproxyaction.getBaseURI() == null) this.Diagnostics().Add(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "Unknown repository." + getX_cmd_repository()); |
---|
225 | } |
---|
226 | if (this.getX_cmd_collections() != null ) { |
---|
227 | mdrepoproxyaction.setCollection(this.getX_cmd_collections()); |
---|
228 | } |
---|
229 | |
---|
230 | } else { |
---|
231 | if (operation.equals("explain") || operation == null){ |
---|
232 | log.debug("explaining", "debug"); |
---|
233 | }else { |
---|
234 | if (operation.equals("scan")){ |
---|
235 | if (this.scanClause == null) { |
---|
236 | this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "scanClause"); |
---|
237 | //return -1; |
---|
238 | } else{ |
---|
239 | if (this.scanClause.equals("cmd.Collections")){ |
---|
240 | |
---|
241 | } else { |
---|
242 | if (this.scanClause.equals("cmd.Repository")){ |
---|
243 | XMLSerializer xmlserializer = new XMLSerializer(); |
---|
244 | String xml = xmlserializer.write( WorkspaceProfile.getRepositories());//mdrepoproxyaction.getRepositories() ); |
---|
245 | this.setResultStream(new ByteArrayInputStream(xml.getBytes("UTF-8"))); |
---|
246 | |
---|
247 | } else { |
---|
248 | |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | }else { |
---|
253 | this.Diagnostics().Add(Diagnostic.UNSUPPOERTED_OPERATION, "Only searchRetrieve,explain scan supported."); } |
---|
254 | } |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | public void sru2MD() throws UnsupportedEncodingException{ |
---|
259 | // special handling of parameter setting, x-cmd-... not alowed in java |
---|
260 | this.setX_cmd_collections(getServletRequest().getParameter("x-cmd-collections")); |
---|
261 | // root call own SRU SERVER |
---|
262 | //operation |
---|
263 | if (operation.equals("explain") || operation == null){ |
---|
264 | // TODO: version param is mandatory for explain as well, but for root-request not! |
---|
265 | //explain |
---|
266 | termsproxyaction = new TermsProxyAction(); |
---|
267 | termsproxyaction.setDiagnostics(diagnostics); |
---|
268 | termsproxyaction.setActionkey("terms"); |
---|
269 | |
---|
270 | termsproxyaction.setQ("all"); |
---|
271 | termsproxyaction.setFormat("sru-explain"); |
---|
272 | |
---|
273 | //return -1; |
---|
274 | } else { |
---|
275 | //version |
---|
276 | if (this.version == null) { |
---|
277 | this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "version"); |
---|
278 | //return -1; |
---|
279 | } else { |
---|
280 | if (!version.equals("1.2")) { |
---|
281 | this.Diagnostics().Add(Diagnostic.UNSUPPORTED_VERSION, "Version 1.2 supported."); |
---|
282 | //return -1; |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | prepareOperation(); |
---|
287 | } |
---|
288 | |
---|
289 | } |
---|
290 | |
---|
291 | public void prepareMD() throws Exception{ |
---|
292 | sru2MD(); |
---|
293 | if (Diagnostics().Accepted()){ |
---|
294 | // root-explain |
---|
295 | if (this.termsproxyaction != null) { |
---|
296 | if (this.termsproxyaction.execute() == SUCCESS){ |
---|
297 | this.setResultStream(this.termsproxyaction.getResultStream()); |
---|
298 | //return SUCCESS; |
---|
299 | } |
---|
300 | } else { |
---|
301 | //TODO diagnostic |
---|
302 | if (this.mdrepoproxyaction.getTargetRequest() == null) { |
---|
303 | Diagnostics().Add(Diagnostic.QUERYSYNTAXERROR); |
---|
304 | //return ERROR; |
---|
305 | } |
---|
306 | if (this.mdrepoproxyaction.execute() == SUCCESS){ |
---|
307 | this.setResultStream(this.mdrepoproxyaction.getResultStream()); |
---|
308 | postprocess(); |
---|
309 | //return SUCCESS; |
---|
310 | } |
---|
311 | } |
---|
312 | } |
---|
313 | //return ERROR; |
---|
314 | } |
---|
315 | */ |
---|
316 | /** TODO: move to Utils |
---|
317 | * |
---|
318 | * @param stream |
---|
319 | * @return |
---|
320 | * @throws IOException |
---|
321 | */ |
---|
322 | /* |
---|
323 | public static String inputStreamAsString(InputStream stream) |
---|
324 | throws IOException { |
---|
325 | BufferedReader br = new BufferedReader(new InputStreamReader(stream)); |
---|
326 | StringBuilder sb = new StringBuilder(); |
---|
327 | String line = null; |
---|
328 | |
---|
329 | while ((line = br.readLine()) != null) { |
---|
330 | sb.append(line + "\n"); |
---|
331 | } |
---|
332 | |
---|
333 | br.close(); |
---|
334 | return sb.toString(); |
---|
335 | } |
---|
336 | |
---|
337 | |
---|
338 | |
---|
339 | public void postprocess() throws TransformerConfigurationException, UnsupportedEncodingException, ParserConfigurationException, TransformerException, TransformerFactoryConfigurationError{ |
---|
340 | //recordPacking |
---|
341 | if (this.getRecordPacking() != null){ |
---|
342 | if (this.getRecordPacking().toLowerCase().equals("string")){ |
---|
343 | |
---|
344 | try { |
---|
345 | // FIXME: Which package is IOUTils from? |
---|
346 | this.setResultStream(IOUtils.toInputStream(StringEscapeUtils.escapeXml(inputStreamAsString(this.getResultStream())))); |
---|
347 | } catch (IOException e) { |
---|
348 | // TODO Auto-generated catch block |
---|
349 | e.printStackTrace(); |
---|
350 | } |
---|
351 | |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | // add diagnostics |
---|
356 | if (getDiagnostics().Exists()){ |
---|
357 | Document doc = Utils.stream2Document(getResultStream()); |
---|
358 | Node r_element= doc.getElementsByTagName("searchRetrieveResponse").item(0); |
---|
359 | Document ddoc = getDiagnostics().buildXMLDocument(); |
---|
360 | Node d_element= ddoc.getElementsByTagName("diagnostics").item(0); |
---|
361 | doc.adoptNode(d_element); |
---|
362 | r_element.appendChild(d_element); |
---|
363 | DOMSource source = new DOMSource(doc); |
---|
364 | StringWriter xmlAsWriter = new StringWriter(); |
---|
365 | StreamResult result = new StreamResult(xmlAsWriter); |
---|
366 | TransformerFactory.newInstance().newTransformer().transform(source, result); |
---|
367 | this.setResultStream(new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"))); |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | //TODO create query + query.toURLParams ? |
---|
372 | public String createURLParams(){ |
---|
373 | String params = ""; |
---|
374 | |
---|
375 | //TODO params |
---|
376 | /* |
---|
377 | if (paramSet == null){ |
---|
378 | if (!(operation == null)){ |
---|
379 | params = params + "operation=" + operation; |
---|
380 | } |
---|
381 | if (!(version == null)){ |
---|
382 | params = params + "&version=" + version; |
---|
383 | } |
---|
384 | if (!(query == null)){ |
---|
385 | params = params + "&query=" + query; |
---|
386 | } |
---|
387 | if (getStartRecord() > 0){ |
---|
388 | params = params + "&startRecord=" + String.valueOf(getStartRecord()); |
---|
389 | } |
---|
390 | if (getMaximumRecords()> 0){ |
---|
391 | params = params + "&numRecords=" + String.valueOf(getMaximumRecords()); |
---|
392 | } |
---|
393 | } else { |
---|
394 | Iterator i = paramSet.iterator(); |
---|
395 | params = ""; |
---|
396 | while(i.hasNext()){ |
---|
397 | Map.Entry me = (Map.Entry)i.next(); |
---|
398 | if (!(me.getKey().equals("x-cmd-repository") || me.getKey().equals("x-cmd-collections"))){ |
---|
399 | params = params + "&" + me.getKey() + "=" + ((String[])me.getValue())[0]; |
---|
400 | } |
---|
401 | } |
---|
402 | params = params.substring(1); |
---|
403 | |
---|
404 | } |
---|
405 | |
---|
406 | return params; |
---|
407 | } |
---|
408 | */ |
---|
409 | |
---|
410 | /* |
---|
411 | @Override |
---|
412 | public void prepare() throws Exception{ |
---|
413 | //this.setX_cmd_repository(getServletRequest().getParameter("x-cmd-repository")); |
---|
414 | //this.setRepository(this.getX_cmd_repository()); |
---|
415 | //TODO xsl usage according particular repositories ? |
---|
416 | //this.setActionkey("sru"); |
---|
417 | //this.setFormat("xml"); |
---|
418 | |
---|
419 | //extern SRU repository |
---|
420 | if (WorkspaceProfile.isSRURepository(getRepository()) || this.getBaseURI() == null){ |
---|
421 | super.prepare(); |
---|
422 | } |
---|
423 | else { |
---|
424 | prepareMD(); |
---|
425 | } |
---|
426 | |
---|
427 | } |
---|
428 | */ |
---|
429 | @Override |
---|
430 | public void setTargetProxy(){ |
---|
431 | |
---|
432 | //Query query = new Query(); |
---|
433 | switch (WorkspaceProfile.RepositoryType.toRepositoryType(WorkspaceProfile.getRepositoryType(getRepository()))) |
---|
434 | { |
---|
435 | case PAZPAR: |
---|
436 | setTargetProxy(new Pz2Proxy(new Query(getActionkey(), this.getParams()))); |
---|
437 | return; |
---|
438 | case SRU: |
---|
439 | setTargetProxy(new SRUProxy(new Query(Query.SRUEXTERN,getParams()))); |
---|
440 | return; |
---|
441 | case MD: |
---|
442 | setTargetProxy(new MDRepoProxy(new Query(Query.RECORDSET,getParams()))); |
---|
443 | return; |
---|
444 | default: |
---|
445 | //TODO error = bad workspaceProfile configuration |
---|
446 | getDiagnostics().Add(Diagnostic.SYSTEM_ERROR, "Error in workspaceprofile configuration", "repository=" + getRepository() ); |
---|
447 | } |
---|
448 | } |
---|
449 | } |
---|