Changeset 14395

Show
Ignore:
Timestamp:
31.08.2007 11:46:25 (12 years ago)
Author:
anna
Message:

For preference page request, call aboutPage action to get the description of the (search) services supported, to enable the search mode option and the search options on the preference page.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/action/PageAction.java

    r14311 r14395  
    33import org.greenstone.gsdl3.core.ModuleInterface; 
    44import org.greenstone.gsdl3.util.*; 
    5 // XML classes 
     5//XML classes 
    66import org.w3c.dom.Node;  
    77import org.w3c.dom.NodeList;  
     
    1515 
    1616public class PageAction extends Action { 
    17     
    18      static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.PageAction.class.getName()); 
    19  
    20     public static final String HOME_PAGE = "home"; 
    21     public static final String ABOUT_PAGE = "about"; 
    22     public static final String PREFS_PAGE = "pref"; 
    23     public static final String GLI4GS3_PAGE="gli4gs3"; 
    24  
    25     public Element process (Element message) { 
     17 
     18    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.PageAction.class.getName()); 
     19 
     20    public static final String HOME_PAGE = "home"; 
     21    public static final String ABOUT_PAGE = "about"; 
     22    public static final String PREFS_PAGE = "pref"; 
     23    public static final String GLI4GS3_PAGE="gli4gs3"; 
     24 
     25    public Element process (Element message) { 
     26 
     27        Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); 
     28        // the page name is the subaction 
     29        String page_name = request.getAttribute(GSXML.SUBACTION_ATT); 
     30        if (page_name.equals("")) { // if no page specified, assume home page 
     31            page_name = HOME_PAGE; 
     32        } 
     33        Element result = this.doc.createElement(GSXML.MESSAGE_ELEM); 
     34        Element response; 
     35        if (page_name.equals(HOME_PAGE)) { 
     36            response = homePage(request); 
     37        //} else if (page_name.equals(ABOUT_PAGE)) { 
     38        } else if (page_name.equals(ABOUT_PAGE) || page_name.equals(PREFS_PAGE)) { 
     39            response = aboutPage(request); 
     40        //}else if (page_name.equals(PREFS_PAGE)) { 
     41            //response = prefsPage(request); 
     42        } else if (page_name.equals(GLI4GS3_PAGE)){ 
     43            response = gli4gs3Page(request); 
     44        }else { // unknown page 
     45 
     46            logger.error("unknown page specified!"); 
     47            response =  unknownPage(request); 
     48        } 
     49 
     50        result.appendChild(this.doc.importNode(response, true)); 
     51        logger.debug("page action result: "+this.converter.getPrettyString(result)); 
     52        return result; 
     53    } 
     54 
     55 
     56    protected Element homePage(Element request) { 
     57 
     58        String lang = request.getAttribute(GSXML.LANG_ATT); 
     59        String uid = request.getAttribute(GSXML.USER_ID_ATT); 
     60        // first, get the message router info 
     61        Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
     62        Element coll_list_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid); 
     63        info_message.appendChild(coll_list_request); 
     64        Element info_response_message = (Element)this.mr.process(info_message); 
     65        if (info_response_message==null) { 
     66            logger.error(" couldn't query the message router!"); 
     67            return null; 
     68        } 
     69        Element info_response = (Element)GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM); 
     70        if (info_response==null) { 
     71            logger.error("couldn't query the message router!"); 
     72            return null; 
     73        } 
     74 
     75        // second, get the metadata for each collection - we only want specific 
     76        // elements but for now, we'll just get it all 
     77        Element collection_list = (Element)GSXML.getChildByTagName(info_response, GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER); 
     78        logger.info(GSXML.xmlNodeToString(collection_list)); 
     79        if (collection_list != null) { 
     80            NodeList colls = GSXML.getChildrenByTagName(collection_list, GSXML.COLLECTION_ELEM); 
     81            if (colls.getLength()>0) { 
     82                sendMultipleRequests(colls, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
     83            } 
     84        } 
     85 
     86        // get metadata for any services 
     87        Element service_list = (Element)GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER); 
     88        if (service_list != null) { 
     89            NodeList services = GSXML.getChildrenByTagName(service_list, GSXML.SERVICE_ELEM); 
     90            if (services.getLength() > 0) { 
     91                sendMultipleRequests(services, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
     92            } 
     93        } 
     94 
     95        // get metadata for service clusters 
     96        Element cluster_list = (Element)GSXML.getChildByTagName(info_response, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER); 
     97        if (cluster_list != null) { 
     98            NodeList clusters = GSXML.getChildrenByTagName(cluster_list, GSXML.CLUSTER_ELEM); 
     99            if (clusters.getLength() > 0) { 
     100                sendMultipleRequests(clusters, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
     101 
     102            } 
     103        } 
     104 
     105        // all the components have been merged into info_response 
     106        return info_response; 
     107 
     108    } // homePage 
     109 
     110    protected Element aboutPage(Element request) { 
     111 
     112        String lang = request.getAttribute(GSXML.LANG_ATT); 
     113        String uid = request.getAttribute(GSXML.USER_ID_ATT); 
     114        // extract the params from the cgi-request,  
     115        Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);         
     116        HashMap params = GSXML.extractParams(cgi_paramList, false); 
     117         
     118        String coll_name = (String)params.get(GSParams.COLLECTION); 
     119        if (coll_name == null || coll_name.equals("")) { 
     120            logger.error("about page requested with no collection or cluster specified!"); 
     121            // return an empty response 
     122            return this.doc.createElement(GSXML.RESPONSE_ELEM); 
     123        } 
     124 
     125        // get the collection or cluster description 
     126        Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
     127 
     128        Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE,  coll_name, lang, uid); 
     129        coll_about_message.appendChild(coll_about_request); 
     130 
     131        Element coll_about_response = (Element)this.mr.process(coll_about_message); 
     132 
     133        if (coll_about_response == null) { 
     134            return null; 
     135        } 
     136         
     137        // second, get the info for each service - we only want display items  
     138        // but for now, we'll just get it all 
     139        NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM); 
     140        if (services.getLength() > 0) { 
     141            sendMultipleRequests(services, coll_name, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
     142        } 
     143 
     144        Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM); 
     145        return response; 
     146    } 
    26147     
    27     Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); 
    28     // the page name is the subaction 
    29     String page_name = request.getAttribute(GSXML.SUBACTION_ATT); 
    30     if (page_name.equals("")) { // if no page specified, assume home page 
    31         page_name = HOME_PAGE; 
    32     } 
    33     Element result = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    34     Element response; 
    35     if (page_name.equals(HOME_PAGE)) { 
    36         response = homePage(request); 
    37     } else if (page_name.equals(ABOUT_PAGE)) { 
    38         response = aboutPage(request); 
    39 //      } else if (page_name.equals(PREFS_PAGE)) { 
    40 //          response = prefsPage(request); 
    41     } else if (page_name.equals(GLI4GS3_PAGE)){ 
    42         response = gli4gs3Page(request); 
    43     }else { // unknown page 
    44          
    45         logger.error("unknown page specified!"); 
    46         response =  unknownPage(request); 
    47     } 
     148    //protected Element prefsPage(Element request) { 
    48149     
    49     result.appendChild(this.doc.importNode(response, true)); 
    50     logger.debug("page action result: "+this.converter.getPrettyString(result)); 
    51     return result; 
    52     } 
    53  
    54  
    55     protected Element homePage(Element request) { 
     150    //  return null; 
     151    //} 
    56152     
    57     String lang = request.getAttribute(GSXML.LANG_ATT); 
    58     String uid = request.getAttribute(GSXML.USER_ID_ATT); 
    59     // first, get the message router info 
    60     Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    61     Element coll_list_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid); 
    62     info_message.appendChild(coll_list_request); 
    63     Element info_response_message = (Element)this.mr.process(info_message); 
    64     if (info_response_message==null) { 
    65         logger.error(" couldn't query the message router!"); 
    66         return null; 
    67     } 
    68     Element info_response = (Element)GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM); 
    69     if (info_response==null) { 
    70         logger.error("couldn't query the message router!"); 
    71         return null; 
    72     } 
    73          
    74     // second, get the metadata for each collection - we only want specific 
    75     // elements but for now, we'll just get it all 
    76     Element collection_list = (Element)GSXML.getChildByTagName(info_response, GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER); 
    77     logger.info(GSXML.xmlNodeToString(collection_list)); 
    78         if (collection_list != null) { 
    79         NodeList colls = GSXML.getChildrenByTagName(collection_list, GSXML.COLLECTION_ELEM); 
    80         if (colls.getLength()>0) { 
    81         sendMultipleRequests(colls, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
    82         } 
    83     } 
    84  
    85     // get metadata for any services 
    86     Element service_list = (Element)GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER); 
    87     if (service_list != null) { 
    88         NodeList services = GSXML.getChildrenByTagName(service_list, GSXML.SERVICE_ELEM); 
    89         if (services.getLength() > 0) { 
    90         sendMultipleRequests(services, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
    91         } 
    92     } 
    93  
    94     // get metadata for service clusters 
    95     Element cluster_list = (Element)GSXML.getChildByTagName(info_response, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER); 
    96     if (cluster_list != null) { 
    97         NodeList clusters = GSXML.getChildrenByTagName(cluster_list, GSXML.CLUSTER_ELEM); 
    98         if (clusters.getLength() > 0) { 
    99         sendMultipleRequests(clusters, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
    100          
    101         } 
    102     } 
    103      
    104     // all the components have been merged into info_response 
    105     return info_response; 
    106      
    107     } // homePage 
    108  
    109     protected Element aboutPage(Element request) { 
    110  
    111     String lang = request.getAttribute(GSXML.LANG_ATT); 
    112     String uid = request.getAttribute(GSXML.USER_ID_ATT); 
    113     // extract the params from the cgi-request,  
    114     Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 
    115     HashMap params = GSXML.extractParams(cgi_paramList, false); 
    116      
    117     String coll_name = (String)params.get(GSParams.COLLECTION); 
    118     if (coll_name == null || coll_name.equals("")) { 
    119         logger.error("about page requested with no collection or cluster specified!"); 
    120         // return an empty response 
    121         return this.doc.createElement(GSXML.RESPONSE_ELEM); 
    122     } 
    123      
    124     // get the collection or cluster description 
    125     Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    126      
    127     Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE,  coll_name, lang, uid); 
    128     coll_about_message.appendChild(coll_about_request); 
    129      
    130     Element coll_about_response = (Element)this.mr.process(coll_about_message); 
    131  
    132     if (coll_about_response == null) { 
    133         return null; 
    134     } 
    135      
    136     // second, get the info for each service - we only want display items  
    137     // but for now, we'll just get it all 
    138     NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM); 
    139     if (services.getLength() > 0) { 
    140         sendMultipleRequests(services, coll_name, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
    141     } 
    142      
    143     Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM); 
    144     return response; 
    145     } 
    146  
    147 //      protected Element prefsPage(Element request) { 
    148  
    149 //      return null; 
    150 //      } 
    151  
    152     /** if we dont know the page type, use this method */ 
    153     protected Element unknownPage(Element request) { 
    154      
    155     String lang = request.getAttribute(GSXML.LANG_ATT); 
    156     String uid = request.getAttribute(GSXML.USER_ID_ATT); 
    157     String page_name = request.getAttribute(GSXML.SUBACTION_ATT); 
    158  
    159     // extract the params from the cgi-request,  
    160     Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 
    161     HashMap params = GSXML.extractParams(cgi_paramList, false); 
    162      
    163     String coll_name = (String)params.get(GSParams.COLLECTION); 
    164     if (coll_name == null || coll_name.equals("")) { 
    165         // just return an empty response 
    166         return this.doc.createElement(GSXML.RESPONSE_ELEM); 
    167     } 
    168      
    169     // else get the coll description - actually this is the same as for the about page - should we merge these two methods?? 
    170      
    171     // if there is a service specified should we get the service description instead?? 
    172     // get the collection or cluster description 
    173     Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    174      
    175     Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE,  coll_name, lang, uid); 
    176     coll_about_message.appendChild(coll_about_request); 
    177      
    178     Element coll_about_response = (Element)this.mr.process(coll_about_message); 
    179  
    180     Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM); 
    181     return response; 
    182      
    183     } 
    184  
    185  
    186     protected boolean sendMultipleRequests(NodeList items, String path_prefix, String request_type, String lang, String uid) { 
    187  
    188     // we will send all the requests in a single message 
    189     Element message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    190     for (int i=0; i<items.getLength(); i++) { 
    191         Element c = (Element)items.item(i); 
    192         String path = c.getAttribute(GSXML.NAME_ATT);        
    193         if (path_prefix != null) { 
    194         path = GSPath.appendLink(path_prefix, path); 
    195         } 
    196         Element request = GSXML.createBasicRequest(this.doc, request_type, path, lang, uid); 
    197         message.appendChild(request); 
    198     } 
    199      
    200     Element response_message = (Element)this.mr.process(message); 
    201          
    202     NodeList responses = response_message.getElementsByTagName(GSXML.RESPONSE_ELEM); 
    203     // check that have same number of responses as requests 
    204     if (items.getLength() != responses.getLength()) { 
    205         logger.error("didn't get a response for each request - somethings gone wrong!"); 
    206         return false; 
    207     }  
    208      
    209     for (int i=0; i<items.getLength(); i++) { 
    210         Element c1 = (Element)items.item(i); 
    211         Element c2 = (Element)GSXML.getChildByTagName((Element)responses.item(i), c1.getTagName()); 
    212             if (c1 != null && c2 !=null && c1.getAttribute(GSXML.NAME_ATT).endsWith(c2.getAttribute(GSXML.NAME_ATT))) { 
    213         //add the new data into the original element 
    214         GSXML.mergeElements(c1, c2); 
    215         } else { 
    216         logger.error(" response does not correspond to request!"); 
    217         }  
    218          
    219     } 
    220      
    221     return true; 
    222      
    223     } 
    224  
    225     protected Element gli4gs3Page(Element request) { 
    226     String lang = request.getAttribute(GSXML.LANG_ATT); 
    227     String uid = request.getAttribute(GSXML.USER_ID_ATT); 
    228  
    229     Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM); 
    230      
    231     Element applet_elem = this.doc.createElement("Applet"); 
    232     page_response.appendChild(applet_elem); 
    233     applet_elem.setAttribute("ARCHIVE","SignedGatherer.jar"); 
    234     applet_elem.setAttribute("CODE","org.greenstone.gatherer.GathererApplet4gs3"); 
    235     applet_elem.setAttribute("CODEBASE","applet"); 
    236     applet_elem.setAttribute("HEIGHT","50"); 
    237     applet_elem.setAttribute("WIDTH","380"); 
    238     Element gwcgi_param_elem= this.doc.createElement("PARAM"); 
    239     gwcgi_param_elem.setAttribute("name","gwcgi"); 
    240     String library_name=GlobalProperties.getGSDL3WebAddress(); 
    241     gwcgi_param_elem.setAttribute("value",library_name); 
    242     applet_elem.appendChild(gwcgi_param_elem); 
    243  
    244     Element gsdl3_param_elem= this.doc.createElement("PARAM"); 
    245     gsdl3_param_elem.setAttribute("name","gsdl3"); 
    246     gsdl3_param_elem.setAttribute("value","true"); 
    247     applet_elem.appendChild(gsdl3_param_elem); 
    248      
    249     return page_response; 
    250     } 
     153    /** if we dont know the page type, use this method */ 
     154    protected Element unknownPage(Element request) { 
     155 
     156        String lang = request.getAttribute(GSXML.LANG_ATT); 
     157        String uid = request.getAttribute(GSXML.USER_ID_ATT); 
     158        String page_name = request.getAttribute(GSXML.SUBACTION_ATT); 
     159 
     160        // extract the params from the cgi-request,  
     161        Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 
     162        HashMap params = GSXML.extractParams(cgi_paramList, false); 
     163 
     164        String coll_name = (String)params.get(GSParams.COLLECTION); 
     165        if (coll_name == null || coll_name.equals("")) { 
     166            // just return an empty response 
     167            return this.doc.createElement(GSXML.RESPONSE_ELEM); 
     168        } 
     169 
     170        // else get the coll description - actually this is the same as for the about page - should we merge these two methods?? 
     171 
     172        // if there is a service specified should we get the service description instead?? 
     173        // get the collection or cluster description 
     174        Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
     175 
     176        Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE,  coll_name, lang, uid); 
     177        coll_about_message.appendChild(coll_about_request); 
     178         
     179        Element coll_about_response = (Element)this.mr.process(coll_about_message); 
     180 
     181        Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM); 
     182        return response; 
     183 
     184    } 
     185 
     186 
     187    protected boolean sendMultipleRequests(NodeList items, String path_prefix, String request_type, String lang, String uid) { 
     188 
     189        // we will send all the requests in a single message 
     190        Element message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
     191        for (int i=0; i<items.getLength(); i++) { 
     192            Element c = (Element)items.item(i); 
     193            String path = c.getAttribute(GSXML.NAME_ATT);        
     194            if (path_prefix != null) { 
     195                path = GSPath.appendLink(path_prefix, path); 
     196            } 
     197            Element request = GSXML.createBasicRequest(this.doc, request_type, path, lang, uid); 
     198            message.appendChild(request); 
     199        } 
     200         
     201        Element response_message = (Element)this.mr.process(message); 
     202 
     203        NodeList responses = response_message.getElementsByTagName(GSXML.RESPONSE_ELEM); 
     204        // check that have same number of responses as requests 
     205        if (items.getLength() != responses.getLength()) { 
     206            logger.error("didn't get a response for each request - somethings gone wrong!"); 
     207            return false; 
     208        }  
     209 
     210        for (int i=0; i<items.getLength(); i++) { 
     211            Element c1 = (Element)items.item(i); 
     212            Element c2 = (Element)GSXML.getChildByTagName((Element)responses.item(i), c1.getTagName()); 
     213            if (c1 != null && c2 !=null && c1.getAttribute(GSXML.NAME_ATT).endsWith(c2.getAttribute(GSXML.NAME_ATT))) { 
     214                //add the new data into the original element 
     215                GSXML.mergeElements(c1, c2); 
     216            } else { 
     217                logger.error(" response does not correspond to request!"); 
     218            }  
     219             
     220        } 
     221 
     222        return true; 
     223 
     224    } 
     225 
     226    protected Element gli4gs3Page(Element request) { 
     227        String lang = request.getAttribute(GSXML.LANG_ATT); 
     228        String uid = request.getAttribute(GSXML.USER_ID_ATT); 
     229 
     230        Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM); 
     231 
     232        Element applet_elem = this.doc.createElement("Applet"); 
     233        page_response.appendChild(applet_elem); 
     234        applet_elem.setAttribute("ARCHIVE","SignedGatherer.jar"); 
     235        applet_elem.setAttribute("CODE","org.greenstone.gatherer.GathererApplet4gs3"); 
     236        applet_elem.setAttribute("CODEBASE","applet"); 
     237        applet_elem.setAttribute("HEIGHT","50"); 
     238        applet_elem.setAttribute("WIDTH","380"); 
     239        Element gwcgi_param_elem= this.doc.createElement("PARAM"); 
     240        gwcgi_param_elem.setAttribute("name","gwcgi"); 
     241        String library_name=GlobalProperties.getGSDL3WebAddress(); 
     242        gwcgi_param_elem.setAttribute("value",library_name); 
     243        applet_elem.appendChild(gwcgi_param_elem); 
     244 
     245        Element gsdl3_param_elem= this.doc.createElement("PARAM"); 
     246        gsdl3_param_elem.setAttribute("name","gsdl3"); 
     247        gsdl3_param_elem.setAttribute("value","true"); 
     248        applet_elem.appendChild(gsdl3_param_elem); 
     249 
     250        return page_response; 
     251    } 
    251252}