KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > portlet > MockPortletURL


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.mock.web.portlet;
18
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.net.URLEncoder JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.portlet.PortalContext;
27 import javax.portlet.PortletMode;
28 import javax.portlet.PortletModeException;
29 import javax.portlet.PortletSecurityException;
30 import javax.portlet.PortletURL;
31 import javax.portlet.WindowState;
32 import javax.portlet.WindowStateException;
33
34 import org.springframework.core.CollectionFactory;
35 import org.springframework.util.Assert;
36 import org.springframework.util.CollectionUtils;
37
38 /**
39  * Mock implementation of the {@link javax.portlet.PortletURL} interface.
40  *
41  * @author John A. Lewis
42  * @author Juergen Hoeller
43  * @since 2.0
44  */

45 public class MockPortletURL implements PortletURL {
46
47     public static final String JavaDoc URL_TYPE_RENDER = "render";
48
49     public static final String JavaDoc URL_TYPE_ACTION = "action";
50
51     private static final String JavaDoc ENCODING = "UTF-8";
52
53
54     private final PortalContext portalContext;
55
56     private final String JavaDoc urlType;
57
58     private WindowState windowState;
59
60     private PortletMode portletMode;
61
62     private final Map JavaDoc parameters = CollectionFactory.createLinkedMapIfPossible(16);
63
64     private boolean secure = false;
65
66
67     /**
68      * Create a new MockPortletURL for the given URL type.
69      * @param portalContext the PortalContext defining the supported
70      * PortletModes and WindowStates
71      * @param urlType the URL type, for example "render" or "action"
72      * @see #URL_TYPE_RENDER
73      * @see #URL_TYPE_ACTION
74      */

75     public MockPortletURL(PortalContext portalContext, String JavaDoc urlType) {
76         Assert.notNull(portalContext, "PortalContext is required");
77         this.portalContext = portalContext;
78         this.urlType = urlType;
79     }
80     
81     
82     //---------------------------------------------------------------------
83
// PortletURL methods
84
//---------------------------------------------------------------------
85

86     public void setWindowState(WindowState windowState) throws WindowStateException {
87         if (!CollectionUtils.contains(this.portalContext.getSupportedWindowStates(), windowState)) {
88             throw new WindowStateException("WindowState not supported", windowState);
89         }
90         this.windowState = windowState;
91     }
92
93     public void setPortletMode(PortletMode portletMode) throws PortletModeException {
94         if (!CollectionUtils.contains(this.portalContext.getSupportedPortletModes(), portletMode)) {
95             throw new PortletModeException("PortletMode not supported", portletMode);
96         }
97         this.portletMode = portletMode;
98     }
99
100     public void setParameter(String JavaDoc key, String JavaDoc value) {
101         Assert.notNull(key, "Parameter key must be null");
102         Assert.notNull(value, "Parameter value must not be null");
103         this.parameters.put(key, new String JavaDoc[] {value});
104     }
105
106     public void setParameter(String JavaDoc key, String JavaDoc[] values) {
107         Assert.notNull(key, "Parameter key must be null");
108         Assert.notNull(values, "Parameter values must not be null");
109         this.parameters.put(key, values);
110     }
111
112     public void setParameters(Map JavaDoc parameters) {
113         Assert.notNull(parameters, "Parameters Map must not be null");
114         this.parameters.clear();
115         for (Iterator JavaDoc it = parameters.entrySet().iterator(); it.hasNext();) {
116             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
117             Assert.isTrue(entry.getKey() instanceof String JavaDoc, "Key must be of type String");
118             Assert.isTrue(entry.getValue() instanceof String JavaDoc[], "Value must be of type String[]");
119             this.parameters.put(entry.getKey(), entry.getValue());
120         }
121     }
122
123     public Set JavaDoc getParameterNames() {
124         return this.parameters.keySet();
125     }
126
127     public String JavaDoc getParameter(String JavaDoc name) {
128         String JavaDoc[] arr = (String JavaDoc[]) this.parameters.get(name);
129         return (arr != null && arr.length > 0 ? arr[0] : null);
130     }
131
132     public String JavaDoc[] getParameterValues(String JavaDoc name) {
133         return (String JavaDoc[]) this.parameters.get(name);
134     }
135
136     public Map JavaDoc getParameterMap() {
137         return Collections.unmodifiableMap(this.parameters);
138     }
139
140     public void setSecure(boolean secure) throws PortletSecurityException {
141         this.secure = secure;
142     }
143
144     public boolean isSecure() {
145         return secure;
146     }
147
148     public String JavaDoc toString() {
149         StringBuffer JavaDoc query = new StringBuffer JavaDoc();
150         query.append(encodeParameter("urlType", this.urlType));
151         if (this.windowState != null) {
152             query.append(";" + encodeParameter("windowState", this.windowState.toString()));
153         }
154         if (this.portletMode != null) {
155             query.append(";" + encodeParameter("portletMode", this.portletMode.toString()));
156         }
157         for (Iterator JavaDoc it = this.parameters.entrySet().iterator(); it.hasNext();) {
158             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
159             String JavaDoc name = (String JavaDoc) entry.getKey();
160             String JavaDoc[] values = (String JavaDoc[]) entry.getValue();
161             query.append(";" + encodeParameter("param_" + name, values));
162         }
163         return (this.secure ? "https:" : "http:") +
164                 "//localhost/mockportlet?" + query.toString();
165     }
166
167
168     private String JavaDoc encodeParameter(String JavaDoc name, String JavaDoc value) {
169         try {
170             return URLEncoder.encode(name, ENCODING) + "=" +
171                     URLEncoder.encode(value, ENCODING);
172         }
173         catch (UnsupportedEncodingException JavaDoc ex) {
174             return null;
175         }
176     }
177
178     private String JavaDoc encodeParameter(String JavaDoc name, String JavaDoc[] values) {
179         try {
180             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
181             for (int i = 0, n = values.length; i < n; i++) {
182                 buf.append((i > 0 ? ";" : "") +
183                         URLEncoder.encode(name, ENCODING) + "=" +
184                         URLEncoder.encode(values[i], ENCODING));
185             }
186             return buf.toString();
187         }
188         catch (UnsupportedEncodingException JavaDoc ex) {
189             return null;
190         }
191     }
192
193 }
194
Popular Tags