1 16 17 package org.springframework.mock.web; 18 19 import javax.servlet.RequestDispatcher ; 20 import javax.servlet.ServletRequest ; 21 import javax.servlet.ServletResponse ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.springframework.util.Assert; 27 28 38 public class MockRequestDispatcher implements RequestDispatcher { 39 40 private final Log logger = LogFactory.getLog(getClass()); 41 42 private final String url; 43 44 45 49 public MockRequestDispatcher(String url) { 50 Assert.notNull(url, "URL must not be null"); 51 this.url = url; 52 } 53 54 55 public void forward(ServletRequest request, ServletResponse response) { 56 Assert.notNull(request, "Request must not be null"); 57 Assert.notNull(response, "Response must not be null"); 58 if (response.isCommitted()) { 59 throw new IllegalStateException ("Cannot perform forward - response is already committed"); 60 } 61 if (!(response instanceof MockHttpServletResponse)) { 62 throw new IllegalArgumentException ("MockRequestDispatcher requires MockHttpServletResponse"); 63 } 64 ((MockHttpServletResponse) response).setForwardedUrl(this.url); 65 if (logger.isDebugEnabled()) { 66 logger.debug("MockRequestDispatcher: forwarding to URL [" + this.url + "]"); 67 } 68 } 69 70 public void include(ServletRequest request, ServletResponse response) { 71 Assert.notNull(request, "Request must not be null"); 72 Assert.notNull(response, "Response must not be null"); 73 if (!(response instanceof MockHttpServletResponse)) { 74 throw new IllegalArgumentException ("MockRequestDispatcher requires MockHttpServletResponse"); 75 } 76 ((MockHttpServletResponse) response).setIncludedUrl(this.url); 77 if (logger.isDebugEnabled()) { 78 logger.debug("MockRequestDispatcher: including URL [" + this.url + "]"); 79 } 80 } 81 82 } 83 | Popular Tags |