source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/ProcessAction.java@ 25054

Last change on this file since 25054 was 24993, checked in by sjm84, 12 years ago

Adding UserContext to replace the use of lang and uid

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