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 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
21 |
|
---|
22 | // get the collection and service params
|
---|
23 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
24 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
25 |
|
---|
26 | // request_type is display (d) or request (r)
|
---|
27 | String request_type = (String)params.get(GSCGI.REQUEST_TYPE_ARG);
|
---|
28 | if (!request_type.equals("d")&&!request_type.equals("r")) {
|
---|
29 | System.err.println("AppletAction Error: the rt arg should be either d or r, instead it was "+request_type+"!");
|
---|
30 | return null;
|
---|
31 | }
|
---|
32 |
|
---|
33 | String collection = (String)params.get(GSCGI.COLLECTION_ARG);
|
---|
34 | boolean coll_specified = true;
|
---|
35 | String service_name=(String)params.get(GSCGI.SERVICE_ARG);
|
---|
36 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
37 | String to=null;
|
---|
38 | if (collection==null||collection.equals("") ) {
|
---|
39 | coll_specified = false;
|
---|
40 | to = service_name;
|
---|
41 | } else {
|
---|
42 | to = GSPath.appendLink(collection, service_name);
|
---|
43 | }
|
---|
44 |
|
---|
45 | if (request_type.equals("r")) {
|
---|
46 | // 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
|
---|
47 |
|
---|
48 | Element mr_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
49 | Element mr_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_PROCESS, to, lang);
|
---|
50 | mr_message.appendChild(mr_request);
|
---|
51 | // just append all the params for now - should filter out unneeded ones
|
---|
52 | mr_request.appendChild(doc_.importNode(cgi_param_list, true));
|
---|
53 |
|
---|
54 | // process the request
|
---|
55 | Element mr_response = (Element)mr_.process(mr_message);
|
---|
56 | // get the applet data out and pass it back as is.
|
---|
57 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.APPLET_DATA_ELEM);
|
---|
58 | Element applet_info = (Element)GSXML.getNodeByPath(mr_response, path).getFirstChild();
|
---|
59 | return applet_info;
|
---|
60 |
|
---|
61 | }
|
---|
62 |
|
---|
63 | // else request type = 'd'
|
---|
64 | // find the stylesheet
|
---|
65 | String stylesheet = GSFile.stylesheetFile(config_, "applet.xsl");
|
---|
66 |
|
---|
67 | if (stylesheet==null) {
|
---|
68 | System.err.println("AppletAction Error: applet stylesheet not found!");
|
---|
69 | return null;
|
---|
70 | }
|
---|
71 |
|
---|
72 | // we are just displaying the applet - get the description from
|
---|
73 | // the service, and process using xslt
|
---|
74 |
|
---|
75 |
|
---|
76 | // create the return page tree
|
---|
77 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
78 | page.setAttribute(GSXML.LANG_ATT, lang);
|
---|
79 | // add the lang stuff from message
|
---|
80 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
|
---|
81 | // add the config stuff from message
|
---|
82 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
83 |
|
---|
84 | // add in the page Request
|
---|
85 | Element page_request = GSXML.duplicateWithNewName(doc_, request, "pageRequest", true);
|
---|
86 | page.appendChild(page_request);
|
---|
87 |
|
---|
88 | // get the applet description, and the collection info if a collection is specified
|
---|
89 |
|
---|
90 | Element mr_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
91 | Element applet_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, to, lang);
|
---|
92 | mr_message.appendChild(applet_request);
|
---|
93 |
|
---|
94 | if (coll_specified) {
|
---|
95 | Element coll_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, collection, lang);
|
---|
96 | mr_message.appendChild(coll_request);
|
---|
97 | }
|
---|
98 |
|
---|
99 | Element mr_response = (Element)mr_.process(mr_message);
|
---|
100 |
|
---|
101 | Element page_response = doc_.createElement(GSXML.PAGE_RESPONSE_ELEM);
|
---|
102 | page.appendChild(page_response);
|
---|
103 | // add in the applet info
|
---|
104 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM);
|
---|
105 | path = GSPath.appendLink(path, GSXML.APPLET_ELEM);
|
---|
106 | Element app_desc = (Element)doc_.importNode(GSXML.getNodeByPath(mr_response, path), true);
|
---|
107 | // must handle any params that have values that are not
|
---|
108 | // necessarily known by the service
|
---|
109 | editLocalParams(app_desc, config_.library_name_, collection);
|
---|
110 | page_response.appendChild(app_desc);
|
---|
111 |
|
---|
112 | if (coll_specified) {
|
---|
113 | Element coll_response = (Element) mr_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1);
|
---|
114 | Element coll_elem = (Element)GSXML.getChildByTagName(coll_response, GSXML.COLLECTION_ELEM);
|
---|
115 | page_response.appendChild(doc_.importNode(coll_elem, true));
|
---|
116 | }
|
---|
117 |
|
---|
118 | // process using the stylesheet
|
---|
119 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
120 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
121 | return (Element)transformer_.transform(style_doc, page);
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 | /** this method looks through the PARAMs of the applet description for
|
---|
126 | * 'library' or 'collection'. If found, the params are set to
|
---|
127 | * the appropriate values
|
---|
128 | */
|
---|
129 | protected void editLocalParams(Element description, String library_name,
|
---|
130 | String full_collection_name) {
|
---|
131 |
|
---|
132 | Node child = description.getFirstChild();
|
---|
133 | while (child != null) {
|
---|
134 | if (child.getNodeType() == Node.ELEMENT_NODE) {
|
---|
135 | String param = child.getNodeName();
|
---|
136 | if (param.equals("PARAM")||param.equals("param")) {
|
---|
137 | String name = ((Element)child).getAttribute("NAME");
|
---|
138 | if (name == null || name.equals("")) {
|
---|
139 | // somethings wrong!!
|
---|
140 | } else if (name.equals("library")) {
|
---|
141 | ((Element)child).setAttribute("VALUE", library_name);
|
---|
142 | } else if (name.equals("collection")) {
|
---|
143 | ((Element)child).setAttribute("VALUE", full_collection_name);
|
---|
144 | }
|
---|
145 |
|
---|
146 | }
|
---|
147 | }
|
---|
148 | child = child.getNextSibling();
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | }
|
---|