001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.mock.web;
018
019import javax.servlet.RequestDispatcher;
020import javax.servlet.ServletRequest;
021import javax.servlet.ServletResponse;
022import javax.servlet.http.HttpServletResponseWrapper;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026
027import org.springframework.util.Assert;
028
029/**
030 * Mock implementation of the {@link javax.servlet.RequestDispatcher} interface.
031 *
032 * @author Rod Johnson
033 * @author Juergen Hoeller
034 * @author Sam Brannen
035 * @since 1.0.2
036 * @see MockHttpServletRequest#getRequestDispatcher(String)
037 */
038public class MockRequestDispatcher implements RequestDispatcher {
039
040        private final Log logger = LogFactory.getLog(getClass());
041
042        private final String resource;
043
044
045        /**
046         * Create a new MockRequestDispatcher for the given resource.
047         * @param resource the server resource to dispatch to, located at a
048         * particular path or given by a particular name
049         */
050        public MockRequestDispatcher(String resource) {
051                Assert.notNull(resource, "Resource must not be null");
052                this.resource = resource;
053        }
054
055
056        @Override
057        public void forward(ServletRequest request, ServletResponse response) {
058                Assert.notNull(request, "Request must not be null");
059                Assert.notNull(response, "Response must not be null");
060                Assert.state(!response.isCommitted(), "Cannot perform forward - response is already committed");
061                getMockHttpServletResponse(response).setForwardedUrl(this.resource);
062                if (logger.isDebugEnabled()) {
063                        logger.debug("MockRequestDispatcher: forwarding to [" + this.resource + "]");
064                }
065        }
066
067        @Override
068        public void include(ServletRequest request, ServletResponse response) {
069                Assert.notNull(request, "Request must not be null");
070                Assert.notNull(response, "Response must not be null");
071                getMockHttpServletResponse(response).addIncludedUrl(this.resource);
072                if (logger.isDebugEnabled()) {
073                        logger.debug("MockRequestDispatcher: including [" + this.resource + "]");
074                }
075        }
076
077        /**
078         * Obtain the underlying {@link MockHttpServletResponse}, unwrapping
079         * {@link HttpServletResponseWrapper} decorators if necessary.
080         */
081        protected MockHttpServletResponse getMockHttpServletResponse(ServletResponse response) {
082                if (response instanceof MockHttpServletResponse) {
083                        return (MockHttpServletResponse) response;
084                }
085                if (response instanceof HttpServletResponseWrapper) {
086                        return getMockHttpServletResponse(((HttpServletResponseWrapper) response).getResponse());
087                }
088                throw new IllegalArgumentException("MockRequestDispatcher requires MockHttpServletResponse");
089        }
090
091}