View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.pluto.driver.url.impl;
18  
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.Map;
24  
25  import javax.portlet.PortletMode;
26  import javax.portlet.WindowState;
27  import javax.servlet.ServletContext;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.apache.pluto.driver.AttributeKeys;
32  import org.apache.pluto.driver.config.DriverConfiguration;
33  import org.apache.pluto.driver.services.portal.PageConfig;
34  import org.apache.pluto.driver.url.PortalURL;
35  import org.apache.pluto.driver.url.PortalURLParameter;
36  import org.apache.pluto.driver.url.PortalURLParser;
37  
38  /**
39   * The portal URL.
40   * @since 1.0
41   */
42  public class RelativePortalURLImpl implements PortalURL {
43  
44      private static final Logger LOG = LoggerFactory.getLogger(RelativePortalURLImpl.class);
45  
46      private String urlBase;
47      private String servletPath;
48      private String renderPath;
49      private String actionWindow;
50      private String resourceWindow;
51      private String cacheLevel;
52      private String resourceID;
53      private String _nLutecePortletID;
54  
55      private Map<String, String[]> publicParameterCurrent = new HashMap<String, String[]>();
56  
57      private Map<String, String[]> publicParameterNew = new HashMap<String, String[]>();
58      private Map<String, String[]> privateRenderParameters = new HashMap<String, String[]>();
59      
60      /**
61       * PortalURLParser used to construct the string
62       * representation of this portal url.
63       */
64      private PortalURLParser urlParser;
65  
66      /** The window states: key is the window ID, value is WindowState. */
67      private Map<String, WindowState> windowStates = new HashMap<String, WindowState>();
68  
69      private Map<String, PortletMode> portletModes = new HashMap<String, PortletMode>();
70  
71      /** Parameters of the portlet windows. */
72      private Map<String, PortalURLParameter> parameters = new HashMap<String, PortalURLParameter>();
73  
74      /**
75       * Constructs a PortalURLImpl instance using customized port.
76       * @param urlBase      the absolute (protocol://domain:port) request url base
77       * @param contextPath  the servlet context path.
78       * @param servletName  the servlet name.
79       * @param urlParser    the {@link PortalURLParser} used to construct a string representation of the url.
80       */
81      public RelativePortalURLImpl(String urlBase, String contextPath, String servletName, PortalURLParser urlParser) {
82          this.urlBase = urlBase;
83      	StringBuffer buffer = new StringBuffer();
84      	buffer.append(contextPath);
85      	buffer.append(servletName);
86          servletPath = buffer.toString();
87          this.urlParser = urlParser;
88      }
89  
90      /**
91       * Internal private constructor used by method <code>clone()</code>.
92       * @see #clone()
93       */
94      private RelativePortalURLImpl() {
95      	// Do nothing.
96      }
97  
98      // Public Methods ----------------------------------------------------------
99  
100     public void setRenderPath(String renderPath) {
101         this.renderPath = renderPath;
102     }
103 
104     public String getRenderPath() {
105         return renderPath;
106     }
107 
108     public void addParameter(PortalURLParameter param) {
109         parameters.put(param.getWindowId() + param.getName(), param);
110     }
111 
112     public Collection<PortalURLParameter> getParameters() {
113         return parameters.values();
114     }
115 
116     public void setActionWindow(String actionWindow) {
117         this.actionWindow = actionWindow;
118     }
119 
120     public String getActionWindow() {
121         return actionWindow;
122     }
123 
124     public Map<String, PortletMode> getPortletModes() {
125         return Collections.unmodifiableMap(portletModes);
126     }
127 
128     public PortletMode getPortletMode(String windowId) {
129         PortletMode mode = portletModes.get(windowId);
130         if (mode == null) {
131             mode = PortletMode.VIEW;
132         }
133         return mode;
134     }
135 
136     public void setPortletMode(String windowId, PortletMode portletMode) {
137         portletModes.put(windowId, portletMode);
138     }
139 
140     public Map<String, WindowState> getWindowStates() {
141         return Collections.unmodifiableMap(windowStates);
142     }
143 
144     /**
145      * Returns the window state of the specified window.
146      * @param windowId  the window ID.
147      * @return the window state. Default to NORMAL.
148      */
149     public WindowState getWindowState(String windowId) {
150         WindowState state = windowStates.get(windowId);
151         if (state == null) {
152             state = WindowState.NORMAL;
153         }
154         return state;
155     }
156 
157     /**
158      * Sets the window state of the specified window.
159      * @param windowId  the window ID.
160      * @param windowState  the window state.
161      */
162     public void setWindowState(String windowId, WindowState windowState) {
163         this.windowStates.put(windowId, windowState);
164     }
165 
166     /**
167      * Clear parameters of the specified window.
168      * @param windowId  the window ID.
169      */
170     public void clearParameters(String windowId) {
171     	for (Iterator<Map.Entry<String, PortalURLParameter>> it = parameters.entrySet().iterator(); it.hasNext(); ) {
172             Map.Entry<String, PortalURLParameter> entry = it.next();
173             PortalURLParameter param = entry.getValue();
174             if (param.getWindowId()!=null){
175             	if (param.getWindowId().equals(windowId)) {
176                 	it.remove();
177                 }
178             }
179         }
180     }
181     
182     public void setCacheability(String cacheLevel)
183     {
184         this.cacheLevel = cacheLevel;
185     }
186     
187     public String getCacheability()
188     {
189         return cacheLevel;
190     }
191     
192     public void setResourceID(String resourceID)
193     {
194         this.resourceID = resourceID;
195     }
196     
197     public String getResourceID()
198     {
199         return resourceID;
200     }
201     
202     public void setLutecePortletId(String strLutecePortletID)
203     {
204     	this._nLutecePortletID = strLutecePortletID;
205     }
206 
207     public String getLutecePortletId()
208     {
209     	return _nLutecePortletID;
210     }
211     /**
212      * Converts to a string representing the portal URL.
213      * @deprecated use toURL(boolean absolute) instead
214      * @return a string representing the portal URL.
215      * @see PortalURLParserImpl#toString(org.apache.pluto.driver.url.PortalURL)
216      */
217     public String toString() {
218         return toURL(false);
219     }
220     
221     /**
222      * Converts to a string representing the portal URL.
223      * @return a string representing the portal URL.
224      * @see PortalURLParserImpl#toString(org.apache.pluto.driver.url.PortalURL)
225      */
226     public String toURL(boolean absolute)
227     {
228         String result = urlParser.toString(this);
229         if (absolute)
230         {
231             return urlBase + result;
232         }
233         return result;
234     }
235 
236     /**
237      * Returns the server URI (protocol, name, port).
238      * @return the server URI portion of the portal URL.
239      * @deprecated
240      */
241     @Deprecated
242     public String getServerURI() {
243         return null;
244     }
245 
246     /**
247      * Returns the servlet path (context path + servlet name).
248      * @return the servlet path.
249      */
250     public String getServletPath() {
251         return servletPath;
252     }
253 
254     /**
255      * Clone a copy of itself.
256      * @return a copy of itself.
257      */
258     public synchronized PortalURL clone() {
259     	RelativePortalURLImpl portalURL = new RelativePortalURLImpl();
260     	portalURL.servletPath = this.servletPath;
261     	portalURL.parameters = new HashMap<String, PortalURLParameter>(parameters);
262     	portalURL.privateRenderParameters = new HashMap<String, String[]>(privateRenderParameters);
263     	portalURL.portletModes = new HashMap<String, PortletMode>(portletModes);
264     	portalURL.windowStates = new HashMap<String, WindowState>(windowStates);
265     	portalURL.cacheLevel = cacheLevel;
266     	portalURL.resourceID = resourceID;
267     	portalURL.renderPath = renderPath;
268     	portalURL.actionWindow = actionWindow;
269         portalURL.urlParser = urlParser;
270     	portalURL.resourceWindow = resourceWindow;
271     	portalURL.publicParameterCurrent = publicParameterCurrent;
272         return portalURL;
273     }
274 //JSR-286 methods
275 
276     public void addPublicRenderParametersNew(Map<String, String[]> parameters){
277     	for (Iterator<String> iter=parameters.keySet().iterator(); iter.hasNext();) {
278 			String key = iter.next();
279 			if (publicParameterNew.containsKey(key)){
280 				publicParameterNew.remove(key);
281 			}
282 			String[] values = parameters.get(key);
283             publicParameterNew.put(key, values);
284 		}
285     }
286 
287 
288     public void addPublicParameterCurrent(String name, String[] values){
289     	publicParameterCurrent.put(name, values);
290     }
291 
292     public void addPublicParameterActionResourceParameter(String parameterName, String value) {
293     	//add at the first position
294 		if (publicParameterCurrent.containsKey(parameterName)){
295 			String[] tmp = publicParameterCurrent.get(parameterName);
296 
297 			String[] values = new String[tmp.length + 1];
298 			values[0] = value;
299 			for (int i = 0; i < tmp.length; i++) {
300 				values[i+1] = tmp[i];
301 			}
302 			publicParameterCurrent.remove(parameterName);
303 			publicParameterCurrent.put(parameterName, values.clone());
304 		}
305 		else
306 			publicParameterCurrent.put(parameterName, new String[]{value});
307 	}
308 
309     public Map<String, String[]> getPublicParameters() {
310     	Map<String,String[]> tmp = new HashMap<String, String[]>();
311 
312 		for (Iterator<String> iter = publicParameterCurrent.keySet().iterator(); iter.hasNext();) {
313            String paramname = iter.next();
314            if (!publicParameterNew.containsKey(paramname)){
315                String[] paramvalue = publicParameterCurrent.get(paramname);
316                tmp.put(paramname, paramvalue);
317            }
318         }
319 		for (Iterator<String> iter = publicParameterNew.keySet().iterator();iter.hasNext();){
320 			String paramname = iter.next();
321 			String[] paramvalue = publicParameterNew.get(paramname);
322 			if (paramvalue[0]!=null){
323 				tmp.put(paramname, paramvalue);
324 			}
325 		}
326 		return tmp;
327     }
328     
329     public Map<String, String[]> getNewPublicParameters()
330     {
331         return publicParameterNew;
332     }
333     
334     public Map<String, String[]> getPrivateRenderParameters()
335     {
336         return privateRenderParameters;
337     }
338 
339 
340 	public PageConfig getPageConfig(ServletContext servletContext) {
341 		String requestedPageId = getRenderPath();
342         if (LOG.isDebugEnabled()) {
343             LOG.debug("Requested Page: " + requestedPageId);
344         }
345         return ((DriverConfiguration) servletContext.getAttribute(
346         		AttributeKeys.DRIVER_CONFIG)).getPageConfig(requestedPageId);
347 	}
348 
349     public String getResourceWindow() {
350 		return resourceWindow;
351 	}
352 
353 	public void setResourceWindow(String resourceWindow) {
354 		this.resourceWindow = resourceWindow;
355 	}
356 
357 	public synchronized void merge(PortalURL url, String windowId)
358 	{
359         actionWindow = url.getActionWindow();
360         resourceWindow = url.getResourceWindow();
361         setPortletMode(windowId, url.getPortletMode(windowId));
362         setWindowState(windowId, url.getWindowState(windowId));
363         setCacheability(url.getCacheability());
364         setResourceID(url.getResourceID());
365         clearParameters(windowId);
366         for (PortalURLParameter param : url.getParameters())
367         {
368             if (windowId.equals(param.getWindowId()))
369             {
370                 addParameter(new PortalURLParameter(param.getWindowId(), param.getName(), param.getValues()));
371             }
372         }
373         Map<String, String[]> newPublicParameters = url.getNewPublicParameters();
374         for (Map.Entry<String, String[]> entry : newPublicParameters.entrySet())
375         {
376             if (entry.getValue()[0] == null)
377             {
378                 publicParameterCurrent.remove(entry.getKey());
379             }
380             else
381             {
382                 publicParameterCurrent.put(entry.getKey(), entry.getValue());
383             }
384         }
385 	}
386 	
387 }