1 package org.ashkelon.pages; 2 3 import org.ashkelon.db.*; 4 import org.ashkelon.util.*; 5 import org.ashkelon.*; 6 7 import javax.servlet.*; 8 import javax.servlet.http.*; 9 10 import java.util.*; 11 import java.sql.*; 12 import java.io.*; 13 import java.beans.*; 14 15 import org.apache.oro.text.perl.*; 16 import org.apache.oro.text.regex.*; 17 18 19 22 public class FlowController extends HttpServlet 23 { 24 private String errorPage; 25 private String cmdNotFoundPage; 26 private ConfigInfo configInfo; 27 28 private DBMgr dbmgr; 29 30 private Perl5Util perlutil; 31 private Logger log = Logger.getInstance(); 32 33 private Map classPool; 34 35 36 public String getServletInfo() { return "Ashkelon Controller Servlet"; } 37 38 public void init() throws ServletException 39 { 40 errorPage = "/error.jsp"; 41 cmdNotFoundPage = "/cmd_not_found.jsp"; 42 43 try 44 { 45 configInfo = (new ConfigInfo()).load(); 46 } 47 catch (Exception ex) 48 { 49 throw new ServletException("Unable to load config information", ex); 50 } 51 52 dbmgr = DBMgr.getInstance(); 53 dbmgr.setWebApp(getServletContext()); 54 55 log.setTraceLevel(configInfo.getTraceLevel()); 56 String traceFile = "trace.log"; 57 String configTraceFile = configInfo.getTraceFile(); 58 if(!StringUtils.isBlank(configTraceFile)) 59 { 60 try 61 { 62 if (new File(configTraceFile).canWrite()) 63 traceFile = configTraceFile; 64 } 65 catch (Exception ex) 66 { 67 log.error("benign exception: "+ex.getMessage()); 68 } 69 } 70 71 try 72 { 73 PrintWriter writer = new PrintWriter(new FileWriter(traceFile, true)); 74 log.setWriter(writer); 75 } 76 catch (IOException ex) { } 77 78 String sourcepath = getServletContext().getInitParameter("sourcepath"); 79 log.traceln("retrieving source path: "+sourcepath); 80 getServletContext().setAttribute("sourcepath", getPaths(sourcepath)); 81 82 perlutil = new Perl5Util(); 83 84 classPool = new HashMap(30); 85 } 86 87 public void doGet(HttpServletRequest request, HttpServletResponse response) 88 throws ServletException, java.io.IOException 89 { 90 doPost(request, response); 91 } 92 93 public void doPost(HttpServletRequest request, HttpServletResponse response) 94 throws ServletException, java.io.IOException 95 { 96 log.setPrefix("FlowController"); 97 98 String cmd = ServletUtils.getCommand(request, configInfo.getDefaultCmd()); 99 request.setAttribute("cmd", cmd); 100 101 Connection conn = null; 102 try 103 { 104 conn = dbmgr.getConnection(); 105 } 106 catch (SQLException ex) 107 { 108 if (conn != null) dbmgr.releaseConnection(conn); 109 DBUtils.logSQLException(ex); 110 111 request.setAttribute("javax.servlet.jsp.jspException", ex); 112 RequestDispatcher rd = getServletContext().getRequestDispatcher(errorPage); 113 rd.forward(request, response); 114 return; 115 } 116 117 CommandInfo cmdInfo; 118 String pageName, className; 119 int numRedirects = 0; 120 while (!StringUtils.isBlank(cmd) && numRedirects < 3) 121 { 122 cmdInfo = (CommandInfo) configInfo.getCommandMap().get(cmd); 123 124 if (cmdInfo == null) { 126 dbmgr.releaseConnection(conn); 127 RequestDispatcher rd = getServletContext().getRequestDispatcher(cmdNotFoundPage); 128 rd.forward(request, response); 129 return; 130 } 131 132 String jspPathPrefix = getJspPathPrefix(request); 133 pageName = "/" + jspPathPrefix + cmdInfo.getPageName(); 134 log.debug("page name: "+pageName); 135 className = configInfo.getDefaultPkg() + "." + cmdInfo.getClassName(); 136 137 try 138 { 139 Page page = (Page) classPool.get(className); 140 if (page == null) 141 { 142 page = (Page) (Class.forName(className).newInstance()); 143 classPool.put(className, page); 144 } 145 146 page.setConnection(conn); 147 page.setRequest(request); 148 page.setApplication(getServletContext()); 149 150 log.verbose("Received command: "+cmd); 151 cmd = page.handleRequest(); 152 if (StringUtils.isBlank(cmd)) 153 { 154 updateTrail(request, cmdInfo); 155 156 dbmgr.releaseConnection(conn); 157 158 RequestDispatcher rd = getServletContext().getRequestDispatcher(pageName); 159 rd.forward(request, response); 160 return; 161 } 162 163 numRedirects++; 164 } 165 catch (Exception ex) 166 { 167 dbmgr.releaseConnection(conn); 168 if (ex instanceof SQLException) 169 { 170 DBUtils.logSQLException((SQLException) ex); 171 dbmgr.resetConnections(); 172 } 173 else 174 { 175 log.error("Exception: "+ex.getMessage()); 176 } 177 178 request.setAttribute("javax.servlet.jsp.jspException", ex); 179 RequestDispatcher rd = getServletContext().getRequestDispatcher(errorPage); 180 rd.forward(request, response); 181 return; 182 } 183 } 184 185 dbmgr.releaseConnection(conn); 186 187 request.setAttribute("javax.servlet.jsp.jspException", new Exception ("FlowController Says: Too many internal redirects")); 188 RequestDispatcher rd = getServletContext().getRequestDispatcher(errorPage); 189 rd.forward(request, response); 190 return; 191 } 192 193 private void updateTrail(HttpServletRequest request, CommandInfo cmdInfo) throws SQLException 194 { 195 HttpSession session = request.getSession(); 196 LinkedList trail = (LinkedList) session.getAttribute("trail"); 197 if (trail == null) 198 { 199 trail = new LinkedList(); 200 session.setAttribute("trail", trail); 201 } 202 203 if (!cmdInfo.isInTrail()) return; 204 205 String [] uriLabelPair = new String [3]; 206 207 String uri = request.getRequestURI(); 208 String queryString = ServletUtils.getQueryString(request); 209 210 if (queryString != null) 211 uri += "?" + queryString; 212 213 String cmd = cmdInfo.getCommand(); 214 String [] cmdparts = StringUtils.split(cmd, "."); 215 216 uriLabelPair[0] = uri; 217 String caption = cmdInfo.getCaption(); 218 String style = ""; 219 220 String pattern = "m/(.*)\\$(\\w+)\\$(.*)/"; 221 boolean matchsuccess = perlutil.match(pattern, caption); 222 223 if (matchsuccess && perlutil.groups() > 3) 224 { 225 log.debug("Match Length: "+perlutil.groups()); 226 MatchResult mr = perlutil.getMatch(); 227 String first = mr.group(1); 228 String field = mr.group(2); 229 String last = mr.group(3); 230 log.debug("Matched: 1: "+first+"; 2: "+field+"; 3: "+last); 231 232 if (cmdparts.length >= 1) 233 { 234 242 Object obj = request.getAttribute(cmdparts[0]); 243 if (obj == null) 244 { 245 obj = request.getAttribute(cmdparts[0] + "_" + field.toLowerCase()); if (obj == null) 248 caption = first + cmdparts[0] + last; 249 else 250 caption = first + obj.toString() + last; } 252 else if (obj instanceof JDoc) 253 { 254 String [] result = reflectionmagic((JDoc) obj, field.toLowerCase()); 255 String dynvalue = result[0]; 256 caption = first + dynvalue + last; 257 style = result[1]; 258 } 259 } else 260 { 261 caption = first + field + last; 262 } 263 log.debug("caption is: "+caption); 264 } 265 266 uriLabelPair[1] = caption; 267 uriLabelPair[2] = style; 268 269 if (!StringUtils.isBlank(caption)) 270 { 271 if ( !trail.isEmpty() ) 272 { 273 String [] last_pair = (String []) trail.getLast(); 274 String last_uri = last_pair[0]; 275 if (!uri.equals(last_uri)) 276 { 277 trail.addLast(uriLabelPair); 278 log.verbose("Added "+uriLabelPair[0]+" to trail"); 279 } 280 } 281 else 282 { 283 trail.addLast(uriLabelPair); 284 log.verbose("Added "+uriLabelPair[0]+" to trail"); 285 } 286 287 if (trail.size() > configInfo.getMaxTrailLength()) 288 trail.removeFirst(); 289 } 290 291 log.verbose("Updating trail"); 292 session.setAttribute("trail", trail); 293 } 294 295 private String [] reflectionmagic(JDoc doc, String propname) 297 { 298 try 299 { 300 PropertyDescriptor d = new PropertyDescriptor(propname, doc.getClass()); 301 Object value = d.getReadMethod().invoke(doc, null); 302 return new String [] {(String ) value, doc.getStyle()}; 303 } 304 catch (Exception ex) 305 { 306 log.error("failed to lookup value for property "+propname); 307 log.error(ex.getMessage()); 308 } 309 return new String [] {"", ""}; 310 } 311 312 319 private String getJspPathPrefix(HttpServletRequest request) 320 { 321 HttpSession session = request.getSession(); 322 Object uiObj = (String ) session.getAttribute("ui"); 323 String ui = ""; 324 log.debug("session ui value is "+uiObj); 325 if (uiObj == null) 326 { 327 String ua = request.getHeader("User-Agent"); 328 if (ua == null) ua = ""; 329 boolean new_explorer = ua.indexOf("MSIE 6.") > -1 || ua.indexOf("MSIE 5.") > -1; 330 boolean new_mozilla = ua.indexOf("Gecko") > -1; 331 if (new_explorer || new_mozilla) { 335 ui = ""; 336 } 337 else 338 { 339 ui = "basic/"; 340 } 341 session.setAttribute("ui", ui); 342 } 343 else 344 { 345 ui = (String ) uiObj; 346 } 347 log.debug("decided to use ui "+ui); 348 return ui; 349 } 350 351 352 private List getPaths(String pathstring) 353 { 354 log.debug("path string is: "+pathstring); 355 String [] paths = StringUtils.split(pathstring, File.pathSeparator); 356 return Arrays.asList(paths); 357 } 358 359 public void destroy() {} 360 361 } 362 363 | Popular Tags |