001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Caucho Technology (http://www.caucho.com/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
026: * endorse or promote products derived from this software without prior
027: * written permission. For written permission, please contact
028: * info@caucho.com.
029: *
030: * 5. Products derived from this software may not be called "Resin"
031: * nor may "Resin" appear in their names without prior written
032: * permission of Caucho Technology.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
038: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
039: * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
040: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
041: * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
042: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
043: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
044: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
045: *
046: * @author Sam
047: */
048:
049: package com.caucho.portal.generic;
050:
051: import javax.portlet.PortletMode;
052: import javax.portlet.PortletModeException;
053: import javax.portlet.PortletSecurityException;
054: import javax.portlet.WindowState;
055: import javax.portlet.WindowStateException;
056: import java.util.Collections;
057: import java.util.Enumeration;
058: import java.util.Iterator;
059: import java.util.Map;
060:
061: abstract public class InvocationURL {
062: private InvocationFactory _invocationFactory;
063: private String _namespace;
064: private Invocation _invocation;
065:
066: private Boolean _isSecure;
067:
068: /**
069: * @param invocationFactory the InvocationFactory
070: *
071: * @param namespace the namespace that is the default target for setting
072: * parameters, portlet mode, and window state
073: */
074: public InvocationURL(InvocationFactory invocationFactory,
075: String namespace) {
076: _invocationFactory = invocationFactory;
077: _namespace = namespace;
078: }
079:
080: public String getNamespace() {
081: return _namespace;
082: }
083:
084: protected Invocation getInvocation() {
085: if (_invocation == null)
086: _invocation = _invocationFactory.getInvocation(_namespace);
087:
088: return _invocation;
089: }
090:
091: protected Invocation getInvocation(String namespace) {
092: return _invocationFactory.getInvocation(namespace);
093: }
094:
095: protected void setWindowState(Invocation invocation,
096: WindowState windowState) throws WindowStateException {
097: invocation.setWindowState(windowState);
098: }
099:
100: public void setWindowState(WindowState windowState)
101: throws WindowStateException {
102: setWindowState(getInvocation(), windowState);
103: }
104:
105: public void setWindowState(String namespace, WindowState windowState)
106: throws WindowStateException {
107: setWindowState(getInvocation(namespace), windowState);
108: }
109:
110: protected void setPortletMode(Invocation invocation,
111: PortletMode portletMode) throws PortletModeException {
112: invocation.setPortletMode(portletMode);
113: }
114:
115: public void setPortletMode(PortletMode portletMode)
116: throws PortletModeException {
117: setPortletMode(getInvocation(), portletMode);
118: }
119:
120: public void setPortletMode(String namespace, PortletMode portletMode)
121: throws PortletModeException {
122: setPortletMode(getInvocation(namespace), portletMode);
123: }
124:
125: protected Map<String, String[]> getRenderParameterMap() {
126: return getInvocation().getParameterMap();
127: }
128:
129: protected Map<String, String[]> getRenderParameterMap(
130: String namespace) {
131: return getInvocation(namespace).getParameterMap();
132: }
133:
134: private void checkNullName(String name) {
135: if (name == null)
136: throw new IllegalArgumentException(
137: "parameter name cannot be null");
138: }
139:
140: private void checkNullValue(Object value) {
141: if (value == null)
142: throw new IllegalArgumentException(
143: "parameter value cannot be null");
144: }
145:
146: protected String getParameterValue(Map<String, String[]> map,
147: String name) {
148: checkNullName(name);
149:
150: String values[] = map.get(name);
151:
152: return values == null || values.length == 0 ? null : values[0];
153: }
154:
155: protected String[] getParameterValues(Map<String, String[]> map,
156: String name) {
157: checkNullName(name);
158: return map.get(name);
159: }
160:
161: protected Enumeration getParameterNames(Map<String, String[]> map) {
162: return Collections.enumeration(map.keySet());
163: }
164:
165: protected void setParameter(Map<String, String[]> map, String name,
166: String value) {
167: checkNullName(name);
168: checkNullValue(value);
169:
170: map.put(name, new String[] { value });
171: }
172:
173: protected void setParameters(Map<String, String[]> destMap,
174: Map<String, String[]> srcMap) {
175: checkNullValue(srcMap);
176: destMap.clear();
177:
178: Iterator<Map.Entry<String, String[]>> iter = srcMap.entrySet()
179: .iterator();
180:
181: while (iter.hasNext()) {
182: Map.Entry<String, String[]> entry = iter.next();
183:
184: setParameter(destMap, entry.getKey(), entry.getValue());
185: }
186: }
187:
188: protected void setParameter(Map<String, String[]> map, String name,
189: String[] values) {
190: checkNullName(name);
191: checkNullValue(values);
192:
193: if (values.length == 0)
194: map.remove(name);
195: else
196: map.put(name, values);
197: }
198:
199: public void setParameter(String name, String value) {
200: setParameter(getRenderParameterMap(), name, value);
201: }
202:
203: public void setParameter(String name, String[] values) {
204: setParameter(getRenderParameterMap(), name, values);
205: }
206:
207: public void setParameters(Map<String, String[]> parameters) {
208: setParameters(getRenderParameterMap(), parameters);
209: }
210:
211: public void setParameter(String namespace, String name, String value) {
212: setParameter(getRenderParameterMap(namespace), name, value);
213: }
214:
215: public void setParameter(String namespace, String name,
216: String[] values) {
217: setParameter(getRenderParameterMap(namespace), name, values);
218: }
219:
220: public void setParameters(String namespace,
221: Map<String, String[]> parameters) {
222: setParameters(getRenderParameterMap(namespace), parameters);
223: }
224:
225: public void setSecure(boolean secure)
226: throws PortletSecurityException {
227: _isSecure = secure ? Boolean.TRUE : Boolean.FALSE;
228: }
229:
230: /**
231: * True if setSecure() was called for this url, even if it was
232: * called with setSecure(false)
233: */
234: protected boolean isSecureSpecified() {
235: return _isSecure != null;
236: }
237:
238: protected boolean isSecure() {
239: return _isSecure == null || _isSecure == Boolean.FALSE;
240: }
241:
242: /**
243: * Return a partially formed URL, it is then resolved to a full URL by the
244: * Portal.
245: */
246: abstract public String getURL();
247: }
|