1 package org.apache.velocity.tools.view.servlet;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.velocity.tools.view.ViewToolInfo;
23
24 /**
25 * <p>ToolInfo implementation that holds scope information for tools
26 * used in a servlet environment. The ServletToolboxManager uses
27 * this to allow tool definitions to specify the scope/lifecycle
28 * of individual view tools.</p>
29 *
30 * <p>Example of toolbox.xml definitions for servlet tools:<pre>
31 * <tool>
32 * <key>link</key>
33 * <scope>request</scope>
34 * <class>org.apache.velocity.tools.struts.StrutsLinkTool</class>
35 * </tool>
36 * <tool>
37 * <key>math</key>
38 * <scope>application</scope>
39 * <class>org.apache.velocity.tools.generic.MathTool</class>
40 * </tool>
41 * <tool>
42 * <key>user</key>
43 * <scope>session</scope>
44 * <class>com.mycompany.tools.MyUserTool</class>
45 * </tool>
46 * </pre></p>
47 *
48 * @author Nathan Bubna
49 * @deprecated Use {@link org.apache.velocity.tools.ToolInfo}
50 * @version $Id: ServletToolInfo.java 564438 2007-08-10 00:15:18Z nbubna $
51 */
52 @Deprecated
53 public class ServletToolInfo extends ViewToolInfo
54 {
55
56 private String scope;
57 private boolean exactPath;
58 private String path;
59
60
61 public void setScope(String scope) {
62 this.scope = scope;
63 }
64
65 /**
66 * @return the scope of the tool
67 */
68 public String getScope()
69 {
70 return scope;
71 }
72
73 /**
74 * @param path the full or partial request path restriction of the tool
75 * @since VelocityTools 1.3
76 */
77 public void setRequestPath(String path)
78 {
79 // make sure all paths begin with slash
80 if (!path.startsWith("/"))
81 {
82 path = "/" + path;
83 }
84
85 if (path.equals("/*"))
86 {
87 // match all paths
88 this.path = null;
89 }
90 else if(path.endsWith("*"))
91 {
92 // match some paths
93 exactPath = false;
94 this.path = path.substring(0, path.length() - 1);
95 }
96 else
97 {
98 // match one path
99 exactPath = true;
100 this.path = path;
101 }
102 }
103
104 /**
105 * @return request path restriction for this tool
106 * @since VelocityTools 1.3
107 */
108 public String getRequestPath()
109 {
110 return this.path;
111 }
112
113 /**
114 * @param requestedPath the path of the current servlet request
115 * @return <code>true</code> if the path of the specified
116 * request path matches the request path of this tool.
117 * If there is no request path restriction for this tool,
118 * it will always return <code>true</code>.
119 * @since VelocityTools 1.3
120 */
121 public boolean allowsRequestPath(String requestedPath)
122 {
123 if (this.path == null)
124 {
125 return true;
126 }
127
128 if (exactPath)
129 {
130 return this.path.equals(requestedPath);
131 }
132 else if (requestedPath != null)
133 {
134 return requestedPath.startsWith(this.path);
135 }
136 return false;
137 }
138
139 }