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.shared.content.FxPK;
036: import com.flexive.shared.exceptions.FxInvalidParameterException;
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039:
040: import javax.faces.context.FacesContext;
041: import javax.servlet.http.HttpServletRequest;
042: import java.util.regex.Matcher;
043:
044: /**
045: * An URI matcher that provides additional typesafe methods for querying
046: * the parameters contained in a mapped content URI.
047: *
048: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
049: * @version $Rev: 1 $
050: */
051: public class ContentURIMatcher extends URIMatcher {
052: private static final transient Log LOG = LogFactory
053: .getLog(ContentURIMatcher.class);
054: public static final String REQUEST_PK = ContentURIMatcher.class
055: .getName()
056: + ".PK";
057: public static final String REQUEST_ORIG_URI = ContentURIMatcher.class
058: .getName()
059: + ".ORIGINAL_URI";
060: public static final String REQUEST_MAPPED_URI = ContentURIMatcher.class
061: .getName()
062: + ".MAPPED_URI";
063:
064: private FxPK pk;
065:
066: public ContentURIMatcher(ContentURIRoute mapper, String uri,
067: Matcher matcher) {
068: super (mapper, uri, matcher);
069: }
070:
071: /**
072: * {@inheritDoc}
073: */
074: @Override
075: public String apply(FacesContext context) {
076: final HttpServletRequest request = (HttpServletRequest) context
077: .getExternalContext().getRequest();
078: // store content pk found in URI
079: request.setAttribute(REQUEST_PK, getPk());
080: return super .apply(context);
081: }
082:
083: public long getId() {
084: return getPk().getId();
085: }
086:
087: public FxPK getPk() {
088: if (pk == null && isMatched()) {
089: if (route.hasParameter("pk")) {
090: pk = FxPK.fromString(getParameter("pk"));
091: } else if (route.hasParameter("id")) {
092: pk = new FxPK(Long.parseLong(getParameter("id")));
093: }
094: }
095: if (pk == null) {
096: throw new FxInvalidParameterException("uri", LOG,
097: "ex.contentUriRoute.uri.pk", uri)
098: .asRuntimeException();
099: }
100: return pk;
101: }
102: }
|