1 16 19 20 package org.apache.pluto.core.impl; 21 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import javax.portlet.PortletMode; 27 import javax.portlet.PortletModeException; 28 import javax.portlet.PortletSecurityException; 29 import javax.portlet.PortletURL; 30 import javax.portlet.WindowState; 31 import javax.portlet.WindowStateException; 32 33 import org.apache.pluto.om.portlet.ContentTypeSet; 34 import org.apache.pluto.om.portlet.PortletDefinition; 35 import org.apache.pluto.om.window.PortletWindow; 36 import org.apache.pluto.services.information.DynamicInformationProvider; 37 import org.apache.pluto.services.information.InformationProviderAccess; 38 import org.apache.pluto.services.information.PortletURLProvider; 39 import org.apache.pluto.util.StringUtils; 40 41 public class PortletURLImpl implements PortletURL { 42 protected DynamicInformationProvider provider; protected PortletMode mode = null; 44 45 protected Map parameters = new HashMap (); 46 47 protected PortletWindow portletWindow; 48 49 protected boolean secure; 50 protected javax.servlet.http.HttpServletRequest servletRequest; 51 protected javax.servlet.http.HttpServletResponse servletResponse; 52 protected WindowState state; 53 protected boolean isAction; 54 55 public PortletURLImpl(PortletWindow portletWindow, 56 javax.servlet.http.HttpServletRequest servletRequest, 57 javax.servlet.http.HttpServletResponse servletResponse, 58 boolean isAction) 59 { 60 this.portletWindow = portletWindow; 61 this.servletRequest = servletRequest; 62 this.servletResponse = servletResponse; 63 secure = servletRequest.isSecure(); 64 this.isAction = isAction; 65 } 66 67 public void setWindowState(WindowState windowState) throws WindowStateException 69 { 70 if (provider == null) 71 provider = InformationProviderAccess.getDynamicProvider(servletRequest); 72 if (windowState != null && provider.isWindowStateAllowed(windowState)) 73 { 74 state = windowState; 75 return; 76 } 77 78 throw new WindowStateException("unsupported Window State used: " + windowState,windowState); 79 } 80 81 public void setPortletMode (PortletMode portletMode) throws PortletModeException 82 { 83 84 if (provider == null) 87 provider = InformationProviderAccess.getDynamicProvider(servletRequest); 88 if (portletMode != null && provider.isPortletModeAllowed(portletMode)) 89 { 90 if (isPortletModeSupported(portletMode, portletWindow)) 91 { 92 mode = portletMode; 93 return; 94 } 95 } 96 throw new PortletModeException("unsupported Portlet Mode used: " + portletMode,portletMode); 97 } 98 99 public void setParameter(String name, String value) 100 { 101 if (name == null || value == null) { 102 throw new IllegalArgumentException ("name and value must not be null"); 103 } 104 105 parameters.put( name, new String []{value}); 106 } 107 108 public void setParameter (String name, String [] values) 109 { 110 if (name == null || values == null || values.length == 0) { 111 throw new IllegalArgumentException ("name and values must not be null or values be an empty array"); 112 } 113 114 parameters.put(name, StringUtils.copy(values)); 115 } 116 117 120 public void setParameters(Map parameters) { 121 if (parameters == null) { 122 throw new IllegalArgumentException ("Parameters must not be null."); 123 } 124 for (Iterator iter = parameters.entrySet().iterator(); iter.hasNext();) { 125 Map.Entry entry = (Map.Entry )iter.next(); 126 if (!(entry.getKey() instanceof String )) { 127 throw new IllegalArgumentException ("Key must not be null and of type java.lang.String."); 128 } 129 if (!(entry.getValue() instanceof String [])) { 130 throw new IllegalArgumentException ("Value must not be null and of type java.lang.String[]."); 131 } 132 } 133 134 this.parameters = StringUtils.copyParameters(parameters); 135 } 136 137 public void setSecure (boolean secure) throws PortletSecurityException 138 { 139 this.secure = secure; 140 } 141 142 public String toString() 143 { 144 StringBuffer url = new StringBuffer (200); 145 146 PortletURLProvider urlProvider = InformationProviderAccess.getDynamicProvider(servletRequest).getPortletURLProvider(portletWindow); 147 148 if (mode != null) { 149 urlProvider.setPortletMode(mode); 150 } 151 if (state != null) { 152 urlProvider.setWindowState(state); 153 } 154 if (isAction) { 155 urlProvider.setAction(); 156 } 157 if (secure) { 158 urlProvider.setSecure(); 159 } 160 urlProvider.clearParameters(); 161 urlProvider.setParameters(parameters); 162 163 url.append(urlProvider.toString()); 164 165 return url.toString(); 166 } 167 169 private boolean isPortletModeSupported(PortletMode requestedPortletMode,PortletWindow referencedPortletWindow) 171 { 172 if (requestedPortletMode.equals(PortletMode.VIEW) || requestedPortletMode.toString().equalsIgnoreCase("view")) 174 { 175 return true; 176 } 177 PortletDefinition portletDefinition = referencedPortletWindow.getPortletEntity().getPortletDefinition(); 178 ContentTypeSet contentTypes = portletDefinition.getContentTypeSet(); 179 return contentTypes.supportsPortletMode(requestedPortletMode); 180 } 181 183 public String getParameter(String name) 185 { 186 return(String )parameters.get(name); 187 } 188 189 public String [] getParameters(String name) 190 { 191 return(String [])parameters.get(name); 192 } 193 194 public PortletMode getPortletMode () 195 { 196 return mode; 197 } 198 199 public WindowState getWindowState () 200 { 201 return state; 202 } 203 } 205 | Popular Tags |