1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.util.*;
|
---|
4 | // XML classes
|
---|
5 | import org.w3c.dom.Node;
|
---|
6 | import org.w3c.dom.Element;
|
---|
7 |
|
---|
8 | import java.util.HashMap;
|
---|
9 |
|
---|
10 | public class GeneralAction extends Action {
|
---|
11 |
|
---|
12 | /** process a request */
|
---|
13 | public Node process (Node message_node) {
|
---|
14 |
|
---|
15 | Element message = this.converter.nodeToElement(message_node);
|
---|
16 |
|
---|
17 | // the result
|
---|
18 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
19 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
20 | result.appendChild(page_response);
|
---|
21 |
|
---|
22 | // assume only one request
|
---|
23 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
24 | logger.debug(" request="+this.converter.getString(request));
|
---|
25 |
|
---|
26 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
27 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
28 |
|
---|
29 | //A shortcut action serving the flax web page requests
|
---|
30 | if(request.getAttribute(GSXML.SUBACTION_ATT).equals(GSXML.FLAX_PAGE_GENERATION)
|
---|
31 | && request.getAttribute(GSXML.TO_ATT).equals(GSXML.FLAX_PAGE_GENERATION)) {
|
---|
32 | //
|
---|
33 | Element mr_info_response = (Element) this.mr.process(message);
|
---|
34 | Node flaxPageNode = (mr_info_response.getElementsByTagName(GSXML.FLAX_PAGE)).item(0);
|
---|
35 | //logger.info(" raw page="+this.converter.getString(flaxPageNode));
|
---|
36 | if(flaxPageNode != null){
|
---|
37 | page_response.appendChild((Element)this.doc.importNode(flaxPageNode, true));
|
---|
38 | }
|
---|
39 | addSiteMetadata(result, lang, uid);
|
---|
40 | return result;
|
---|
41 | }
|
---|
42 |
|
---|
43 | // get the param list
|
---|
44 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
45 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
46 | String service_name = (String) params.get(GSParams.SERVICE);
|
---|
47 | String cluster_name = (String) params.get(GSParams.CLUSTER);
|
---|
48 | String response_only_p = (String)params.get(GSParams.RESPONSE_ONLY);
|
---|
49 | boolean response_only = false;
|
---|
50 | if (response_only_p!=null) {
|
---|
51 | response_only = (response_only_p.equals("1")?true:false);
|
---|
52 | }
|
---|
53 | String request_type = (String) params.get(GSParams.REQUEST_TYPE);
|
---|
54 | // what is carried out depends on the request_type
|
---|
55 | // if rt=d, then a describe request is done,
|
---|
56 | // is rt=r, a request and then a describe request is done
|
---|
57 | // if rt=s, a status request is done.
|
---|
58 |
|
---|
59 | // if ro=1, then this calls for a process only page - we do the request
|
---|
60 | // (rt should be r or s) and just give the response straight back
|
---|
61 | // without any page processing
|
---|
62 |
|
---|
63 |
|
---|
64 | // where to send requests
|
---|
65 | String to;
|
---|
66 | if (cluster_name != null) {
|
---|
67 | to = GSPath.appendLink(cluster_name, service_name);
|
---|
68 | } else {
|
---|
69 | to = service_name;
|
---|
70 | }
|
---|
71 |
|
---|
72 | if (request_type.equals("r")) {
|
---|
73 | //do the request
|
---|
74 |
|
---|
75 | Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
76 | Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);
|
---|
77 |
|
---|
78 | mr_query_message.appendChild(mr_query_request);
|
---|
79 |
|
---|
80 | Element param_list = null;
|
---|
81 | // add in the service params - except the ones only used by the action
|
---|
82 | HashMap service_params = (HashMap)params.get("s1");
|
---|
83 | if (service_params != null) {
|
---|
84 | param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
85 | GSXML.addParametersToList(this.doc, param_list, service_params);
|
---|
86 | mr_query_request.appendChild(param_list);
|
---|
87 | }
|
---|
88 |
|
---|
89 | Element mr_query_response = (Element)this.mr.process(mr_query_message);
|
---|
90 | Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
91 |
|
---|
92 | if (response_only) {
|
---|
93 | // just send the reponse as is
|
---|
94 | addSiteMetadata(result_response, lang, uid);
|
---|
95 | return result_response;
|
---|
96 | }
|
---|
97 | if (result_response != null){
|
---|
98 | // else append the contents of the response to the page
|
---|
99 | GSXML.copyAllChildren(page_response, result_response);
|
---|
100 | }
|
---|
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 |
|
---|
113 | String path = GSXML.RESPONSE_ELEM;
|
---|
114 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
115 |
|
---|
116 |
|
---|
117 | Node desNode = GSXML.getNodeByPath(mr_info_response, path);
|
---|
118 | if(desNode != null){
|
---|
119 | page_response.appendChild((Element)this.doc.importNode(desNode, true));
|
---|
120 | }
|
---|
121 |
|
---|
122 | addSiteMetadata(page_response, lang, uid);
|
---|
123 | return result;
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|