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: */
18:
19: /* $Id: ResourceExistsAction.java 570151 2007-08-27 15:09:07Z andreas $ */
20:
21: package org.apache.lenya.cms.cocoon.acting;
22:
23: import java.util.Collections;
24: import java.util.Map;
25:
26: import org.apache.avalon.framework.parameters.Parameters;
27: import org.apache.avalon.framework.thread.ThreadSafe;
28: import org.apache.cocoon.acting.AbstractAction;
29: import org.apache.cocoon.environment.Redirector;
30: import org.apache.cocoon.environment.SourceResolver;
31: import org.apache.excalibur.source.Source;
32: import org.apache.excalibur.source.TraversableSource;
33:
34: /**
35: * This action simply checks to see if a given resource exists. It checks
36: * whether the specified in the src attribute source exists or not.
37: * The action returns empty <code>Map</code> if it exists, null otherwise.
38: * <p>Instead of src attribute, source can be specified using
39: * parameter named 'url' (this is old syntax).
40: * <p>In order to differentiate between files and directories, the type can be specified
41: * using the parameter 'type' (<map:parameter name="type" value="file"/> or
42: * <map:parameter name="type" value="directory"/>). The parameter 'type' is optional.
43: * <p>
44: * <strong>Note:</strong> {@link org.apache.cocoon.selection.ResourceExistsSelector}
45: * should be preferred to this component, as the semantics of a Selector better
46: * match the supplied functionality.
47: */
48: public class ResourceExistsAction extends AbstractAction implements
49: ThreadSafe {
50: /**
51: * @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)
52: */
53: public Map act(Redirector redirector, SourceResolver resolver,
54: Map objectModel, String source, Parameters parameters)
55: throws Exception {
56: String url = parameters.getParameter("url", source);
57: String type = parameters.getParameter("type", "resource");
58: Source src = null;
59:
60: try {
61: src = resolver.resolveURI(url);
62:
63: if (src.exists()) {
64:
65: boolean isCollection = false;
66: if (src instanceof TraversableSource) {
67: TraversableSource traversableSource = (TraversableSource) src;
68: isCollection = traversableSource.isCollection();
69: }
70:
71: boolean exists = type.equals("resource")
72: || type.equals("file") && !isCollection
73: || type.equals("directory") && isCollection;
74:
75: if (exists) {
76: getLogger()
77: .debug(type + " exists: " + src.getURI());
78: return Collections.EMPTY_MAP;
79: }
80: }
81: getLogger().debug(
82: ".act(): Resource " + source + " as type \"" + type
83: + "\" does not exist");
84: } finally {
85: if (src != null) {
86: resolver.release(src);
87: }
88: }
89:
90: return null;
91: }
92: }
|