1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | // XML classes
|
---|
6 | import org.w3c.dom.Node;
|
---|
7 | import org.w3c.dom.NodeList;
|
---|
8 | import org.w3c.dom.Document;
|
---|
9 | import org.w3c.dom.Element;
|
---|
10 |
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.io.File;
|
---|
13 |
|
---|
14 | /** action class for handling applets */
|
---|
15 | public class AppletAction extends Action {
|
---|
16 |
|
---|
17 |
|
---|
18 | public Element process (Element message) {
|
---|
19 |
|
---|
20 |
|
---|
21 | // find the stylesheet
|
---|
22 | String stylesheet = GSFile.stylesheetFile(config_, "applet.xsl");
|
---|
23 |
|
---|
24 | if (stylesheet==null) {
|
---|
25 | System.err.println("AppletAction Error: applet stylesheet not found!");
|
---|
26 | return null;
|
---|
27 | }
|
---|
28 |
|
---|
29 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
30 |
|
---|
31 | // subaction is display (d) or request (r)
|
---|
32 | String request_type = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
33 | if (!request_type.equals("d")&&!request_type.equals("r")) {
|
---|
34 | System.err.println("AppletAction Error: the sa arg should be either d or r, instead it was "+request_type+"!");
|
---|
35 | return null;
|
---|
36 | }
|
---|
37 |
|
---|
38 | // get the collection and service param
|
---|
39 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
40 | HashMap params = GSXML.extractParams(cgi_param_list);
|
---|
41 |
|
---|
42 | String collection = (String)params.get("collection");
|
---|
43 | String service_name=(String)params.get("serviceName");
|
---|
44 | service_name+= "Applet";
|
---|
45 |
|
---|
46 | String to=null;
|
---|
47 | if (collection==null||collection.equals("") ) {
|
---|
48 | to = service_name;
|
---|
49 | } else {
|
---|
50 | to = GSPath.appendLink(collection, service_name);
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (request_type.equals("d")) {
|
---|
54 | // we are just displaying the applet - get the description from
|
---|
55 | // the service, and process using xslt
|
---|
56 |
|
---|
57 | //build up the mr request
|
---|
58 |
|
---|
59 | Element mr_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
60 | Element mr_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
61 | mr_message.appendChild(mr_request);
|
---|
62 | mr_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
63 | mr_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
64 | mr_request.setAttribute(GSXML.TO_ATT, to);
|
---|
65 |
|
---|
66 | Element mr_response = (Element)mr_.process(mr_message);
|
---|
67 |
|
---|
68 | // create the return page tree
|
---|
69 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
70 | page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
71 | // add the lang stuff from message
|
---|
72 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
|
---|
73 | // add the config stuff from message
|
---|
74 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
75 |
|
---|
76 | page.appendChild(doc_.importNode(request, true));
|
---|
77 |
|
---|
78 | // add in the applet info
|
---|
79 | String path = GSXML.RESPONSE_ELEM;
|
---|
80 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
81 | path = GSPath.appendLink(path, GSXML.APPLET_ELEM);
|
---|
82 | Element app_desc = (Element)doc_.importNode(GSXML.getNodeByPath(mr_response, path), true);
|
---|
83 | // must handle any params that have values that are not
|
---|
84 | // necessarily known by the service
|
---|
85 | editLocalParams(app_desc, config_.library_name_, collection);
|
---|
86 | page.appendChild(app_desc);
|
---|
87 |
|
---|
88 | // process using the stylesheet
|
---|
89 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
90 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
91 | return (Element)transformer_.transform(style_doc, page);
|
---|
92 |
|
---|
93 | }
|
---|
94 | else if (request_type.equals("r")) {
|
---|
95 | // 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
|
---|
96 |
|
---|
97 | Element mr_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
98 | Element mr_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
99 | mr_message.appendChild(mr_request);
|
---|
100 | mr_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_QUERY);
|
---|
101 | mr_request.setAttribute(GSXML.TO_ATT, to);
|
---|
102 | mr_request.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
103 |
|
---|
104 | // just append the params for now
|
---|
105 | mr_request.appendChild(doc_.importNode(cgi_param_list, true));
|
---|
106 |
|
---|
107 | Element mr_response = (Element)mr_.process(mr_message);
|
---|
108 | // add in the applet data
|
---|
109 | String path = GSXML.RESPONSE_ELEM;
|
---|
110 | path = GSPath.appendLink(path, GSXML.APPLET_DATA_ELEM);
|
---|
111 | Element applet_info = (Element)GSXML.getNodeByPath(mr_response, path).getFirstChild();
|
---|
112 | return applet_info;
|
---|
113 |
|
---|
114 | }
|
---|
115 | // should never get here
|
---|
116 | return null;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** this method looks through the PARAMs of the applet description for
|
---|
120 | * one with the name 'library'. If its found, it sets the value to be
|
---|
121 | * library_name
|
---|
122 | */
|
---|
123 | protected void editLocalParams(Element description, String library_name,
|
---|
124 | String full_collection_name) {
|
---|
125 |
|
---|
126 | Node child = description.getFirstChild();
|
---|
127 | while (child != null) {
|
---|
128 | if (child.getNodeType() == Node.ELEMENT_NODE) {
|
---|
129 | String param = child.getNodeName();
|
---|
130 | if (param.equals("PARAM")||param.equals("param")) {
|
---|
131 | String name = ((Element)child).getAttribute("NAME");
|
---|
132 | if (name == null || name.equals("")) {
|
---|
133 | // somethings wrong!!
|
---|
134 | } else if (name.equals("library")) {
|
---|
135 | ((Element)child).setAttribute("VALUE", library_name);
|
---|
136 | } else if (name.equals("collection")) {
|
---|
137 | ((Element)child).setAttribute("VALUE", full_collection_name);
|
---|
138 | }
|
---|
139 |
|
---|
140 | }
|
---|
141 | }
|
---|
142 | child = child.getNextSibling();
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | }
|
---|