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

Last change on this file since 14531 was 14226, checked in by xiao, 17 years ago

change getFirstChild() to getFirstElementChild() in case an extra line break or white space added before the first element child which might cause a cast exception.

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