source: trunk/gsdl3/src/java/org/greenstone/gsdl3/LibraryServlet.java@ 4259

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

LibraryServlet now caches cgi args, and adds them into the request

  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1package org.greenstone.gsdl3;
2
3import org.greenstone.gsdl3.core.*;
4import org.greenstone.gsdl3.util.*;
5import org.greenstone.gsdl3.action.PageAction; // used to get the default action
6import org.w3c.dom.Document;
7import org.w3c.dom.Element;
8import org.w3c.dom.NodeList;
9import java.io.*;
10import javax.servlet.*;
11import javax.servlet.http.*;
12import java.util.Enumeration;
13import java.io.File;
14
15/** a servlet to serve the greenstone library - we are using servlets instead
16 * of cgi
17 * the init method is called only once - the first time the servlet classes
18 * are loaded. Each time a request comes in to the servlet, the session()
19 * method is called in a new thread (calls doGet/doPut etc)
20 * takes the a=p&p=home type args and builds a simple request to send to
21 * its receptionist, which returns a result in html, cos output=html
22 * is set in the request
23 * @see Receptionist
24 */
25public class LibraryServlet extends HttpServlet {
26
27 /** the receptionist to send messages to */
28 protected WebReceptionist recept=null;
29 /** the default language - is specified by setting a servlet param,
30 * otherwise DEFAULT_LANG is used*/
31 protected String default_lang= null;
32 /** The default default - used if a default lang is not specified
33 * in the servlet params */
34 protected final String DEFAULT_LANG = "en";
35 /** container Document to create XML Nodes */
36 protected Document doc=null;
37 /** a converter class to parse XML and create Docs */
38 protected XMLConverter converter=null;
39 /** the cgi stuff - the Receptionist can add new args to this
40 * its used by the servlet to determine what args to save */
41 protected GSCGI cgi = null;
42 /** initialise the servlet
43 */
44 public void init(ServletConfig config) throws ServletException {
45 // always call super.init;
46 super.init(config);
47
48 ConfigVars config_vars = new ConfigVars();
49 String library_name = config.getInitParameter("libraryname");
50 String gsdl3_home = config.getInitParameter("gsdl3home");
51 String site_name = config.getInitParameter("sitename");
52 String interface_name = config.getInitParameter("interfacename");
53 this.default_lang = config.getInitParameter("defaultlang");
54 if (library_name == null || gsdl3_home == null || site_name ==null || interface_name ==null) {
55 // must have this
56 System.err.println("initialisation parameters not all set!");
57 System.err.println(" you must have libraryname, gsdl3home, sitename, interfacename");
58 System.exit(1);
59 }
60
61 if (this.default_lang == null) {
62 // choose english
63 this.default_lang = DEFAULT_LANG;
64 }
65
66 config_vars.library_name_ = library_name;
67 config_vars.gsdl3_home_ = gsdl3_home;
68 config_vars.site_name_ = site_name;
69 config_vars.interface_name_ =interface_name;
70 config_vars.default_language_ = this.default_lang;
71 config_vars.createXML();
72
73 this.converter = new XMLConverter();
74 this.doc = this.converter.newDOM();
75
76 // now try and create MR and recpt
77 // new message router - create it and pass reference to recept.
78 // the servlet wont use this directly
79 String mr_name = (String)config.getInitParameter("messagerouter");
80 MessageRouter message_router = null;
81 if (mr_name == null) { // just use the normal MR
82 message_router = new MessageRouter();
83 } else { // try the specified one
84 try {
85 message_router = (MessageRouter)Class.forName("org.greenstone.gsdl3.core."+mr_name).newInstance();
86 } catch (Exception e) { // cant use this new one, so use normal one
87 System.out.println("LibraryServlet configure exception when trying to use a new MessageRouter "+mr_name+": "+e.getMessage());
88 e.printStackTrace();
89 message_router = new MessageRouter();
90 }
91 }
92
93 message_router.setSiteHome(GSFile.siteHome(config_vars.gsdl3_home_,
94 config_vars.site_name_));
95 message_router.configure();
96
97 // the receptionist -the servlet will talk to this
98 String recept_name = (String)config.getInitParameter("receptionist");
99 if (recept_name == null) {
100 this.recept = new DefaultReceptionist();
101 } else {
102 try {
103 this.recept = (WebReceptionist)Class.forName("org.greenstone.gsdl3.core."+recept_name).newInstance();
104 } catch (Exception e) { // cant use this new one, so use normal one
105 System.out.println("LibraryServlet configure exception when trying to use a new Receptionist "+recept_name+": "+e.getMessage());
106 e.printStackTrace();
107 this.recept = new DefaultReceptionist();
108 }
109 }
110 this.recept.setConfigVars(config_vars);
111 this.recept.setMessageRouter(message_router);
112 this.recept.configure();
113 // the cgi arg thingy
114
115 String cgi_name = (String)config.getInitParameter("cgiargs");
116 if (cgi_name == null) {
117 this.cgi = new GSCGI();
118 } else {
119 try {
120 this.cgi = (GSCGI)Class.forName("org.greenstone.gsdl3.util."+cgi_name).newInstance();
121 } catch (Exception e) {
122 System.out.println("LibraryServlet configure exception when trying to use a new cgi thing "+cgi_name+": "+e.getMessage());
123 e.printStackTrace();
124 this.cgi = new GSCGI();
125 }
126 }
127 // pass it to the receptionist
128 this.recept.setCGI(this.cgi);
129 }
130
131 public void doGet(HttpServletRequest request,
132 HttpServletResponse response)
133 throws ServletException, IOException {
134
135 HttpSession session = request.getSession(true);
136
137 // print them out to test it
138// Enumeration stored_names = session.getAttributeNames();
139// System.out.println("session values are:");
140// while (stored_names.hasMoreElements()) {
141// String name = (String)stored_names.nextElement();
142// System.out.println("stored "+name+":" +session.getAttribute(name));
143// }
144
145 request.setCharacterEncoding("UTF-8");
146 response.setContentType("text/html;charset=UTF-8");
147 PrintWriter out = response.getWriter();
148
149 String lang = request.getParameter(GSCGI.LANGUAGE_ARG);
150 if (lang==null || lang.equals("")) {
151 // try the session cached lang
152 lang = (String)session.getAttribute(GSCGI.LANGUAGE_ARG);
153 if (lang==null || lang.equals("")) {
154 // still not set, use the default
155 lang = this.default_lang;
156 }
157 }
158
159 // set the lang in the session
160 session.setAttribute(GSCGI.LANGUAGE_ARG, lang);
161
162 String output = request.getParameter(GSCGI.OUTPUT_ARG);
163 if (output==null || output.equals("")) {
164 output = "html"; // uses html by default
165 }
166
167 // the request to the receptionist
168 Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
169 Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_CGI, "", lang);
170 xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
171 xml_message.appendChild(xml_request);
172
173
174 String action = request.getParameter(GSCGI.ACTION_ARG);
175 String subaction = request.getParameter(GSCGI.SUBACTION_ARG);
176 if (action==null || action.equals("")) {
177 // should we do all the following stuff if using default page?
178 // display the home page - the default page
179 action = "p";
180 subaction = PageAction.HOME_PAGE;
181 xml_request.setAttribute(GSXML.ACTION_ATT, action);
182 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
183
184 } else {
185
186 xml_request.setAttribute(GSXML.ACTION_ATT, action);
187 if (subaction != null) {
188 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
189 }
190
191 // create the param list for the greenstone request - includes
192 // the params from the current request and any others from the saved session
193 Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
194 xml_request.appendChild(xml_param_list);
195
196 Enumeration params = request.getParameterNames();
197 while(params.hasMoreElements()) {
198 String name = (String)params.nextElement();
199 if (!name.equals(GSCGI.ACTION_ARG) && !name.equals(GSCGI.SUBACTION_ARG) && !name.equals(GSCGI.LANGUAGE_ARG) && !name.equals(GSCGI.OUTPUT_ARG)) {// we have already dealt with these
200 String value="";
201 String [] values = request.getParameterValues(name);
202 value = values[0];
203 if (values.length > 1) {
204 for (int i=1; i< values.length; i++) {
205 value += ","+values[i];
206 }
207 }
208 // either add it to the param list straight away, or save it to the session and add it later
209 if (this.cgi.shouldSave(name)) {
210 session.setAttribute(name, value);
211 } else {
212 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
213 param.setAttribute(GSXML.NAME_ATT, name);
214 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
215 xml_param_list.appendChild(param);
216 }
217 }
218 }
219
220 // put in all the params from the session cache
221 params = session.getAttributeNames();
222 while(params.hasMoreElements()) {
223 String name = (String)params.nextElement();
224
225 if ( !name.equals(GSCGI.LANGUAGE_ARG) ) { // lang is stored but we dont want it in the param list cos its already in the request
226
227 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
228 param.setAttribute(GSXML.NAME_ATT, name);
229 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String)session.getAttribute(name)));
230 xml_param_list.appendChild(param);
231 }
232 }
233
234
235 }
236
237 if (!output.equals("html")) {
238 response.setContentType("text/xml"); // for now use text
239 }
240
241 Element xml_result = this.recept.process(xml_message);
242 encodeURLs(xml_result, response);
243 out.println(this.converter.getPrettyString(xml_result));
244 }
245
246 /** this goes through each URL and adds in a session id if needed--
247 * its needed if the browser doesn't accept cookies
248 */
249 protected void encodeURLs(Element data, HttpServletResponse response) {
250
251 // get all the <a> elements
252 NodeList hrefs = data.getElementsByTagName("a");
253 for (int i=0; i<hrefs.getLength(); i++) {
254 Element a = (Element)hrefs.item(i);
255 a.setAttribute("href", response.encodeURL(a.getAttribute("href")));
256 }
257
258 // now find any submit bits - get all the <form> elements
259 NodeList forms = data.getElementsByTagName("form");
260 for (int i=0; i<forms.getLength(); i++) {
261 Element form = (Element)forms.item(i);
262 form.setAttribute("action", response.encodeURL(form.getAttribute("action")));
263 }
264 // are these the only cases where URLs occur??
265 // we should only do this for greenstone urls?
266
267 }
268}
Note: See TracBrowser for help on using the repository browser.