1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import java.util.Collections;
22 import java.util.Map;
23 import java.util.TreeMap;
24
25
26
27
28
29
30
31
32
33
34
35
36 public class PrefixTemplateUriIndex implements UriIndex
37 {
38
39
40 private Map<String, IndexEntry> index = new TreeMap<String, IndexEntry>(Collections.reverseOrder());
41
42
43
44
45 public void clear()
46 {
47 index.clear();
48 }
49
50
51
52
53 public int getSize()
54 {
55 return index.size();
56 }
57
58
59
60
61 public void registerUri(WebScript script, String uri)
62 {
63 Description desc = script.getDescription();
64
65
66 boolean wildcard = false;
67 boolean extension = true;
68 int queryArgIdx = uri.indexOf('?');
69 if (queryArgIdx != -1)
70 {
71 uri = uri.substring(0, queryArgIdx);
72 }
73 int tokenIdx = uri.indexOf('{');
74 if (tokenIdx != -1)
75 {
76 uri = uri.substring(0, tokenIdx);
77 wildcard = true;
78 }
79 if (desc.getFormatStyle() != Description.FormatStyle.argument)
80 {
81 int extIdx = uri.lastIndexOf(".");
82 if (extIdx != -1)
83 {
84 uri = uri.substring(0, extIdx);
85 }
86 extension = false;
87 }
88
89
90 String uriIdx = desc.getMethod() + ":" + uri;
91 if (index.containsKey(uriIdx))
92 {
93 IndexEntry urlIndex = index.get(uriIdx);
94 WebScript existingService = urlIndex.script;
95 if (!existingService.getDescription().getId().equals(desc.getId()))
96 {
97 String msg = "Web Script document " + desc.getDescPath() + " is attempting to define the url '" + uriIdx + "' already defined by " + existingService.getDescription().getDescPath();
98 throw new WebScriptException(msg);
99 }
100 }
101 else
102 {
103 IndexEntry urlIndex = new IndexEntry(uri, wildcard, extension, script);
104 index.put(uriIdx, urlIndex);
105 }
106 }
107
108
109
110
111 public Match findWebScript(String method, String uri)
112 {
113 String matchedPath = null;
114 Match scriptMatch = null;
115 String match = method.toUpperCase() + ":" + uri;
116 String matchNoExt = method.toUpperCase() + ":" + ((uri.indexOf('.') != -1) ? uri.substring(0, uri.indexOf('.')) : uri);
117
118
119 for (Map.Entry<String, IndexEntry> entry : index.entrySet())
120 {
121 IndexEntry urlIndex = entry.getValue();
122 String index = entry.getKey();
123 String test = urlIndex.includeExtension ? match : matchNoExt;
124 if ((urlIndex.wildcardPath && test.startsWith(index)) || (!urlIndex.wildcardPath && test.equals(index)))
125 {
126 scriptMatch = new Match(urlIndex.path, null, urlIndex.path, urlIndex.script);
127 break;
128 }
129 else if ((urlIndex.wildcardPath && uri.startsWith(urlIndex.path)) || (!urlIndex.wildcardPath && uri.equals(urlIndex.path)))
130 {
131 matchedPath = urlIndex.path;
132 }
133 }
134
135
136 if (scriptMatch == null && matchedPath != null)
137 {
138 scriptMatch = new Match(matchedPath, null, matchedPath);
139 }
140
141 return scriptMatch;
142 }
143
144
145
146
147 private static class IndexEntry
148 {
149 private IndexEntry(String path, boolean wildcardPath, boolean includeExtension, WebScript script)
150 {
151 this.path = path;
152 this.wildcardPath = wildcardPath;
153 this.includeExtension = includeExtension;
154 this.script = script;
155 }
156
157 private String path;
158 private boolean wildcardPath;
159 private boolean includeExtension;
160 private WebScript script;
161 }
162
163 }