Ignore:
Timestamp:
04/04/12 08:38:32 (12 years ago)
Author:
gaba
Message:

cache changes

File:
1 edited

Legend:

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

    r1832 r1852  
    4141         * Properties to be filled by Struts with according request-parameters
    4242         */     
    43         /*
    44         private String q;
    45         private String version;
    46         private String operation;
    47         private String query;
    48         private String scanClause;
    49         private int startRecord;
    50         private int maximumRecords;
    51         private String recordPacking;
    52         private String recordSchema;
    53         private String recordXPath;
    54         private int resultSetTTL;
    55         private String sortKeys;
    56         private String stylesheet;     
    57         private String x_cmd_repository;
    58         private String x_cmd_collections;
    59         private Set paramSet;
    60 */
    6143       
    6244        private static int OP_SEARCHRETRIEVE = 1;
     
    6648       
    6749        //TODO cache after analysis ?
     50        /*
    6851        @Override
    6952        public String getCache() {
    7053                return Cache.SKIP;
    7154        }
     55        */
    7256       
    7357        @Override
     
    9074        }
    9175       
     76        /*
    9277        public String getOperation() {
    9378                if (getParams().get("operation") == null){
     
    9681                return getParam("operation");
    9782        }
    98        
     83        */
    9984         protected final static HashMap<String,Integer> operation_code = new HashMap<String,Integer>();
    10085         static
     
    11499                return "";
    115100        }
    116 /*
    117         @Override
    118         protected void setDefaultParams() {             
    119                 // set defaults
    120         /*      if ( params.get("version") == null){
    121                         addParam("version","1.2");
    122                 }
    123                 if ( params.get("operation") == null){
    124                         addParam("operation","searchRetrieve");
    125                 }
    126                 //q=query                       
    127                 if (getQ() != null) {
    128                         addParam("q",getQ());
    129                         addParam("query",getQ());
    130                 } else {
    131                         addParam("q",this.getParam("query"));
    132                 }
    133                
    134                 if ( params.get("startRecord") == null){
    135                         addParam("startRecord","1");
    136                 }
    137                 if ( params.get("maximumRecords") == null){
    138                         addParam("maximumRecords","50");
    139                 }
    140                
    141                 if ( params.get("x-cmd-maxdepth") == null){
    142                         if (params.get("maxdepth") == null){
    143                                 addParam("maxdepth","2");
    144                         }
    145                 } else {
    146                         addParam("maxdepth",params.get("x-cmd-maxdepth")[0]);
    147                 }
    148                
    149                 if ( params.get("x-cmd-repository") == null){
    150                         if (params.get("repository") == null){
    151                                 addParam("repository",WorkspaceProfile.getRepositoryByIndex(0));
    152                         }
    153                 } else {
    154                         addParam("repository",params.get("x-cmd-repository")[0]);
    155                 }
    156                
    157                 if ( params.get("x-cmd-cache") == null){
    158                         if (params.get("cache") == null){
    159                                 addParam("cache",Cache.SKIP);
    160                         }
    161                 } else {
    162                         addParam("cache",params.get("x-cmd-cache")[0]);
    163                 }
    164                
    165                 if ( params.get("x-cmd-lang") == null){
    166                         if (params.get("lang") == null){
    167                                 addParam("lang","en");
    168                         }
    169                 } else {
    170                         addParam("lang",params.get("x-cmd-lang")[0]);
    171                 }
    172                
    173                 if ( params.get("x-cmd-format") == null){
    174                         if (params.get("format") == null){
    175                                 if (this.format == null) {
    176                                         addParam("format","xml");
    177                                 } else if (this.format.equals("")){
    178                                         addParam("format","xml");
    179                                 } else {
    180                                         addParam("format",this.format);
    181                                 }
    182                         }
    183                 } else {
    184                         addParam("format",params.get("x-cmd-format")[0]);
    185                 }
    186 
    187                 addParam("fullformat",getFullFormat());
    188                
    189         }
    190 */
    191         /*
    192         public void prepareOperation() throws UnsupportedEncodingException{
    193                
    194                 mdrepoproxyaction = new MDRepoProxyAction();
    195                 mdrepoproxyaction.setDiagnostics(diagnostics);
    196                 mdrepoproxyaction.setActionkey(hash_operations.get(this.getOperation()));
    197                
    198                 // set the SRU extern property in proxyaction
    199                 if (this.getX_cmd_repository() != null ) {
    200                         mdrepoproxyaction.setRepository(this.getX_cmd_repository());
    201                         //TODO\
    202                         if (mdrepoproxyaction.getBaseURI() == null) this.Diagnostics().Add(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "Unknown repository." + getX_cmd_repository());
    203                 }
    204                
    205                 if (operation.equals("searchRetrieve")){
    206                        
    207                         //query
    208                         mdrepoproxyaction.setSquery("");
    209                         if (this.query == null) {
    210                                 this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "query");
    211                                 //return -1;
    212                         }
    213                         mdrepoproxyaction.setQ(getQuery());                     
    214                         mdrepoproxyaction.setFormat("xml");
    215                        
    216                         if ((this.startRecord < 0 ) ||((getServletRequest().getParameter("startRecord") != null)&&(getServletRequest().getParameter("startRecord").equals("0")))) {
    217                                 this.Diagnostics().Add(new Diagnostic(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "startRecord must by grater than 0."));//,Diagnostic.NONFATALNONSURORGATE));
    218                                 //mdrepoproxyaction.setStartItem("1");
    219                         } else {
    220                                 mdrepoproxyaction.setStartItem(Integer.toString(getStartRecord()));
    221                         }
    222                
    223                         if ((this.maximumRecords < 0 ) ||((getServletRequest().getParameter("maximumRecords") != null) && (getServletRequest().getParameter("maximumRecords").equals("0")))){
    224                                    this.Diagnostics().Add(new Diagnostic(Diagnostic.UNSUPPORTED_PARAMETERVALUE,
    225                                                    "maximumRecords must by greater than 0."));
    226                         } else {
    227                                 mdrepoproxyaction.setMaximumItems(Integer.toString(this.getMaximumRecords())); 
    228                         }
    229                        
    230                         if (this.getX_cmd_repository() != null ) {
    231                                 mdrepoproxyaction.setRepository(this.getX_cmd_repository());
    232                                 //TODO\
    233                                 if (mdrepoproxyaction.getBaseURI() == null) this.Diagnostics().Add(Diagnostic.UNSUPPORTED_PARAMETERVALUE, "Unknown repository." + getX_cmd_repository());
    234                         }
    235                         if (this.getX_cmd_collections() != null ) {
    236                                 mdrepoproxyaction.setCollection(this.getX_cmd_collections());
    237                         }
    238 
    239                 } else {
    240                         if (operation.equals("explain") || operation == null){
    241                                 log.debug("explaining", "debug");
    242                         }else {
    243                                 if (operation.equals("scan")){
    244                                         if (this.scanClause == null) {
    245                                                 this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "scanClause");
    246                                                 //return -1;
    247                                         } else{
    248                                                 if (this.scanClause.equals("cmd.Collections")){
    249                                                        
    250                                                 } else {
    251                                                         if (this.scanClause.equals("cmd.Repository")){
    252                                                                 XMLSerializer xmlserializer = new XMLSerializer();
    253                                                                 String xml = xmlserializer.write( WorkspaceProfile.getRepositories());//mdrepoproxyaction.getRepositories() ); 
    254                                                                 this.setResultStream(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    255 
    256                                                         } else {
    257                                                                
    258                                                         }
    259                                                 }
    260                                         }
    261                                 }else {
    262                                         this.Diagnostics().Add(Diagnostic.UNSUPPOERTED_OPERATION, "Only searchRetrieve,explain scan supported.");                               }
    263                         }
    264                 }
    265         }
    266101       
    267         public void sru2MD() throws UnsupportedEncodingException{
    268                 // special handling of parameter setting, x-cmd-... not alowed in java
    269                 this.setX_cmd_collections(getServletRequest().getParameter("x-cmd-collections"));               
    270                 // root call own SRU SERVER
    271                 //operation
    272                 if (operation.equals("explain") || operation == null){
    273                         // TODO: version param is mandatory for explain as well, but for root-request not!
    274                         //explain
    275                         termsproxyaction = new TermsProxyAction();
    276                         termsproxyaction.setDiagnostics(diagnostics);
    277                         termsproxyaction.setActionkey("terms");
    278                        
    279                         termsproxyaction.setQ("all");                   
    280                         termsproxyaction.setFormat("sru-explain");                     
    281                        
    282                         //return -1;
    283                 } else {                               
    284                         //version               
    285                         if (this.version == null) {
    286                                 this.Diagnostics().Add(Diagnostic.MANDATORY_NOTSUPPLIED, "version");
    287                                 //return -1;
    288                         } else {
    289                                 if (!version.equals("1.2")) {
    290                                         this.Diagnostics().Add(Diagnostic.UNSUPPORTED_VERSION, "Version 1.2 supported.");
    291                                         //return -1;
    292                                 }
    293                         }
    294                        
    295                         prepareOperation();
    296                 }
    297                
    298         }
    299102       
    300         public void  prepareMD() throws Exception{
    301                 sru2MD();
    302                 if (Diagnostics().Accepted()){
    303                         // root-explain
    304                         if (this.termsproxyaction != null) {                   
    305                                 if (this.termsproxyaction.execute() == SUCCESS){
    306                                         this.setResultStream(this.termsproxyaction.getResultStream());
    307                                         //return SUCCESS;
    308                                 }
    309                         } else {
    310                                 //TODO diagnostic                       
    311                                 if (this.mdrepoproxyaction.getTargetRequest() == null) {
    312                                         Diagnostics().Add(Diagnostic.QUERYSYNTAXERROR);
    313                                         //return ERROR;
    314                                 }
    315                                 if (this.mdrepoproxyaction.execute() == SUCCESS){
    316                                         this.setResultStream(this.mdrepoproxyaction.getResultStream());
    317                                         postprocess();
    318                                         //return SUCCESS;
    319                                 }
    320                         }
    321                 }
    322                 //return ERROR;
    323         }
    324         */
    325 /** TODO: move to Utils
    326  *
    327  * @param stream
    328  * @return
    329  * @throws IOException
    330  */
    331         /*
    332         public static String inputStreamAsString(InputStream stream)
    333                 throws IOException {
    334                 BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    335                 StringBuilder sb = new StringBuilder();
    336                 String line = null;
    337 
    338                 while ((line = br.readLine()) != null) {
    339                 sb.append(line + "\n");
    340                 }
    341 
    342                 br.close();
    343                 return sb.toString();
    344         }
    345 
    346        
    347 
    348         public void postprocess() throws TransformerConfigurationException, UnsupportedEncodingException, ParserConfigurationException, TransformerException, TransformerFactoryConfigurationError{
    349                 //recordPacking
    350                 if (this.getRecordPacking() != null){
    351                         if (this.getRecordPacking().toLowerCase().equals("string")){
    352                                
    353                                 try {
    354                                         // FIXME: Which package is IOUTils from?
    355                                         this.setResultStream(IOUtils.toInputStream(StringEscapeUtils.escapeXml(inputStreamAsString(this.getResultStream()))));
    356                                 } catch (IOException e) {
    357                                         // TODO Auto-generated catch block
    358                                         e.printStackTrace();
    359                                 }
    360 
    361                         }
    362                 }
    363                
    364                 // add diagnostics
    365                 if (getDiagnostics().Exists()){
    366                         Document doc = Utils.stream2Document(getResultStream());
    367                         Node r_element=  doc.getElementsByTagName("searchRetrieveResponse").item(0);
    368                         Document ddoc = getDiagnostics().buildXMLDocument();
    369                         Node d_element=  ddoc.getElementsByTagName("diagnostics").item(0);
    370                         doc.adoptNode(d_element);
    371                         r_element.appendChild(d_element);
    372                         DOMSource source = new DOMSource(doc);   
    373                         StringWriter xmlAsWriter = new StringWriter();   
    374                         StreamResult result = new StreamResult(xmlAsWriter);   
    375                         TransformerFactory.newInstance().newTransformer().transform(source, result);
    376                         this.setResultStream(new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8")));
    377                 }
    378         }
    379        
    380         //TODO create query + query.toURLParams ?
    381         public String createURLParams(){
    382                 String params = "";
    383                
    384                 //TODO params
    385                 /*
    386                 if (paramSet == null){
    387                         if (!(operation == null)){
    388                                 params = params + "operation=" + operation;
    389                         }
    390                         if (!(version == null)){
    391                                 params = params + "&version=" + version;
    392                         }
    393                         if (!(query == null)){
    394                                 params = params + "&query=" + query;
    395                         }
    396                         if (getStartRecord() > 0){
    397                                 params = params + "&startRecord=" + String.valueOf(getStartRecord());
    398                         }
    399                         if (getMaximumRecords()> 0){
    400                                 params = params + "&numRecords=" + String.valueOf(getMaximumRecords());
    401                         }
    402                 } else {
    403                         Iterator i = paramSet.iterator();
    404                         params = "";
    405                     while(i.hasNext()){
    406                               Map.Entry me = (Map.Entry)i.next();
    407                               if (!(me.getKey().equals("x-cmd-repository") || me.getKey().equals("x-cmd-collections"))){
    408                                       params = params + "&" + me.getKey() + "=" + ((String[])me.getValue())[0]; 
    409                               }
    410                         }
    411                     params = params.substring(1);
    412 
    413                 }
    414 
    415                 return params;
    416         }
    417 */     
    418 
    419 /*     
    420         @Override
    421         public void prepare() throws Exception{
    422                 //this.setX_cmd_repository(getServletRequest().getParameter("x-cmd-repository"));
    423                 //this.setRepository(this.getX_cmd_repository());
    424                 //TODO xsl usage according particular repositories ?
    425                 //this.setActionkey("sru");
    426                 //this.setFormat("xml");
    427                
    428                 //extern SRU repository
    429                 if (WorkspaceProfile.isSRURepository(getRepository()) || this.getBaseURI() == null){
    430                         super.prepare();
    431                 }
    432                 else {
    433                          prepareMD();
    434                 }
    435                
    436         }
    437 */
    438103        @Override
    439104        public void setTargetProxy(){
Note: See TracChangeset for help on using the changeset viewer.