001: /*
002: * Copyright 2006 The Apache Software Foundation
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: * http://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: package org.apache.commons.chain.web.portlet;
017:
018: import org.apache.commons.chain.web.MockEnumeration;
019: import org.apache.commons.chain.web.MockPrincipal;
020:
021: import javax.portlet.PortalContext;
022: import javax.portlet.PortletRequest;
023: import javax.portlet.PortletSession;
024: import javax.portlet.PortletContext;
025: import javax.portlet.PortletMode;
026: import javax.portlet.PortletPreferences;
027: import javax.portlet.WindowState;
028: import java.security.Principal;
029: import java.util.Map;
030: import java.util.HashMap;
031: import java.util.Enumeration;
032: import java.util.Locale;
033:
034: // Mock Object for PortletRequest
035: public class MockPortletRequest implements PortletRequest {
036:
037: private String contextPath;
038: private String authType;
039: private Locale locale;
040: private String scheme = "http";
041: private String serverName = "localhost";
042: private int serverPort = 8080;
043: private PortalContext portalContext;
044: private PortletContext context;
045: private PortletSession session;
046: private PortletMode portletMode;
047: private PortletPreferences portletPreferences;
048: private WindowState windowState;
049: private Principal principal;
050: private Map parameters = new HashMap();
051: private Map attributes = new HashMap();
052: private Map properties = new HashMap();
053:
054: public MockPortletRequest() {
055: this (null, null, null);
056: }
057:
058: public MockPortletRequest(String contextPath,
059: PortletContext context, PortletSession session) {
060: this .contextPath = contextPath;
061: this .context = (context == null ? new MockPortletContext()
062: : context);
063: this .session = session;
064: }
065:
066: // --------------------------------------------------------- Public Methods
067:
068: public void addParameter(String name, String value) {
069: String values[] = (String[]) parameters.get(name);
070: if (values == null) {
071: String results[] = new String[] { value };
072: parameters.put(name, results);
073: return;
074: }
075: String results[] = new String[values.length + 1];
076: System.arraycopy(values, 0, results, 0, values.length);
077: results[values.length] = value;
078: parameters.put(name, results);
079: }
080:
081: public void addProperty(String name, String value) {
082: String values[] = (String[]) properties.get(name);
083: if (values == null) {
084: String results[] = new String[] { value };
085: properties.put(name, results);
086: return;
087: }
088: String results[] = new String[values.length + 1];
089: System.arraycopy(values, 0, results, 0, values.length);
090: results[values.length] = value;
091: properties.put(name, results);
092: }
093:
094: public void setAuthType(String authType) {
095: this .authType = authType;
096: }
097:
098: public void setContextPath(String contextPath) {
099: this .contextPath = contextPath;
100: }
101:
102: public void setLocale(Locale locale) {
103: this .locale = locale;
104: }
105:
106: public void setPortalContext(PortalContext portalContext) {
107: this .portalContext = portalContext;
108: }
109:
110: public void setPortletContext(PortletContext context) {
111: this .context = context;
112: }
113:
114: public void setPortletMode(PortletMode portletMode) {
115: this .portletMode = portletMode;
116: }
117:
118: public void setPortletPreferences(
119: PortletPreferences portletPreferences) {
120: this .portletPreferences = portletPreferences;
121: }
122:
123: public void setPortletSession(PortletSession session) {
124: this .session = session;
125: }
126:
127: public void setScheme(String scheme) {
128: this .scheme = scheme;
129: }
130:
131: public void setServerName(String serverName) {
132: this .serverName = serverName;
133: }
134:
135: public void setServerPort(int serverPort) {
136: this .serverPort = serverPort;
137: }
138:
139: public void setUserPrincipal(Principal principal) {
140: this .principal = principal;
141: }
142:
143: public void setUserPrincipal(WindowState windowState) {
144: this .windowState = windowState;
145: }
146:
147: // --------------------------------------------- PortletRequest Methods
148:
149: public Object getAttribute(String name) {
150: return attributes.get(name);
151: }
152:
153: public Enumeration getAttributeNames() {
154: return new MockEnumeration(attributes.keySet().iterator());
155: }
156:
157: public String getAuthType() {
158: return authType;
159: }
160:
161: public String getContextPath() {
162: return contextPath;
163: }
164:
165: public Locale getLocale() {
166: return locale;
167: }
168:
169: public Enumeration getLocales() {
170: throw new UnsupportedOperationException();
171: }
172:
173: public String getParameter(String name) {
174: String values[] = (String[]) parameters.get(name);
175: if (values != null) {
176: return values[0];
177: } else {
178: return null;
179: }
180: }
181:
182: public Map getParameterMap() {
183: return parameters;
184: }
185:
186: public Enumeration getParameterNames() {
187: return new MockEnumeration(parameters.keySet().iterator());
188: }
189:
190: public String[] getParameterValues(String name) {
191: return (String[]) parameters.get(name);
192: }
193:
194: public PortalContext getPortalContext() {
195: return portalContext;
196: }
197:
198: public PortletMode getPortletMode() {
199: return portletMode;
200: }
201:
202: public PortletSession getPortletSession() {
203: return getPortletSession(true);
204: }
205:
206: public PortletSession getPortletSession(boolean create) {
207: if (create && session == null) {
208: session = new MockPortletSession(context);
209: }
210: return session;
211: }
212:
213: public PortletPreferences getPreferences() {
214: return portletPreferences;
215: }
216:
217: public Enumeration getProperties(String name) {
218: throw new UnsupportedOperationException();
219: }
220:
221: public String getProperty(String name) {
222: String values[] = (String[]) properties.get(name);
223: if (values != null) {
224: return values[0];
225: } else {
226: return null;
227: }
228: }
229:
230: public Enumeration getPropertyNames() {
231: return new MockEnumeration(properties.keySet().iterator());
232: }
233:
234: public String getRemoteUser() {
235: if (principal != null) {
236: return principal.getName();
237: } else {
238: return null;
239: }
240: }
241:
242: public String getRequestedSessionId() {
243: throw new UnsupportedOperationException();
244: }
245:
246: public String getResponseContentType() {
247: throw new UnsupportedOperationException();
248: }
249:
250: public Enumeration getResponseContentTypes() {
251: throw new UnsupportedOperationException();
252: }
253:
254: public String getScheme() {
255: return scheme;
256: }
257:
258: public String getServerName() {
259: return serverName;
260: }
261:
262: public int getServerPort() {
263: return serverPort;
264: }
265:
266: public Principal getUserPrincipal() {
267: return principal;
268: }
269:
270: public WindowState getWindowState() {
271: return windowState;
272: }
273:
274: public boolean isPortletModeAllowed(PortletMode mode) {
275: throw new UnsupportedOperationException();
276: }
277:
278: public boolean isRequestedSessionIdValid() {
279: throw new UnsupportedOperationException();
280: }
281:
282: public boolean isSecure() {
283: return false;
284: }
285:
286: public boolean isUserInRole(String role) {
287: if ((principal != null) && (principal instanceof MockPrincipal)) {
288: return ((MockPrincipal) principal).isUserInRole(role);
289: } else {
290: return false;
291: }
292: }
293:
294: public boolean isWindowStateAllowed(WindowState state) {
295: throw new UnsupportedOperationException();
296: }
297:
298: public void removeAttribute(String name) {
299: attributes.remove(name);
300: }
301:
302: public void setAttribute(String name, Object value) {
303: if (value == null) {
304: attributes.remove(name);
305: } else {
306: attributes.put(name, value);
307: }
308: }
309:
310: }
|