source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/GeneralAction.java@ 13974

Last change on this file since 13974 was 13974, checked in by shaoqun, 17 years ago

added a guard

  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 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
16
17public class GeneralAction extends Action {
18
19 /** process a request */
20 public Element process (Element message) {
21
22 // the result
23 Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
24 Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
25 result.appendChild(page_response);
26
27 // assume only one request
28 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
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 response_only = (response_only_p.equals("1")?true:false);
39 }
40 String request_type = (String) params.get(GSParams.REQUEST_TYPE);
41 String lang = request.getAttribute(GSXML.LANG_ATT);
42 String uid = request.getAttribute(GSXML.USER_ID_ATT);
43 // what is carried out depends on the request_type
44 // if rt=d, then a describe request is done,
45 // is rt=r, a request and then a describe request is done
46 // if rt=s, a status request is done.
47
48 // if ro=1, then this calls for a process only page - we do the request
49 // (rt should be r or s) and just give the response straight back
50 // without any page processing
51
52
53 // where to send requests
54 String to;
55 if (cluster_name != null) {
56 to = GSPath.appendLink(cluster_name, service_name);
57 } else {
58 to = service_name;
59 }
60
61 if (request_type.equals("r")) {
62 //do the request
63
64 Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
65 Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, lang, uid);
66
67 mr_query_message.appendChild(mr_query_request);
68
69 Element param_list = null;
70 // add in the service params - except the ones only used by the action
71 HashMap service_params = (HashMap)params.get("s1");
72 if (service_params != null) {
73 param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
74 GSXML.addParametersToList(this.doc, param_list, service_params);
75 mr_query_request.appendChild(param_list);
76 }
77
78 Element mr_query_response = (Element)this.mr.process(mr_query_message);
79 Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
80
81 if (response_only) {
82 // just send the reponse as is
83 return result_response;
84 }
85 if (result_response != null){
86 // else append the contents of the response to the page
87 GSXML.copyAllChildren(page_response, result_response);
88 }
89 }
90
91
92 // another part of the page is the service description
93
94 // request the service info for the selected service - should be cached
95 Element mr_info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
96 Element mr_info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang, uid);
97 mr_info_message.appendChild(mr_info_request);
98 Element mr_info_response = (Element) this.mr.process(mr_info_message);
99
100
101 String path = GSXML.RESPONSE_ELEM;
102 path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
103
104
105 Node desNode = GSXML.getNodeByPath(mr_info_response, path);
106 if(desNode != null){
107 page_response.appendChild((Element)this.doc.importNode(desNode, true));
108 }
109
110 return result;
111 }
112
113}
Note: See TracBrowser for help on using the repository browser.