1 | package org.greenstone.gsdl3;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.*;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 | import org.greenstone.gsdl3.action.PageAction; // used to get the default action
|
---|
6 | import org.w3c.dom.Document;
|
---|
7 | import org.w3c.dom.Element;
|
---|
8 | import org.w3c.dom.NodeList;
|
---|
9 | import java.io.*;
|
---|
10 | import javax.servlet.*;
|
---|
11 | import javax.servlet.http.*;
|
---|
12 | import java.util.Enumeration;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import 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 | */
|
---|
26 | public 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 | // disable preferences - does this work anyway??
|
---|
51 | //System.setProperty("java.util.prefs.PreferencesFactory", "org.greenstone.gsdl3.util.DisabledPreferencesFactory");
|
---|
52 |
|
---|
53 | String library_name = config.getInitParameter(GSConstants.LIBRARY_NAME);
|
---|
54 | String gsdl3_home = config.getInitParameter(GSConstants.GSDL3_HOME);
|
---|
55 | String site_name = config.getInitParameter(GSConstants.SITE_NAME);
|
---|
56 | String interface_name = config.getInitParameter(GSConstants.INTERFACE_NAME);
|
---|
57 | this.default_lang = config.getInitParameter(GSConstants.DEFAULT_LANG);
|
---|
58 | if (library_name == null || gsdl3_home == null || site_name ==null || interface_name ==null) {
|
---|
59 | // must have this
|
---|
60 | System.err.println("initialisation parameters not all set!");
|
---|
61 | System.err.println(" you must have libraryname, gsdl3home, sitename, interfacename");
|
---|
62 | System.exit(1);
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (this.default_lang == null) {
|
---|
66 | // choose english
|
---|
67 | this.default_lang = DEFAULT_LANG;
|
---|
68 | }
|
---|
69 |
|
---|
70 | HashMap config_params = new HashMap();
|
---|
71 |
|
---|
72 | config_params.put(GSConstants.LIBRARY_NAME, library_name);
|
---|
73 | config_params.put(GSConstants.GSDL3_HOME, gsdl3_home);
|
---|
74 | config_params.put(GSConstants.SITE_NAME, site_name);
|
---|
75 | config_params.put(GSConstants.INTERFACE_NAME, interface_name);
|
---|
76 |
|
---|
77 | this.converter = new XMLConverter();
|
---|
78 | this.doc = this.converter.newDOM();
|
---|
79 |
|
---|
80 | // now try and create MR and recpt
|
---|
81 | // new message router - create it and pass reference to recept.
|
---|
82 | // the servlet wont use this directly
|
---|
83 | String mr_name = (String)config.getInitParameter("messagerouter_class");
|
---|
84 | MessageRouter message_router = null;
|
---|
85 | if (mr_name == null) { // just use the normal MR
|
---|
86 | message_router = new MessageRouter();
|
---|
87 | } else { // try the specified one
|
---|
88 | try {
|
---|
89 | message_router = (MessageRouter)Class.forName("org.greenstone.gsdl3.core."+mr_name).newInstance();
|
---|
90 | } catch (Exception e) { // cant use this new one, so use normal one
|
---|
91 | System.err.println("LibraryServlet configure exception when trying to use a new MessageRouter "+mr_name+": "+e.getMessage());
|
---|
92 | e.printStackTrace();
|
---|
93 | message_router = new MessageRouter();
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | message_router.setSiteHome(GSFile.siteHome(gsdl3_home,
|
---|
98 | site_name));
|
---|
99 | message_router.configure();
|
---|
100 |
|
---|
101 | // the receptionist -the servlet will talk to this
|
---|
102 | String recept_name = (String)config.getInitParameter("receptionist_class");
|
---|
103 | if (recept_name == null) {
|
---|
104 | this.recept = new DefaultReceptionist();
|
---|
105 | } else {
|
---|
106 | try {
|
---|
107 | this.recept = (Receptionist)Class.forName("org.greenstone.gsdl3.core."+recept_name).newInstance();
|
---|
108 | } catch (Exception e) { // cant use this new one, so use normal one
|
---|
109 | System.err.println("LibraryServlet configure exception when trying to use a new Receptionist "+recept_name+": "+e.getMessage());
|
---|
110 | e.printStackTrace();
|
---|
111 | this.recept = new DefaultReceptionist();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | this.recept.setConfigParams(config_params);
|
---|
115 | this.recept.setMessageRouter(message_router);
|
---|
116 | // the params arg thingy
|
---|
117 |
|
---|
118 | String params_name = (String)config.getInitParameter("params_class");
|
---|
119 | if (params_name == null) {
|
---|
120 | this.params = new GSParams();
|
---|
121 | } else {
|
---|
122 | try {
|
---|
123 | this.params = (GSParams)Class.forName("org.greenstone.gsdl3.util."+params_name).newInstance();
|
---|
124 | } catch (Exception e) {
|
---|
125 | System.err.println("LibraryServlet configure exception when trying to use a new params thing "+params_name+": "+e.getMessage());
|
---|
126 | e.printStackTrace();
|
---|
127 | this.params = new GSParams();
|
---|
128 | }
|
---|
129 | }
|
---|
130 | // pass it to the receptionist
|
---|
131 | this.recept.setParams(this.params);
|
---|
132 | this.recept.configure();
|
---|
133 |
|
---|
134 | }
|
---|
135 |
|
---|
136 | public void doGet(HttpServletRequest request,
|
---|
137 | HttpServletResponse response)
|
---|
138 | throws ServletException, IOException {
|
---|
139 |
|
---|
140 | HttpSession session = request.getSession(true);
|
---|
141 |
|
---|
142 | request.setCharacterEncoding("UTF-8");
|
---|
143 | response.setContentType("text/html;charset=UTF-8");
|
---|
144 | PrintWriter out = response.getWriter();
|
---|
145 |
|
---|
146 | String lang = request.getParameter(GSParams.LANGUAGE);
|
---|
147 | if (lang==null || lang.equals("")) {
|
---|
148 | // try the session cached lang
|
---|
149 | lang = (String)session.getAttribute(GSParams.LANGUAGE);
|
---|
150 | if (lang==null || lang.equals("")) {
|
---|
151 | // still not set, use the default
|
---|
152 | lang = this.default_lang;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | // set the lang in the session
|
---|
157 | session.setAttribute(GSParams.LANGUAGE, lang);
|
---|
158 |
|
---|
159 | String output = request.getParameter(GSParams.OUTPUT);
|
---|
160 | if (output==null || output.equals("")) {
|
---|
161 | output = "html"; // uses html by default
|
---|
162 | }
|
---|
163 |
|
---|
164 | // the request to the receptionist
|
---|
165 | Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
166 | Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PAGE, "", lang);
|
---|
167 | xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
|
---|
168 | xml_message.appendChild(xml_request);
|
---|
169 |
|
---|
170 |
|
---|
171 | String action = request.getParameter(GSParams.ACTION);
|
---|
172 | String subaction = request.getParameter(GSParams.SUBACTION);
|
---|
173 | if (action==null || action.equals("")) {
|
---|
174 | // should we do all the following stuff if using default page?
|
---|
175 | // display the home page - the default page
|
---|
176 | action = "p";
|
---|
177 | subaction = PageAction.HOME_PAGE;
|
---|
178 | xml_request.setAttribute(GSXML.ACTION_ATT, action);
|
---|
179 | xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
|
---|
180 |
|
---|
181 | } else {
|
---|
182 |
|
---|
183 | xml_request.setAttribute(GSXML.ACTION_ATT, action);
|
---|
184 | if (subaction != null) {
|
---|
185 | xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
|
---|
186 | }
|
---|
187 |
|
---|
188 | // create the param list for the greenstone request - includes
|
---|
189 | // the params from the current request and any others from the saved session
|
---|
190 | Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
191 | xml_request.appendChild(xml_param_list);
|
---|
192 |
|
---|
193 | Enumeration params = request.getParameterNames();
|
---|
194 | while(params.hasMoreElements()) {
|
---|
195 | String name = (String)params.nextElement();
|
---|
196 | if (!name.equals(GSParams.ACTION) && !name.equals(GSParams.SUBACTION) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSParams.OUTPUT)) {// we have already dealt with these
|
---|
197 | String value="";
|
---|
198 | String [] values = request.getParameterValues(name);
|
---|
199 | value = values[0];
|
---|
200 | if (values.length > 1) {
|
---|
201 | for (int i=1; i< values.length; i++) {
|
---|
202 | value += ","+values[i];
|
---|
203 | }
|
---|
204 | }
|
---|
205 | // either add it to the param list straight away, or save it to the session and add it later
|
---|
206 | if (this.params.shouldSave(name)) {
|
---|
207 | session.setAttribute(name, value);
|
---|
208 | } else {
|
---|
209 | Element param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
210 | param.setAttribute(GSXML.NAME_ATT, name);
|
---|
211 | param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
|
---|
212 | xml_param_list.appendChild(param);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | // put in all the params from the session cache
|
---|
218 | params = session.getAttributeNames();
|
---|
219 | while(params.hasMoreElements()) {
|
---|
220 | String name = (String)params.nextElement();
|
---|
221 |
|
---|
222 | if ( !name.equals(GSParams.LANGUAGE) ) { // lang is stored but we dont want it in the param list cos its already in the request
|
---|
223 |
|
---|
224 | Element param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
225 | param.setAttribute(GSXML.NAME_ATT, name);
|
---|
226 | param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String)session.getAttribute(name)));
|
---|
227 | xml_param_list.appendChild(param);
|
---|
228 | }
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (!output.equals("html")) {
|
---|
235 | response.setContentType("text/xml"); // for now use text
|
---|
236 | }
|
---|
237 |
|
---|
238 | Element xml_result = this.recept.process(xml_message);
|
---|
239 | encodeURLs(xml_result, response);
|
---|
240 | out.println(this.converter.getPrettyString(xml_result));
|
---|
241 | }
|
---|
242 |
|
---|
243 | /** this goes through each URL and adds in a session id if needed--
|
---|
244 | * its needed if the browser doesn't accept cookies
|
---|
245 | */
|
---|
246 | protected void encodeURLs(Element data, HttpServletResponse response) {
|
---|
247 |
|
---|
248 | if (data == null) {
|
---|
249 | return;
|
---|
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 | }
|
---|