001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.internal.impl;
018:
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import javax.portlet.PortalContext;
025: import javax.portlet.PortletMode;
026: import javax.portlet.PortletModeException;
027: import javax.portlet.PortletSecurityException;
028: import javax.portlet.PortletURL;
029: import javax.portlet.WindowState;
030: import javax.portlet.WindowStateException;
031:
032: import org.apache.pluto.PortletContainer;
033: import org.apache.pluto.descriptors.portlet.PortletDD;
034: import org.apache.pluto.descriptors.portlet.SupportsDD;
035: import org.apache.pluto.internal.InternalPortletWindow;
036: import org.apache.pluto.spi.PortletURLProvider;
037: import org.apache.pluto.util.StringManager;
038: import org.apache.pluto.util.StringUtils;
039:
040: public class PortletURLImpl implements PortletURL {
041:
042: private static final StringManager EXCEPTIONS = StringManager
043: .getManager(PortletURLImpl.class.getPackage().getName());
044:
045: private PortletContainer container;
046: protected PortletMode mode = null;
047:
048: protected Map parameters = new HashMap();
049:
050: protected InternalPortletWindow internalPortletWindow;
051:
052: protected boolean secure;
053: protected javax.servlet.http.HttpServletRequest servletRequest;
054: protected javax.servlet.http.HttpServletResponse servletResponse;
055: protected WindowState state;
056: protected boolean isAction;
057:
058: private PortalContext context;
059:
060: public PortletURLImpl(PortletContainer container,
061: InternalPortletWindow internalPortletWindow,
062: javax.servlet.http.HttpServletRequest servletRequest,
063: javax.servlet.http.HttpServletResponse servletResponse,
064: boolean isAction) {
065: this .container = container;
066: this .internalPortletWindow = internalPortletWindow;
067: this .servletRequest = servletRequest;
068: this .servletResponse = servletResponse;
069: secure = servletRequest.isSecure();
070: this .isAction = isAction;
071: this .context = container.getRequiredContainerServices()
072: .getPortalContext();
073: }
074:
075: // javax.portlet.PortletURL -------------------------------------------------------------------
076: public void setWindowState(WindowState windowState)
077: throws WindowStateException {
078: if (windowState != null && isWindowStateAllowed(windowState)) {
079: state = windowState;
080: return;
081: }
082:
083: throw new WindowStateException(
084: "unsupported Window State used: " + windowState,
085: windowState);
086: }
087:
088: public void setPortletMode(PortletMode portletMode)
089: throws PortletModeException {
090: // Test and throw exception if not allowed.
091: isPortletModeAllowed(portletMode);
092: mode = portletMode;
093: }
094:
095: public void setParameter(String name, String value) {
096: if (name == null || value == null) {
097: throw new IllegalArgumentException(
098: "name and value must not be null");
099: }
100:
101: parameters.put(name, new String[] { value });
102: }
103:
104: public void setParameter(String name, String[] values) {
105: if (name == null || values == null || values.length == 0) {
106: throw new IllegalArgumentException(
107: "name and values must not be null or values be an empty array");
108: }
109:
110: parameters.put(name, StringUtils.copy(values));
111: }
112:
113: /* (non-Javadoc)
114: * @see javax.portlet.PortletURL#setParameters(Map)
115: */
116: public void setParameters(Map parameters) {
117: if (parameters == null) {
118: throw new IllegalArgumentException(
119: "Parameters must not be null.");
120: }
121: for (Iterator it = parameters.entrySet().iterator(); it
122: .hasNext();) {
123: Map.Entry entry = (Map.Entry) it.next();
124: if (!(entry.getKey() instanceof String)) {
125: throw new IllegalArgumentException(
126: "Key must not be null and of type java.lang.String.");
127: }
128: if (!(entry.getValue() instanceof String[])) {
129: throw new IllegalArgumentException(
130: "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)
138: throws PortletSecurityException {
139: PortletURLProvider urlProvider = container
140: .getRequiredContainerServices()
141: .getPortalCallbackService().getPortletURLProvider(
142: servletRequest, internalPortletWindow);
143: if (urlProvider.isSecureSupported()) {
144: throw new PortletSecurityException(
145: "Secure URLs not supported.");
146: }
147: }
148:
149: public String toString() {
150: StringBuffer url = new StringBuffer(200);
151:
152: PortletURLProvider urlProvider = container
153: .getRequiredContainerServices()
154: .getPortalCallbackService().getPortletURLProvider(
155: servletRequest, internalPortletWindow);
156:
157: if (mode != null) {
158: urlProvider.setPortletMode(mode);
159: }
160: if (state != null) {
161: urlProvider.setWindowState(state);
162: }
163: if (isAction) {
164: urlProvider.setAction(true);
165: }
166: if (secure && urlProvider.isSecureSupported()) {
167: try {
168: urlProvider.setSecure();
169: } catch (PortletSecurityException e) {
170: throw new IllegalStateException(
171: "URL Provider is misconfigured."
172: + " It claims to support secure urls,"
173: + " yet it threw a PortletSecurityException");
174: }
175: }
176: urlProvider.clearParameters();
177: urlProvider.setParameters(parameters);
178:
179: url.append(urlProvider.toString());
180:
181: return url.toString();
182: }
183:
184: // --------------------------------------------------------------------------------------------
185:
186: // additional methods -------------------------------------------------------------------------
187: public String getParameter(String name) {
188: return (String) parameters.get(name);
189: }
190:
191: public String[] getParameters(String name) {
192: return (String[]) parameters.get(name);
193: }
194:
195: public PortletMode getPortletMode() {
196: return mode;
197: }
198:
199: public WindowState getWindowState() {
200: return state;
201: }
202:
203: // --------------------------------------------------------------------------------------------
204:
205: private boolean isPortletModeAllowed(PortletMode mode)
206: throws PortletModeException {
207: if (mode == null) {
208: throw new PortletModeException(
209: EXCEPTIONS
210: .getString("javax.portlet.PortletModeException.null"),
211: null);
212: }
213:
214: return isPortletModeAllowedByPortlet(mode)
215: && isPortletModeAllowedByPortal(mode);
216: }
217:
218: private boolean isPortletModeAllowedByPortlet(PortletMode mode)
219: throws PortletModeException {
220: // PLT 8.1: View Portlet Mode should always be
221: // supported by a portlet, even if not defined in the descriptor
222: if (mode.equals(PortletMode.VIEW)) {
223: return true;
224: }
225:
226: PortletDD dd = internalPortletWindow.getPortletEntity()
227: .getPortletDefinition();
228: Iterator supports = dd.getSupports().iterator();
229: while (supports.hasNext()) {
230: SupportsDD support = (SupportsDD) supports.next();
231: Iterator modes = support.getPortletModes().iterator();
232: while (modes.hasNext()) {
233: String md = (String) modes.next();
234: if (md.toUpperCase().equals(
235: mode.toString().toUpperCase())) {
236: return true;
237: }
238: }
239: }
240: String message = EXCEPTIONS.getString(
241: "javax.portlet.PortletModeException.portlet", mode
242: .toString());
243:
244: throw new PortletModeException(message, mode);
245: }
246:
247: private boolean isPortletModeAllowedByPortal(PortletMode mode)
248: throws PortletModeException {
249: Enumeration supportedModes = context.getSupportedPortletModes();
250: while (supportedModes.hasMoreElements()) {
251: if (supportedModes.nextElement().toString().toUpperCase()
252: .equals((mode.toString().toUpperCase()))) {
253: return true;
254: }
255: }
256: String message = EXCEPTIONS.getString(
257: "javax.portlet.PortletModeException.portal", mode
258: .toString());
259:
260: throw new PortletModeException(message, mode);
261: }
262:
263: private boolean isWindowStateAllowed(WindowState state) {
264: Enumeration supportedStates = context
265: .getSupportedWindowStates();
266: while (supportedStates.hasMoreElements()) {
267: if (supportedStates.nextElement().toString().toUpperCase()
268: .equals((state.toString().toUpperCase()))) {
269: return true;
270: }
271: }
272: return false;
273: }
274: }
|