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.resource;
18  
19  import java.io.IOException;
20  import java.util.HashMap;
21  
22  import javax.portlet.PortletException;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.jetspeed.PortalReservedParameters;
29  import org.apache.jetspeed.container.window.PortletWindowAccessor;
30  import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
31  import org.apache.jetspeed.om.page.ContentFragment;
32  import org.apache.jetspeed.om.page.ContentFragmentImpl;
33  import org.apache.jetspeed.om.page.Fragment;
34  import org.apache.jetspeed.om.page.Page;
35  import org.apache.jetspeed.pipeline.PipelineException;
36  import org.apache.jetspeed.pipeline.valve.AbstractValve;
37  import org.apache.jetspeed.pipeline.valve.ValveContext;
38  import org.apache.jetspeed.request.RequestContext;
39  import org.apache.pluto.PortletContainer;
40  import org.apache.pluto.PortletContainerException;
41  import org.apache.pluto.om.window.PortletWindow;
42  
43  /***
44   * <p>
45   * ResourceValveImpl
46   * </p>
47   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
48   * @version $Id: ResourceValveImpl.java 544024 2007-06-04 00:59:09Z ate $
49   *
50   */
51  public class ResourceValveImpl extends AbstractValve
52  {
53  
54      private static final Log log = LogFactory.getLog(ResourceValveImpl.class);
55      private PortletContainer container;
56      private PortletWindowAccessor windowAccessor;
57  
58      public ResourceValveImpl(PortletContainer container, PortletWindowAccessor windowAccessor)
59      {
60          this.container = container;
61          this.windowAccessor = windowAccessor;
62      }
63      
64      /***
65       * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext, org.apache.jetspeed.pipeline.valve.ValveContext)
66       */
67      public void invoke(RequestContext request, ValveContext context) throws PipelineException
68      {     
69          PortletWindow resourceWindow = request.getPortalURL().getNavigationalState().getPortletWindowOfResource();
70          
71          if ( resourceWindow != null )
72          {
73              try
74              {            
75                  Page page = request.getPage();
76                  Fragment fragment = page.getFragmentById(resourceWindow.getId().toString());
77                  // If portlet entity is null, try to refresh the resourceWindow.
78                  // Under some clustered environments, a cached portlet window could have null entity.
79                  if (null == resourceWindow.getPortletEntity())
80                  {
81                      try 
82                      {
83                          ContentFragment contentFragment = new ContentFragmentImpl(fragment, new HashMap());
84                          resourceWindow = this.windowAccessor.getPortletWindow(contentFragment);
85                      } 
86                      catch (Exception e)
87                      {
88                          log.error("Failed to refresh resource window.", e);
89                      }
90                  }
91                  ((MutablePortletEntity)resourceWindow.getPortletEntity()).setFragment(fragment);
92                  HttpServletResponse response = request.getResponse();
93                  HttpServletRequest requestForWindow = request.getRequestForWindow(resourceWindow);
94                  requestForWindow.setAttribute(PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE, request);
95                  requestForWindow.setAttribute(PortalReservedParameters.PAGE_ATTRIBUTE, request.getPage());
96                  requestForWindow.setAttribute(PortalReservedParameters.FRAGMENT_ATTRIBUTE, fragment);
97                  request.setAttribute(PortalReservedParameters.REQUEST_CONTEXT_OBJECTS, request.getObjects());                        
98                  request.setAttribute(PortalReservedParameters.PATH_ATTRIBUTE, request.getAttribute(PortalReservedParameters.PATH_ATTRIBUTE));
99                  request.setAttribute(PortalReservedParameters.PORTLET_WINDOW_ATTRIBUTE, resourceWindow);
100                 BufferedHttpServletResponse bufferedResponse = new BufferedHttpServletResponse(response);
101                 container.renderPortlet(resourceWindow, requestForWindow, bufferedResponse);
102                 bufferedResponse.flush(response);
103             }
104             catch (PortletContainerException e)
105             {
106                 log.fatal("Unable to retrieve portlet container!", e);
107                 throw new PipelineException("Unable to retrieve portlet container!", e);
108             }
109             catch (PortletException e)
110             {
111                 log.warn("Unexpected PortletException", e);
112 
113             }
114             catch (IOException e)
115             {
116                 log.error("Unexpected IOException", e);
117             }
118             catch (IllegalStateException e)
119             {
120                 log.error("Unexpected IllegalStateException.", e);
121             }
122             catch (Exception t)
123             {
124                 log.error("Unexpected Exception", t);
125             }
126         }
127         else
128         {
129             // Pass control to the next Valve in the Pipeline
130             context.invokeNext(request);
131         }
132     }
133 
134     public String toString()
135     {
136         return "ResourceValveImpl";
137     }
138 }