source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/AppletAction.java@ 4144

Last change on this file since 4144 was 4144, checked in by kjdon, 21 years ago

all stuff for the page generated by teh Receptionist (config and display) is now put into a pageExtra element, so actions only need to append one extra piece to the page

  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 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
14/** action class for handling applets */
15public 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 page extra stuff from message
80 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.PAGE_EXTRA_ELEM), true));
81
82 // add in the page Request
83 Element page_request = GSXML.duplicateWithNewName(doc_, request, "pageRequest", true);
84 page.appendChild(page_request);
85
86 // get the applet description, and the collection info if a collection is specified
87
88 Element mr_message = doc_.createElement(GSXML.MESSAGE_ELEM);
89 Element applet_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, to, lang);
90 mr_message.appendChild(applet_request);
91
92 if (coll_specified) {
93 Element coll_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, collection, lang);
94 mr_message.appendChild(coll_request);
95 }
96
97 Element mr_response = (Element)mr_.process(mr_message);
98
99 Element page_response = doc_.createElement(GSXML.PAGE_RESPONSE_ELEM);
100 page.appendChild(page_response);
101 // add in the applet info
102 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM);
103 path = GSPath.appendLink(path, GSXML.APPLET_ELEM);
104 Element app_desc = (Element)doc_.importNode(GSXML.getNodeByPath(mr_response, path), true);
105 // must handle any params that have values that are not
106 // necessarily known by the service
107 editLocalParams(app_desc, config_.library_name_, collection);
108 page_response.appendChild(app_desc);
109
110 if (coll_specified) {
111 Element coll_response = (Element) mr_response.getElementsByTagName(GSXML.RESPONSE_ELEM).item(1);
112 Element coll_elem = (Element)GSXML.getChildByTagName(coll_response, GSXML.COLLECTION_ELEM);
113 page_response.appendChild(doc_.importNode(coll_elem, true));
114 }
115
116 // process using the stylesheet
117 Document style_doc = converter_.getDOM(new File(stylesheet));
118 GSXSLT.absoluteIncludePaths(style_doc, config_);
119 return (Element)transformer_.transform(style_doc, page);
120
121 }
122
123 /** this method looks through the PARAMs of the applet description for
124 * 'library' or 'collection'. If found, the params are set to
125 * the appropriate values
126 */
127 protected void editLocalParams(Element description, String library_name,
128 String full_collection_name) {
129
130 Node child = description.getFirstChild();
131 while (child != null) {
132 if (child.getNodeType() == Node.ELEMENT_NODE) {
133 String param = child.getNodeName();
134 if (param.equals("PARAM")||param.equals("param")) {
135 String name = ((Element)child).getAttribute("NAME");
136 if (name == null || name.equals("")) {
137 // somethings wrong!!
138 } else if (name.equals("library")) {
139 ((Element)child).setAttribute("VALUE", library_name);
140 } else if (name.equals("collection")) {
141 ((Element)child).setAttribute("VALUE", full_collection_name);
142 }
143
144 }
145 }
146 child = child.getNextSibling();
147 }
148 }
149
150}
Note: See TracBrowser for help on using the repository browser.