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.util.Map; 020import javax.portlet.PortalContext; 021import javax.portlet.PortletMode; 022import javax.portlet.PortletModeException; 023import javax.portlet.PortletURL; 024import javax.portlet.WindowState; 025import javax.portlet.WindowStateException; 026 027import org.springframework.util.Assert; 028import org.springframework.util.CollectionUtils; 029 030/** 031 * Mock implementation of the {@link javax.portlet.PortletURL} interface. 032 * 033 * @author John A. Lewis 034 * @author Juergen Hoeller 035 * @since 2.0 036 */ 037public class MockPortletURL extends MockBaseURL implements PortletURL { 038 039 public static final String URL_TYPE_RENDER = "render"; 040 041 public static final String URL_TYPE_ACTION = "action"; 042 043 044 private final PortalContext portalContext; 045 046 private final String urlType; 047 048 private WindowState windowState; 049 050 private PortletMode portletMode; 051 052 053 /** 054 * Create a new MockPortletURL for the given URL type. 055 * @param portalContext the PortalContext defining the supported 056 * PortletModes and WindowStates 057 * @param urlType the URL type, for example "render" or "action" 058 * @see #URL_TYPE_RENDER 059 * @see #URL_TYPE_ACTION 060 */ 061 public MockPortletURL(PortalContext portalContext, String urlType) { 062 Assert.notNull(portalContext, "PortalContext is required"); 063 this.portalContext = portalContext; 064 this.urlType = urlType; 065 } 066 067 068 //--------------------------------------------------------------------- 069 // PortletURL methods 070 //--------------------------------------------------------------------- 071 072 @Override 073 public void setWindowState(WindowState windowState) throws WindowStateException { 074 if (!CollectionUtils.contains(this.portalContext.getSupportedWindowStates(), windowState)) { 075 throw new WindowStateException("WindowState not supported", windowState); 076 } 077 this.windowState = windowState; 078 } 079 080 @Override 081 public WindowState getWindowState() { 082 return this.windowState; 083 } 084 085 @Override 086 public void setPortletMode(PortletMode portletMode) throws PortletModeException { 087 if (!CollectionUtils.contains(this.portalContext.getSupportedPortletModes(), portletMode)) { 088 throw new PortletModeException("PortletMode not supported", portletMode); 089 } 090 this.portletMode = portletMode; 091 } 092 093 @Override 094 public PortletMode getPortletMode() { 095 return this.portletMode; 096 } 097 098 @Override 099 public void removePublicRenderParameter(String name) { 100 this.parameters.remove(name); 101 } 102 103 104 @Override 105 public String toString() { 106 StringBuilder sb = new StringBuilder(); 107 sb.append(encodeParameter("urlType", this.urlType)); 108 if (this.windowState != null) { 109 sb.append(";").append(encodeParameter("windowState", this.windowState.toString())); 110 } 111 if (this.portletMode != null) { 112 sb.append(";").append(encodeParameter("portletMode", this.portletMode.toString())); 113 } 114 for (Map.Entry<String, String[]> entry : this.parameters.entrySet()) { 115 sb.append(";").append(encodeParameter("param_" + entry.getKey(), entry.getValue())); 116 } 117 return (isSecure() ? "https:" : "http:") + 118 "//localhost/mockportlet?" + sb.toString(); 119 } 120 121}