Changeset 9914


Ignore:
Timestamp:
2005-05-19T12:31:00+12:00 (19 years ago)
Author:
kjdon
Message:

added in some handling for new messaging type requests which can go between requests to modify either requests after it, or responses. eg copy nodes from the first response into the next request

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/core/MessageRouter.java

    r9874 r9914  
    237237    ///ystem.out.println("MR received request");
    238238    ///ystem.out.println(this.converter.getString(message));
    239                
    240239    // check that its a correct message tag
    241240    if (!message.getTagName().equals(GSXML.MESSAGE_ELEM)) {
     
    261260    for (int i=0; i< num_requests; i++) {
    262261        Node result=null;
    263         Element req = (Element)requests.item(0);
     262        Element req = (Element)requests.item(i);
     263        if (req == null) {
     264        System.err.println("request "+i+" is null");
     265        continue;
     266        }
    264267        String path = req.getAttribute(GSXML.TO_ATT); // returns "" if no att of this name
    265268        if (path.equals("")) {
    266269        // its a message for the message router
    267         result = processMessage(req);
    268         mainResult.appendChild(this.doc.importNode(result, true));
     270        if (req.getAttribute(GSXML.TYPE_ATT).equals(GSXML.REQUEST_TYPE_MESSAGING)) {
     271            // its a messaging request - modifies the requests/responses
     272            result = modifyMessages(req, message, mainResult);
     273            mainResult.appendChild(this.doc.importNode(result, true));
     274        } else {
     275            // standard request
     276            result = processMessage(req);
     277            mainResult.appendChild(this.doc.importNode(result, true));
     278        }
    269279        } else {
    270280        String [] modules = path.split(",");
     281        Element mess = this.doc.createElement(GSXML.MESSAGE_ELEM);
     282        Element copied_request = (Element)this.doc.importNode(req, true);
     283        mess.appendChild(copied_request);
     284       
    271285        for (int j=0; j<modules.length; j++) {
    272286            String this_mod = modules[j];
    273287            // find the module to pass it on to
    274288            // need to put the request into a message element
    275             Element mess = message_doc.createElement(GSXML.MESSAGE_ELEM);
    276             req.setAttribute(GSXML.TO_ATT, this_mod);
    277             mess.appendChild(req);
    278289            String obj = GSPath.getFirstLink(this_mod);
     290           
    279291            if (this.module_map.containsKey(obj)) {
     292            copied_request.setAttribute(GSXML.TO_ATT, this_mod);
    280293            result = ((ModuleInterface)this.module_map.get(obj)).process(mess);
    281294            if (result !=null ) {
     
    682695       
    683696    }
    684 
     697    protected Element modifyMessages(Element request, Element message, Element result) {
     698    Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
     699    response.setAttribute(GSXML.FROM_ATT, "");
     700    response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_MESSAGING);
     701
     702    NodeList commands = request.getElementsByTagName("command");
     703    if (commands == null) {
     704        System.err.println("no commands, "+converter.getPrettyString(request));
     705        return response;
     706    }
     707    for (int i=0; i<commands.getLength(); i++) {
     708        Element action = (Element)commands.item(i);
     709        String type = action.getAttribute(GSXML.TYPE_ATT);
     710        if (type.equals("copyNode")) {
     711        // copies the from node as a child of to node
     712        String from_path = action.getAttribute("from");
     713        String to_path = action.getAttribute("to");
     714        Element from_node = null;
     715        String from_node_root = GSPath.getFirstLink(from_path);
     716        if (from_node_root.startsWith(GSXML.REQUEST_ELEM)) {
     717            from_node = message;
     718        } else if (from_node_root.startsWith(GSXML.RESPONSE_ELEM)) {
     719            from_node = result;
     720        }
     721        if (from_node == null) {
     722            continue;
     723        }
     724        Element to_node = null;
     725        String to_node_root = GSPath.getFirstLink(to_path);
     726        if (to_node_root.startsWith(GSXML.REQUEST_ELEM)) {
     727            to_node = message;
     728        } else if (to_node_root.startsWith(GSXML.RESPONSE_ELEM)) {
     729            to_node = result;
     730        }
     731        if (to_node == null) {
     732            continue;
     733        }
     734        // now we know what node to copy where
     735        Node orig_node = GSXML.getNodeByPathIndexed(from_node, from_path);
     736        if (orig_node == null) {
     737            continue;
     738        }
     739        Node new_parent = GSXML.getNodeByPathIndexed(to_node, to_path);
     740        if (new_parent == null) {
     741            continue;
     742           
     743        }
     744        new_parent.appendChild(to_node.getOwnerDocument().importNode(orig_node, true));
     745        }
     746       
     747        else if (type.equals("copyChildren")) {
     748
     749        }
     750    } // for each command
     751    return response;
     752    }
     753   
    685754    // ****************************************************
    686755    // other methods
Note: See TracChangeset for help on using the changeset viewer.