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: */
018:
019: package org.apache.lenya.cms.cocoon.acting;
020:
021: import java.util.Collections;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.parameters.Parameters;
025: import org.apache.avalon.framework.service.ServiceSelector;
026: import org.apache.cocoon.acting.ConfigurableServiceableAction;
027: import org.apache.cocoon.environment.ObjectModelHelper;
028: import org.apache.cocoon.environment.Redirector;
029: import org.apache.cocoon.environment.Request;
030: import org.apache.cocoon.environment.SourceResolver;
031: import org.apache.lenya.ac.AccessController;
032: import org.apache.lenya.ac.AccessControllerResolver;
033: import org.apache.lenya.util.ServletHelper;
034:
035: /**
036: * Super class for access control actions.
037: *
038: * @version $Id: AccessControlAction.java 473861 2006-11-12 03:51:14Z gregor $
039: */
040: public abstract class AccessControlAction extends
041: ConfigurableServiceableAction {
042:
043: private AccessController accessController;
044:
045: /**
046: * <p>
047: * Invokes the access control functionality.
048: * If no access controller was found for the requested URL, an empty map is returned.
049: * </p>
050: * <p>
051: * This is a template method. Implement doAct() to add your functionality.
052: * </p>
053: * @see org.apache.cocoon.acting.Action#act(org.apache.cocoon.environment.Redirector, org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
054: */
055: public Map act(Redirector redirector,
056: SourceResolver sourceResolver, Map objectModel,
057: String source, Parameters parameters) throws Exception {
058:
059: ServiceSelector selector = null;
060: AccessControllerResolver resolver = null;
061: this .accessController = null;
062:
063: Request request = ObjectModelHelper.getRequest(objectModel);
064:
065: Map result = null;
066:
067: try {
068: selector = (ServiceSelector) this .manager
069: .lookup(AccessControllerResolver.ROLE + "Selector");
070:
071: getLogger().debug(
072: "Resolving AC resolver for type ["
073: + AccessControllerResolver.DEFAULT_RESOLVER
074: + "]");
075: resolver = (AccessControllerResolver) selector
076: .select(AccessControllerResolver.DEFAULT_RESOLVER);
077: getLogger()
078: .debug("Resolved AC resolver [" + resolver + "]");
079:
080: String webappUrl = ServletHelper.getWebappURI(request);
081: this .accessController = resolver
082: .resolveAccessController(webappUrl);
083:
084: if (this .accessController == null) {
085: result = Collections.EMPTY_MAP;
086: } else {
087: this .accessController.setupIdentity(request);
088: result = doAct(redirector, sourceResolver, objectModel,
089: source, parameters);
090: }
091:
092: } finally {
093: if (selector != null) {
094: if (resolver != null) {
095: selector.release(resolver);
096: }
097: this .manager.release(selector);
098: }
099: }
100: return result;
101: }
102:
103: /**
104: * The actual act method.
105: * @param redirector The <code>Redirector</code> in charge
106: * @param resolver The <code>SourceResolver</code> in charge
107: * @param objectModel The <code>Map</code> with object of the
108: * calling environment which can be used
109: * to select values this controller may need
110: * (ie Request, Response).
111: * @param source A source <code>String</code> to the Action
112: * @param parameters The <code>Parameters</code> for this invocation
113: * @return Map The returned <code>Map</code> object with
114: * sitemap substitution values which can be used
115: * in subsequent elements attributes like src=
116: * using a xpath like expression: src="mydir/{myval}/foo"
117: * If the return value is null the processing inside
118: * the <map:act> element of the sitemap will
119: * be skipped.
120: * @exception Exception Indicates something is totally wrong
121: */
122: protected abstract Map doAct(Redirector redirector,
123: SourceResolver resolver, Map objectModel, String source,
124: Parameters parameters) throws Exception;
125:
126: /**
127: * Returns the access controller.
128: * @return An access controller.
129: */
130: public AccessController getAccessController() {
131: return this.accessController;
132: }
133:
134: }
|