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

sru - base

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.