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