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;
18  
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.apache.jetspeed.request.RequestContext;
22  import org.apache.jetspeed.container.state.MutableNavigationalState;
23  import org.apache.jetspeed.om.page.Page;
24  import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
25  import org.apache.jetspeed.om.window.impl.PortletWindowImpl;
26  import org.apache.jetspeed.pipeline.PipelineException;
27  import org.apache.jetspeed.pipeline.valve.AbstractValve;
28  import org.apache.jetspeed.pipeline.valve.ValveContext;
29  import org.apache.pluto.om.window.PortletWindow;
30  
31  /***
32   * Determines the action window in the current request If no action was found,
33   * sets the request context's action window to null denoting that there is no
34   * targeted action for this request otherwise the target action window is set
35   * here
36   * 
37   * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
38   * @version $Id: ContainerValve.java 589933 2007-10-30 01:51:50Z woonsan $
39   */
40  public class ContainerValve extends AbstractValve
41  {
42      public void invoke(RequestContext request, ValveContext context) throws PipelineException
43      {
44          try
45          {
46              // create a session if not already created, necessary for Tomcat 5
47              request.getRequest().getSession(true);
48  
49              // PortletContainerServices.prepare();
50              MutableNavigationalState state = (MutableNavigationalState)request.getPortalURL().getNavigationalState();
51              if (state != null)
52              {
53                  boolean redirect = false;
54                  Page page = request.getPage();
55                  PortletWindow window = state.getPortletWindowOfResource();
56                  if (window != null && page.getFragmentById(window.getId().toString()) == null)
57                  {
58                      // target window doesn't exists anymore or the target page is not accessible (anymore)
59                      request.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND);
60                      return;
61                  }
62                  window = state.getPortletWindowOfAction();
63                  if (window != null && page.getFragmentById(window.getId().toString()) == null)
64                  {
65                      if (!((PortletWindowImpl) window).isInstantlyRendered())
66                      {
67                          // target window doesn't exists anymore or the target page is not accessible (anymore)
68                          // remove any navigational state for the window
69                          state.removeState(window);
70                          // as this is an action request which cannot be handled, perform a direct redirect after sync state (for the other windows)
71                          redirect = true;
72                      }
73                  }
74                  window = state.getMaximizedWindow();
75                  if (window != null && page.getFragmentById(window.getId().toString()) == null)
76                  {
77                      // target window doesn't exists anymore or the target page is not accessible (anymore)
78                      // remove any navigational state for the window
79                      state.removeState(window);
80                  }
81                  state.sync(request);
82                  if (redirect)
83                  {
84                      // target page doesn't contain (anymore) the targeted windowOfAction 
85                      // this can also occur when a session is expired and the target page isn't accessible for the anonymous user
86                      // Redirect the user back to the target page (with possibly retaining the other windows navigational state).
87                      request.getResponse().sendRedirect(request.getPortalURL().getPortalURL());
88                      return;
89                  }
90  
91                  PortletWindow actionWindow = state.getPortletWindowOfAction();
92                  if (null == actionWindow)
93                  {
94                      // set to null to denote that no action was requested
95                      request.setActionWindow(null);
96                  }
97                  else
98                  {
99                      // set the requested action window
100                     request.setActionWindow(actionWindow);
101                 }
102             }
103         }
104         catch (Exception e)
105         {
106             throw new PipelineException(e);
107         }
108         // Pass control to the next Valve in the Pipeline
109         context.invokeNext(request);
110     }
111 
112     public String toString()
113     {
114         return "ContainerValve";
115     }
116 }