Changeset 24989

Show
Ignore:
Timestamp:
26.01.2012 11:36:32 (8 years ago)
Author:
sjm84
Message:

Reformatting this file ahead of some changes

Files:
1 modified

Legend:

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

    r24583 r24989  
    55import org.greenstone.gsdl3.util.*; 
    66//XML classes 
    7 import org.w3c.dom.Node;  
     7import org.w3c.dom.Node; 
    88import org.w3c.dom.NodeList; 
    99import org.w3c.dom.Document; 
     
    1515import org.apache.log4j.*; 
    1616 
    17 public class PageAction extends Action { 
     17public class PageAction extends Action 
     18{ 
    1819 
    1920    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.PageAction.class.getName()); 
     
    2223    public static final String ABOUT_PAGE = "about"; 
    2324    public static final String PREFS_PAGE = "pref"; 
    24     public static final String GLI4GS3_PAGE="gli4gs3"; 
    25  
    26     public Node process (Node message_node) { 
    27  
    28         Element message = this.converter.nodeToElement(message_node); 
    29  
    30         Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); 
    31         // the page name is the subaction 
    32         String page_name = request.getAttribute(GSXML.SUBACTION_ATT); 
    33         if (page_name.equals("")) { // if no page specified, assume home page 
    34         page_name = HOME_PAGE; 
    35         } 
    36         Element result = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    37         Element response; 
    38         if (page_name.equals(HOME_PAGE)) { 
    39         response = homePage(request); 
    40         //} else if (page_name.equals(ABOUT_PAGE)) { 
    41         } else if (page_name.equals(ABOUT_PAGE) || page_name.equals(PREFS_PAGE)) { 
    42         response = aboutPage(request); 
    43         //}else if (page_name.equals(PREFS_PAGE)) { 
    44         //response = prefsPage(request); 
    45         } else if (page_name.equals(GLI4GS3_PAGE)){ 
    46         response = gli4gs3Page(request); 
    47         }else { // unknown page 
    48          
    49         logger.error("unknown page specified!"); 
    50         response =  unknownPage(request); 
    51         } 
    52          
    53         result.appendChild(this.doc.importNode(response, true)); 
    54         logger.debug("page action result: "+this.converter.getPrettyString(result)); 
    55         return result; 
    56     } 
    57      
    58  
    59     protected Element homePage(Element request) { 
     25    public static final String GLI4GS3_PAGE = "gli4gs3"; 
     26 
     27    public Node process(Node message_node) 
     28    { 
     29 
     30        Element message = this.converter.nodeToElement(message_node); 
     31 
     32        Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); 
     33        // the page name is the subaction 
     34        String page_name = request.getAttribute(GSXML.SUBACTION_ATT); 
     35        if (page_name.equals("")) 
     36        { // if no page specified, assume home page 
     37            page_name = HOME_PAGE; 
     38        } 
     39        Element result = this.doc.createElement(GSXML.MESSAGE_ELEM); 
     40        Element response; 
     41        if (page_name.equals(HOME_PAGE)) 
     42        { 
     43            response = homePage(request); 
     44            //} else if (page_name.equals(ABOUT_PAGE)) { 
     45        } 
     46        else if (page_name.equals(ABOUT_PAGE) || page_name.equals(PREFS_PAGE)) 
     47        { 
     48            response = aboutPage(request); 
     49            //}else if (page_name.equals(PREFS_PAGE)) { 
     50            //response = prefsPage(request); 
     51        } 
     52        else if (page_name.equals(GLI4GS3_PAGE)) 
     53        { 
     54            response = gli4gs3Page(request); 
     55        } 
     56        else 
     57        { // unknown page 
     58 
     59            logger.error("unknown page specified!"); 
     60            response = unknownPage(request); 
     61        } 
     62 
     63        result.appendChild(this.doc.importNode(response, true)); 
     64        logger.debug("page action result: " + this.converter.getPrettyString(result)); 
     65        return result; 
     66    } 
     67 
     68    protected Element homePage(Element request) 
     69    { 
    6070 
    6171        String lang = request.getAttribute(GSXML.LANG_ATT); 
     
    6575        Element coll_list_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid); 
    6676        info_message.appendChild(coll_list_request); 
    67         Element info_response_message = (Element)this.mr.process(info_message); 
    68         if (info_response_message==null) { 
     77        Element info_response_message = (Element) this.mr.process(info_message); 
     78        if (info_response_message == null) 
     79        { 
    6980            logger.error(" couldn't query the message router!"); 
    7081            return null; 
    7182        } 
    72         Element info_response = (Element)GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM); 
    73         if (info_response==null) { 
     83        Element info_response = (Element) GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM); 
     84        if (info_response == null) 
     85        { 
    7486            logger.error("couldn't query the message router!"); 
    7587            return null; 
     
    7890        // second, get the metadata for each collection - we only want specific 
    7991        // elements but for now, we'll just get it all 
    80         Element collection_list = (Element)GSXML.getChildByTagName(info_response, GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER); 
     92        Element collection_list = (Element) GSXML.getChildByTagName(info_response, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER); 
    8193        logger.debug(GSXML.xmlNodeToString(collection_list, false)); 
    82         if (collection_list != null) { 
     94        if (collection_list != null) 
     95        { 
    8396            NodeList colls = GSXML.getChildrenByTagName(collection_list, GSXML.COLLECTION_ELEM); 
    84             if (colls.getLength()>0) { 
     97            if (colls.getLength() > 0) 
     98            { 
    8599                sendMultipleRequests(colls, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
    86100            } 
     
    88102 
    89103        // get metadata for any services 
    90         Element service_list = (Element)GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER); 
    91         if (service_list != null) { 
     104        Element service_list = (Element) GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER); 
     105        if (service_list != null) 
     106        { 
    92107            NodeList services = GSXML.getChildrenByTagName(service_list, GSXML.SERVICE_ELEM); 
    93             if (services.getLength() > 0) { 
     108            if (services.getLength() > 0) 
     109            { 
    94110                sendMultipleRequests(services, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
    95111            } 
     
    97113 
    98114        // get metadata for service clusters 
    99         Element cluster_list = (Element)GSXML.getChildByTagName(info_response, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER); 
    100         if (cluster_list != null) { 
     115        Element cluster_list = (Element) GSXML.getChildByTagName(info_response, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER); 
     116        if (cluster_list != null) 
     117        { 
    101118            NodeList clusters = GSXML.getChildrenByTagName(cluster_list, GSXML.CLUSTER_ELEM); 
    102             if (clusters.getLength() > 0) { 
     119            if (clusters.getLength() > 0) 
     120            { 
    103121                sendMultipleRequests(clusters, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
    104122 
     
    111129    } // homePage 
    112130 
    113     protected Element aboutPage(Element request) { 
     131    protected Element aboutPage(Element request) 
     132    { 
    114133 
    115134        String lang = request.getAttribute(GSXML.LANG_ATT); 
    116135        String uid = request.getAttribute(GSXML.USER_ID_ATT); 
    117136        // extract the params from the cgi-request,  
    118         Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 
     137        Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
    119138        HashMap params = GSXML.extractParams(cgi_paramList, false); 
    120          
    121         String coll_name = (String)params.get(GSParams.COLLECTION); 
    122         if (coll_name == null || coll_name.equals("")) { 
     139 
     140        String coll_name = (String) params.get(GSParams.COLLECTION); 
     141        if (coll_name == null || coll_name.equals("")) 
     142        { 
    123143            logger.error("about page requested with no collection or cluster specified!"); 
    124144            // return an empty response 
     
    133153        Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid); 
    134154        coll_about_message.appendChild(coll_about_request); 
    135         Element coll_about_response = (Element)this.mr.process(coll_about_message); 
     155        Element coll_about_response = (Element) this.mr.process(coll_about_message); 
    136156 
    137157        // add collection type attribute to paramList 
    138158        String col_type = ""; 
    139         NodeList collect_elem = coll_about_response.getElementsByTagName(GSXML.COLLECTION_ELEM);     
    140         if(collect_elem.getLength() != 0) { 
    141             for (int i=0; i < collect_elem.getLength(); i++) { 
    142               Element e = (Element) collect_elem.item(i); 
    143               col_type = e.getAttribute(GSXML.TYPE_ATT); 
    144             }    
    145         } else { 
     159        NodeList collect_elem = coll_about_response.getElementsByTagName(GSXML.COLLECTION_ELEM); 
     160        if (collect_elem.getLength() != 0) 
     161        { 
     162            for (int i = 0; i < collect_elem.getLength(); i++) 
     163            { 
     164                Element e = (Element) collect_elem.item(i); 
     165                col_type = e.getAttribute(GSXML.TYPE_ATT); 
     166            } 
     167        } 
     168        else 
     169        { 
    146170            logger.error(GSXML.COLLECTION_ELEM + " element is null"); 
    147171        } 
    148                  
    149         NodeList paramList = request.getElementsByTagName(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);     
    150         if(paramList.getLength() != 0) { 
    151             for (int i=0; i<paramList.getLength(); i++) { 
    152               Element e = (Element) paramList.item(i);                                     
    153               Element ct = GSXML.createParameter(request.getOwnerDocument(), GSParams.COLLECTION_TYPE, col_type.equalsIgnoreCase("mg") ? "0" : "1"); 
    154               e.appendChild(ct); 
    155             }    
    156         } else { 
     172 
     173        NodeList paramList = request.getElementsByTagName(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
     174        if (paramList.getLength() != 0) 
     175        { 
     176            for (int i = 0; i < paramList.getLength(); i++) 
     177            { 
     178                Element e = (Element) paramList.item(i); 
     179                Element ct = GSXML.createParameter(request.getOwnerDocument(), GSParams.COLLECTION_TYPE, col_type.equalsIgnoreCase("mg") ? "0" : "1"); 
     180                e.appendChild(ct); 
     181            } 
     182        } 
     183        else 
     184        { 
    157185            logger.info("paramList is null!!"); 
    158         }    
    159                  
    160         if (coll_about_response == null) { 
     186        } 
     187 
     188        if (coll_about_response == null) 
     189        { 
    161190            return null; 
    162191        } 
    163  
    164192 
    165193        // second, get the info for each service - we only want display items  
    166194        // but for now, we'll just get it all 
    167195        NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM); 
    168         if (services.getLength() > 0) { 
     196        if (services.getLength() > 0) 
     197        { 
    169198            sendMultipleRequests(services, coll_name, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid); 
    170199        } 
     
    175204        return response; 
    176205    } 
    177      
     206 
    178207    //protected Element prefsPage(Element request) { 
    179      
     208 
    180209    //  return null; 
    181210    //} 
    182      
     211 
    183212    /** if we dont know the page type, use this method */ 
    184     protected Element unknownPage(Element request) { 
     213    protected Element unknownPage(Element request) 
     214    { 
    185215 
    186216        String lang = request.getAttribute(GSXML.LANG_ATT); 
     
    189219 
    190220        // extract the params from the cgi-request,  
    191         Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 
     221        Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
    192222        HashMap params = GSXML.extractParams(cgi_paramList, false); 
    193223 
    194         String coll_name = (String)params.get(GSParams.COLLECTION); 
    195         if (coll_name == null || coll_name.equals("")) { 
     224        String coll_name = (String) params.get(GSParams.COLLECTION); 
     225        if (coll_name == null || coll_name.equals("")) 
     226        { 
    196227            // just return an empty response 
    197228            Element response = this.doc.createElement(GSXML.RESPONSE_ELEM); 
     
    206237        Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    207238 
    208         Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE,  coll_name, lang, uid); 
     239        Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid); 
    209240        coll_about_message.appendChild(coll_about_request); 
    210          
    211         Element coll_about_response = (Element)this.mr.process(coll_about_message); 
     241 
     242        Element coll_about_response = (Element) this.mr.process(coll_about_message); 
    212243 
    213244        Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM); 
     
    220251    } 
    221252 
    222  
    223     protected boolean sendMultipleRequests(NodeList items, String path_prefix, String request_type, String lang, String uid) { 
     253    protected boolean sendMultipleRequests(NodeList items, String path_prefix, String request_type, String lang, String uid) 
     254    { 
    224255 
    225256        // we will send all the requests in a single message 
    226257        Element message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    227         for (int i=0; i<items.getLength(); i++) { 
    228             Element c = (Element)items.item(i); 
    229             String path = c.getAttribute(GSXML.NAME_ATT);        
    230             if (path_prefix != null) { 
     258        for (int i = 0; i < items.getLength(); i++) 
     259        { 
     260            Element c = (Element) items.item(i); 
     261            String path = c.getAttribute(GSXML.NAME_ATT); 
     262            if (path_prefix != null) 
     263            { 
    231264                path = GSPath.appendLink(path_prefix, path); 
    232265            } 
     
    234267            message.appendChild(request); 
    235268        } 
    236          
    237         Element response_message = (Element)this.mr.process(message); 
     269 
     270        Element response_message = (Element) this.mr.process(message); 
    238271 
    239272        NodeList responses = response_message.getElementsByTagName(GSXML.RESPONSE_ELEM); 
    240273        // check that have same number of responses as requests 
    241         if (items.getLength() != responses.getLength()) { 
     274        if (items.getLength() != responses.getLength()) 
     275        { 
    242276            logger.error("didn't get a response for each request - somethings gone wrong!"); 
    243277            return false; 
    244         }  
    245  
    246         for (int i=0; i<items.getLength(); i++) { 
    247             Element c1 = (Element)items.item(i); 
    248             Element c2 = (Element)GSXML.getChildByTagName((Element)responses.item(i), c1.getTagName()); 
    249             if (c1 != null && c2 !=null && c1.getAttribute(GSXML.NAME_ATT).endsWith(c2.getAttribute(GSXML.NAME_ATT))) { 
     278        } 
     279 
     280        for (int i = 0; i < items.getLength(); i++) 
     281        { 
     282            Element c1 = (Element) items.item(i); 
     283            Element c2 = (Element) GSXML.getChildByTagName((Element) responses.item(i), c1.getTagName()); 
     284            if (c1 != null && c2 != null && c1.getAttribute(GSXML.NAME_ATT).endsWith(c2.getAttribute(GSXML.NAME_ATT))) 
     285            { 
    250286                //add the new data into the original element 
    251287                GSXML.mergeElements(c1, c2); 
    252             } else { 
     288            } 
     289            else 
     290            { 
    253291                logger.debug(" response does not correspond to request!"); 
    254             }  
    255              
     292            } 
     293 
    256294        } 
    257295 
     
    260298    } 
    261299 
    262     protected Element gli4gs3Page(Element request) { 
     300    protected Element gli4gs3Page(Element request) 
     301    { 
    263302        String lang = request.getAttribute(GSXML.LANG_ATT); 
    264303        String uid = request.getAttribute(GSXML.USER_ID_ATT); 
     
    268307        Element applet_elem = this.doc.createElement("Applet"); 
    269308        page_response.appendChild(applet_elem); 
    270         applet_elem.setAttribute("ARCHIVE","SignedGatherer.jar"); 
    271         applet_elem.setAttribute("CODE","org.greenstone.gatherer.GathererApplet"); 
    272         applet_elem.setAttribute("CODEBASE","applet"); 
    273         applet_elem.setAttribute("HEIGHT","50"); 
    274         applet_elem.setAttribute("WIDTH","380"); 
    275         Element gwcgi_param_elem= this.doc.createElement("PARAM"); 
    276         gwcgi_param_elem.setAttribute("name","gwcgi"); 
    277         String library_name=GlobalProperties.getGSDL3WebAddress(); 
    278         gwcgi_param_elem.setAttribute("value",library_name); 
     309        applet_elem.setAttribute("ARCHIVE", "SignedGatherer.jar"); 
     310        applet_elem.setAttribute("CODE", "org.greenstone.gatherer.GathererApplet"); 
     311        applet_elem.setAttribute("CODEBASE", "applet"); 
     312        applet_elem.setAttribute("HEIGHT", "50"); 
     313        applet_elem.setAttribute("WIDTH", "380"); 
     314        Element gwcgi_param_elem = this.doc.createElement("PARAM"); 
     315        gwcgi_param_elem.setAttribute("name", "gwcgi"); 
     316        String library_name = GlobalProperties.getGSDL3WebAddress(); 
     317        gwcgi_param_elem.setAttribute("value", library_name); 
    279318        applet_elem.appendChild(gwcgi_param_elem); 
    280319 
    281         Element gsdl3_param_elem= this.doc.createElement("PARAM"); 
    282         gsdl3_param_elem.setAttribute("name","gsdl3"); 
    283         gsdl3_param_elem.setAttribute("value","true"); 
     320        Element gsdl3_param_elem = this.doc.createElement("PARAM"); 
     321        gsdl3_param_elem.setAttribute("name", "gsdl3"); 
     322        gsdl3_param_elem.setAttribute("value", "true"); 
    284323        applet_elem.appendChild(gsdl3_param_elem); 
    285324 
     
    287326    } 
    288327 
    289  
    290328}