001/* 002 * Copyright 2002-2012 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.portlet; 018 019import java.io.IOException; 020import javax.portlet.PortletException; 021import javax.portlet.PortletRequest; 022import javax.portlet.PortletRequestDispatcher; 023import javax.portlet.PortletResponse; 024import javax.portlet.RenderRequest; 025import javax.portlet.RenderResponse; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029 030import org.springframework.util.Assert; 031 032/** 033 * Mock implementation of the {@link javax.portlet.PortletRequestDispatcher} interface. 034 * 035 * @author John A. Lewis 036 * @author Juergen Hoeller 037 * @since 2.0 038 */ 039public class MockPortletRequestDispatcher implements PortletRequestDispatcher { 040 041 private final Log logger = LogFactory.getLog(getClass()); 042 043 private final String url; 044 045 046 /** 047 * Create a new MockPortletRequestDispatcher for the given URL. 048 * @param url the URL to dispatch to. 049 */ 050 public MockPortletRequestDispatcher(String url) { 051 Assert.notNull(url, "URL must not be null"); 052 this.url = url; 053 } 054 055 056 @Override 057 public void include(RenderRequest request, RenderResponse response) throws PortletException, IOException { 058 include((PortletRequest) request, (PortletResponse) response); 059 } 060 061 @Override 062 public void include(PortletRequest request, PortletResponse response) throws PortletException, IOException { 063 Assert.notNull(request, "Request must not be null"); 064 Assert.notNull(response, "Response must not be null"); 065 if (!(response instanceof MockMimeResponse)) { 066 throw new IllegalArgumentException("MockPortletRequestDispatcher requires MockMimeResponse"); 067 } 068 ((MockMimeResponse) response).setIncludedUrl(this.url); 069 if (logger.isDebugEnabled()) { 070 logger.debug("MockPortletRequestDispatcher: including URL [" + this.url + "]"); 071 } 072 } 073 074 @Override 075 public void forward(PortletRequest request, PortletResponse response) throws PortletException, IOException { 076 Assert.notNull(request, "Request must not be null"); 077 Assert.notNull(response, "Response must not be null"); 078 if (!(response instanceof MockMimeResponse)) { 079 throw new IllegalArgumentException("MockPortletRequestDispatcher requires MockMimeResponse"); 080 } 081 ((MockMimeResponse) response).setForwardedUrl(this.url); 082 if (logger.isDebugEnabled()) { 083 logger.debug("MockPortletRequestDispatcher: forwarding to URL [" + this.url + "]"); 084 } 085 } 086 087}