source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/ProcessAction.java@ 8801

Last change on this file since 8801 was 8801, checked in by kjdon, 19 years ago

the service may not be part of a cluster/collection, so check the cluster name before using it in the request address

  • 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 /** 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;
54 if (cluster_name != null) {
55
56 to = GSPath.appendLink(cluster_name, service_name);
57 } else {
58 to = service_name;
59 }
60
61 if (!request_type.equals("d")) {
62 // if rt=s or rt=r, do the request
63
64 Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
65 String request_type_att;
66 Element param_list = null;
67 if (request_type.equals("s")) { // status
68 request_type_att = GSXML.REQUEST_TYPE_STATUS;
69 param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
70 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
71 param.setAttribute(GSXML.NAME_ATT, GSParams.PROCESS_ID);
72 param.setAttribute(GSXML.VALUE_ATT, (String)params.get(GSParams.PROCESS_ID));
73 param_list.appendChild(param);
74 } else {
75 request_type_att = GSXML.REQUEST_TYPE_PROCESS;
76 // add in the service params - except the ones only used by the action
77 HashMap service_params = (HashMap)params.get("s1");
78 if (service_params != null) {
79 param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
80 GSXML.addParametersToList(this.doc, param_list, service_params);
81 }
82
83 }
84 Element mr_query_request = GSXML.createBasicRequest(this.doc, request_type_att, to, lang, uid);
85 if (param_list != null) {
86 mr_query_request.appendChild(param_list);
87 }
88 mr_query_message.appendChild(mr_query_request);
89
90 Element mr_query_response = (Element)this.mr.process(mr_query_message);
91 Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
92
93 if (response_only) {
94 // just send the reponse as is
95 return result_response;
96 }
97
98 // else append the contents of the response to the page - just the status elem for now
99 Element status = (Element)GSXML.getChildByTagName(result_response, GSXML.STATUS_ELEM);
100 page_response.appendChild(this.doc.importNode(status, true));
101 }
102
103
104 // another part of the page is the service description
105
106 // request the service info for the selected service - should be cached
107 Element mr_info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
108 Element mr_info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, uid);
109 mr_info_message.appendChild(mr_info_request);
110 Element mr_info_response = (Element) this.mr.process(mr_info_message);
111
112 String path = GSXML.RESPONSE_ELEM;
113 path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
114 Element description = (Element)this.doc.importNode(GSXML.getNodeByPath(mr_info_response, path), true);
115
116 page_response.appendChild(description);
117
118 return result;
119 }
120
121 protected Element getServiceParamList(Element cgi_param_list) {
122
123 Element new_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
124 Element param;
125 NodeList cgi_params = cgi_param_list.getChildNodes();
126 for (int i=0; i<cgi_params.getLength(); i++) {
127 Element p = (Element) cgi_params.item(i);
128 String name = p.getAttribute(GSXML.NAME_ATT);
129 if (name.equals(GSParams.SERVICE) || name.equals(GSParams.REQUEST_TYPE) || name.equals(GSParams.CLUSTER)) {
130 continue;
131 }
132 // else add it in to the list
133 new_param_list.appendChild(this.doc.importNode(p, true));
134 }
135 return new_param_list;
136 }
137}
Note: See TracBrowser for help on using the repository browser.