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

Last change on this file since 11022 was 10297, checked in by kjdon, 19 years ago

gsdl3home now comes from globalProperties, not the web.xml file

  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 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
45 /** user id - new one per session. This doesn't work if session state is saved between restarts - this requires this value to be saved too. */
46 protected int next_user_id = 0;
47
48
49 /** initialise the servlet
50 */
51 public void init(ServletConfig config) throws ServletException {
52 // always call super.init;
53 super.init(config);
54 // disable preferences - does this work anyway??
55 //System.setProperty("java.util.prefs.PreferencesFactory", "org.greenstone.gsdl3.util.DisabledPreferencesFactory");
56
57 String library_name = config.getInitParameter(GSConstants.LIBRARY_NAME);
58 String gsdl3_home = config.getInitParameter(GSConstants.GSDL3_HOME);
59 String site_name = config.getInitParameter(GSConstants.SITE_NAME);
60 String interface_name = config.getInitParameter(GSConstants.INTERFACE_NAME);
61 this.default_lang = config.getInitParameter(GSConstants.DEFAULT_LANG);
62 if (library_name == null || site_name ==null || interface_name ==null) {
63 // must have this
64 System.err.println("initialisation parameters not all set!");
65 System.err.println(" you must have libraryname, sitename, interfacename");
66 System.exit(1);
67 }
68
69 if (this.default_lang == null) {
70 // choose english
71 this.default_lang = DEFAULT_LANG;
72 }
73
74 HashMap config_params = new HashMap();
75
76 config_params.put(GSConstants.LIBRARY_NAME, library_name);
77 config_params.put(GSConstants.SITE_NAME, site_name);
78 config_params.put(GSConstants.INTERFACE_NAME, interface_name);
79
80 this.converter = new XMLConverter();
81 this.doc = this.converter.newDOM();
82
83 // now try and create MR and recpt
84 // new message router - create it and pass reference to recept.
85 // the servlet wont use this directly
86 String mr_name = (String)config.getInitParameter("messagerouter_class");
87 MessageRouter message_router = null;
88 if (mr_name == null) { // just use the normal MR
89 message_router = new MessageRouter();
90 } else { // try the specified one
91 try {
92 message_router = (MessageRouter)Class.forName("org.greenstone.gsdl3.core."+mr_name).newInstance();
93 } catch (Exception e) { // cant use this new one, so use normal one
94 System.err.println("LibraryServlet configure exception when trying to use a new MessageRouter "+mr_name+": "+e.getMessage());
95 e.printStackTrace();
96 message_router = new MessageRouter();
97 }
98 }
99
100 message_router.setSiteName(site_name);
101 message_router.configure();
102
103 // the receptionist -the servlet will talk to this
104 String recept_name = (String)config.getInitParameter("receptionist_class");
105 if (recept_name == null) {
106 this.recept = new DefaultReceptionist();
107 } else {
108 try {
109 this.recept = (Receptionist)Class.forName("org.greenstone.gsdl3.core."+recept_name).newInstance();
110 } catch (Exception e) { // cant use this new one, so use normal one
111 System.err.println("LibraryServlet configure exception when trying to use a new Receptionist "+recept_name+": "+e.getMessage());
112 e.printStackTrace();
113 this.recept = new DefaultReceptionist();
114 }
115 }
116 this.recept.setConfigParams(config_params);
117 this.recept.setMessageRouter(message_router);
118 // the params arg thingy
119
120 String params_name = (String)config.getInitParameter("params_class");
121 if (params_name == null) {
122 this.params = new GSParams();
123 } else {
124 try {
125 this.params = (GSParams)Class.forName("org.greenstone.gsdl3.util."+params_name).newInstance();
126 } catch (Exception e) {
127 System.err.println("LibraryServlet configure exception when trying to use a new params thing "+params_name+": "+e.getMessage());
128 e.printStackTrace();
129 this.params = new GSParams();
130 }
131 }
132 // pass it to the receptionist
133 this.recept.setParams(this.params);
134 this.recept.configure();
135
136 }
137
138 public void doGet(HttpServletRequest request,
139 HttpServletResponse response)
140 throws ServletException, IOException {
141
142 HttpSession session = request.getSession(true);
143 String uid = (String)session.getAttribute(GSXML.USER_ID_ATT);
144 if (uid ==null) {
145 uid = ""+getNextUserId();
146 session.setAttribute(GSXML.USER_ID_ATT, uid);
147 }
148 request.setCharacterEncoding("UTF-8");
149 response.setContentType("text/html;charset=UTF-8");
150 PrintWriter out = response.getWriter();
151
152 String lang = request.getParameter(GSParams.LANGUAGE);
153 if (lang==null || lang.equals("")) {
154 // try the session cached lang
155 lang = (String)session.getAttribute(GSParams.LANGUAGE);
156 if (lang==null || lang.equals("")) {
157 // still not set, use the default
158 lang = this.default_lang;
159 }
160 }
161
162 // set the lang in the session
163 session.setAttribute(GSParams.LANGUAGE, lang);
164
165 String output = request.getParameter(GSParams.OUTPUT);
166 if (output==null || output.equals("")) {
167 output = "html"; // uses html by default
168 }
169
170 // the request to the receptionist
171 Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
172 Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", lang, uid);
173 xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
174 xml_message.appendChild(xml_request);
175
176
177 String action = request.getParameter(GSParams.ACTION);
178 String subaction = request.getParameter(GSParams.SUBACTION);
179 if (action==null || action.equals("")) {
180 // should we do all the following stuff if using default page?
181 // display the home page - the default page
182 action = "p";
183 subaction = PageAction.HOME_PAGE;
184 xml_request.setAttribute(GSXML.ACTION_ATT, action);
185 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
186
187 } else {
188
189 xml_request.setAttribute(GSXML.ACTION_ATT, action);
190 if (subaction != null) {
191 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
192 }
193
194 // create the param list for the greenstone request - includes
195 // the params from the current request and any others from the saved session
196 Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
197 xml_request.appendChild(xml_param_list);
198
199 Enumeration params = request.getParameterNames();
200 while(params.hasMoreElements()) {
201 String name = (String)params.nextElement();
202 if (!name.equals(GSParams.ACTION) && !name.equals(GSParams.SUBACTION) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSParams.OUTPUT)) {// we have already dealt with these
203 String value="";
204 String [] values = request.getParameterValues(name);
205 value = values[0];
206 if (values.length > 1) {
207 for (int i=1; i< values.length; i++) {
208 value += ","+values[i];
209 }
210 }
211 // either add it to the param list straight away, or save it to the session and add it later
212 if (this.params.shouldSave(name)) {
213 session.setAttribute(name, value);
214 } else {
215 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
216 param.setAttribute(GSXML.NAME_ATT, name);
217 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
218 xml_param_list.appendChild(param);
219 }
220 }
221 }
222
223 // put in all the params from the session cache
224 params = session.getAttributeNames();
225 while(params.hasMoreElements()) {
226 String name = (String)params.nextElement();
227
228 if ( !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT)) { // lang and uid are stored but we dont want it in the param list cos its already in the request
229
230 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
231 param.setAttribute(GSXML.NAME_ATT, name);
232 String value = GSXML.xmlSafe((String)session.getAttribute(name));
233 // ugly hack to undo : escaping
234 value = value.replaceAll("%3A", "\\:");
235 param.setAttribute(GSXML.VALUE_ATT,value);
236 xml_param_list.appendChild(param);
237 }
238 }
239
240
241 }
242
243 if (!output.equals("html")) {
244 response.setContentType("text/xml"); // for now use text
245 }
246
247 Element xml_result = this.recept.process(xml_message);
248 encodeURLs(xml_result, response);
249 out.println(this.converter.getPrettyString(xml_result));
250 }
251
252 /** this goes through each URL and adds in a session id if needed--
253 * its needed if the browser doesn't accept cookies
254 * also escapes things if needed
255 */
256 protected void encodeURLs(Element data, HttpServletResponse response) {
257
258 if (data == null) {
259 return;
260 }
261 // get all the <a> elements
262 NodeList hrefs = data.getElementsByTagName("a");
263 for (int i=0; i<hrefs.getLength(); i++) {
264 Element a = (Element)hrefs.item(i);
265 // ugly hack to get rid of : in the args - interferes with session handling
266 String href = a.getAttribute("href");
267 if (!href.equals("")) {
268 if (href.indexOf("?")!=-1) {
269 String[] parts = href.split("\\?", -1);
270 parts[1]=parts[1].replaceAll(":", "%3A");
271 href = parts[0]+"?"+parts[1];
272 }
273 a.setAttribute("href", response.encodeURL(href));
274 }
275 }
276
277 // now find any submit bits - get all the <form> elements
278 NodeList forms = data.getElementsByTagName("form");
279 for (int i=0; i<forms.getLength(); i++) {
280 Element form = (Element)forms.item(i);
281 form.setAttribute("action", response.encodeURL(form.getAttribute("action")));
282 }
283 // are these the only cases where URLs occur??
284 // we should only do this for greenstone urls?
285
286 }
287
288 synchronized protected int getNextUserId() {
289 next_user_id++;
290 return next_user_id;
291 }
292
293}
Note: See TracBrowser for help on using the repository browser.