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

Last change on this file since 6301 was 6301, checked in by kjdon, 20 years ago

servlet now saves a uid with the session - this is passed around as an attribute in a request, to be used by whoever needs it

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