Changeset 24986

Show
Ignore:
Timestamp:
26.01.2012 11:32:05 (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/AppletAction.java

    r16688 r24986  
    44import org.greenstone.gsdl3.util.*; 
    55// XML classes 
    6 import org.w3c.dom.Node;  
    7 import org.w3c.dom.NodeList;  
    8 import org.w3c.dom.Document;  
    9 import org.w3c.dom.Element;  
     6import org.w3c.dom.Node; 
     7import org.w3c.dom.NodeList; 
     8import org.w3c.dom.Document; 
     9import org.w3c.dom.Element; 
    1010 
    1111import java.util.HashMap; 
     
    1515 
    1616/** action class for handling applets */ 
    17 public class AppletAction extends Action { 
     17public class AppletAction extends Action 
     18{ 
    1819 
    19       static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.AppletAction.class.getName());    
     20    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.AppletAction.class.getName()); 
    2021 
    21     public Node process (Node message_node) { 
    22      
    23     Element message = this.converter.nodeToElement(message_node); 
     22    public Node process(Node message_node) 
     23    { 
    2424 
    25     Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); 
    26     Element result = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    27     Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM); 
    28     result.appendChild(page_response); 
     25        Element message = this.converter.nodeToElement(message_node); 
    2926 
    30     // get the collection and service params 
    31     Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 
    32     HashMap params = GSXML.extractParams(cgi_param_list, false); 
     27        Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); 
     28        Element result = this.doc.createElement(GSXML.MESSAGE_ELEM); 
     29        Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM); 
     30        result.appendChild(page_response); 
    3331 
    34     // request_type is display (d) or request (r) 
    35     String request_type = (String)params.get(GSParams.REQUEST_TYPE); 
    36     if (!request_type.equals("d")&&!request_type.equals("r")) { 
    37         logger.error("AppletAction Error: the rt arg should be either d or r, instead it was "+request_type+"!"); 
    38         return result; 
    39     } 
    40      
    41     String collection = (String)params.get(GSParams.COLLECTION); 
    42     boolean coll_specified = true; 
    43     String service_name=(String)params.get(GSParams.SERVICE); 
    44     String lang = request.getAttribute(GSXML.LANG_ATT); 
    45     String uid = request.getAttribute(GSXML.USER_ID_ATT); 
    46     String to=null; 
    47     if (collection==null||collection.equals("") ) { 
    48         coll_specified = false; 
    49         to = service_name; 
    50     } else { 
    51         to = GSPath.appendLink(collection, service_name); 
     32        // get the collection and service params 
     33        Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 
     34        HashMap params = GSXML.extractParams(cgi_param_list, false); 
     35 
     36        // request_type is display (d) or request (r) 
     37        String request_type = (String) params.get(GSParams.REQUEST_TYPE); 
     38        if (!request_type.equals("d") && !request_type.equals("r")) 
     39        { 
     40            logger.error("AppletAction Error: the rt arg should be either d or r, instead it was " + request_type + "!"); 
     41            return result; 
     42        } 
     43 
     44        String collection = (String) params.get(GSParams.COLLECTION); 
     45        boolean coll_specified = true; 
     46        String service_name = (String) params.get(GSParams.SERVICE); 
     47        String lang = request.getAttribute(GSXML.LANG_ATT); 
     48        String uid = request.getAttribute(GSXML.USER_ID_ATT); 
     49        String to = null; 
     50        if (collection == null || collection.equals("")) 
     51        { 
     52            coll_specified = false; 
     53            to = service_name; 
     54        } 
     55        else 
     56        { 
     57            to = GSPath.appendLink(collection, service_name); 
     58        } 
     59 
     60        if (request_type.equals("r")) 
     61        { 
     62            // we are processing stuff for the applet send a message to the service, type="query", and take out the something element, and return that as our result - the applet must take xml 
     63 
     64            Element mr_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
     65            Element mr_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid); 
     66            mr_message.appendChild(mr_request); 
     67            // just append all the params for now - should filter out unneeded ones 
     68            mr_request.appendChild(this.doc.importNode(cgi_param_list, true)); 
     69 
     70            // process the request 
     71            Element mr_response = (Element) this.mr.process(mr_message); 
     72            // get the applet data out and pass it back as is.  
     73            String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.APPLET_DATA_ELEM); 
     74            Element applet_info = GSXML.getFirstElementChild(GSXML.getNodeByPath(mr_response, path)); 
     75            //Element applet_info = (Element)GSXML.getNodeByPath(mr_response, path).getFirstChild(); 
     76            return applet_info; 
     77 
     78        } 
     79 
     80        // get the applet description, and the collection info if a collection is specified 
     81 
     82        Element mr_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
     83        Element applet_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, uid); 
     84        mr_message.appendChild(applet_request); 
     85 
     86        Element mr_response = (Element) this.mr.process(mr_message); 
     87 
     88        // add in the applet info 
     89        String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM); 
     90        //path = GSPath.appendLink(path, GSXML.APPLET_ELEM); 
     91        Element service_elem = (Element) this.doc.importNode(GSXML.getNodeByPath(mr_response, path), true); 
     92        Element applet_elem = (Element) GSXML.getChildByTagName(service_elem, GSXML.APPLET_ELEM); 
     93        // must handle any params that have values that are not  
     94        // necessarily known by the service 
     95        // should this be done here or by web receptionist?? 
     96        // cant really have an applet without web? 
     97        editLocalParams(applet_elem, (String) config_params.get(GSConstants.LIBRARY_NAME), collection); 
     98        page_response.appendChild(service_elem); 
     99 
     100        return result; 
     101 
    52102    } 
    53103 
    54     if (request_type.equals("r")) { 
    55         // we are processing stuff for the applet send a message to the service, type="query", and take out the something element, and return that as our result - the applet must take xml 
     104    /** 
     105     * this method looks through the PARAMs of the applet description for 
     106     * 'library' or 'collection'. If found, the params are set to the 
     107     * appropriate values should this be done here or in the receptionist? 
     108     */ 
     109    protected void editLocalParams(Element description, String library_name, String full_collection_name) 
     110    { 
    56111 
    57         Element mr_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    58         Element mr_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);  
    59         mr_message.appendChild(mr_request); 
    60         // just append all the params for now - should filter out unneeded ones 
    61         mr_request.appendChild(this.doc.importNode(cgi_param_list, true)); 
    62         
    63         // process the request 
    64         Element mr_response = (Element)this.mr.process(mr_message); 
    65         // get the applet data out and pass it back as is.  
    66         String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.APPLET_DATA_ELEM); 
    67 Element applet_info = GSXML.getFirstElementChild(GSXML.getNodeByPath(mr_response, path)); 
    68         //Element applet_info = (Element)GSXML.getNodeByPath(mr_response, path).getFirstChild(); 
    69         return applet_info; 
    70          
     112        Node child = description.getFirstChild(); 
     113        while (child != null) 
     114        { 
     115            if (child.getNodeType() == Node.ELEMENT_NODE) 
     116            { 
     117                String param = child.getNodeName(); 
     118                if (param.equals("PARAM") || param.equals("param")) 
     119                { 
     120                    String name = ((Element) child).getAttribute("NAME"); 
     121                    if (name == null || name.equals("")) 
     122                    { 
     123                        // somethings wrong!! 
     124                    } 
     125                    else if (name.equals("library")) 
     126                    { 
     127                        ((Element) child).setAttribute("VALUE", library_name); 
     128                    } 
     129                    else if (name.equals("collection")) 
     130                    { 
     131                        ((Element) child).setAttribute("VALUE", full_collection_name); 
     132                    } 
     133 
     134                } 
     135            } 
     136            child = child.getNextSibling(); 
     137        } 
    71138    } 
    72      
    73139 
    74     // get the applet description, and the collection info if a collection is specified 
    75      
    76     Element mr_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 
    77     Element applet_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, uid);  
    78     mr_message.appendChild(applet_request); 
    79  
    80      
    81     Element mr_response = (Element)this.mr.process(mr_message); 
    82  
    83     // add in the applet info 
    84     String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM); 
    85     //path = GSPath.appendLink(path, GSXML.APPLET_ELEM); 
    86     Element service_elem = (Element)this.doc.importNode(GSXML.getNodeByPath(mr_response, path), true); 
    87     Element applet_elem = (Element)GSXML.getChildByTagName(service_elem, GSXML.APPLET_ELEM); 
    88     // must handle any params that have values that are not  
    89     // necessarily known by the service 
    90     // should this be done here or by web receptionist?? 
    91     // cant really have an applet without web? 
    92     editLocalParams(applet_elem, (String)config_params.get(GSConstants.LIBRARY_NAME), collection); 
    93     page_response.appendChild(service_elem); 
    94      
    95     return result; 
    96      
    97     } 
    98  
    99     /** this method looks through the PARAMs of the applet description for  
    100      * 'library' or 'collection'. If found, the params are set to   
    101      * the appropriate values  
    102      * should this be done here or in the receptionist?  
    103      */ 
    104     protected void editLocalParams(Element description, String library_name, 
    105                    String full_collection_name) { 
    106  
    107     Node child = description.getFirstChild(); 
    108     while (child != null) { 
    109         if (child.getNodeType() == Node.ELEMENT_NODE) { 
    110         String param = child.getNodeName(); 
    111         if (param.equals("PARAM")||param.equals("param")) { 
    112             String name = ((Element)child).getAttribute("NAME"); 
    113             if (name == null || name.equals("")) { 
    114             // somethings wrong!! 
    115             } else if (name.equals("library")) { 
    116             ((Element)child).setAttribute("VALUE", library_name); 
    117             } else if (name.equals("collection")) { 
    118             ((Element)child).setAttribute("VALUE", full_collection_name); 
    119             } 
    120              
    121         } 
    122         } 
    123         child = child.getNextSibling(); 
    124     } 
    125     } 
    126      
    127140}