Changeset 1627 for MDService2


Ignore:
Timestamp:
11/24/11 14:15:08 (12 years ago)
Author:
gaba
Message:

sru - base

Location:
MDService2/branches/MDService_simple3/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/action/GenericAction.java

    r1621 r1627  
    335335                params = new HashMap(getServletRequest().getParameterMap());   
    336336
    337                 // set defaults
    338                 if ( params.get("version") == null){
    339                         addParam("version","1.2");
    340                 }
    341                 if ( params.get("operation") == null){
    342                         addParam("operation","searchRetrieve");
    343                 }
    344                 //q=query                       
    345                 if (getQ() != null) {
    346                         addParam("q",getQ());
    347                         addParam("query",getQ());
    348                 } else {
    349                         addParam("q",this.getParam("query"));
    350                 }
    351                
    352                 if ( params.get("startRecord") == null){
    353                         addParam("startRecord","1");
    354                 }
    355                 if ( params.get("maximumRecords") == null){
    356                         addParam("maximumRecords","50");
    357                 }
    358                
    359                 if ( params.get("x-cmd-maxdepth") == null){
    360                         if (params.get("maxdepth") == null){
    361                                 addParam("maxdepth","2");
    362                         }
    363                 } else {
    364                         addParam("maxdepth",params.get("x-cmd-maxdepth")[0]);
    365                 }
    366                
    367                 if ( params.get("x-cmd-repository") == null){
    368                         if (params.get("repository") == null){
    369                                 addParam("repository",WorkspaceProfile.getRepositoryByIndex(0));
    370                         }
    371                 } else {
    372                         addParam("repository",params.get("x-cmd-repository")[0]);
    373                 }
    374                
    375                 if ( params.get("x-cmd-cache") == null){
    376                         if (params.get("cache") == null){
    377                                 addParam("cache",Cache.SKIP);
    378                         }
    379                 } else {
    380                         addParam("cache",params.get("x-cmd-cache")[0]);
    381                 }
    382                
    383                 if ( params.get("x-cmd-lang") == null){
    384                         if (params.get("lang") == null){
    385                                 addParam("lang","en");
    386                         }
    387                 } else {
    388                         addParam("lang",params.get("x-cmd-lang")[0]);
    389                 }
    390                
    391                 if ( params.get("x-cmd-format") == null){
    392                         if (params.get("format") == null){
    393                                 if (this.format == null) {
    394                                         addParam("format","xml");
    395                                 } else if (this.format.equals("")){
    396                                         addParam("format","xml");
    397                                 } else {
    398                                         addParam("format",this.format);
    399                                 }
    400                         }
    401                 } else {
    402                         addParam("format",params.get("x-cmd-format")[0]);
    403                 }
    404 
    405                 addParam("fullformat",getFullFormat());
    406                
     337                setDefaultParams();             
    407338        }
    408339       
     340        protected void setDefaultParams(){
     341               
     342        }
    409343        /**
    410344         * Gets the local parameter map.
  • MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/action/RepoAction.java

    r1623 r1627  
    22
    33import java.net.URL;
     4import java.util.HashMap;
    45
    56import org.apache.log4j.Logger;
     7
     8import eu.clarin.cmdi.mdservice.internal.Cache;
    69import eu.clarin.cmdi.mdservice.model.Diagnostic;
    710import eu.clarin.cmdi.mdservice.model.Query;
     
    3033       
    3134        protected URL base_url ;
    32         Query query;
     35        //Query query;
    3336       
    3437        @Override
     
    3841
    3942        @Override
    40         @SuppressWarnings("unchecked")
    41         protected void loadParams() {   
    42                 super.loadParams();
    43                 query = new Query(getActionkey(), this.getParams());
     43        protected void setDefaultParams() {     
     44                //q=query                       
     45                if (getQ() != null) {
     46                        addParam("q",getQ());
     47                        addParam("query",getQ());
     48                } else {
     49                        addParam("q",this.getParam("query"));
     50                }
     51               
     52                // set defaults
     53                /*
     54                if ( params.get("version") == null){
     55                        addParam("version","1.2");
     56                }
     57                if ( params.get("operation") == null){
     58                        addParam("operation","searchRetrieve");
     59                }
     60                //q=query                       
     61                if (getQ() != null) {
     62                        addParam("q",getQ());
     63                        addParam("query",getQ());
     64                } else {
     65                        addParam("q",this.getParam("query"));
     66                }
     67               
     68                if ( params.get("startRecord") == null){
     69                        addParam("startRecord","1");
     70                }
     71                if ( params.get("maximumRecords") == null){
     72                        addParam("maximumRecords","50");
     73                }
     74               
     75                if ( params.get("x-cmd-maxdepth") == null){
     76                        if (params.get("maxdepth") == null){
     77                                addParam("maxdepth","2");
     78                        }
     79                } else {
     80                        addParam("maxdepth",params.get("x-cmd-maxdepth")[0]);
     81                }
     82               
     83                if ( params.get("x-cmd-repository") == null){
     84                        if (params.get("repository") == null){
     85                                addParam("repository",WorkspaceProfile.getRepositoryByIndex(0));
     86                        }
     87                } else {
     88                        addParam("repository",params.get("x-cmd-repository")[0]);
     89                }
     90               
     91                if ( params.get("x-cmd-cache") == null){
     92                        if (params.get("cache") == null){
     93                                addParam("cache",Cache.SKIP);
     94                        }
     95                } else {
     96                        addParam("cache",params.get("x-cmd-cache")[0]);
     97                }
     98               
     99                if ( params.get("x-cmd-lang") == null){
     100                        if (params.get("lang") == null){
     101                                addParam("lang","en");
     102                        }
     103                } else {
     104                        addParam("lang",params.get("x-cmd-lang")[0]);
     105                }
     106               
     107                if ( params.get("x-cmd-format") == null){
     108                        if (params.get("format") == null){
     109                                if (this.format == null) {
     110                                        addParam("format","xml");
     111                                } else if (this.format.equals("")){
     112                                        addParam("format","xml");
     113                                } else {
     114                                        addParam("format",this.format);
     115                                }
     116                        }
     117                } else {
     118                        addParam("format",params.get("x-cmd-format")[0]);
     119                }
     120
     121                addParam("fullformat",getFullFormat());
     122                */
    44123        }
    45124        /**
     
    62141                        {               
    63142                            case PAZPAR:
    64                                 setTargetProxy(new Pz2Proxy());
     143                                setTargetProxy(new Pz2Proxy(new Query(getActionkey(), this.getParams())));
    65144                                return;
    66145                            case SRU:
    67                                 setTargetProxy(new SRUProxy(query));
     146                                setTargetProxy(new SRUProxy(new Query(getActionkey(), this.getParams())));
    68147                                return;
    69148                            case MD:
    70                                 setTargetProxy(new MDRepoProxy(query));
     149                                setTargetProxy(new MDRepoProxy(new Query(getActionkey(), this.getParams())));
    71150                                return;
    72151                            default:
  • MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/action/SRUAction.java

    r1540 r1627  
    2222import eu.clarin.cmdi.mdservice.internal.Cache;
    2323import eu.clarin.cmdi.mdservice.internal.Utils;
     24import eu.clarin.cmdi.mdservice.model.Diagnostic;
     25import eu.clarin.cmdi.mdservice.model.Query;
     26import eu.clarin.cmdi.mdservice.model.WorkspaceProfile;
     27import eu.clarin.cmdi.mdservice.proxy.MDRepoProxy;
     28import eu.clarin.cmdi.mdservice.proxy.Pz2Proxy;
     29import eu.clarin.cmdi.mdservice.proxy.SRUProxy;
    2430
    2531
     
    6470        }
    6571       
    66         private final static HashMap<String,String> hash_operations = new HashMap<String,String>();
    67          static
    68          {       
    69                  hash_operations.put("searchRetrieve", "recordset");
    70                  hash_operations.put("explain", "model"); /* could be terms*/
    71                  hash_operations.put("scan", "values");
    72          }
    73          
     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       
    7491         private final static HashMap<String,Integer> operation_code = new HashMap<String,Integer>();
    7592         static
     
    8097         }
    8198         
    82          public SRUAction(){
    83                  super();
    84                  initialize();
    85          }
    86          
    87          protected void  initialize(){
    88                  //repository = 1;
    89                  //this.startRecord = 1;
    90                  //this.maximumRecords = 50;
    91                  /*
    92                  if (getServletRequest() != null){
    93                          this.setX_cmd_repository(getServletRequest().getParameter("x-cmd-repository"));
    94                         //this.setRepository(this.getX_cmd_repository());
    95                          //paramSet = getServletRequest().getParameterMap().entrySet();                 
    96                  }
    97                  */
    98                  
    99          }
    100         /*
    101          public String getQ() {
    102                         return q;
    103                 }
    104 
    105                 public void setQ(String q) {
    106                         this.q = q;
    107                 }
    108          
    109 
    110         public String getVersion() {
    111                 return version;
    112         }
    113 
    114         public void setVersion(String version) {
    115                 this.version = version;
    116         }
    117 
    118         public String getOperation() {
    119                 return operation;
    120         }
    121 
    122         public void setOperation(String operation) {
    123                 this.operation = operation;
    124         }
    125        
    126         public String getQuery() {
    127                 return query;
    128         }
    129 
    130         public void setQuery(String query) {
    131                 this.query = query;
    132         }
    133 
    134         public String getScanClause() {
    135                 return scanClause;
    136         }
    137 
    138         public void setScanClause(String scanClause) {
    139                 this.scanClause = scanClause;
    140         }
    141 
    142        
    143         public int getStartRecord() {
    144                 return startRecord;
    145         }
    146 
    147         public void setStartRecord(int startRecord) {
    148                 this.startRecord = startRecord;
    149         }
    150         public int getMaximumRecords() {
    151                 return maximumRecords;
    152         }
    153 
    154         public void setMaximumRecords(int maximumRecords) {
    155                 this.maximumRecords = maximumRecords;
    156         }
    157        
    158         public String getRecordPacking() {
    159                 return recordPacking;
    160         }
    161 
    162         public void setRecordPacking(String recordPacking) {
    163                 this.recordPacking = recordPacking;
    164         }
    165 
    166         public String getRecordSchema() {
    167                 return recordSchema;
    168         }
    169 
    170         public void setRecordSchema(String recordSchema) {
    171                 this.recordSchema = recordSchema;
    172         }
    173 
    174         public String getRecordXPath() {
    175                 return recordXPath;
    176         }
    177 
    178         public void setRecordXPath(String recordXPath) {
    179                 this.recordXPath = recordXPath;
    180         }
    181 
    182         public int getResultSetTTL() {
    183                 return resultSetTTL;
    184         }
    185 
    186         public void setResultSetTTL(int resultSetTTL) {
    187                 this.resultSetTTL = resultSetTTL;
    188         }
    189        
    190         public String getSortKeys() {
    191                 return sortKeys;
    192         }
    193 
    194         public void setSortKeys(String sortKeys) {
    195                 this.sortKeys = sortKeys;
    196         }
    197                
    198         public String getStylesheet() {
    199                 return stylesheet;
    200         }
    201 
    202         public void setStylesheet(String stylesheet) {
    203                 this.stylesheet = stylesheet;
    204         }
    205        
    206         public String getX_cmd_repository(){
    207                 return x_cmd_repository;
    208         }
    209        
    210         public void setX_cmd_repository(String x_cmd_repository){
    211                 this.x_cmd_repository = x_cmd_repository;
    212         }
    213 
    214         public String getX_cmd_collections(){
    215                 return x_cmd_collections;
    216         }
    217        
    218         public void setX_cmd_collections(String x_cmd_collections){
    219                 this.x_cmd_collections = x_cmd_collections;
    220         }
    221 
    222         */
    223         public String getDiagnosticStreamName() throws TransformerConfigurationException, UnsupportedEncodingException, ParserConfigurationException, TransformerException, TransformerFactoryConfigurationError{
    224                 this.setResultStream(Utils.document2Stream(getDiagnostics().buildXMLDocument()));//getDiagnostics().buildXMLStream());
    225                 return "resultStream";
    226         }
    227        
    22899        /**
    229100         * primitive identification of the target-proxy
     
    235106                return "";
    236107        }
    237         /*
     108
    238109        @Override
    239         public String getBaseURI() {           
    240                 log.debug("SRU-repositorypath:" + WorkspaceProfile.getRepositoryPath(getRepository()));
    241                 String uri = WorkspaceProfile.getRepositoryPath(getRepository());//Utils.getConfig().getProperty(getProxyKey() + ".uri");               
    242                 return uri;
    243         }
    244         */
    245        
     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        }
    246182        /*
    247183        public void prepareOperation() throws UnsupportedEncodingException{
     
    384320 * @throws IOException
    385321 */
     322        /*
    386323        public static String inputStreamAsString(InputStream stream)
    387324                throws IOException {
     
    404341                if (this.getRecordPacking() != null){
    405342                        if (this.getRecordPacking().toLowerCase().equals("string")){
    406                                 /*
     343                               
    407344                                try {
    408345                                        // FIXME: Which package is IOUTils from?
     
    411348                                        // TODO Auto-generated catch block
    412349                                        e.printStackTrace();
    413                                 }*/
     350                                }
    414351
    415352                        }
     
    466403
    467404                }
    468 */
     405
    469406                return params;
    470407        }
    471        
     408*/     
    472409
    473410/*     
     
    490427        }
    491428*/
    492        
     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                }
    493448}
     449}
  • MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/model/Query.java

    r1626 r1627  
    190190                params.put(key, sarr); 
    191191        }
     192       
     193        public void setParam(String key, String value){
     194                addParam(key, value);   
     195        }
     196       
    192197        /**
    193198         * This is for simplified access to the the values of the request-parameters
     
    410415
    411416                } else if (type.equals(SRUEXTERN)) {
    412                         targetRequest = "";//fromCMDIndex2Xpath() + "&maxdepth=" + getMaxdepth()  ; /* + "&maxdepth=" + getMaxdepth() );  "&collection=" + getCollection() + */
    413                        
    414                         Set set = sruMap.entrySet();
     417                        String paramsign = "?";
     418                        targetRequest = "";
     419                       
     420                        Set set = params.entrySet();
    415421                        Iterator i = set.iterator();
    416422                       
    417423                    while(i.hasNext()){
    418424                              Map.Entry me = (Map.Entry)i.next();
    419                               targetRequest = targetRequest + "&" + me.getKey() + "=" + me.getValue();
     425                              if (!(((String)me.getKey()).startsWith("x-cmd"))){
     426                                  targetRequest = targetRequest + paramsign + me.getKey() + "=" + ((String[])me.getValue())[0];
     427                                      paramsign = "&";
     428                              }   
    420429                        }
    421430                                       
  • MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/proxy/MDRepoProxy.java

    r1623 r1627  
    11package eu.clarin.cmdi.mdservice.proxy;
    22
     3import java.io.ByteArrayInputStream;
    34import java.io.IOException;
     5import java.io.UnsupportedEncodingException;
    46import java.net.URL;
    57import java.util.HashMap;
     8
     9import net.sf.json.xml.XMLSerializer;
     10
    611import org.apache.log4j.Logger;
    712
    813import eu.clarin.cmdi.mdservice.internal.CQLParseException;
     14import eu.clarin.cmdi.mdservice.internal.Cache;
    915import eu.clarin.cmdi.mdservice.model.Diagnostic;
    1016import eu.clarin.cmdi.mdservice.model.Query;
     
    2935
    3036       
    31         public MDRepoProxy(Query q){
    32                 setQuery(q);
    33         }
     37        private final static HashMap<String,String> hash_sruoperations = new HashMap<String,String>();
     38         static
     39         {       
     40                 hash_sruoperations.put("searchRetrieve", Query.RECORDSET);
     41                 hash_sruoperations.put("explain", Query.MODEL); /* could be terms*/
     42                 hash_sruoperations.put("scan", Query.VALUES);
     43         }
    3444       
    3545       
     46         public MDRepoProxy(Query q) {
     47                        super(q);
     48        }
     49
    3650        /**
    3751         * Override of the basic method provided by the super-class
     
    6074         }
    6175 
     76         
     77         public void addParam (String key, String value){
     78                 getSourceAction().addParam(key, value);
     79         }
     80         @Override
     81         public void checkParams() {
     82                // set defaults
     83                       
     84                if ( getParams().get("startRecord") == null){
     85                                addParam("startRecord","1");
     86                        }
     87                if ( getParams().get("maximumRecords") == null){
     88                                addParam("maximumRecords","50");
     89                        }
     90                       
     91                if ( getParams().get("x-cmd-maxdepth") == null){
     92                                if (getParams().get("maxdepth") == null){
     93                                        addParam("maxdepth","2");
     94                                }
     95                } else {
     96                                addParam("maxdepth",getParams().get("x-cmd-maxdepth")[0]);
     97                }
     98                       
     99                if ( getParams().get("x-cmd-repository") == null){
     100                                if (getParams().get("repository") == null){
     101                                        addParam("repository",WorkspaceProfile.getRepositoryByIndex(0));
     102                                }
     103                } else {
     104                                addParam("repository",getParams().get("x-cmd-repository")[0]);
     105                }
     106                       
     107                if ( getParams().get("x-cmd-cache") == null){
     108                                if (getParams().get("cache") == null){
     109                                        addParam("cache",Cache.SKIP);
     110                                }
     111                } else {
     112                                addParam("cache",getParams().get("x-cmd-cache")[0]);
     113                }
     114                       
     115                if ( getParams().get("x-cmd-lang") == null){
     116                                if (getParams().get("lang") == null){
     117                                        addParam("lang","en");
     118                                }
     119                } else {
     120                                addParam("lang",getParams().get("x-cmd-lang")[0]);
     121                }
     122                       
     123                if ( getParams().get("x-cmd-format") == null){
     124                                if (getParams().get("format") == null){
     125                                        if (getSourceAction().getFormat() == null) {
     126                                                addParam("format","xml");
     127                                        } else if (getSourceAction().getFormat().equals("")){
     128                                                addParam("format","xml");
     129                                        } else {
     130                                                addParam("format",getSourceAction().getFormat());
     131                                        }
     132                                }
     133                } else {
     134                                addParam("format",getParams().get("x-cmd-format")[0]);
     135                }
     136
     137                addParam("fullformat",getSourceAction().getFullFormat());
     138                 
     139                super.checkParams();
     140               
     141                // TODO ?????
     142                 if (getSourceAction().getActionkey().equals("sru")){
     143                         getQuery().setType(hash_sruoperations.get(getParam("operation")));
     144                         getSourceAction().setActionkey(hash_sruoperations.get(getParam("operation")));
     145                 }
     146         }
     147         
    62148        /**
    63149         * uses base_url + url_pattern (parametrized by actionkey) to form a url
  • MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/proxy/Pz2Proxy.java

    r1623 r1627  
    4444public class Pz2Proxy extends RepoProxy {
    4545
     46       
     47
    4648        private static final long serialVersionUID = 1L;
    4749        private static Logger log = Logger.getLogger("Pz2Proxy");
    4850       
    4951        private String proxy_key = "pazpar2";
     52       
     53        public Pz2Proxy(Query q) {
     54                super(q);
     55        }
    5056       
    5157        //private String command;
  • MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/proxy/RepoProxy.java

    r1623 r1627  
    1313import eu.clarin.cmdi.mdservice.internal.NoStylesheetException;
    1414import eu.clarin.cmdi.mdservice.model.Diagnostic;
     15import eu.clarin.cmdi.mdservice.model.Diagnostics;
    1516import eu.clarin.cmdi.mdservice.model.Query;
    1617import eu.clarin.cmdi.mdservice.model.WorkspaceProfile;
     
    3435 
    3536        private String proxy_key = "repo";
    36                
     37        private Diagnostics diagnostics;
     38
    3739        // should perhaps be RepoAction, but it gets complicated with the types
    3840        private GenericAction source_action;
     
    4143       
    4244       
     45        public RepoProxy(Query q){
     46                setQuery(q);
     47        }
    4348       
    4449        public GenericAction getSourceAction() {
     
    5459                source_action = action;
    5560               
     61        }
     62       
     63        public Diagnostics getDiagnostics(){
     64                return this.diagnostics;
     65        }
     66       
     67        public void setDiagnostics(Diagnostics diagnostics){
     68                this.diagnostics = diagnostics;
    5669        }
    5770       
     
    101114        }
    102115
     116        public Diagnostics Diagnostics(){
     117                if (this.diagnostics == null){
     118                        this.diagnostics = new Diagnostics();
     119                }
     120                return this.diagnostics;
     121        }
    103122        /**
    104123         * Provides the base-URL specific to given target repository or registry
  • MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/proxy/SRUProxy.java

    r1623 r1627  
    11package eu.clarin.cmdi.mdservice.proxy;
    22
    3 import java.io.BufferedReader;
    43import java.io.IOException;
    5 import java.io.InputStream;
    6 import java.io.InputStreamReader;
    74import java.net.URL;
    8 import java.util.HashMap;
    9 import java.util.Iterator;
    10 import java.util.Map;
    11 import java.util.Set;
    12 
    13 import javax.xml.parsers.DocumentBuilder;
    14 import javax.xml.parsers.DocumentBuilderFactory;
    15 import javax.xml.parsers.ParserConfigurationException;
    165
    176import org.apache.log4j.Logger;
    18 import org.w3c.dom.Document;
    19 import org.xml.sax.InputSource;
    20 import org.xml.sax.SAXException;
    217
    22 import eu.clarin.cmdi.mdservice.model.Diagnostics;
     8import eu.clarin.cmdi.mdservice.internal.CQLParseException;
     9import eu.clarin.cmdi.mdservice.model.Diagnostic;
    2310import eu.clarin.cmdi.mdservice.model.Query;
    2411import eu.clarin.cmdi.mdservice.model.WorkspaceProfile;
     
    2613
    2714public class SRUProxy extends RepoProxy
    28 //implements ServletRequestAware
    2915{
    3016
     
    3218       
    3319        private String proxy_key = "sru";
    34         private static Logger log = Logger.getLogger("SRUProxy");
    35        
    36         private Diagnostics diagnostics;
    37        
    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         */
    59         private Set paramSet;
    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        
    67        
    68         private final static HashMap<String,String> hash_operations = new HashMap<String,String>();
    69          static
    70          {       
    71                  hash_operations.put("searchRetrieve", "recordset");
    72                  hash_operations.put("explain", "model"); /* could be terms*/
    73                  hash_operations.put("scan", "values");
    74          }
     20        private static Logger log = Logger.getLogger("SRUProxy");               
    7521         
    76          private final static HashMap<String,Integer> operation_code = new HashMap<String,Integer>();
    77          static
    78          {       
    79                  operation_code.put("searchRetrieve", OP_SEARCHRETRIEVE);
    80                  operation_code.put("explain", OP_EXPLAIN);
    81                  operation_code.put("scan", OP_SCAN);
    82          }
     22        public SRUProxy(Query q) {
     23                super(q);
     24                // TODO Auto-generated constructor stub
     25        }
    8326         
    84          public SRUProxy(){
    85                  super();
    86                  initialize();
    87          }
    88          
    89          public SRUProxy(Query q){
    90                         setQuery(q);
    91                 }
    92          
    93          protected void  initialize(){
    94                 /*             
    95                  // TODO set SRU params
    96                         // missing collections, columns...
    97                         setOperation("searchRetrieve");
    98                         //TODO what version
    99                         setVersion("1.2");
    100                         //setStartRecord(Integer.parseInt(getSourceAction().getStartItem()));
    101                         //setMaximumRecords(Integer.parseInt(getSourceAction().getMaximumItems()));
    102                 // This is probably bloedsinn:
    103                         setX_cmd_repository(getSourceAction().getRepository());
    104                         setQuery(this.fullQueryString());
    105                 */     
    106         /*       
    107                  if (getServletRequest() != null){
    108                          this.setX_cmd_repository(getServletRequest().getParameter("x-cmd-repository"));
    109                          this.setRepository(this.getX_cmd_repository());
    110                          paramSet = getServletRequest().getParameterMap().entrySet();                   
    111                  }
    112                 */
    113          }
    114          
    115          @Override
     27        @Override
    11628        public String getProxyKey() {                           
    11729                        return proxy_key;               
    11830        }
    119 
    120          /*@Override
    121                 public void setServletRequest(HttpServletRequest arg0) {
    122                         request = arg0;
     31       
     32        @Override
     33        public void checkParams() {
     34                // required Params
     35                // check and set defaults
     36                if (getParam("operation").equals("")) {
     37                        getSourceAction().getDiagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "operation");
     38                }
     39                if (getParam("version").equals("")) {
     40                        getSourceAction().getDiagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "version");
     41                }
     42                if (getParam("query").equals("")) {
     43                        getSourceAction().getDiagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "query");
    12344                }
    12445
    125                 public HttpServletRequest getServletRequest() {
    126                         return request;
    127                 }
    128                 */
    129                 public Diagnostics getDiagnostics(){
    130                         return this.diagnostics;
    131                 }
    132                
    133                 public void setDiagnostics(Diagnostics diagnostics){
    134                         this.diagnostics = diagnostics;
    135                 }
    136        
    137         /*     
    138          public String getQ() {
    139                         return q;
    140                 }
    14146
    142                 public void setQ(String q) {
    143                         this.q = q;
    144                 }
    145                 */
    146          /*
    147          public InputStream getResultStream() {
    148              return resultStream;
    149          }
    150          public void setResultStream(InputStream _resultStream){
    151                 resultStream = _resultStream;
    152          }
    153         */
    154 /*
    155         public String getVersion() {
    156                 return version;
    15747        }
    158 
    159         public void setVersion(String version) {
    160                 this.version = version;
    161         }
    162 
    163         public String getOperation() {
    164                 return operation;
    165         }
    166 
    167         public void setOperation(String operation) {
    168                 this.operation = operation;
    169         }
    170        
    171         public String getQuery() {
    172                 return query;
    173         }
    174 
    175         public void setQuery(String query) {
    176                 this.query = query;
    177         }
    178 
    179         public String getScanClause() {
    180                 return scanClause;
    181         }
    182 
    183         public void setScanClause(String scanClause) {
    184                 this.scanClause = scanClause;
    185         }
    186 
    187        
    188         public int getStartRecord() {
    189                 return startRecord;
    190         }
    191 
    192         public void setStartRecord(int startRecord) {
    193                 this.startRecord = startRecord;
    194         }
    195         public int getMaximumRecords() {
    196                 return maximumRecords;
    197         }
    198 
    199         public void setMaximumRecords(int maximumRecords) {
    200                 this.maximumRecords = maximumRecords;
    201         }
    202        
    203         public String getRecordPacking() {
    204                 return recordPacking;
    205         }
    206 
    207         public void setRecordPacking(String recordPacking) {
    208                 this.recordPacking = recordPacking;
    209         }
    210 
    211         public String getRecordSchema() {
    212                 return recordSchema;
    213         }
    214 
    215         public void setRecordSchema(String recordSchema) {
    216                 this.recordSchema = recordSchema;
    217         }
    218 
    219         public String getRecordXPath() {
    220                 return recordXPath;
    221         }
    222 
    223         public void setRecordXPath(String recordXPath) {
    224                 this.recordXPath = recordXPath;
    225         }
    226 
    227         public int getResultSetTTL() {
    228                 return resultSetTTL;
    229         }
    230 
    231         public void setResultSetTTL(int resultSetTTL) {
    232                 this.resultSetTTL = resultSetTTL;
    233         }
    234        
    235         public String getSortKeys() {
    236                 return sortKeys;
    237         }
    238 
    239         public void setSortKeys(String sortKeys) {
    240                 this.sortKeys = sortKeys;
    241         }
    242                
    243         public String getStylesheet() {
    244                 return stylesheet;
    245         }
    246 
    247         public void setStylesheet(String stylesheet) {
    248                 this.stylesheet = stylesheet;
    249         }
    250        
    251         public String getX_cmd_repository(){
    252                 return x_cmd_repository;
    253         }
    254        
    255         public void setX_cmd_repository(String x_cmd_repository){
    256                 this.x_cmd_repository = x_cmd_repository;
    257         }
    258 
    259         public String getX_cmd_collections(){
    260                 return x_cmd_collections;
    261         }
    262        
    263         public void setX_cmd_collections(String x_cmd_collections){
    264                 this.x_cmd_collections = x_cmd_collections;
    265         }
    266         */
    26748       
    26849        @Override
     
    27253                return uri;
    27354        }
    274        
    275         public Diagnostics Diagnostics(){
    276                 if (this.diagnostics == null){
    277                         this.diagnostics = new Diagnostics();
     55
     56         @Override
     57        public URL getTargetRequest() throws IOException, CQLParseException {
     58                               
     59                        log.debug("SRUP.getQuery:" + getParam("query"));
     60                        log.debug("SRUP.getActionkey:" + getSourceAction().getActionkey());             
     61
     62                // check if the query could get parsed
     63                        if (getQuery().isStatus(Query.PARSEERROR)) {
     64                                log.debug("SRUP.query.PARSEERROR:" + getQuery().getMsg());
     65                                throw new CQLParseException("SRUP.query.PARSEERROR:" + getQuery().getMsg());
     66                        } else {
     67                       
     68                                URL targetURL = null;
     69                                URL tmp = getBaseURL();
     70                                targetURL =new URL( tmp.toString() + getQuery().toURLParam());
     71                       
     72                                log.debug("SRUPA.targetURL:" + targetURL);
     73
     74                                return targetURL;
     75                        }
     76                         
    27877                }
    279                 return this.diagnostics;
    280         }
    281 /*     
    282         public void prepareOperation() throws UnsupportedEncodingException{
    283                
    284                 mdrepoproxyaction = new MDRepoProxyAction();
    285                 mdrepoproxyaction.setDiagnostics(diagnostics);
    286                 mdrepoproxyaction.setActionkey(hash_operations.get(this.getOperation()));
    287                
    288                 // set the SRU extern property in proxyaction
    289                 if (this.getX_cmd_repository() != null ) {
    290                         mdrepoproxyaction.setRepository(this.getX_cmd_repository());
    291                         //TODO\
    292                         if (mdrepoproxyaction.getBaseURI() == null) this.Diagnostics().Add(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "Unknown repository." + getX_cmd_repository());
    293                 }
    294                
    295                 if (operation.equals("searchRetrieve")){
    296                        
    297                         //query
    298                         mdrepoproxyaction.setSquery("");
    299                         if (this.query == null) {
    300                                 this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "query");
    301                                 //return -1;
    302                         }
    303                         mdrepoproxyaction.setQ(getQuery());                     
    304                         mdrepoproxyaction.setFormat("xml");
    305                        
    306                         if ((this.startRecord < 0 ) ||((getServletRequest().getParameter("startRecord") != null)&&(getServletRequest().getParameter("startRecord").equals("0")))) {
    307                                 this.Diagnostics().Add(new Diagnostic(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "startRecord must by grater than 0."));//,Diagnostic.NONFATALNONSURORGATE));
    308                                 //mdrepoproxyaction.setStartItem("1");
    309                         } else {
    310                                 mdrepoproxyaction.setStartItem(Integer.toString(getStartRecord()));
    311                         }
    312                
    313                         if ((this.maximumRecords < 0 ) ||((getServletRequest().getParameter("maximumRecords") != null) && (getServletRequest().getParameter("maximumRecords").equals("0")))){
    314                                    this.Diagnostics().Add(new Diagnostic(Diagnostic.UNSUPPORTED_PARAMETERVALUE,
    315                                                    "maximumRecords must by greater than 0."));
    316                         } else {
    317                                 mdrepoproxyaction.setMaximumItems(Integer.toString(this.getMaximumRecords())); 
    318                         }
    319                        
    320                         if (this.getX_cmd_repository() != null ) {
    321                                 mdrepoproxyaction.setRepository(this.getX_cmd_repository());
    322                                 //TODO\
    323                                 if (mdrepoproxyaction.getBaseURI() == null) this.Diagnostics().Add(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "Unknown repository." + getX_cmd_repository());
    324                         }
    325                         if (this.getX_cmd_collections() != null ) {
    326                                 mdrepoproxyaction.setCollection(this.getX_cmd_collections());
    327                         }
    328 
    329                 } else {
    330                         if (operation.equals("explain") || operation == null){
    331                                 log.debug("explaining", "debug");
    332                         }else {
    333                                 if (operation.equals("scan")){
    334                                         if (this.scanClause == null) {
    335                                                 this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "scanClause");
    336                                                 //return -1;
    337                                         } else{
    338                                                 if (this.scanClause.equals("cmd.Collections")){
    339                                                        
    340                                                 } else {
    341                                                         if (this.scanClause.equals("cmd.Repository")){
    342                                                                 XMLSerializer xmlserializer = new XMLSerializer();
    343                                                                 String xml = xmlserializer.write( WorkspaceProfile.getRepositories());//mdrepoproxyaction.getRepositories() ); 
    344                                                                 this.setResultStream(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    345 
    346                                                         } else {
    347                                                                
    348                                                         }
    349                                                 }
    350                                         }
    351                                 }else {
    352                                         this.Diagnostics().Add(Diagnostic.UNSUPPOERTED_OPERATION, "Only searchRetrieve,explain scan supported.");                               }
    353                         }
    354                 }
    355         }
    356        
    357         public void sru2MD() throws UnsupportedEncodingException{
    358                 // special handling of parameter setting, x-cmd-... not alowed in java
    359                 this.setX_cmd_collections(getServletRequest().getParameter("x-cmd-collections"));               
    360                 // root call own SRU SERVER
    361                 //operation
    362                 if (operation.equals("explain") || operation == null){
    363                         // TODO: version param is mandatory for explain as well, but for root-request not!
    364                         //explain
    365                         termsproxyaction = new TermsProxyAction();
    366                         termsproxyaction.setDiagnostics(diagnostics);
    367                         termsproxyaction.setActionkey("terms");
    368                        
    369                         termsproxyaction.setQ("all");                   
    370                         termsproxyaction.setFormat("sru-explain");                     
    371                        
    372                         //return -1;
    373                 } else {                               
    374                         //version               
    375                         if (this.version == null) {
    376                                 this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "version");
    377                                 //return -1;
    378                         } else {
    379                                 if (!version.equals("1.2")) {
    380                                         this.Diagnostics().Add(Diagnostic.UNSUPPORTED_VERSION, "Version 1.2 supported.");
    381                                         //return -1;
    382                                 }
    383                         }
    384                        
    385                         prepareOperation();
    386                 }
    387                
    388         }
    389        
    390         public void  prepareMD() throws Exception{
    391                 sru2MD();
    392                 if (Diagnostics().Accepted()){
    393                         // root-explain
    394                         if (this.termsproxyaction != null) {                   
    395                                 if (this.termsproxyaction.execute() == SUCCESS){
    396                                         this.setResultStream(this.termsproxyaction.getResultStream());
    397                                         //return SUCCESS;
    398                                 }
    399                         } else {
    400                                 //TODO diagnostic                       
    401                                 if (this.mdrepoproxyaction.getTargetRequest() == null) {
    402                                         Diagnostics().Add(Diagnostic.QUERYSYNTAXERROR);
    403                                         //return ERROR;
    404                                 }
    405                                 if (this.mdrepoproxyaction.execute() == SUCCESS){
    406                                         this.setResultStream(this.mdrepoproxyaction.getResultStream());
    407                                         postprocess();
    408                                         //return SUCCESS;
    409                                 }
    410                         }
    411                 }
    412                 //return ERROR;
    413         }
    414         */
    415        
    416 /** TODO: move to Utils
    417  *
    418  * @param stream
    419  * @return
    420  * @throws IOException
    421  */
    422         public static String inputStreamAsString(InputStream stream)
    423                 throws IOException {
    424                 BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    425                 StringBuilder sb = new StringBuilder();
    426                 String line = null;
    427 
    428                 while ((line = br.readLine()) != null) {
    429                 sb.append(line + "\n");
    430                 }
    431 
    432                 br.close();
    433                 return sb.toString();
    434         }
    435 
    436          public static Document newDocumentFromInputStream(InputStream in) {
    437                     DocumentBuilderFactory factory = null;
    438                     DocumentBuilder builder = null;
    439                     Document ret = null;
    440 
    441                     try {
    442                       factory = DocumentBuilderFactory.newInstance();
    443                       builder = factory.newDocumentBuilder();
    444                     } catch (ParserConfigurationException e) {
    445                       e.printStackTrace();
    446                     }
    447 
    448                     try {
    449                       ret = builder.parse(new InputSource(in));
    450                     } catch (SAXException e) {
    451                       e.printStackTrace();
    452                     } catch (IOException e) {
    453                       e.printStackTrace();
    454                     }
    455                     return ret;
    456                   }
    457 
    458 /*
    459         public void postprocess() throws TransformerConfigurationException, UnsupportedEncodingException, ParserConfigurationException, TransformerException, TransformerFactoryConfigurationError{
    460                 //recordPacking
    461                 if (this.getRecordPacking() != null){
    462                         if (this.getRecordPacking().toLowerCase().equals("string")){
    463                                 try {
    464                                         this.setResultStream(IOUtils.toInputStream(StringEscapeUtils.escapeXml(inputStreamAsString(this.getResultStream()))));
    465                                 } catch (IOException e) {
    466                                         // TODO Auto-generated catch block
    467                                         e.printStackTrace();
    468                                 }
    469 
    470                         }
    471                 }
    472                
    473                 // add diagnostics
    474                 if (Diagnostics().Exists()){
    475                         Document doc = newDocumentFromInputStream(this.getResultStream());
    476                         Node r_element=  doc.getElementsByTagName("searchRetrieveResponse").item(0);
    477                         Document ddoc = Diagnostics().buildXMLDocument();
    478                         Node d_element=  ddoc.getElementsByTagName("diagnostics").item(0);
    479                         doc.adoptNode(d_element);
    480                         r_element.appendChild(d_element);
    481                         DOMSource source = new DOMSource(doc);   
    482                         StringWriter xmlAsWriter = new StringWriter();   
    483                         StreamResult result = new StreamResult(xmlAsWriter);   
    484                         TransformerFactory.newInstance().newTransformer().transform(source, result);
    485                         this.setResultStream(new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8")));
    486                 }
    487         }
    488         */
    489         //TODO create query + query.toURLParams ?
    490         public String createURLParams(){
    491                 String params = "";
    492                
    493                 //TODO params
    494                 if (paramSet == null){
    495                         if (!(getParam("operation") == null)){
    496                                 params = params + "operation=" + getParam("operation");
    497                         }
    498                         if (!(getParam("version") == null)){
    499                                 params = params + "&version=" + getParam("version");
    500                         }
    501                         if (!(getParam("query") == null)){
    502                                 params = params + "&query=" + getParam("query");
    503                         }
    504                         if (Integer.parseInt(getParam("startRecord")) > 0){
    505                                 params = params + "&startRecord=" + String.valueOf(getParam("startRecord"));
    506                         }
    507                         if (Integer.parseInt(getParam("maximumRecords"))> 0){
    508                                 params = params + "&numRecords=" + String.valueOf(getParam("maximumRecords"));
    509                         }
    510                 } else {
    511                         Iterator i = paramSet.iterator();
    512                         params = "";
    513                     while(i.hasNext()){
    514                               Map.Entry me = (Map.Entry)i.next();
    515                               if (!(me.getKey().equals("x-cmd-repository") || me.getKey().equals("x-cmd-collections"))){
    516                                       params = params + "&" + me.getKey() + "=" + ((String[])me.getValue())[0]; 
    517                               }
    518                         }
    519                     params = params.substring(1);
    520 
    521                 }
    522 
    523                 return params;
    524         }
    525         @Override
    526         public URL getTargetRequest() throws IOException {
    527                 String requestURI = "";
    528                 URL targetURL;
    529                
    530                 if (this.getBaseURI() == null){
    531                         requestURI = getParam("x_cmd_repository");//this.getX_cmd_repository();
    532                 } else {
    533                         requestURI = this.getBaseURI();
    534                 }
    535            
    536             targetURL = new URL(requestURI + "?" + createURLParams());
    537                 return targetURL;
    538         }
    539 
    540 
    541         /*
    542         @Override
    543         public void prepare() throws Exception{
    544                 //this.setX_cmd_repository(getServletRequest().getParameter("x-cmd-repository"));
    545                 //this.setRepository(this.getX_cmd_repository());
    546                 //TODO xsl usage according particular repositories ?
    547                 //this.setActionkey("sru");
    548                 //this.setFormat("xml");
    549                
    550                 //extern SRU repository
    551                 if (WorkspaceProfile.isSRURepository(getRepository()) || this.getBaseURI() == null){
    552                         super.prepare();
    553                 }
    554                 else {
    555                          prepareMD();
    556                 }
    557                
    558         }
    559         */
    560 
    56178}
  • MDService2/branches/MDService_simple3/src/struts.xml

    r1613 r1627  
    122122               
    123123                <action name="sru" class="eu.clarin.cmdi.mdservice.action.SRUAction" method="execute">
    124                         <param name="q">{1}</param>
     124                        <param name="actionkey">sru</param>
    125125                        <result name="success" type="stream">                         
    126126                                <param name="contentType">text/xml</param>
Note: See TracChangeset for help on using the changeset viewer.