1 64 65 package com.jcorporate.expresso.core.servlet; 66 67 72 73 import com.jcorporate.expresso.core.i18n.Messages; 74 import com.jcorporate.expresso.core.misc.ConfigManager; 75 import com.jcorporate.expresso.core.misc.HTTPUtil; 76 import com.jcorporate.expresso.core.misc.StringUtil; 77 78 import javax.servlet.ServletConfig ; 79 import javax.servlet.ServletException ; 80 import javax.servlet.http.HttpServlet ; 81 import javax.servlet.http.HttpServletRequest ; 82 import javax.servlet.http.HttpServletResponse ; 83 import javax.servlet.http.HttpSession ; 84 import java.io.IOException ; 85 86 87 94 public abstract class StdServlet 95 extends HttpServlet { 96 protected static final String thisClass = StdServlet.class.getName() + "."; 97 protected String mySchema = null; 98 99 110 public void doGet(HttpServletRequest request, HttpServletResponse response) 111 throws ServletException , IOException { 112 HttpSession session = request.getSession(false); 113 114 if (session == null) { 115 session = request.getSession(true); 116 } 117 try { 118 handleParam(request, response); 119 } catch (ServletException se) { 120 showError(se, request, response); 121 122 return; 123 } 124 } 125 126 127 137 public void doPost(HttpServletRequest req, HttpServletResponse res) 138 throws ServletException , IOException { 139 try { 140 handleParam(req, res); 141 } catch (Exception de) { 142 showError(de, req, res); 143 144 return; 145 } 146 } 147 148 149 155 protected String getServerName(HttpServletRequest request) { 156 return request.getServerName(); 157 } 158 159 165 public int getServerPort(HttpServletRequest request) { 166 return request.getServerPort(); 167 } 168 169 179 public String getServletPrefix(HttpServletRequest request) 180 throws ServletException { 181 return (request.getRequestURI()); 182 } 183 184 185 190 public String getServletPrefix(HttpServletRequest request, String servlet) 191 throws ServletException { 192 String initPath = getServletPrefix(request); 193 int lastPos = initPath.lastIndexOf("/"); 194 195 if (lastPos > 0) { 196 return initPath.substring(0, lastPos) + "/" + servlet; 197 } else { 198 return initPath + "/" + servlet; 199 } 200 } 201 202 203 209 public String getTitle() { 210 return ("No Title - " + getClass().getName()); 211 } 212 213 221 protected void handleParam(HttpServletRequest request, 222 HttpServletResponse response) 223 throws IOException , ServletException { 224 HTTPUtil.setBackURL(request); 225 HTTPUtil.setContinueURLFromQueryString(request); 226 } 227 228 229 236 protected void showError(String errorMessage, HttpServletRequest request, 237 HttpServletResponse response) 238 throws ServletException { 239 Exception e = new Exception ("No specific exception available"); 240 showError(e, request, response); 241 } 242 243 244 252 protected void showError(Throwable t, HttpServletRequest request, 253 HttpServletResponse response) 254 throws ServletException { 255 throw new ServletException (t); 256 } 257 258 259 266 public void init(ServletConfig sc) 267 throws ServletException { 268 269 super.init(sc); 270 271 if (!ConfigManager.isInitialized()) { 272 ConfigManager.config(sc); 273 } 274 } 275 276 277 280 public void destroy() { 281 } 282 283 284 293 protected void setSchema(String schemaClass) { 294 StringUtil.assertNotBlank(schemaClass, 295 "Schema cannot be set to blank or null"); 296 mySchema = schemaClass; 297 } 298 299 308 public String getString(HttpServletRequest req, String stringCode, 309 Object [] args) 310 throws ServletException { 311 if (mySchema == null) { 312 setSchema("com.jcorporate.expresso.core.ExpressoSchema"); 313 } 314 315 return Messages.getString(getSchema(), req, stringCode, args); 316 } 317 318 319 326 public String getString(HttpServletRequest req, String stringCode) 327 throws ServletException { 328 Object [] args = {}; 329 330 return getString(req, stringCode, args); 331 } 332 333 334 340 protected String getSchema() { 341 return mySchema; 342 } 343 344 } 345 | Popular Tags |