001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.resource;
018:
019: import java.io.IOException;
020: import java.util.HashMap;
021:
022: import javax.portlet.PortletException;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.jetspeed.PortalReservedParameters;
029: import org.apache.jetspeed.container.window.PortletWindowAccessor;
030: import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
031: import org.apache.jetspeed.om.page.ContentFragment;
032: import org.apache.jetspeed.om.page.ContentFragmentImpl;
033: import org.apache.jetspeed.om.page.Fragment;
034: import org.apache.jetspeed.om.page.Page;
035: import org.apache.jetspeed.pipeline.PipelineException;
036: import org.apache.jetspeed.pipeline.valve.AbstractValve;
037: import org.apache.jetspeed.pipeline.valve.ValveContext;
038: import org.apache.jetspeed.request.RequestContext;
039: import org.apache.pluto.PortletContainer;
040: import org.apache.pluto.PortletContainerException;
041: import org.apache.pluto.om.window.PortletWindow;
042:
043: /**
044: * <p>
045: * ResourceValveImpl
046: * </p>
047: * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
048: * @version $Id: ResourceValveImpl.java 544024 2007-06-04 00:59:09Z ate $
049: *
050: */
051: public class ResourceValveImpl extends AbstractValve {
052:
053: private static final Log log = LogFactory
054: .getLog(ResourceValveImpl.class);
055: private PortletContainer container;
056: private PortletWindowAccessor windowAccessor;
057:
058: public ResourceValveImpl(PortletContainer container,
059: PortletWindowAccessor windowAccessor) {
060: this .container = container;
061: this .windowAccessor = windowAccessor;
062: }
063:
064: /**
065: * @see org.apache.jetspeed.pipeline.valve.Valve#invoke(org.apache.jetspeed.request.RequestContext, org.apache.jetspeed.pipeline.valve.ValveContext)
066: */
067: public void invoke(RequestContext request, ValveContext context)
068: throws PipelineException {
069: PortletWindow resourceWindow = request.getPortalURL()
070: .getNavigationalState().getPortletWindowOfResource();
071:
072: if (resourceWindow != null) {
073: try {
074: Page page = request.getPage();
075: Fragment fragment = page.getFragmentById(resourceWindow
076: .getId().toString());
077: // If portlet entity is null, try to refresh the resourceWindow.
078: // Under some clustered environments, a cached portlet window could have null entity.
079: if (null == resourceWindow.getPortletEntity()) {
080: try {
081: ContentFragment contentFragment = new ContentFragmentImpl(
082: fragment, new HashMap());
083: resourceWindow = this .windowAccessor
084: .getPortletWindow(contentFragment);
085: } catch (Exception e) {
086: log.error("Failed to refresh resource window.",
087: e);
088: }
089: }
090: ((MutablePortletEntity) resourceWindow
091: .getPortletEntity()).setFragment(fragment);
092: HttpServletResponse response = request.getResponse();
093: HttpServletRequest requestForWindow = request
094: .getRequestForWindow(resourceWindow);
095: requestForWindow
096: .setAttribute(
097: PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE,
098: request);
099: requestForWindow.setAttribute(
100: PortalReservedParameters.PAGE_ATTRIBUTE,
101: request.getPage());
102: requestForWindow.setAttribute(
103: PortalReservedParameters.FRAGMENT_ATTRIBUTE,
104: fragment);
105: request
106: .setAttribute(
107: PortalReservedParameters.REQUEST_CONTEXT_OBJECTS,
108: request.getObjects());
109: request
110: .setAttribute(
111: PortalReservedParameters.PATH_ATTRIBUTE,
112: request
113: .getAttribute(PortalReservedParameters.PATH_ATTRIBUTE));
114: request
115: .setAttribute(
116: PortalReservedParameters.PORTLET_WINDOW_ATTRIBUTE,
117: resourceWindow);
118: BufferedHttpServletResponse bufferedResponse = new BufferedHttpServletResponse(
119: response);
120: container.renderPortlet(resourceWindow,
121: requestForWindow, bufferedResponse);
122: bufferedResponse.flush(response);
123: } catch (PortletContainerException e) {
124: log.fatal("Unable to retrieve portlet container!", e);
125: throw new PipelineException(
126: "Unable to retrieve portlet container!", e);
127: } catch (PortletException e) {
128: log.warn("Unexpected PortletException", e);
129:
130: } catch (IOException e) {
131: log.error("Unexpected IOException", e);
132: } catch (IllegalStateException e) {
133: log.error("Unexpected IllegalStateException.", e);
134: } catch (Exception t) {
135: log.error("Unexpected Exception", t);
136: }
137: } else {
138: // Pass control to the next Valve in the Pipeline
139: context.invokeNext(request);
140: }
141: }
142:
143: public String toString() {
144: return "ResourceValveImpl";
145: }
146: }
|