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

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

now use GSParams class instead of GSCGI class

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