001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces;
034:
035: import com.flexive.faces.beans.FxContentViewBean;
036: import com.flexive.shared.CacheAdmin;
037: import com.flexive.shared.content.FxContent;
038: import com.flexive.shared.content.FxPK;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: import javax.faces.application.NavigationHandler;
043: import javax.faces.context.FacesContext;
044: import java.io.IOException;
045: import java.util.Map;
046:
047: /**
048: * <p>The sole purpose of this navigation handler is to support external content URIs when
049: * creating a new content instance with {@link FxContentViewBean}. When a new content was
050: * created in the current request, the response is redirected to the external URI matching
051: * the newly created content. Otherwise all subsequent calls relying on the external URI
052: * (i.e. code that extracts the content ID from the URI) would fail.</p>
053: * <p>
054: * Note that you have to provide a constructor taking a single NavigationHandler argument for
055: * your class, or JSF won't be able to add your handler to the chain.
056: * </p>
057: *
058: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
059: * @version $Rev: 1 $
060: */
061: public abstract class AbstractContentNavigationHandler extends
062: NavigationHandler {
063: private static final Log LOG = LogFactory
064: .getLog(AbstractContentNavigationHandler.class);
065:
066: protected final NavigationHandler parent;
067: protected final URIRouteCollection<ContentURIRoute> routes;
068:
069: protected AbstractContentNavigationHandler(
070: NavigationHandler parent,
071: URIRouteCollection<ContentURIRoute> routes) {
072: this .parent = parent;
073: this .routes = routes;
074: }
075:
076: @Override
077: public void handleNavigation(FacesContext facesContext,
078: String fromAction, String outcome) {
079: final Map requestMap = facesContext.getExternalContext()
080: .getRequestMap();
081: final Boolean newFlag = (Boolean) requestMap
082: .get(FxContentViewBean.REQUEST_ISNEW);
083: if (newFlag != null && newFlag) {
084: // got PK from content creation, redirect
085: final FxContent content = (FxContent) requestMap
086: .get(FxContentViewBean.REQUEST_CONTENT);
087: final FxPK pk = (FxPK) requestMap
088: .get(FxContentViewBean.REQUEST_PK);
089: final ContentURIRoute route = ContentURIRoute.findForType(
090: routes.getRoutes(), CacheAdmin.getEnvironment()
091: .getType(content.getTypeId()).getName());
092: if (route != null) {
093: try {
094: facesContext.getExternalContext().redirect(
095: facesContext.getExternalContext()
096: .getRequestContextPath()
097: + route.getMappedUri(pk));
098: return;
099: } catch (IOException e) {
100: LOG.error("Failed to redirect to content URL: "
101: + e.getMessage(), e);
102: }
103: }
104: }
105: parent.handleNavigation(facesContext, fromAction, outcome);
106: }
107: }
|