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: * $Header:$
018: */
019: package org.apache.beehive.netui.pageflow.faces.internal;
020:
021: import javax.faces.application.NavigationHandler;
022: import javax.faces.context.FacesContext;
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025: import javax.servlet.ServletContext;
026: import javax.servlet.ServletException;
027:
028: import java.io.IOException;
029:
030: import org.apache.beehive.netui.pageflow.PageFlowController;
031: import org.apache.beehive.netui.pageflow.PageFlowUtils;
032: import org.apache.beehive.netui.pageflow.PageFlowConstants;
033: import org.apache.beehive.netui.pageflow.FlowControllerFactory;
034: import org.apache.beehive.netui.pageflow.RequestContext;
035: import org.apache.beehive.netui.pageflow.internal.InternalConstants;
036: import org.apache.beehive.netui.util.logging.Logger;
037:
038: /**
039: * Internal class used in JSF/Page Flow integration. This NavigationHandler raises Page Flow actions for JSF pages
040: * that are in Page Flow directories.
041: * @see org.apache.beehive.netui.pageflow.faces.PageFlowApplicationFactory
042: */
043: public class PageFlowNavigationHandler extends NavigationHandler {
044: private static final Logger _log = Logger
045: .getInstance(PageFlowNavigationHandler.class);
046: static final String ALREADY_FORWARDED_ATTR = InternalConstants.ATTR_PREFIX
047: + "navHandled";
048:
049: private NavigationHandler _baseHandler;
050:
051: public PageFlowNavigationHandler(NavigationHandler base) {
052: if (_log.isDebugEnabled()) {
053: _log.debug("Adapting NavigationHandler" + base);
054: }
055:
056: _baseHandler = base;
057: }
058:
059: public void handleNavigation(FacesContext context,
060: String fromAction, String outcome) {
061: Object request = context.getExternalContext().getRequest();
062: Object response = context.getExternalContext().getResponse();
063: Object extContext = context.getExternalContext().getContext();
064:
065: if (request instanceof HttpServletRequest
066: && response instanceof HttpServletResponse
067: && extContext instanceof ServletContext) {
068: HttpServletRequest httpRequest = (HttpServletRequest) request;
069: HttpServletResponse httpResponse = (HttpServletResponse) response;
070:
071: //
072: // If we're coming in on a forwarded request from this NavigationHandler, bail out; we don't want to
073: // forward again.
074: //
075: if (httpRequest.getAttribute(ALREADY_FORWARDED_ATTR) != null) {
076: httpRequest.removeAttribute(ALREADY_FORWARDED_ATTR);
077: return;
078: }
079:
080: try {
081: //
082: // We only forward to Page Flow actions if there's a page flow appropriate for this request.
083: //
084: ServletContext servletContext = (ServletContext) extContext;
085: FlowControllerFactory fcFactory = FlowControllerFactory
086: .get(servletContext);
087: PageFlowController pfc = fcFactory
088: .getPageFlowForRequest(new RequestContext(
089: httpRequest, httpResponse));
090:
091: if (pfc != null) {
092: if (outcome != null) {
093: String actionURI = outcome
094: + PageFlowConstants.ACTION_EXTENSION;
095:
096: if (_log.isDebugEnabled()) {
097: _log.debug("Forwarding to " + actionURI);
098: }
099:
100: context.responseComplete();
101: httpRequest.setAttribute(
102: ALREADY_FORWARDED_ATTR, actionURI);
103:
104: try {
105: httpRequest.getRequestDispatcher(actionURI)
106: .forward(httpRequest, httpResponse);
107: } catch (IOException e) {
108: _log.error("Could not forward to "
109: + actionURI, e);
110: } catch (ServletException e) {
111: _log.error("Could not forward to "
112: + actionURI, e.getRootCause());
113: }
114: }
115:
116: return;
117: }
118: } catch (InstantiationException e) {
119: _log.error(
120: "Could not instantiate PageFlowController for request "
121: + httpRequest.getRequestURI(), e);
122: return;
123: } catch (IllegalAccessException e) {
124: _log.error(
125: "Could not instantiate PageFlowController for request "
126: + httpRequest.getRequestURI(), e);
127: return;
128: }
129: }
130:
131: // Fall back to base JSF navigation.
132: _baseHandler.handleNavigation(context, fromAction, outcome);
133: }
134: }
|