package org.greenstone.gsdl3.action; import org.greenstone.gsdl3.core.ModuleInterface; import org.greenstone.gsdl3.util.*; // XML classes import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Document; import org.w3c.dom.Element; import java.util.HashMap; import java.util.Map; import java.util.Iterator; import java.io.File; public class ProcessAction extends Action { /** process a request */ public Element process (Element message) { // assume only one request Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); // we ignore the subaction for now - all types are processed by the same method //String subaction = request.getAttribute(GSXML.SUBACTION_ATT); // get the param list Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); HashMap params = GSXML.extractParams(cgi_param_list, false); String service_name = (String) params.get(GSCGI.SERVICE_ARG); String cluster_name = (String) params.get(GSCGI.CLUSTER_ARG); String request_only_p = (String)params.get(GSCGI.REQUEST_ONLY_ARG); boolean request_only = false; if (request_only_p!=null) { request_only = (request_only_p.equals("1")?true:false); } String request_type = (String) params.get(GSCGI.REQUEST_TYPE_ARG); // the return page Element page = doc_.createElement(GSXML.PAGE_ELEM); page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT)); System.out.println("(ProcessAction) Page:\n" + converter_.getPrettyString(page)); // what is carried out depends on the request_type // if rt=d, then a describe request is done, // is rt=r, a request and then a describe request is done // if rt=s, a status request is done. // if ro=1, then this calls for a process only page - we do the request // (rt should be r or s) and just give the response straight back // without any page processing //the stylesheet String stylesheet = GSFile.stylesheetFile(config_, "process.xsl"); if (!request_only) { // check that the stylesheet is present if (stylesheet==null) { System.err.println("ProcessAction Error: process stylesheet not found!"); return null; } } // where to send requests String to = cluster_name; to = GSPath.appendLink(to, service_name); if (!request_type.equals("d")) { // if rt=s or rt=r, do the request Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM); Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM); mr_query_message.appendChild(mr_query_request); mr_query_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT)); mr_query_request.setAttribute(GSXML.TO_ATT, to); Element param_list; if (request_type.equals("s")) { // status mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_STATUS); // only need the handle param param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); Element param = doc_.createElement(GSXML.PARAM_ELEM); param.setAttribute(GSXML.NAME_ATT, GSCGI.PROCESS_ID_ARG); param.setAttribute(GSXML.VALUE_ATT, (String)params.get(GSCGI.PROCESS_ID_ARG)); param_list.appendChild(param); } else { mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS); // add in the params - except the ones only used by the action param_list = getServiceParamList(cgi_param_list); } mr_query_request.appendChild(param_list); Element mr_query_response = (Element)mr_.process(mr_query_message); Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM); if (request_only) { // just send the reponse as is return result_response; } // else append the response to the page page.appendChild(doc_.importNode(result_response, true)); } // add the lang stuff from message page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true)); // add the system stuff from message page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true)); // another part of the page is the service description // request the service info for the selected service - should be cached Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM); Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM); mr_info_message.appendChild(mr_info_request); mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE); mr_info_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT)); mr_info_request.setAttribute(GSXML.TO_ATT, to); Element mr_info_response = (Element) mr_.process(mr_info_message); String path = GSXML.RESPONSE_ELEM; path = GSPath.appendLink(path, GSXML.SERVICE_ELEM); Element description = (Element)doc_.importNode(GSXML.getNodeByPath(mr_info_response, path), true); Element pl = (Element)GSXML.getChildByTagName(description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); if (pl !=null) { // add short names to the params in the param list cgi_.paramListAddShortNames(pl); // for each param in the description, overwrite teh default value with the currently set value if present Element param = (Element)pl.getFirstChild(); while (param !=null) { if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case String name = param.getAttribute(GSXML.NAME_ATT); String current = (String)params.get(name); if (current !=null && !current.equals("")) { param.setAttribute(GSXML.DEFAULT_ATT, current); } } param = (Element)param.getNextSibling(); } } page.appendChild(description); // part of the data for the page is the cgi-params // if we have this here, do we need to do the previous step? Element cgi_request = (Element)doc_.importNode(request, true); page.appendChild(cgi_request); // now process the page and return the result Document style_doc = converter_.getDOM(new File(stylesheet)); GSXSLT.absoluteIncludePaths(style_doc, config_); return (Element)transformer_.transform(style_doc, page); } protected Element getServiceParamList(Element cgi_param_list) { Element new_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); Element param; NodeList cgi_params = cgi_param_list.getChildNodes(); for (int i=0; i