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

Last change on this file since 16688 was 16688, checked in by davidb, 16 years ago

Changed 'Element process(Element)' in ModuleInterface to 'Node process(Node)'. After some deliberation is was decided this is a more useful (generic) layer of the DOM to pass information around in. Helps with the DocType problem when producing XSL Transformed pages, for example. When this was an Element, it would loose track of its DocType. Supporting method provided in XMLConverter 'Element nodeToElement(Node)' which checks a nodes docType and casts to Element if appropriate, or if a Document, typecasts to that and then extracts the top-level Element. With this fundamental change in ModuleInterface, around 20 files needed to be updated (Actions, Services, etc) that build on top of 'process()' to reflect this change, and use nodeToElement where necessary.

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