source: MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/action/SRUAction.java @ 1524

Last change on this file since 1524 was 1524, checked in by gaba, 13 years ago

sru-params

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