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

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

small change

  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 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 java.io.*;
9import javax.servlet.*;
10import javax.servlet.http.*;
11import java.util.Enumeration;
12import java.io.File;
13
14/** a servlet to serve the greenstone library - we are using servlets instead
15 * of cgi
16 * the init method is called only once - the first time the servlet classes
17 * are loaded. Each time a request comes in to the servlet, the session()
18 * method is called in a new thread (calls doGet/doPut etc)
19 * takes the a=p&p=home type args and builds a simple request to send to
20 * its receptionist, which returns a result in html, cos output=html
21 * is set in the request
22 * @see Receptionist
23 */
24public class LibraryServlet extends HttpServlet {
25
26 /** the receptionist to send messages to */
27 protected Receptionist recept_=null;
28 /** the default language - is specified by setting a servlet param,
29 * otherwise DEFUALT_LANG is used*/
30 protected String default_lang_= null;
31 /** The default default - used if a default lang is not specified
32 * in the servlet params */
33 protected final String DEFAULT_LANG = "en";
34 /** container Document to create XML Nodes */
35 protected Document doc_=null;
36 /** a converter class to parse XML and create Docs */
37 protected XMLConverter converter_=null;
38
39 /** initialise the servlet
40 */
41 public void init(ServletConfig config) throws ServletException {
42 // always call super.init;
43 super.init(config);
44
45 ConfigVars config_vars = new ConfigVars();
46 String library_name = config.getInitParameter("libraryname");
47 String gsdl3_home = config.getInitParameter("gsdl3home");
48 String site_name = config.getInitParameter("sitename");
49 String interface_name = config.getInitParameter("interfacename");
50 default_lang_ = config.getInitParameter("defaultlang");
51 if (library_name == null || gsdl3_home == null || site_name ==null || interface_name ==null) {
52 // must have this
53 System.err.println("initialisation parameters not all set!");
54 System.err.println(" you must have libraryname, gsdl3home, sitename, interfacename");
55 System.exit(1);
56 }
57
58 if (default_lang_ == null) {
59 // choose english
60 default_lang_ = DEFAULT_LANG;
61 }
62
63 config_vars.library_name_ = library_name;
64 config_vars.gsdl3_home_ = gsdl3_home;
65 config_vars.site_name_ = site_name;
66 config_vars.interface_name_ =interface_name;
67 config_vars.default_language_ = default_lang_;
68 config_vars.createXML();
69
70 // new message router - create it and pass reference to recept.
71 // the servlet wont use this directly
72 MessageRouter message_router = new MessageRouter();
73 message_router.setSiteHome(GSFile.siteHome(config_vars.gsdl3_home_,
74 config_vars.site_name_));
75 message_router.configure();
76
77 // the receptionist -the servlet will talk to this
78 recept_ = new Receptionist();
79 recept_.setConfigVars(config_vars);
80 recept_.setMessageRouter(message_router);
81 recept_.configure();
82
83 converter_ = new XMLConverter();
84 doc_ = converter_.newDOM();
85
86 }
87
88 public void doGet(HttpServletRequest request,
89 HttpServletResponse response)
90 throws ServletException, IOException {
91
92 request.setCharacterEncoding("UTF-8");
93 response.setContentType("text/html;charset=UTF-8");
94 PrintWriter out = response.getWriter();
95
96 String lang = request.getParameter(GSCGI.LANGUAGE_ARG);
97 if (lang==null || lang.equals("")) {
98 lang = default_lang_; // the default
99 }
100 String output = request.getParameter(GSCGI.OUTPUT_ARG);
101 if (output==null || output.equals("")) {
102 output = "html"; // uses html by default
103 }
104
105 // the request to the receptionist
106 Element xml_message = doc_.createElement(GSXML.MESSAGE_ELEM);
107 Element xml_request = doc_.createElement(GSXML.REQUEST_ELEM);
108 xml_message.appendChild(xml_request);
109 xml_request.setAttribute(GSXML.LANG_ATT, lang);
110 xml_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_CGI);
111 xml_request.setAttribute(GSXML.OUTPUT_ATT, output);
112
113 String action = request.getParameter(GSCGI.ACTION_ARG);
114 String subaction = request.getParameter(GSCGI.SUBACTION_ARG);
115 if (action==null || action.equals("")) {
116 // should we do all the following stuff if using default page?
117 // display the home page - the default page
118 action = "p";
119 subaction = PageAction.HOME_PAGE;
120 xml_request.setAttribute(GSXML.ACTION_ATT, action);
121 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
122
123 Element xml_result = recept_.process(xml_message);
124 out.println(converter_.getString(xml_result));
125 return;
126 }
127
128 xml_request.setAttribute(GSXML.ACTION_ATT, action);
129 if (subaction != null) {
130 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
131 }
132 Element xml_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
133 xml_request.appendChild(xml_param_list);
134 Enumeration params = request.getParameterNames();
135 while(params.hasMoreElements()) {
136 String name = (String)params.nextElement();
137 if (!name.equals(GSCGI.ACTION_ARG) && !name.equals(GSCGI.SUBACTION_ARG) && !name.equals(GSCGI.LANGUAGE_ARG) && !name.equals(GSCGI.OUTPUT_ARG)) { // these ones are in the header so leave out
138 String value="";
139 String []values = request.getParameterValues(name);
140 value = values[0];
141 if (values.length > 1) {
142 for (int i=1; i< values.length; i++) {
143 value += ","+values[i];
144 }
145 }
146 Element param = doc_.createElement(GSXML.PARAM_ELEM);
147 param.setAttribute(GSXML.NAME_ATT, name);
148 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value));
149 xml_param_list.appendChild(param);
150
151 }
152 }
153
154 if (!output.equals("html")) {
155 response.setContentType("text/xml"); // for now use text
156 }
157
158 Element xml_result = recept_.process(xml_message);
159 out.println(converter_.getString(xml_result));
160 }
161}
Note: See TracBrowser for help on using the repository browser.