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 |
|
---|
13 | /** process a request */
|
---|
14 | public Node process(Node message_node)
|
---|
15 | {
|
---|
16 |
|
---|
17 | Element message = this.converter.nodeToElement(message_node);
|
---|
18 |
|
---|
19 | // the result
|
---|
20 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
21 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
22 | result.appendChild(page_response);
|
---|
23 |
|
---|
24 | // assume only one request
|
---|
25 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
26 | logger.debug(" request=" + this.converter.getString(request));
|
---|
27 |
|
---|
28 | UserContext userContext = new UserContext(request);
|
---|
29 |
|
---|
30 | // get the param list
|
---|
31 | Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
32 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
33 | String service_name = (String) params.get(GSParams.SERVICE);
|
---|
34 | String cluster_name = (String) params.get(GSParams.CLUSTER);
|
---|
35 | String response_only_p = (String) params.get(GSParams.RESPONSE_ONLY);
|
---|
36 | boolean response_only = false;
|
---|
37 | if (response_only_p != null)
|
---|
38 | {
|
---|
39 | response_only = (response_only_p.equals("1") ? true : false);
|
---|
40 | }
|
---|
41 | String request_type = (String) params.get(GSParams.REQUEST_TYPE);
|
---|
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 | // where to send requests
|
---|
52 | String to;
|
---|
53 | if (cluster_name != null)
|
---|
54 | {
|
---|
55 | to = GSPath.appendLink(cluster_name, service_name);
|
---|
56 | }
|
---|
57 | else
|
---|
58 | {
|
---|
59 | to = service_name;
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (request_type.equals("r") || request_type.equals("s"))
|
---|
63 | {
|
---|
64 | //do the request
|
---|
65 |
|
---|
66 | Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
67 | Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
|
---|
68 |
|
---|
69 | if(request_type.equals("s"))
|
---|
70 | {
|
---|
71 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_STATUS);
|
---|
72 | }
|
---|
73 |
|
---|
74 | mr_query_message.appendChild(mr_query_request);
|
---|
75 |
|
---|
76 | Element param_list = null;
|
---|
77 | // add in the service params - except the ones only used by the action
|
---|
78 | HashMap service_params = (HashMap) params.get("s1");
|
---|
79 | if (service_params != null)
|
---|
80 | {
|
---|
81 | param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
82 | GSXML.addParametersToList(this.doc, param_list, service_params);
|
---|
83 | mr_query_request.appendChild(param_list);
|
---|
84 | }
|
---|
85 |
|
---|
86 | Element mr_query_response = (Element) this.mr.process(mr_query_message);
|
---|
87 | Element result_response = (Element) GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
88 |
|
---|
89 | if (response_only)
|
---|
90 | {
|
---|
91 | // just send the reponse as is
|
---|
92 | addSiteMetadata(result_response, userContext);
|
---|
93 | return result_response;
|
---|
94 | }
|
---|
95 | if (result_response != null)
|
---|
96 | {
|
---|
97 | // else append the contents of the response to the page
|
---|
98 | GSXML.copyAllChildren(page_response, result_response);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | // another part of the page is the service description
|
---|
103 |
|
---|
104 | // request the service info for the selected service - should be cached
|
---|
105 | Element mr_info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
106 | Element mr_info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
|
---|
107 | mr_info_message.appendChild(mr_info_request);
|
---|
108 | Element mr_info_response = (Element) this.mr.process(mr_info_message);
|
---|
109 |
|
---|
110 | String path = GSXML.RESPONSE_ELEM;
|
---|
111 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
112 |
|
---|
113 | Node desNode = GSXML.getNodeByPath(mr_info_response, path);
|
---|
114 | if (desNode != null)
|
---|
115 | {
|
---|
116 | page_response.appendChild((Element) this.doc.importNode(desNode, true));
|
---|
117 | }
|
---|
118 |
|
---|
119 | addSiteMetadata(page_response, userContext);
|
---|
120 |
|
---|
121 | return result;
|
---|
122 | }
|
---|
123 |
|
---|
124 | }
|
---|