001: package org.apache.velocity.tools.view.servlet;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import javax.servlet.http.HttpServletRequest;
023: import org.apache.velocity.tools.view.ViewToolInfo;
024:
025: /**
026: * <p>ToolInfo implementation that holds scope information for tools
027: * used in a servlet environment. The ServletToolboxManager uses
028: * this to allow tool definitions to specify the scope/lifecycle
029: * of individual view tools.</p>
030: *
031: * <p>Example of toolbox.xml definitions for servlet tools:<pre>
032: * <tool>
033: * <key>link</key>
034: * <scope>request</scope>
035: * <class>org.apache.velocity.tools.struts.StrutsLinkTool</class>
036: * </tool>
037: * <tool>
038: * <key>math</key>
039: * <scope>application</scope>
040: * <class>org.apache.velocity.tools.generic.MathTool</class>
041: * </tool>
042: * <tool>
043: * <key>user</key>
044: * <scope>session</scope>
045: * <class>com.mycompany.tools.MyUserTool</class>
046: * </tool>
047: * </pre></p>
048: *
049: * @author Nathan Bubna
050: *
051: * @version $Id: ServletToolInfo.java 479724 2006-11-27 18:49:37Z nbubna $
052: */
053: public class ServletToolInfo extends ViewToolInfo {
054:
055: private String scope;
056: private boolean exactPath;
057: private String path;
058:
059: public void setScope(String scope) {
060: this .scope = scope;
061: }
062:
063: /**
064: * @return the scope of the tool
065: */
066: public String getScope() {
067: return scope;
068: }
069:
070: /**
071: * @param path the full or partial request path restriction of the tool
072: * @since VelocityTools 1.3
073: */
074: public void setRequestPath(String path) {
075: // make sure all paths begin with slash
076: if (!path.startsWith("/")) {
077: path = "/" + path;
078: }
079:
080: if (path.equals("/*")) {
081: // match all paths
082: this .path = null;
083: } else if (path.endsWith("*")) {
084: // match some paths
085: exactPath = false;
086: this .path = path.substring(0, scope.length() - 1);
087: } else {
088: // match one path
089: exactPath = true;
090: this .path = path;
091: }
092: }
093:
094: /**
095: * @return request path restriction for this tool
096: * @since VelocityTools 1.3
097: */
098: public String getRequestPath() {
099: return this .path;
100: }
101:
102: /**
103: * @param requestedPath the path of the current servlet request
104: * @return <code>true</code> if the path of the specified
105: * request path matches the request path of this tool.
106: * If there is no request path restriction for this tool,
107: * it will always return <code>true</code>.
108: * @since VelocityTools 1.3
109: */
110: public boolean allowsRequestPath(String requestedPath) {
111: if (this .path == null) {
112: return true;
113: }
114:
115: if (exactPath) {
116: return this .path.equals(requestedPath);
117: } else if (requestedPath != null) {
118: return requestedPath.startsWith(this .path);
119: }
120: return false;
121: }
122:
123: }
|