source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/AppletAction.java@ 24993

Last change on this file since 24993 was 24993, checked in by sjm84, 12 years ago

Adding UserContext to replace the use of lang and uid

  • Property svn:keywords set to Author Date Id Revision
File size: 4.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.io.File;
13
14import org.apache.log4j.*;
15
16/** action class for handling applets */
17public class AppletAction extends Action
18{
19
20 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.AppletAction.class.getName());
21
22 public Node process(Node message_node)
23 {
24
25 Element message = this.converter.nodeToElement(message_node);
26
27 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
28 Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
29 Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
30 result.appendChild(page_response);
31
32 // get the collection and service params
33 Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
34 HashMap params = GSXML.extractParams(cgi_param_list, false);
35
36 // request_type is display (d) or request (r)
37 String request_type = (String) params.get(GSParams.REQUEST_TYPE);
38 if (!request_type.equals("d") && !request_type.equals("r"))
39 {
40 logger.error("AppletAction Error: the rt arg should be either d or r, instead it was " + request_type + "!");
41 return result;
42 }
43
44 String collection = (String) params.get(GSParams.COLLECTION);
45 boolean coll_specified = true;
46 String service_name = (String) params.get(GSParams.SERVICE);
47 UserContext userContext = new UserContext(request);
48 String to = null;
49 if (collection == null || collection.equals(""))
50 {
51 coll_specified = false;
52 to = service_name;
53 }
54 else
55 {
56 to = GSPath.appendLink(collection, service_name);
57 }
58
59 if (request_type.equals("r"))
60 {
61 // we are processing stuff for the applet send a message to the service, type="query", and take out the something element, and return that as our result - the applet must take xml
62
63 Element mr_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
64 Element mr_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
65 mr_message.appendChild(mr_request);
66 // just append all the params for now - should filter out unneeded ones
67 mr_request.appendChild(this.doc.importNode(cgi_param_list, true));
68
69 // process the request
70 Element mr_response = (Element) this.mr.process(mr_message);
71 // get the applet data out and pass it back as is.
72 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.APPLET_DATA_ELEM);
73 Element applet_info = GSXML.getFirstElementChild(GSXML.getNodeByPath(mr_response, path));
74 //Element applet_info = (Element)GSXML.getNodeByPath(mr_response, path).getFirstChild();
75 return applet_info;
76
77 }
78
79 // get the applet description, and the collection info if a collection is specified
80
81 Element mr_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
82 Element applet_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
83 mr_message.appendChild(applet_request);
84
85 Element mr_response = (Element) this.mr.process(mr_message);
86
87 // add in the applet info
88 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM);
89 //path = GSPath.appendLink(path, GSXML.APPLET_ELEM);
90 Element service_elem = (Element) this.doc.importNode(GSXML.getNodeByPath(mr_response, path), true);
91 Element applet_elem = (Element) GSXML.getChildByTagName(service_elem, GSXML.APPLET_ELEM);
92 // must handle any params that have values that are not
93 // necessarily known by the service
94 // should this be done here or by web receptionist??
95 // cant really have an applet without web?
96 editLocalParams(applet_elem, (String) config_params.get(GSConstants.LIBRARY_NAME), collection);
97 page_response.appendChild(service_elem);
98
99 return result;
100
101 }
102
103 /**
104 * this method looks through the PARAMs of the applet description for
105 * 'library' or 'collection'. If found, the params are set to the
106 * appropriate values should this be done here or in the receptionist?
107 */
108 protected void editLocalParams(Element description, String library_name, String full_collection_name)
109 {
110
111 Node child = description.getFirstChild();
112 while (child != null)
113 {
114 if (child.getNodeType() == Node.ELEMENT_NODE)
115 {
116 String param = child.getNodeName();
117 if (param.equals("PARAM") || param.equals("param"))
118 {
119 String name = ((Element) child).getAttribute("NAME");
120 if (name == null || name.equals(""))
121 {
122 // somethings wrong!!
123 }
124 else if (name.equals("library"))
125 {
126 ((Element) child).setAttribute("VALUE", library_name);
127 }
128 else if (name.equals("collection"))
129 {
130 ((Element) child).setAttribute("VALUE", full_collection_name);
131 }
132
133 }
134 }
135 child = child.getNextSibling();
136 }
137 }
138
139}
Note: See TracBrowser for help on using the repository browser.