KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > Mapping


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone;
8
9 /**
10  * Encapsulates the parsing of URL patterns, as well as the mapping of a
11  * url pattern to a servlet instance
12  *
13  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
14  * @version $Id: Mapping.java,v 1.8 2006/06/17 06:52:48 rickknowles Exp $
15  */

16 public class Mapping implements java.util.Comparator JavaDoc {
17     public static final int EXACT_PATTERN = 1;
18     public static final int FOLDER_PATTERN = 2;
19     public static final int EXTENSION_PATTERN = 3;
20     public static final int DEFAULT_SERVLET = 4;
21
22     public static final String JavaDoc STAR = "*";
23     public static final String JavaDoc SLASH = "/";
24
25     private String JavaDoc urlPattern;
26     private String JavaDoc linkName; // used to map filters to a specific servlet by
27
// name
28
private String JavaDoc mappedTo;
29     private int patternType;
30     private boolean isPatternFirst; // ie is this a blah* pattern, not *blah
31
// (extensions only)
32

33     protected Mapping(String JavaDoc mappedTo) {
34         this.mappedTo = mappedTo;
35     }
36
37     /**
38      * Factory constructor method - this parses the url pattern into pieces we can use to match
39      * against incoming URLs.
40      */

41     public static Mapping createFromURL(String JavaDoc mappedTo, String JavaDoc pattern) {
42         if ((pattern == null) || (mappedTo == null))
43             throw new WinstoneException(Launcher.RESOURCES.getString(
44                     "Mapping.InvalidMount", new String JavaDoc[] { mappedTo, pattern }));
45         
46         // Compatibility hacks - add a leading slash if one is not found and not
47
// an extension mapping
48
if (!pattern.equals("") && !pattern.startsWith(STAR) &&
49                 !pattern.startsWith(SLASH)) {
50             pattern = SLASH + pattern;
51         } else if (pattern.equals(STAR)) {
52             Logger.log(Logger.WARNING, Launcher.RESOURCES, "Mapping.RewritingStarMount");
53             pattern = SLASH + STAR;
54         }
55         
56         Mapping me = new Mapping(mappedTo);
57
58         int firstStarPos = pattern.indexOf(STAR);
59         int lastStarPos = pattern.lastIndexOf(STAR);
60         int patternLength = pattern.length();
61
62         // check for default servlet, ie mapping = exactly /
63
if (pattern.equals(SLASH)) {
64             me.urlPattern = "";
65             me.patternType = DEFAULT_SERVLET;
66         }
67
68         else if (firstStarPos == -1) {
69             me.urlPattern = pattern;
70             me.patternType = EXACT_PATTERN;
71         }
72
73         // > 1 star = error
74
else if (firstStarPos != lastStarPos)
75             throw new WinstoneException(Launcher.RESOURCES.getString(
76                     "Mapping.InvalidMount", new String JavaDoc[] { mappedTo, pattern }));
77
78         // check for folder style mapping (ends in /*)
79
else if (pattern.indexOf(SLASH + STAR) == (patternLength - (SLASH + STAR).length())) {
80             me.urlPattern = pattern.substring(0, pattern.length()
81                     - (SLASH + STAR).length());
82             me.patternType = FOLDER_PATTERN;
83         }
84         
85         // check for non-extension match
86
else if (pattern.indexOf(SLASH) != -1)
87             throw new WinstoneException(Launcher.RESOURCES.getString(
88                     "Mapping.InvalidMount", new String JavaDoc[] { mappedTo, pattern }));
89
90         // check for extension match at the beginning (eg *blah)
91
else if (firstStarPos == 0) {
92             me.urlPattern = pattern.substring(STAR.length());
93             me.patternType = EXTENSION_PATTERN;
94             me.isPatternFirst = false;
95         }
96         // check for extension match at the end (eg blah*)
97
else if (firstStarPos == (patternLength - STAR.length())) {
98             me.urlPattern = pattern.substring(0, patternLength - STAR.length());
99             me.patternType = EXTENSION_PATTERN;
100             me.isPatternFirst = true;
101         } else
102             throw new WinstoneException(Launcher.RESOURCES.getString(
103                     "Mapping.InvalidMount", new String JavaDoc[] { mappedTo, pattern }));
104
105         Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES, "Mapping.MappedPattern",
106                 new String JavaDoc[] { mappedTo, pattern });
107         return me;
108     }
109
110     /**
111      * Factory constructor method - this turns a servlet name into a mapping element
112      */

