1 7 package com.inversoft.verge.mvc.controller; 8 9 10 import java.io.IOException ; 11 import java.net.URISyntaxException ; 12 13 import javax.servlet.RequestDispatcher ; 14 import javax.servlet.ServletException ; 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 18 import org.apache.log4j.Logger; 19 20 import com.inversoft.error.ErrorList; 21 import com.inversoft.verge.mvc.MVCException; 22 import com.inversoft.verge.mvc.MVCRequest; 23 import com.inversoft.verge.mvc.MVCURLTools; 24 25 26 63 public class DefaultControllerParser implements ControllerParser { 64 65 68 private static final Logger logger = Logger.getLogger(DefaultControllerParser.class); 69 70 73 public static final String URL_CATEGORY_NAME = "controller"; 74 75 76 79 public DefaultControllerParser() { 80 super(); 81 } 82 83 84 97 public void preExecute(MVCRequest mvcRequest) throws MVCException { 98 99 HttpServletRequest request = mvcRequest.getRequest(); 101 ControllerMVCInfo info = MVCURLTools.decodeURL(request); 102 mvcRequest.setControllerInfo(info); 103 info.getHandler().preExecute(mvcRequest); 104 } 105 106 112 public void execute(MVCRequest mvcRequest) throws MVCException { 113 114 ControllerMVCInfo info = mvcRequest.getControllerInfo(); 116 if (info == null) { 117 return; 118 } 119 120 ControllerHandler handler = info.getHandler(); 121 Result result = handler.execute(mvcRequest); 122 handleResult(mvcRequest, result); 123 } 124 125 138 protected void handleResult(MVCRequest mvcRequest, Result result) 139 throws MVCException { 140 HttpServletRequest request = mvcRequest.getRequest(); 141 HttpServletResponse response = mvcRequest.getResponse(); 142 if (result != null) { 143 144 if (result.isForward()) { 145 146 String url = null; 147 try { 148 url = result.getGeneratedURL(request); 149 150 if (logger.isDebugEnabled()) { 151 logger.debug("Forwarding to URL: " + url); 152 } 153 154 RequestDispatcher rd = request.getRequestDispatcher(url); 155 rd.forward(request, response); 156 } catch (ServletException se) { 157 ErrorList el = new ErrorList(); 158 el.addError("This exception is generally the result of an" + 159 "exception thrown by the JSP page. Most containers " + 160 "smother these exceptions and thrown a SerlvetException " + 161 "making it very difficult to determine the root cause."); 162 163 MVCException mvce = new MVCException("Error forwarding to:" + 164 url, se); 165 mvce.setErrors(el); 166 throw mvce; 167 } catch (IOException ioe) { 168 throw new MVCException("Error forwarding to: " + url, ioe); 169 } catch (URISyntaxException use) { 170 throw new MVCException("Error forwarding to: " + result.getURL(), use); 171 } 172 } else { 173 String url = null; 174 try { 175 url = result.getGeneratedURL(request); 176 url = response.encodeRedirectURL(url); 177 response.sendRedirect(url); 178 } catch (IOException ioe) { 179 throw new MVCException("Error redirecting to:" + url, ioe); 180 } catch (URISyntaxException use) { 181 throw new MVCException("Error redirecting to: " + result.getURL(), use); 182 } 183 } 184 } 185 } 186 } | Popular Tags |