1 package org.apache.velocity.demo; 2 3 18 19 import java.util.*; 21 import java.io.StringWriter ; 22 import java.io.PrintWriter ; 23 import java.io.IOException ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileInputStream ; 26 27 import javax.servlet.*; 29 import javax.servlet.http.HttpServlet ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.http.HttpSession ; 33 34 import org.apache.velocity.context.Context; 36 import org.apache.velocity.Template; 37 import org.apache.velocity.servlet.VelocityServlet; 38 39 import org.apache.velocity.exception.ParseErrorException; 40 import org.apache.velocity.exception.ResourceNotFoundException; 41 import org.apache.velocity.exception.MethodInvocationException; 42 43 import org.apache.velocity.demo.action.*; 45 46 54 public class ControllerServlet extends VelocityServlet 55 { 56 private static String ERR_MSG_TAG = "forumdemo_current_error_msg"; 57 58 59 63 protected Properties loadConfiguration(ServletConfig config ) 64 throws IOException , FileNotFoundException 65 { 66 String propsFile = config.getInitParameter(INIT_PROPS_KEY); 67 68 73 74 if ( propsFile != null ) 75 { 76 String realPath = getServletContext().getRealPath(propsFile); 77 78 if ( realPath != null ) 79 { 80 propsFile = realPath; 81 } 82 } 83 84 Properties p = new Properties(); 85 p.load( new FileInputStream (propsFile) ); 86 87 88 94 95 String path = p.getProperty("file.resource.loader.path"); 96 97 if (path != null) 98 { 99 path = getServletContext().getRealPath( path ); 100 p.setProperty( "file.resource.loader.path", path ); 101 } 102 103 path = p.getProperty("runtime.log"); 104 105 if (path != null) 106 { 107 path = getServletContext().getRealPath( path ); 108 p.setProperty("runtime.log", path ); 109 } 110 111 return p; 112 } 113 114 120 public Template handleRequest( Context ctx ) 121 { 122 HttpServletRequest req = (HttpServletRequest )ctx.get(VelocityServlet.REQUEST); 123 HttpServletResponse resp = (HttpServletResponse )ctx.get(VelocityServlet.RESPONSE); 124 Template template = null; 125 String templateName = null; 126 127 HttpSession sess = req.getSession(); 128 sess.setAttribute(ERR_MSG_TAG, "all ok" ); 129 130 try 131 { 132 templateName = processRequest( req, resp, ctx ); 134 template = getTemplate( templateName ); 136 } 137 catch( ResourceNotFoundException rnfe ) 138 { 139 String err = "ForumDemo -> ControllerServlet.handleRequest() : Cannot find template " + templateName ; 140 sess.setAttribute( ERR_MSG_TAG, err ); 141 System.out.println(err ); 142 } 143 catch( ParseErrorException pee ) 144 { 145 String err = "ForumDemo -> ControllerServlet.handleRequest() : Syntax error in template " + templateName + ":" + pee ; 146 sess.setAttribute( ERR_MSG_TAG, err ); 147 System.out.println(err ); 148 } 149 catch( Exception e ) 150 { 151 String err = "Error handling the request: " + e ; 152 sess.setAttribute( ERR_MSG_TAG, err ); 153 System.out.println(err ); 154 } 155 156 return template; 157 } 158 159 167 private String processRequest( HttpServletRequest req, HttpServletResponse resp, Context context ) 168 throws Exception 169 { 170 Command c = null; 171 String template = null; 172 String name = req.getParameter("action"); 173 174 if ( name == null || name.length() == 0 ) 175 { 176 throw new Exception ("Unrecognized action request!"); 177 } 178 179 if ( name.equalsIgnoreCase("list") ) 180 { 181 c = new ListCommand( req, resp); 182 template = c.exec( context ); 183 } 184 else if ( name.equalsIgnoreCase("post") ) 185 { 186 c = new PostCommand( req, resp ); 187 template = c.exec( context ); 188 } 189 else if ( name.equalsIgnoreCase("reply") ) 190 { 191 c = new ReplyCommand( req, resp ); 192 template = c.exec( context ); 193 } 194 else if ( name.equalsIgnoreCase("postreply") ) 195 { 196 c = new PostReplyCommand( req, resp ); 197 template = c.exec( context ); 198 } 199 else if ( name.equalsIgnoreCase("view") ) 200 { 201 c = new ViewCommand( req, resp ); 202 template = c.exec( context ); 203 } 204 return template; 205 } 206 207 211 protected void error( HttpServletRequest request, HttpServletResponse response, Exception cause ) 212 throws ServletException, IOException 213 { 214 HttpSession sess = request.getSession(); 215 String err = (String ) sess.getAttribute( ERR_MSG_TAG ); 216 217 StringBuffer html = new StringBuffer (); 218 html.append("<html>"); 219 html.append("<body bgcolor=\"#ffffff\">"); 220 html.append("<h2>ForumDemo : Error processing the request</h2>"); 221 html.append("<br><br>There was a problem in the request." ); 222 html.append("<br><br>The relevant error is :<br>"); 223 html.append( err ); 224 html.append("<br><br><br>"); 225 html.append("The error occurred at :<br><br>"); 226 227 StringWriter sw = new StringWriter (); 228 cause.printStackTrace( new PrintWriter ( sw ) ); 229 230 html.append( sw.toString() ); 231 html.append("</body>"); 232 html.append("</html>"); 233 response.getOutputStream().print( html.toString() ); 234 } 235 } 236 237 238 239 240 241 | Popular Tags |