1 | /* |
2 | * Copyright 2003,2004 The Apache Software Foundation. |
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 | package org.apache.pluto.driver.url; |
17 | |
18 | import javax.portlet.PortletMode; |
19 | import javax.portlet.WindowState; |
20 | |
21 | import java.util.Collection; |
22 | import java.util.Collections; |
23 | import java.util.HashMap; |
24 | import java.util.Iterator; |
25 | import java.util.Map; |
26 | |
27 | /** |
28 | * The portal URL. |
29 | * @author <a href="mailto:zheng@apache.org">ZHENG Zhong</a> |
30 | * @author <a href="mailto:ddewolf@apache.org">David H. DeWolf</a> |
31 | * @since 1.0 |
32 | */ |
33 | public class PortalURL implements Cloneable { |
34 | |
35 | /** Server URI contains protocol, host name, and (optional) port. */ |
36 | private String serverURI = null; |
37 | |
38 | private String servletPath = null; |
39 | private String renderPath = null; |
40 | private String actionWindow = null; |
41 | |
42 | /** The window states: key is the window ID, value is WindowState. */ |
43 | private Map windowStates = new HashMap(); |
44 | |
45 | private Map portletModes = new HashMap(); |
46 | |
47 | /** Parameters of the portlet windows. */ |
48 | private Map parameters = new HashMap(); |
49 | |
50 | |
51 | // Constructors ------------------------------------------------------------ |
52 | |
53 | /** |
54 | * Constructs a PortalURL instance using default port. |
55 | * @param protocol the protocol. |
56 | * @param hostName the host name. |
57 | * @param contextPath the servlet context path. |
58 | * @param servletName the servlet name. |
59 | */ |
60 | public PortalURL(String protocol, |
61 | String hostName, |
62 | String contextPath, |
63 | String servletName) { |
64 | this(protocol, hostName, -1, contextPath, servletName); |
65 | } |
66 | |
67 | /** |
68 | * Constructs a PortalURL instance using customized port. |
69 | * @param protocol the protocol. |
70 | * @param hostName the host name. |
71 | * @param port the port number: 0 or negative means using default port. |
72 | * @param contextPath the servlet context path. |
73 | * @param servletName the servlet name. |
74 | */ |
75 | public PortalURL(String protocol, |
76 | String hostName, |
77 | int port, |
78 | String contextPath, |
79 | String servletName) { |
80 | StringBuffer buffer = new StringBuffer(); |
81 | buffer.append(protocol); |
82 | buffer.append(hostName); |
83 | if (port > 0) { |
84 | buffer.append(":").append(port); |
85 | } |
86 | serverURI = buffer.toString(); |
87 | |
88 | buffer = new StringBuffer(); |
89 | buffer.append(contextPath); |
90 | buffer.append(servletName); |
91 | servletPath = buffer.toString(); |
92 | } |
93 | |
94 | /** |
95 | * Internal private constructor used by method <code>clone()</code>. |
96 | * @see #clone() |
97 | */ |
98 | private PortalURL() { |
99 | // Do nothing. |
100 | } |
101 | |
102 | // Public Methods ---------------------------------------------------------- |
103 | |
104 | public void setRenderPath(String renderPath) { |
105 | this.renderPath = renderPath; |
106 | } |
107 | |
108 | public String getRenderPath() { |
109 | return renderPath; |
110 | } |
111 | |
112 | public void addParameter(PortalURLParameter param) { |
113 | parameters.put(param.getWindowId() + param.getName(), param); |
114 | } |
115 | |
116 | public Collection getParameters() { |
117 | return parameters.values(); |
118 | } |
119 | |
120 | public void setActionWindow(String actionWindow) { |
121 | this.actionWindow = actionWindow; |
122 | } |
123 | |
124 | public String getActionWindow() { |
125 | return actionWindow; |
126 | } |
127 | |
128 | public Map getPortletModes() { |
129 | return Collections.unmodifiableMap(portletModes); |
130 | } |
131 | |
132 | public PortletMode getPortletMode(String windowId) { |
133 | PortletMode mode = (PortletMode) portletModes.get(windowId); |
134 | if (mode == null) { |
135 | mode = PortletMode.VIEW; |
136 | } |
137 | return mode; |
138 | } |
139 | |
140 | public void setPortletMode(String windowId, PortletMode portletMode) { |
141 | portletModes.put(windowId, portletMode); |
142 | } |
143 | |
144 | public Map getWindowStates() { |
145 | return Collections.unmodifiableMap(windowStates); |
146 | } |
147 | |
148 | /** |
149 | * Returns the window state of the specified window. |
150 | * @param windowId the window ID. |
151 | * @return the window state. Default to NORMAL. |
152 | */ |
153 | public WindowState getWindowState(String windowId) { |
154 | WindowState state = (WindowState) windowStates.get(windowId); |
155 | if (state == null) { |
156 | state = WindowState.NORMAL; |
157 | } |
158 | return state; |
159 | } |
160 | |
161 | /** |
162 | * Sets the window state of the specified window. |
163 | * @param windowId the window ID. |
164 | * @param windowState the window state. |
165 | */ |
166 | public void setWindowState(String windowId, WindowState windowState) { |
167 | this.windowStates.put(windowId, windowState); |
168 | } |
169 | |
170 | /** |
171 | * Clear parameters of the specified window. |
172 | * @param windowId the window ID. |
173 | */ |
174 | public void clearParameters(String windowId) { |
175 | for (Iterator it = parameters.entrySet().iterator(); it.hasNext(); ) { |
176 | Map.Entry entry = (Map.Entry) it.next(); |
177 | PortalURLParameter param = (PortalURLParameter) entry.getValue(); |
178 | if (param.getWindowId().equals(windowId)) { |
179 | it.remove(); |
180 | } |
181 | } |
182 | } |
183 | |
184 | /** |
185 | * Converts to a string representing the portal URL. |
186 | * @return a string representing the portal URL. |
187 | * @see org.apache.pluto.driver.url.PortalURLParser#toString(PortalURL) |
188 | */ |
189 | public String toString() { |
190 | return PortalURLParser.getParser().toString(this); |
191 | } |
192 | |
193 | |
194 | /** |
195 | * Returns the server URI (protocol, name, port). |
196 | * @return the server URI portion of the portal URL. |
197 | */ |
198 | public String getServerURI() { |
199 | return serverURI; |
200 | } |
201 | |
202 | /** |
203 | * Returns the servlet path (context path + servlet name). |
204 | * @return the servlet path. |
205 | */ |
206 | public String getServletPath() { |
207 | return servletPath; |
208 | } |
209 | |
210 | /** |
211 | * Clone a copy of itself. |
212 | * @return a copy of itself. |
213 | */ |
214 | public Object clone() { |
215 | PortalURL portalURL = new PortalURL(); |
216 | portalURL.serverURI = this.serverURI; |
217 | portalURL.servletPath = this.servletPath; |
218 | portalURL.parameters = new HashMap(parameters); |
219 | portalURL.portletModes = new HashMap(portletModes); |
220 | portalURL.windowStates = new HashMap(windowStates); |
221 | portalURL.renderPath = renderPath; |
222 | portalURL.actionWindow = actionWindow; |
223 | return portalURL; |
224 | } |
225 | } |