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.url.impl;
18  
19  import java.io.IOException;
20  
21  import org.apache.jetspeed.container.state.NavigationalState;
22  import org.apache.jetspeed.container.url.PortalURL;
23  import org.apache.jetspeed.desktop.JetspeedDesktop;
24  import org.apache.jetspeed.pipeline.PipelineException;
25  import org.apache.jetspeed.pipeline.valve.AbstractValve;
26  import org.apache.jetspeed.pipeline.valve.ValveContext;
27  import org.apache.jetspeed.request.RequestContext;
28  
29  /***
30   * This Valve will clean encoded navstate from the browser url by sending a client side redirect
31   * to the same url with the navstate removed.
32   * <p>
33   * This Valve will only do this:
34   * <ul>
35   *   <li>on a GET Render request (not for Resource or Action requests)</li>
36   *   <li>the request is not served by the Desktop</li>
37   *   <li>the navstate is encoded as PathInfo</li>
38   *   <li>all the navstate is maintained in the session (portlet mode, window state and render parameters)</li>
39   * </ul>
40   * </p>
41   * <p>
42   * This valve needs to be added to the portal pipeline *after* the ContainerValve to ensure navstate is properly synchronized with the session.
43   * </p>
44   * <p>
45   * Caveats:<br/>
46   * <ul>
47   *   <li>bookmarking browser url will no longer retain nav state, but with SessionFullNavigationState this wasn't really reliable anyway.</li>
48   *   <li>back button history is no longer maintained by browsers for GET render urls, somewhat similar to Ajax based requests (e.g. Desktop)</li>
49   * </ul>
50   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
51   * @version $Id: CleanPathInfoEncodedNavStateFromPortalURLValve.java 605989 2007-12-20 18:26:54Z ate $
52   * 
53   */
54  public class CleanPathInfoEncodedNavStateFromPortalURLValve extends AbstractValve
55  {
56      public void invoke(RequestContext request, ValveContext context) throws PipelineException
57      {
58          NavigationalState state = request.getPortalURL().getNavigationalState();
59          PortalURL portalURL = request.getPortalURL();
60  
61          Boolean desktopEnabled = (Boolean) request.getAttribute(JetspeedDesktop.DESKTOP_ENABLED_REQUEST_ATTRIBUTE);
62  
63          if (request.getRequest().getMethod().equals("GET") && portalURL.hasEncodedNavState()
64                  && portalURL.isPathInfoEncodingNavState() && state.isNavigationalParameterStateFull()
65                  && state.isRenderParameterStateFull() && state.getPortletWindowOfAction() == null
66                  && state.getPortletWindowOfResource() == null
67                  && (desktopEnabled == null || desktopEnabled.booleanValue() == false))
68          {
69              try
70              {
71                  StringBuffer location = new StringBuffer(request.getPortalURL().getBasePath());
72                  String str = request.getPortalURL().getPath();
73                  if (str != null)
74                  {
75                      location.append(str);
76                  }
77                  str = request.getRequest().getQueryString();
78                  if (str != null && str.length() > 0)
79                  {
80                      location.append('?').append(request.getRequest().getQueryString());
81                  }
82                  request.getResponse().sendRedirect(request.getResponse().encodeRedirectURL(location.toString()));
83              }
84              catch (IOException e)
85              {
86                  throw new PipelineException(e);
87              }
88          }
89          // Pass control to the next Valve in the Pipeline
90          context.invokeNext(request);
91      }
92  }