1 | package org.greenstone.gsdl3;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.*;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 |
|
---|
6 | import java.io.*;
|
---|
7 | import javax.servlet.*;
|
---|
8 | import javax.servlet.http.*;
|
---|
9 | import java.util.Enumeration;
|
---|
10 | import java.io.File;
|
---|
11 |
|
---|
12 | /** a servlet to serve the greenstone library - using servlets instead
|
---|
13 | * of cgi
|
---|
14 | * the init method is called only once - the first time the servlet classes
|
---|
15 | * are loaded. Each time a request comes in to the servlet, the session()
|
---|
16 | * method is called in a new thread (calls doGet/doPut etc)
|
---|
17 | * takes the a=p&p=home type args and builds a simple request to send to
|
---|
18 | * its receptionist, which returns the result in html, cos output=html
|
---|
19 | * is set in the request
|
---|
20 | * @see Receptionist
|
---|
21 | */
|
---|
22 | public class LibraryServlet extends HttpServlet {
|
---|
23 |
|
---|
24 | private ConfigVars config_=null; // holds all teh system set up vars
|
---|
25 | private Receptionist recept_=null;
|
---|
26 | private ModuleInterface message_router_=null;
|
---|
27 | /** initialise the servlet
|
---|
28 | */
|
---|
29 | public void init(ServletConfig config) throws ServletException {
|
---|
30 | // always call super.init;
|
---|
31 | super.init(config);
|
---|
32 |
|
---|
33 | config_ = new ConfigVars();
|
---|
34 | String sites_home_ = config.getInitParameter("siteshome");
|
---|
35 | String site_name_ = config.getInitParameter("sitename");
|
---|
36 | String interfaces_home_ = config.getInitParameter("interfaceshome");
|
---|
37 | String interface_name_ = config.getInitParameter("interfacename");
|
---|
38 | if (sites_home_ == null || site_name_==null || interfaces_home_ ==null || interface_name_==null) {
|
---|
39 | // must have this
|
---|
40 | System.err.println("initialisation parameters not all set!");
|
---|
41 | System.err.println(" you must have siteshome, sitename, interfaceshome, interfacename");
|
---|
42 | System.exit(1);
|
---|
43 | }
|
---|
44 |
|
---|
45 | config_.sites_home_ = sites_home_;
|
---|
46 | config_.site_name_ = site_name_;
|
---|
47 | config_.interfaces_home_ = interfaces_home_;
|
---|
48 | config_.interface_name_ =interface_name_;
|
---|
49 |
|
---|
50 | config_.createXML();
|
---|
51 |
|
---|
52 | // new message router - create it and pass a handle to recept.
|
---|
53 | // the servlet wont use this directly
|
---|
54 | MessageRouter mr = new MessageRouter();
|
---|
55 | mr.setSiteHome(config_.sites_home_+File.separatorChar
|
---|
56 | +config_.site_name_);
|
---|
57 | mr.configure();
|
---|
58 | // new receptionist
|
---|
59 | recept_ = new Receptionist();
|
---|
60 | recept_.setConfigVars(config_);
|
---|
61 | recept_.setMessageRouter(mr);
|
---|
62 | recept_.configure();
|
---|
63 |
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void doGet(HttpServletRequest request,
|
---|
67 | HttpServletResponse response)
|
---|
68 | throws ServletException, IOException {
|
---|
69 |
|
---|
70 | response.setContentType("text/html");
|
---|
71 | PrintWriter out = response.getWriter();
|
---|
72 |
|
---|
73 | String lang = request.getParameter("l");
|
---|
74 | if (lang==null || lang.equals("")) {
|
---|
75 | lang = "en"; // the default
|
---|
76 | }
|
---|
77 | // the request to the receptionist
|
---|
78 | String xml_request="<message lang='"+lang+"'>";
|
---|
79 |
|
---|
80 | String action = request.getParameter("a");
|
---|
81 | if (action==null || action.equals("")) {
|
---|
82 | // display the home page
|
---|
83 | // a=p&p=home
|
---|
84 | xml_request +="<request type='action' output='html' info='p/home'/>";
|
---|
85 | } else {
|
---|
86 | String subaction = request.getParameter("sa");
|
---|
87 | xml_request += "<request type='action' info='"+action;
|
---|
88 | if (subaction != null) {
|
---|
89 | xml_request += "/"+subaction;
|
---|
90 | }
|
---|
91 | xml_request += "'><paramList>";
|
---|
92 | Enumeration params = request.getParameterNames();
|
---|
93 | while(params.hasMoreElements()) {
|
---|
94 | String name = (String)params.nextElement();
|
---|
95 | if (!name.equals("a") && !name.equals("sa")) {
|
---|
96 | String value = request.getParameter(name);
|
---|
97 | // for now, just add value as attribute - should un-url
|
---|
98 | // encode them etc and so some might need to be text not atts
|
---|
99 | // or is that done already?
|
---|
100 | xml_request += "<param name='"+name+"' value='"+value+"'/>";
|
---|
101 | }
|
---|
102 | }
|
---|
103 | xml_request += "</paramList></request>";
|
---|
104 | }
|
---|
105 | xml_request += "</message>";
|
---|
106 |
|
---|
107 | System.out.println("request="+xml_request);
|
---|
108 |
|
---|
109 | String result = recept_.process(xml_request);
|
---|
110 | out.println(result);
|
---|
111 |
|
---|
112 |
|
---|
113 |
|
---|
114 | }
|
---|
115 | }
|
---|