01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.acting;
18:
19: import org.apache.avalon.framework.parameters.Parameters;
20: import org.apache.avalon.framework.thread.ThreadSafe;
21: import org.apache.cocoon.environment.Redirector;
22: import org.apache.cocoon.environment.SourceResolver;
23: import org.apache.excalibur.source.Source;
24: import org.apache.excalibur.source.SourceNotFoundException;
25:
26: import java.util.Map;
27:
28: /**
29: * This action simply checks to see if a resource identified by the <code>src</code>
30: * sitemap attribute exists or not. The action returns empty <code>Map</code> if
31: * resource exists, <code>null</code> otherwise.
32: *
33: * <p>Instead of src attribute, source can be specified using
34: * parameter named <code>url</code> (this is old syntax, should be removed soon).
35: *
36: * <p><b>NOTE:</b> {@link org.apache.cocoon.selection.ResourceExistsSelector}
37: * should be preferred to this component, as the semantics of a Selector better
38: * matches the supplied functionality.
39: *
40: * @author <a href="mailto:balld@apache.org">Donald Ball</a>
41: * @version CVS $Id: ResourceExistsAction.java 433543 2006-08-22 06:22:54Z crossley $
42: */
43: public class ResourceExistsAction extends ServiceableAction implements
44: ThreadSafe {
45:
46: public Map act(Redirector redirector, SourceResolver resolver,
47: Map objectModel, String src, Parameters parameters)
48: throws Exception {
49: String resourceURI = parameters.getParameter("url", src);
50: Source source = null;
51: try {
52: source = resolver.resolveURI(resourceURI);
53: if (source.exists()) {
54: return EMPTY_MAP;
55: }
56: } catch (SourceNotFoundException e) {
57: // Do not log
58: } catch (Exception e) {
59: getLogger().warn(
60: "Exception resolving resource " + resourceURI, e);
61: } finally {
62: if (source != null) {
63: resolver.release(source);
64: }
65: }
66: return null;
67: }
68: }
|