Changeset 4259
- Timestamp:
- 2003-05-08T16:17:29+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/LibraryServlet.java
r4152 r4259 6 6 import org.w3c.dom.Document; 7 7 import org.w3c.dom.Element; 8 import org.w3c.dom.NodeList; 8 9 import java.io.*; 9 10 import javax.servlet.*; … … 25 26 26 27 /** the receptionist to send messages to */ 27 protected Receptionist recept_=null;28 protected WebReceptionist recept=null; 28 29 /** the default language - is specified by setting a servlet param, 29 * otherwise DEF UALT_LANG is used*/30 protected String default_lang _= null;30 * otherwise DEFAULT_LANG is used*/ 31 protected String default_lang= null; 31 32 /** The default default - used if a default lang is not specified 32 33 * in the servlet params */ 33 34 protected final String DEFAULT_LANG = "en"; 34 35 /** container Document to create XML Nodes */ 35 protected Document doc _=null;36 protected Document doc=null; 36 37 /** a converter class to parse XML and create Docs */ 37 protected XMLConverter converter_=null; 38 38 protected XMLConverter converter=null; 39 /** the cgi stuff - the Receptionist can add new args to this 40 * its used by the servlet to determine what args to save */ 41 protected GSCGI cgi = null; 39 42 /** initialise the servlet 40 43 */ … … 48 51 String site_name = config.getInitParameter("sitename"); 49 52 String interface_name = config.getInitParameter("interfacename"); 50 default_lang_= config.getInitParameter("defaultlang");53 this.default_lang = config.getInitParameter("defaultlang"); 51 54 if (library_name == null || gsdl3_home == null || site_name ==null || interface_name ==null) { 52 55 // must have this … … 56 59 } 57 60 58 if ( default_lang_== null) {61 if (this.default_lang == null) { 59 62 // choose english 60 default_lang_= DEFAULT_LANG;63 this.default_lang = DEFAULT_LANG; 61 64 } 62 65 … … 65 68 config_vars.site_name_ = site_name; 66 69 config_vars.interface_name_ =interface_name; 67 config_vars.default_language_ = default_lang_;70 config_vars.default_language_ = this.default_lang; 68 71 config_vars.createXML(); 69 72 70 converter_= new XMLConverter();71 doc_ = converter_.newDOM();72 73 this.converter = new XMLConverter(); 74 this.doc = this.converter.newDOM(); 75 73 76 // now try and create MR and recpt 74 77 // new message router - create it and pass reference to recept. … … 85 88 e.printStackTrace(); 86 89 message_router = new MessageRouter(); 87 } 88 89 }90 } 91 } 92 90 93 message_router.setSiteHome(GSFile.siteHome(config_vars.gsdl3_home_, 91 94 config_vars.site_name_)); … … 95 98 String recept_name = (String)config.getInitParameter("receptionist"); 96 99 if (recept_name == null) { 97 recept_ = newReceptionist();100 this.recept = new DefaultReceptionist(); 98 101 } else { 99 102 try { 100 recept_ = (Receptionist)Class.forName("org.greenstone.gsdl3.core."+recept_name).newInstance();103 this.recept = (WebReceptionist)Class.forName("org.greenstone.gsdl3.core."+recept_name).newInstance(); 101 104 } catch (Exception e) { // cant use this new one, so use normal one 102 105 System.out.println("LibraryServlet configure exception when trying to use a new Receptionist "+recept_name+": "+e.getMessage()); 103 106 e.printStackTrace(); 104 recept_ = new Receptionist(); 105 } 106 } 107 recept_.setConfigVars(config_vars); 108 recept_.setMessageRouter(message_router); 109 recept_.configure(); 110 107 this.recept = new DefaultReceptionist(); 108 } 109 } 110 this.recept.setConfigVars(config_vars); 111 this.recept.setMessageRouter(message_router); 112 this.recept.configure(); 113 // the cgi arg thingy 114 115 String cgi_name = (String)config.getInitParameter("cgiargs"); 116 if (cgi_name == null) { 117 this.cgi = new GSCGI(); 118 } else { 119 try { 120 this.cgi = (GSCGI)Class.forName("org.greenstone.gsdl3.util."+cgi_name).newInstance(); 121 } catch (Exception e) { 122 System.out.println("LibraryServlet configure exception when trying to use a new cgi thing "+cgi_name+": "+e.getMessage()); 123 e.printStackTrace(); 124 this.cgi = new GSCGI(); 125 } 126 } 127 // pass it to the receptionist 128 this.recept.setCGI(this.cgi); 111 129 } 112 130 … … 115 133 throws ServletException, IOException { 116 134 135 HttpSession session = request.getSession(true); 136 137 // print them out to test it 138 // Enumeration stored_names = session.getAttributeNames(); 139 // System.out.println("session values are:"); 140 // while (stored_names.hasMoreElements()) { 141 // String name = (String)stored_names.nextElement(); 142 // System.out.println("stored "+name+":" +session.getAttribute(name)); 143 // } 144 117 145 request.setCharacterEncoding("UTF-8"); 118 146 response.setContentType("text/html;charset=UTF-8"); … … 121 149 String lang = request.getParameter(GSCGI.LANGUAGE_ARG); 122 150 if (lang==null || lang.equals("")) { 123 lang = default_lang_; // the default 124 } 151 // try the session cached lang 152 lang = (String)session.getAttribute(GSCGI.LANGUAGE_ARG); 153 if (lang==null || lang.equals("")) { 154 // still not set, use the default 155 lang = this.default_lang; 156 } 157 } 158 159 // set the lang in the session 160 session.setAttribute(GSCGI.LANGUAGE_ARG, lang); 161 125 162 String output = request.getParameter(GSCGI.OUTPUT_ARG); 126 163 if (output==null || output.equals("")) { … … 129 166 130 167 // the request to the receptionist 131 Element xml_message = doc_.createElement(GSXML.MESSAGE_ELEM); 132 Element xml_request = doc_.createElement(GSXML.REQUEST_ELEM); 168 Element xml_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 169 Element xml_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_CGI, "", lang); 170 xml_request.setAttribute(GSXML.OUTPUT_ATT, output); 133 171 xml_message.appendChild(xml_request); 134 xml_request.setAttribute(GSXML.LANG_ATT, lang); 135 xml_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_CGI); 136 xml_request.setAttribute(GSXML.OUTPUT_ATT, output); 172 137 173 138 174 String action = request.getParameter(GSCGI.ACTION_ARG); … … 146 182 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction); 147 183 148 Element xml_result = recept_.process(xml_message); 149 out.println(converter_.getString(xml_result)); 150 return; 151 } 152 153 xml_request.setAttribute(GSXML.ACTION_ATT, action); 154 if (subaction != null) { 155 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction); 156 } 157 Element xml_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 158 xml_request.appendChild(xml_param_list); 159 Enumeration params = request.getParameterNames(); 160 while(params.hasMoreElements()) { 161 String name = (String)params.nextElement(); 162 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 163 String value=""; 164 String []values = request.getParameterValues(name); 165 value = values[0]; 166 if (values.length > 1) { 167 for (int i=1; i< values.length; i++) { 168 value += ","+values[i]; 184 } else { 185 186 xml_request.setAttribute(GSXML.ACTION_ATT, action); 187 if (subaction != null) { 188 xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction); 189 } 190 191 // create the param list for the greenstone request - includes 192 // the params from the current request and any others from the saved session 193 Element xml_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 194 xml_request.appendChild(xml_param_list); 195 196 Enumeration params = request.getParameterNames(); 197 while(params.hasMoreElements()) { 198 String name = (String)params.nextElement(); 199 if (!name.equals(GSCGI.ACTION_ARG) && !name.equals(GSCGI.SUBACTION_ARG) && !name.equals(GSCGI.LANGUAGE_ARG) && !name.equals(GSCGI.OUTPUT_ARG)) {// we have already dealt with these 200 String value=""; 201 String [] values = request.getParameterValues(name); 202 value = values[0]; 203 if (values.length > 1) { 204 for (int i=1; i< values.length; i++) { 205 value += ","+values[i]; 206 } 207 } 208 // either add it to the param list straight away, or save it to the session and add it later 209 if (this.cgi.shouldSave(name)) { 210 session.setAttribute(name, value); 211 } else { 212 Element param = this.doc.createElement(GSXML.PARAM_ELEM); 213 param.setAttribute(GSXML.NAME_ATT, name); 214 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value)); 215 xml_param_list.appendChild(param); 169 216 } 170 217 } 171 Element param = doc_.createElement(GSXML.PARAM_ELEM); 172 param.setAttribute(GSXML.NAME_ATT, name); 173 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe(value)); 174 xml_param_list.appendChild(param); 218 } 219 220 // put in all the params from the session cache 221 params = session.getAttributeNames(); 222 while(params.hasMoreElements()) { 223 String name = (String)params.nextElement(); 175 224 176 } 177 } 178 225 if ( !name.equals(GSCGI.LANGUAGE_ARG) ) { // lang is stored but we dont want it in the param list cos its already in the request 226 227 Element param = this.doc.createElement(GSXML.PARAM_ELEM); 228 param.setAttribute(GSXML.NAME_ATT, name); 229 param.setAttribute(GSXML.VALUE_ATT, GSXML.xmlSafe((String)session.getAttribute(name))); 230 xml_param_list.appendChild(param); 231 } 232 } 233 234 235 } 236 179 237 if (!output.equals("html")) { 180 238 response.setContentType("text/xml"); // for now use text 181 239 } 182 183 Element xml_result = recept_.process(xml_message); 184 out.println(converter_.getPrettyString(xml_result)); 240 241 Element xml_result = this.recept.process(xml_message); 242 encodeURLs(xml_result, response); 243 out.println(this.converter.getPrettyString(xml_result)); 244 } 245 246 /** this goes through each URL and adds in a session id if needed-- 247 * its needed if the browser doesn't accept cookies 248 */ 249 protected void encodeURLs(Element data, HttpServletResponse response) { 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 185 267 } 186 268 }
Note:
See TracChangeset
for help on using the changeset viewer.