113     public static Mapping createFromLink(String JavaDoc mappedTo, String JavaDoc linkName) {
114         if ((linkName == null) || (mappedTo == null))
115             throw new WinstoneException(Launcher.RESOURCES.getString(
116                     "Mapping.InvalidLink", new String JavaDoc[] { mappedTo, linkName }));
117
118         Mapping me = new Mapping(mappedTo);
119         me.linkName = linkName;
120         return me;
121     }
122
123     public int getPatternType() {
124         return this.patternType;
125     }
126
127     public String JavaDoc getUrlPattern() {
128         return this.urlPattern;
129     }
130
131     public String JavaDoc getMappedTo() {
132         return this.mappedTo;
133     }
134
135     public String JavaDoc getLinkName() {
136         return this.linkName;
137     }
138
139     /**
140      * Try to match this pattern against the incoming url
141      *
142      * @param inputPattern The URL we want to check for a match
143      * @param servletPath An empty stringbuffer for the servletPath of a successful match
144      * @param pathInfo An empty stringbuffer for the pathInfo of a successful match
145      * @return true if the match is successful
146      */

147     public boolean match(String JavaDoc inputPattern, StringBuffer JavaDoc servletPath,
148             StringBuffer JavaDoc pathInfo) {
149         // Logger.log(Logger.FULL_DEBUG, "Matching input=" + inputPattern + "
150
// me=" + toString());
151
switch (this.patternType) {
152         case FOLDER_PATTERN:
153             if (inputPattern.startsWith(this.urlPattern + '/') ||
154                     inputPattern.equals(this.urlPattern)) {
155                 if (servletPath != null)
156                     servletPath.append(this.urlPattern);
157                 if (pathInfo != null)
158                     pathInfo.append(inputPattern.substring(this.urlPattern.length()));
159                 return true;
160             } else
161                 return false;
162
163         case EXTENSION_PATTERN:
164             // Strip down to the last item in the path
165
int slashPos = inputPattern.lastIndexOf(SLASH);
166             if ((slashPos == -1) || (slashPos == inputPattern.length() - 1))
167                 return false;
168             String JavaDoc fileName = inputPattern.substring(slashPos + 1);
169             if ((this.isPatternFirst && fileName.startsWith(this.urlPattern))
170                     || (!this.isPatternFirst && fileName.endsWith(this.urlPattern))) {
171                 if (servletPath != null)
172                     servletPath.append(inputPattern);
173                 return true;
174             } else
175                 return false;
176
177         case EXACT_PATTERN:
178             if (inputPattern.equals(this.urlPattern)) {
179                 if (servletPath != null)
180                     servletPath.append(inputPattern);
181                 return true;
182             } else
183                 return false;
184
185         case DEFAULT_SERVLET:
186             if (servletPath != null)
187                 servletPath.append(inputPattern);
188             return true;
189
190         default:
191             return false;
192         }
193     }
194
195     /**
196      * Used to compare two url patterns. Always sorts so that lowest pattern
197      * type then longest path come first.
198      */

199     public int compare(Object JavaDoc objOne, Object JavaDoc objTwo) {
200         Mapping one = (Mapping) objOne;
201         Mapping two = (Mapping) objTwo;
202
203         Integer JavaDoc intOne = new Integer JavaDoc(one.getPatternType());
204         Integer JavaDoc intTwo = new Integer JavaDoc(two.getPatternType());
205         int order = -1 * intOne.compareTo(intTwo);
206         if (order != 0) {
207             return order;
208         }
209         if (one.getLinkName() != null) {
210             // servlet name mapping - just alphabetical sort
211
return one.getLinkName().compareTo(two.getLinkName());
212         } else {
213             return -1 * one.getUrlPattern().compareTo(two.getUrlPattern());
214         }
215     }
216
217     public String JavaDoc toString() {
218         return this.linkName != null ? "Link:" + this.linkName
219                 : "URLPattern:type=" + this.patternType + ",pattern="
220                         + this.urlPattern;
221     }
222 }
223
Popular Tags