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.jetspeed.container.state.impl;
18  
19  import java.io.UnsupportedEncodingException;
20  import java.util.Collections;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import javax.portlet.PortletMode;
25  import javax.portlet.WindowState;
26  
27  import org.apache.jetspeed.JetspeedActions;
28  import org.apache.jetspeed.cache.JetspeedContentCache;
29  import org.apache.jetspeed.container.state.MutableNavigationalState;
30  import org.apache.jetspeed.om.common.portlet.PortletApplication;
31  import org.apache.pluto.om.window.PortletWindow;
32  
33  /***
34   * BaseNavigationalState
35   *
36   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
37   * @version $Id: AbstractNavigationalState.java 554926 2007-07-10 13:12:26Z ate $
38   */
39  public abstract class AbstractNavigationalState implements MutableNavigationalState
40  {
41      private NavigationalStateCodec codec;
42      private PortletWindowRequestNavigationalStates requestStates;
43      protected JetspeedContentCache cache;
44      protected JetspeedContentCache decorationCache;
45      
46      public AbstractNavigationalState(NavigationalStateCodec codec, JetspeedContentCache cache)
47      {
48          this(codec, cache, null);
49      }
50  
51      public AbstractNavigationalState(NavigationalStateCodec codec, JetspeedContentCache cache, JetspeedContentCache decorationCache)
52      {
53          this.codec = codec;
54          this.cache = cache;
55          this.decorationCache = decorationCache;
56      }
57      
58      public void init(String encodedState, String characterEncoding)
59      throws UnsupportedEncodingException
60      {
61          if ( requestStates == null )
62          {
63              requestStates = codec.decode(encodedState, characterEncoding);
64          }
65      }
66      
67      protected PortletWindowRequestNavigationalStates getPortletWindowRequestNavigationalStates()
68      {
69          return requestStates;
70      }
71      
72      public void setState(PortletWindow window, WindowState windowState)
73      {
74          if ( windowState != null )
75          {
76              if (!JetspeedActions.getStandardWindowStates().contains(windowState))
77              {
78                  PortletApplication pa = (PortletApplication)window.getPortletEntity().getPortletDefinition().getPortletApplicationDefinition();
79                  windowState = pa.getMappedWindowState(windowState);
80              }
81              String windowId = window.getId().toString();
82              PortletWindowRequestNavigationalState state = requestStates.getPortletWindowNavigationalState(windowId);
83              if (state != null && (state.getWindowState() == null || !state.getWindowState().equals(windowState)))
84              {
85                  state.setWindowState(windowState);
86              }
87              else
88              {
89                  state = new PortletWindowRequestNavigationalState(windowId);
90                  requestStates.addPortletWindowNavigationalState(windowId, state);
91                  state.setWindowState(windowState);
92              }
93              if (windowState.equals(WindowState.MAXIMIZED))
94              {
95                  requestStates.setMaximizedWindow(window);
96              }
97          }
98      }
99  
100     public void setMode(PortletWindow window, PortletMode portletMode)
101     {
102         if ( portletMode != null )
103         {
104             if (!JetspeedActions.getStandardPortletModes().contains(portletMode))
105             {
106                 PortletApplication pa = (PortletApplication)window.getPortletEntity().getPortletDefinition().getPortletApplicationDefinition();
107                 portletMode = pa.getMappedPortletMode(portletMode);
108             }
109             String windowId = window.getId().toString();
110             PortletWindowRequestNavigationalState state = requestStates.getPortletWindowNavigationalState(windowId);
111             if (state != null && (state.getPortletMode() == null || !state.getPortletMode().equals(portletMode)))
112             {
113                 state.setPortletMode(portletMode);
114             }
115             else
116             {
117                 state = new PortletWindowRequestNavigationalState(windowId);
118                 requestStates.addPortletWindowNavigationalState(windowId, state);
119                 state.setPortletMode(portletMode);
120             }
121         }
122     }
123 
124     public WindowState getMappedState(String windowId)
125     {
126         WindowState windowState = null;
127         PortletWindowRequestNavigationalState state = requestStates.getPortletWindowNavigationalState(windowId);
128         if (state != null)
129         {
130             windowState = state.getWindowState();
131         }
132         return windowState != null ? windowState : WindowState.NORMAL;
133     }
134 
135     /***
136      * @deprecated
137      */
138     public WindowState getState(String windowId)
139     {
140         return getMappedState(windowId);
141     }
142 
143     public WindowState getState(PortletWindow window)
144     {
145         WindowState state = getMappedState(window.getId().toString());
146         if (state != null && !JetspeedActions.getStandardWindowStates().contains(state))
147         {
148             PortletApplication pa = (PortletApplication)window.getPortletEntity().getPortletDefinition().getPortletApplicationDefinition();
149             state = pa.getCustomWindowState(state);
150         }
151         return state;
152     }
153 
154     public WindowState getMappedState(PortletWindow window)
155     {
156         return getMappedState(window.getId().toString());
157     }
158 
159     public PortletMode getMappedMode(String windowId)
160     {
161         PortletMode portletMode = null;
162         PortletWindowRequestNavigationalState state = requestStates.getPortletWindowNavigationalState(windowId);
163         if (state != null)
164         {
165             portletMode = state.getPortletMode();
166         }
167         return portletMode != null ? portletMode : PortletMode.VIEW;
168     }
169     
170     /***
171      * @deprecated
172      */
173     public PortletMode getMode(String windowId)
174     {
175         return getMappedMode(windowId);
176     }
177     
178     public PortletMode getMode(PortletWindow window)
179     {
180         PortletMode mode = getMappedMode(window.getId().toString());
181         if (mode != null && !JetspeedActions.getStandardPortletModes().contains(mode))
182         {
183             PortletApplication pa = (PortletApplication)window.getPortletEntity().getPortletDefinition().getPortletApplicationDefinition();
184             mode = pa.getCustomPortletMode(mode);
185         }
186         return mode;
187     }
188 
189     public PortletMode getMappedMode(PortletWindow window)
190     {
191         return getMappedMode(window.getId().toString());
192     }
193 
194     public PortletWindow getMaximizedWindow()
195     {
196         return requestStates.getMaximizedWindow();
197     }
198 
199     public Iterator getParameterNames(PortletWindow window)
200     {
201         PortletWindowRequestNavigationalState state = requestStates.getPortletWindowNavigationalState(window.getId().toString());
202         if ( state != null && state.getParametersMap() != null )
203         {
204             return state.getParametersMap().keySet().iterator();
205         }
206         else
207         {
208             return Collections.EMPTY_LIST.iterator();
209         }
210     }
211 
212     public String[] getParameterValues(PortletWindow window, String parameterName)
213     {
214         PortletWindowRequestNavigationalState state = requestStates.getPortletWindowNavigationalState(window.getId().toString());
215         if ( state != null && state.getParametersMap() != null )
216         {
217             return (String[])state.getParametersMap().get(parameterName);
218         }
219         else
220         {
221             return null;
222         }
223     }
224 
225     public PortletWindow getPortletWindowOfAction()
226     {
227         return requestStates.getActionWindow();
228     }
229     
230     public PortletWindow getPortletWindowOfResource()
231     {
232         return requestStates.getResourceWindow();
233     }
234 
235     public String encode(PortletWindow window, Map parameters, PortletMode mode, WindowState state, boolean action)
236     throws UnsupportedEncodingException
237     {
238         if ( mode != null || state != null )
239         {
240             PortletApplication pa = null;
241             if (mode != null && !JetspeedActions.getStandardPortletModes().contains(mode))
242             {
243                 pa = (PortletApplication)window.getPortletEntity().getPortletDefinition().getPortletApplicationDefinition();
244                 mode = pa.getMappedPortletMode(mode);
245             }
246             if (state != null && !JetspeedActions.getStandardWindowStates().contains(state))
247             {
248                 if ( pa == null )
249                 {
250                     pa = (PortletApplication)window.getPortletEntity().getPortletDefinition().getPortletApplicationDefinition();
251                 }
252                 state = pa.getMappedWindowState(state);
253             }
254         }
255         return codec.encode(requestStates, window, parameters, mode, state, action, isNavigationalParameterStateFull(),
256                 isRenderParameterStateFull());
257     }
258 
259     public String encode(PortletWindow window, PortletMode mode, WindowState state)
260     throws UnsupportedEncodingException
261     {
262         if ( mode != null || state != null )
263         {
264             PortletApplication pa = null;
265             if (mode != null && !JetspeedActions.getStandardPortletModes().contains(mode))
266             {
267                 pa = (PortletApplication)window.getPortletEntity().getPortletDefinition().getPortletApplicationDefinition();
268                 mode = pa.getMappedPortletMode(mode);
269             }
270             if (state != null && !JetspeedActions.getStandardWindowStates().contains(state))
271             {
272                 if ( pa == null )
273                 {
274                     pa = (PortletApplication)window.getPortletEntity().getPortletDefinition().getPortletApplicationDefinition();
275                 }
276                 state = pa.getMappedWindowState(state);
277             }
278         }
279         String encodedState = null;
280         Map currentWindowStates = null;
281         PortletWindowExtendedNavigationalState windowNavState = null;
282         PortletMode targetMode = mode;
283         WindowState targetState = state;
284         if (this instanceof SessionNavigationalState)
285         {
286             currentWindowStates = ((SessionNavigationalState)this).getCurrentPageWindowStates();
287             if (currentWindowStates != null)
288             {
289                 windowNavState = (PortletWindowExtendedNavigationalState)currentWindowStates.get(window.getId().toString());
290                 if (windowNavState != null)
291                 {
292                     if (targetMode == null)
293                     {
294                         targetMode = windowNavState.getPortletMode();
295                     }
296                     if (targetState == null)
297                     {
298                         targetState = windowNavState.getWindowState();
299                     }
300                    encodedState = windowNavState.getDecoratorActionEncoding(targetMode, targetState);
301                 }
302             }
303         }
304         if (encodedState == null)
305         {
306             encodedState = codec.encode(requestStates, window, mode, state, isNavigationalParameterStateFull(), isRenderParameterStateFull());
307             if (currentWindowStates != null)
308             {
309                 if (windowNavState == null)
310                 {
311                     windowNavState = new PortletWindowExtendedNavigationalState();
312                     currentWindowStates.put(window.getId().toString(), windowNavState);
313                 }
314                 windowNavState.setDecoratorActionEncoding(targetMode, targetState, encodedState);
315             }
316         }
317         return encodedState;
318     }
319     
320     public String encode() throws UnsupportedEncodingException
321     {
322         return codec.encode(requestStates, isNavigationalParameterStateFull(), isRenderParameterStateFull());
323     }
324 
325     public Iterator getWindowIdIterator()
326     {
327         return requestStates.getWindowIdIterator();
328     }
329     
330     public void clearParameters(PortletWindow window)
331     {
332         PortletWindowRequestNavigationalState state = requestStates.getPortletWindowNavigationalState(window.getId().toString());
333         if (state != null)
334         {
335             Map map = state.getParametersMap();
336             if (map != null)
337             {
338                 map.clear();
339                 state.setClearParameters(true);
340             }
341         }
342     }
343     
344     public void removeState(PortletWindow window)
345     {
346         requestStates.removePortletWindowNavigationalState(window.getId().toString());
347     }
348 }