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