Changeset 32991 for main

Show
Ignore:
Timestamp:
05.04.2019 14:58:22 (8 months ago)
Author:
kjdon
Message:

refactored the code as lots of functions were doing the same thing. for new verify page, we get and add the security info to teh page response. this may still need tidying up as I have run out of time today

Files:
1 modified

Legend:

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

    r32945 r32991  
    1919public class PageAction extends Action 
    2020{ 
    21     static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.PageAction.class.getName()); 
    22  
    23     public static final String HOME_PAGE = "home"; 
     21   
     22    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.PageAction.class.getName()); 
     23 
     24        public static final String HOME_PAGE = "home"; 
    2425    public static final String ABOUT_PAGE = "about"; 
    2526    public static final String PREFS_PAGE = "pref"; 
    2627    public static final String GLI4GS3_PAGE = "gli4gs3"; 
     28  public static final String VERIFY_PAGE = "verify"; 
    2729 
    2830  // this gets set to the groupInfo service name if there is one. 
     
    5355            page_name = HOME_PAGE; 
    5456        } 
     57 
    5558        Element result = doc.createElement(GSXML.MESSAGE_ELEM); 
    5659        Element response; 
     
    5861        { 
    5962          response = homePage(request); 
    60             //} else if (page_name.equals(ABOUT_PAGE)) { 
    61         } 
    62         else if (page_name.equals(ABOUT_PAGE) || page_name.equals(PREFS_PAGE)) 
    63         { 
    64           response = aboutOrPrefsPage(request, page_name); 
     63             
    6564        } 
    6665        else if (page_name.equals(GLI4GS3_PAGE)) 
    6766        { 
    6867          response = gli4gs3Page(request); 
    69         } 
    70         else 
    71         { // unknown page 
    72           response = unknownPage(request); 
    73         } 
    74  
    75         Element formatMessage = doc.createElement(GSXML.MESSAGE_ELEM); 
    76         Element formatRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_FORMAT, collection, new UserContext(request)); 
    77         formatMessage.appendChild(formatRequest); 
    78         Element formatResponseMessage = (Element) this.mr.process(formatMessage); 
    79         Element formatResponse = (Element) GSXML.getChildByTagName(formatResponseMessage, GSXML.RESPONSE_ELEM); 
    80  
    81         Element globalFormat = (Element) GSXML.getChildByTagName(formatResponse, GSXML.FORMAT_ELEM); 
    82         if (globalFormat != null) 
    83         { 
    84           response.appendChild(response.getOwnerDocument().importNode(globalFormat, true)); 
    85         } 
     68        } else  if (collection.equals("")) { 
     69          // we are not in a collection. eg could be library prefs or other page 
     70          response = generalLibraryPage(request, page_name); 
     71        } else { 
     72          // we are in a collection 
     73          response = collectionPage(request, page_name, collection); 
     74        } 
     75         
    8676 
    8777        result.appendChild(doc.importNode(response, true)); 
     
    9080        return result; 
    9181    } 
     82 
     83  // A general library page just adds site metadata and interface options. 
     84  // customise: prefs: add language list 
     85  protected Element generalLibraryPage(Element request, String page_name) { 
     86 
     87    Document doc = XMLConverter.newDOM(); 
     88    UserContext userContext = new UserContext(request); 
     89    Element response = doc.createElement(GSXML.RESPONSE_ELEM); 
     90    addSiteMetadata(response, userContext); 
     91    addInterfaceOptions(response); 
     92    if (page_name.equals(PREFS_PAGE) && this.language_list != null) { 
     93      response.appendChild(doc.importNode(this.language_list, true)); 
     94    } 
     95    return response; 
     96  } 
     97 
     98  protected Element collectionPage(Element request, String page_name, String collection) { 
     99 
     100    Document doc = XMLConverter.newDOM(); 
     101     
     102    UserContext userContext = new UserContext(request); 
     103    // extract the params from the cgi-request,  
     104    Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
     105    HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false); 
     106     
     107    // get the collection or cluster description 
     108    Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM); 
     109     
     110    Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, collection, userContext); 
     111    coll_about_message.appendChild(coll_about_request); 
     112     
     113    Element coll_about_response = (Element) this.mr.process(coll_about_message); 
     114     
     115    Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM); 
     116 
     117    if (response == null) { 
     118      // what should we be returning? 
     119      return null; 
     120    } 
     121     
     122    //add the site metadata 
     123    addSiteMetadata(response, userContext); 
     124    addInterfaceOptions(response); 
     125 
     126    if (page_name.equals(PREFS_PAGE) && this.language_list != null) { 
     127      response.appendChild(response.getOwnerDocument().importNode(this.language_list, true)); 
     128    } 
     129 
     130    // is this collection part of a group?  
     131    String group = (String) params.get(GSParams.GROUP); 
     132    if (group != null && !group.equals("")) { 
     133      // ...yes it is. get the group path info 
     134      Element group_info_response = getGroupInfo(group, userContext);  
     135      Element path_list = (Element) GSXML.getChildByTagName(group_info_response, 
     136                                GSXML.PATH_ELEM + GSXML.LIST_MODIFIER); 
     137      if (path_list != null) { 
     138    response.appendChild(response.getOwnerDocument().importNode(path_list, true)); 
     139      } 
     140    } 
     141 
     142    if (page_name.equals(ABOUT_PAGE)) { 
     143      // get service descriptions. Actually only want displayItems 
     144      NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM); 
     145      if (services.getLength() > 0) 
     146    { 
     147      sendMultipleRequests(doc, services, collection, GSXML.REQUEST_TYPE_DESCRIBE, userContext); 
     148    } 
     149 
     150    } 
     151    // old code had adding a ct param to the paramList - for about/prefs page, only used for gs2 interface. Since we don't support that anymore, am leaving it out. 
     152 
     153    // add the global collection format info 
     154    Element formatMessage = doc.createElement(GSXML.MESSAGE_ELEM); 
     155    Element formatRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_FORMAT, collection, userContext); 
     156    formatMessage.appendChild(formatRequest); 
     157    Element formatResponseMessage = (Element) this.mr.process(formatMessage); 
     158    Element formatResponse = (Element) GSXML.getChildByTagName(formatResponseMessage, GSXML.RESPONSE_ELEM); 
     159     
     160    Element globalFormat = (Element) GSXML.getChildByTagName(formatResponse, GSXML.FORMAT_ELEM); 
     161    if (globalFormat != null) 
     162      { 
     163    response.appendChild(response.getOwnerDocument().importNode(globalFormat, true)); 
     164      } 
     165 
     166    if (page_name.equals(VERIFY_PAGE)) { 
     167      addSecurityInfo(request, collection, response); 
     168    } 
     169     
     170    return response; 
     171  } 
     172 
    92173 
    93174  protected Element homePage(Element request) 
    94175    { 
    95176      Document doc = XMLConverter.newDOM(); 
    96          
    97177 
    98178        UserContext userContext = new UserContext(request); 
     
    151231         
    152232        if (!this.groupInfoServiceName.equals("NO_GROUPS")) { 
    153  
    154233          String group = null; 
    155234          Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
     
    170249          if (collection_list != null) { 
    171250            info_response.appendChild(doc.importNode(collection_list, true)); 
     251          } else { 
     252            logger.warn("Home page had no collection list"); 
    172253          } 
    173254          Element group_list = (Element) GSXML.getChildByTagName(group_info_response, 
     
    211292                sendMultipleRequests(doc, colls, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext); 
    212293            } 
    213         } 
     294        }  
    214295 
    215296        // get metadata for any services 
     
    264345  } 
    265346   
    266   protected Element aboutOrPrefsPage(Element request, String page_name) 
    267     { 
    268       Document doc = XMLConverter.newDOM(); 
    269          
    270         UserContext userContext = new UserContext(request); 
    271         // extract the params from the cgi-request,  
    272         Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
    273         HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false); 
    274  
    275         String coll_name = (String) params.get(GSParams.COLLECTION); 
    276         if (coll_name == null || coll_name.equals("")) 
    277         { 
    278           // return a response with no collection info - must be prefs from home page 
    279             Element response = doc.createElement(GSXML.RESPONSE_ELEM); 
    280             addSiteMetadata(response, userContext); 
    281             addInterfaceOptions(response); 
    282             if (this.language_list != null) { 
    283               response.appendChild(doc.importNode(this.language_list, true)); 
    284             } 
    285             return response; 
    286         } 
    287  
    288         // get the collection or cluster description 
    289         Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM); 
    290  
    291         Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext); 
    292         coll_about_message.appendChild(coll_about_request); 
    293         Element coll_about_response = (Element) this.mr.process(coll_about_message); 
    294  
    295         // add collection type attribute to paramList 
    296         String col_type = ""; 
    297         NodeList collect_elem = coll_about_response.getElementsByTagName(GSXML.COLLECTION_ELEM); 
    298         if (collect_elem.getLength() != 0) 
    299         { 
    300             for (int i = 0; i < collect_elem.getLength(); i++) 
    301             { 
    302                 Element e = (Element) collect_elem.item(i); 
    303                 col_type = e.getAttribute(GSXML.TYPE_ATT); 
    304             } 
    305         } 
    306         else 
    307         { 
    308             logger.error(GSXML.COLLECTION_ELEM + " element is null"); 
    309         } 
    310  
    311         // adding a ct param to paramlist. only needed for gs2 interface, not default 
    312         NodeList paramList_list = request.getElementsByTagName(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
    313         if (paramList_list.getLength() != 0) 
    314         { 
    315             for (int i = 0; i < paramList_list.getLength(); i++) 
    316             { 
    317                 Element e = (Element) paramList_list.item(i); 
    318                 Element ct = GSXML.createParameter(request.getOwnerDocument(), GSParams.COLLECTION_TYPE, col_type.equalsIgnoreCase("mg") ? "0" : "1"); 
    319                 e.appendChild(ct); 
    320             } 
    321         } 
    322         else 
    323         { 
    324             logger.info("paramList is null!!"); 
    325         } 
    326  
    327         if (coll_about_response == null) 
    328         { 
    329             return null; 
    330         } 
    331  
    332         // second, get the info for each service - we only want display items  
    333         // but for now, we'll just get it all 
    334         NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM); 
    335         if (services.getLength() > 0) 
    336         { 
    337             sendMultipleRequests(doc, services, coll_name, GSXML.REQUEST_TYPE_DESCRIBE, userContext); 
    338         } 
    339  
    340         Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM); 
    341  
    342         // is this collection part of a group?  
    343         String group = (String) params.get(GSParams.GROUP); 
    344         if (group != null && !group.equals("")) { 
    345           // ...yes it is. get the group path info 
    346           Element group_info_response = getGroupInfo(group, userContext);  
    347           Element path_list = (Element) GSXML.getChildByTagName(group_info_response, 
    348                                     GSXML.PATH_ELEM + GSXML.LIST_MODIFIER); 
    349           if (path_list != null) { 
    350             response.appendChild(response.getOwnerDocument().importNode(path_list, true)); 
    351           } 
    352         } 
    353          
    354         //add the site metadata 
    355         addSiteMetadata(response, userContext); 
    356         addInterfaceOptions(response); 
    357         if (page_name.equals(PREFS_PAGE) && this.language_list != null) { 
    358           response.appendChild(response.getOwnerDocument().importNode(this.language_list, true)); 
    359         } 
    360         return response; 
    361     } 
    362  
    363  
    364     /** if we dont know the page type, use this method */ 
    365     protected Element unknownPage(Element request) 
    366     { 
    367       Document doc = XMLConverter.newDOM(); 
    368          
    369         UserContext userContext = new UserContext(request); 
    370         String page_name = request.getAttribute(GSXML.SUBACTION_ATT); 
    371  
    372         // extract the params from the cgi-request,  
    373         Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
    374         HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false); 
    375  
    376         String coll_name = (String) params.get(GSParams.COLLECTION); 
    377         if (coll_name == null || coll_name.equals("")) 
    378         { 
    379             // just return an empty response 
    380             Element response = doc.createElement(GSXML.RESPONSE_ELEM); 
    381             addSiteMetadata(response, userContext); 
    382             addInterfaceOptions(response); 
    383             return response; 
    384         } 
    385  
    386         // else get the coll description - actually this is the same as for the about page - should we merge these two methods?? 
    387  
    388         // if there is a service specified should we get the service description instead?? 
    389         // get the collection or cluster description 
    390         Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM); 
    391  
    392         Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext); 
    393         coll_about_message.appendChild(coll_about_request); 
    394  
    395         Element coll_about_response = (Element) this.mr.process(coll_about_message); 
    396  
    397         Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM); 
    398  
    399         //add the site metadata 
    400         addSiteMetadata(response, userContext); 
    401         addInterfaceOptions(response); 
    402  
    403         return response; 
    404  
    405     } 
     347 
     348  protected boolean addSecurityInfo(Element request, String collection, Element response) { 
     349    logger.error("in add security"); 
     350    Document doc = XMLConverter.newDOM(); 
     351    Element securityMessage = doc.createElement(GSXML.MESSAGE_ELEM); 
     352    Element securityRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_SECURITY, collection, new UserContext(request)); 
     353    securityMessage.appendChild(securityRequest); 
     354     
     355    Element securityResponse = (Element) GSXML.getChildByTagName(this.mr.process(securityMessage), GSXML.RESPONSE_ELEM); 
     356    logger.error("security response = "+XMLConverter.getPrettyString(securityResponse)); 
     357     
     358    Document r_doc = response.getOwnerDocument(); 
     359    // just get the top level node and attributes 
     360    // Element new_sec = (Element)r_doc.importNode(securityResponse, true); //createElement(GSXML.SECURITY_ELEMENT); 
     361    Element new_sec = GSXML.duplicateWithNewName(r_doc, securityResponse, "security", true); 
     362    // just in case this is present 
     363    new_sec.removeAttribute("secretKey"); 
     364    logger.error("new security element = "+XMLConverter.getPrettyString(new_sec)); 
     365    response.appendChild(new_sec); 
     366    return true; 
     367     
     368  } 
    406369 
    407370    protected boolean sendMultipleRequests(Document doc, NodeList items, String path_prefix, String request_type, UserContext userContext)