[3645] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
| 3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
| 4 | import org.greenstone.gsdl3.util.*;
|
---|
| 5 | // XML classes
|
---|
[24991] | 6 | import org.w3c.dom.Node;
|
---|
| 7 | import org.w3c.dom.NodeList;
|
---|
| 8 | import org.w3c.dom.Document;
|
---|
| 9 | import org.w3c.dom.Element;
|
---|
[3645] | 10 |
|
---|
| 11 | import java.util.HashMap;
|
---|
| 12 | import java.util.Map;
|
---|
| 13 | import java.util.Iterator;
|
---|
| 14 | import java.io.File;
|
---|
[25635] | 15 | import java.io.Serializable;
|
---|
[3645] | 16 |
|
---|
[24991] | 17 | public class ProcessAction extends Action
|
---|
| 18 | {
|
---|
[3645] | 19 |
|
---|
[24991] | 20 | /** process a request */
|
---|
| 21 | public Node process(Node message_node)
|
---|
| 22 | {
|
---|
[16688] | 23 |
|
---|
[24991] | 24 | Element message = this.converter.nodeToElement(message_node);
|
---|
[4257] | 25 |
|
---|
[24991] | 26 | // the result
|
---|
| 27 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 28 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 29 | result.appendChild(page_response);
|
---|
[3682] | 30 |
|
---|
[24991] | 31 | // assume only one request
|
---|
| 32 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
[3645] | 33 |
|
---|
[24991] | 34 | // get the param list
|
---|
| 35 | Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 36 | HashMap<String, Serializable> params = GSXML.extractParams(cgi_param_list, false);
|
---|
[24991] | 37 | String service_name = (String) params.get(GSParams.SERVICE);
|
---|
| 38 | String cluster_name = (String) params.get(GSParams.CLUSTER);
|
---|
| 39 | String response_only_p = (String) params.get(GSParams.RESPONSE_ONLY);
|
---|
| 40 | boolean response_only = false;
|
---|
| 41 | if (response_only_p != null)
|
---|
| 42 | {
|
---|
| 43 | response_only = (response_only_p.equals("1") ? true : false);
|
---|
| 44 | }
|
---|
| 45 | String request_type = (String) params.get(GSParams.REQUEST_TYPE);
|
---|
[24993] | 46 | UserContext userContext = new UserContext(request);
|
---|
[24991] | 47 | // what is carried out depends on the request_type
|
---|
| 48 | // if rt=d, then a describe request is done,
|
---|
| 49 | // is rt=r, a request and then a describe request is done
|
---|
| 50 | // if rt=s, a status request is done.
|
---|
[3645] | 51 |
|
---|
[24991] | 52 | // if ro=1, then this calls for a process only page - we do the request
|
---|
| 53 | // (rt should be r or s) and just give the response straight back
|
---|
| 54 | // without any page processing
|
---|
[3645] | 55 |
|
---|
[24991] | 56 | // where to send requests
|
---|
| 57 | String to;
|
---|
| 58 | if (cluster_name != null)
|
---|
| 59 | {
|
---|
[3645] | 60 |
|
---|
[24991] | 61 | to = GSPath.appendLink(cluster_name, service_name);
|
---|
[4981] | 62 | }
|
---|
[24991] | 63 | else
|
---|
| 64 | {
|
---|
| 65 | to = service_name;
|
---|
| 66 | }
|
---|
[3645] | 67 |
|
---|
[24991] | 68 | if (!request_type.equals("d"))
|
---|
| 69 | {
|
---|
| 70 | // if rt=s or rt=r, do the request
|
---|
[3645] | 71 |
|
---|
[24991] | 72 | Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 73 | String request_type_att;
|
---|
| 74 | Element param_list = null;
|
---|
| 75 | if (request_type.equals("s"))
|
---|
| 76 | { // status
|
---|
| 77 | request_type_att = GSXML.REQUEST_TYPE_STATUS;
|
---|
| 78 | param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 79 | Element param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
| 80 | param.setAttribute(GSXML.NAME_ATT, GSParams.PROCESS_ID);
|
---|
| 81 | param.setAttribute(GSXML.VALUE_ATT, (String) params.get(GSParams.PROCESS_ID));
|
---|
| 82 | param_list.appendChild(param);
|
---|
| 83 | }
|
---|
| 84 | else
|
---|
| 85 | {
|
---|
| 86 | request_type_att = GSXML.REQUEST_TYPE_PROCESS;
|
---|
| 87 | // add in the service params - except the ones only used by the action
|
---|
| 88 | HashMap service_params = (HashMap) params.get("s1");
|
---|
| 89 | if (service_params != null)
|
---|
| 90 | {
|
---|
| 91 | param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 92 | GSXML.addParametersToList(this.doc, param_list, service_params);
|
---|
| 93 | }
|
---|
[3645] | 94 |
|
---|
[24991] | 95 | }
|
---|
[24993] | 96 | Element mr_query_request = GSXML.createBasicRequest(this.doc, request_type_att, to, userContext);
|
---|
[24991] | 97 | if (param_list != null)
|
---|
| 98 | {
|
---|
| 99 | mr_query_request.appendChild(param_list);
|
---|
| 100 | }
|
---|
| 101 | mr_query_message.appendChild(mr_query_request);
|
---|
[3645] | 102 |
|
---|
[24991] | 103 | Element mr_query_response = (Element) this.mr.process(mr_query_message);
|
---|
| 104 | Element result_response = (Element) GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
[3645] | 105 |
|
---|
[24991] | 106 | if (response_only)
|
---|
| 107 | {
|
---|
| 108 | // just send the reponse as is
|
---|
| 109 | return result_response;
|
---|
| 110 | }
|
---|
[3645] | 111 |
|
---|
[24991] | 112 | // else append the contents of the response to the page - just the status elem for now
|
---|
| 113 | Element status = (Element) GSXML.getChildByTagName(result_response, GSXML.STATUS_ELEM);
|
---|
| 114 | page_response.appendChild(this.doc.importNode(status, true));
|
---|
| 115 | }
|
---|
[3966] | 116 |
|
---|
[24991] | 117 | // another part of the page is the service description
|
---|
| 118 |
|
---|
| 119 | // request the service info for the selected service - should be cached
|
---|
| 120 | Element mr_info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24993] | 121 | Element mr_info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
|
---|
[24991] | 122 | mr_info_message.appendChild(mr_info_request);
|
---|
| 123 | Element mr_info_response = (Element) this.mr.process(mr_info_message);
|
---|
| 124 |
|
---|
| 125 | String path = GSXML.RESPONSE_ELEM;
|
---|
| 126 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
| 127 | Element description = (Element) this.doc.importNode(GSXML.getNodeByPath(mr_info_response, path), true);
|
---|
| 128 |
|
---|
| 129 | page_response.appendChild(description);
|
---|
| 130 |
|
---|
| 131 | return result;
|
---|
[3966] | 132 | }
|
---|
[24991] | 133 |
|
---|
| 134 | protected Element getServiceParamList(Element cgi_param_list)
|
---|
| 135 | {
|
---|
| 136 |
|
---|
| 137 | Element new_param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 138 | Element param;
|
---|
| 139 | NodeList cgi_params = cgi_param_list.getChildNodes();
|
---|
| 140 | for (int i = 0; i < cgi_params.getLength(); i++)
|
---|
| 141 | {
|
---|
| 142 | Element p = (Element) cgi_params.item(i);
|
---|
| 143 | String name = p.getAttribute(GSXML.NAME_ATT);
|
---|
| 144 | if (name.equals(GSParams.SERVICE) || name.equals(GSParams.REQUEST_TYPE) || name.equals(GSParams.CLUSTER))
|
---|
| 145 | {
|
---|
| 146 | continue;
|
---|
| 147 | }
|
---|
| 148 | // else add it in to the list
|
---|
| 149 | new_param_list.appendChild(this.doc.importNode(p, true));
|
---|
| 150 | }
|
---|
| 151 | return new_param_list;
|
---|
| 152 | }
|
---|
[3645] | 153 | }
|
---|