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