/
PathMappings.java
305 lines (272 loc) · 9.96 KB
/
PathMappings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.http.pathmap;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Predicate;
import org.eclipse.jetty.util.ArrayTernaryTrie;
import org.eclipse.jetty.util.Trie;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.annotation.ManagedObject;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
* Path Mappings of PathSpec to Resource.
* <p>
* Sorted into search order upon entry into the Set
*
* @param <E> the type of mapping endpoint
*/
@ManagedObject("Path Mappings")
public class PathMappings<E> implements Iterable<MappedResource<E>>, Dumpable
{
private static final Logger LOG = Log.getLogger(PathMappings.class);
private final Set<MappedResource<E>> _mappings = new TreeSet<>(Comparator.comparing(MappedResource::getPathSpec));
private Trie<MappedResource<E>> _exactMap = new ArrayTernaryTrie<>(false);
private Trie<MappedResource<E>> _prefixMap = new ArrayTernaryTrie<>(false);
private Trie<MappedResource<E>> _suffixMap = new ArrayTernaryTrie<>(false);
@Override
public String dump()
{
return Dumpable.dump(this);
}
@Override
public void dump(Appendable out, String indent) throws IOException
{
Dumpable.dumpObjects(out, indent, toString(), _mappings);
}
@ManagedAttribute(value = "mappings", readonly = true)
public List<MappedResource<E>> getMappings()
{
return new ArrayList<>(_mappings);
}
public int size()
{
return _mappings.size();
}
public void reset()
{
_mappings.clear();
_prefixMap.clear();
_suffixMap.clear();
}
public void removeIf(Predicate<MappedResource<E>> predicate)
{
_mappings.removeIf(predicate);
}
/**
* Return a list of MappedResource matches for the specified path.
*
* @param path the path to return matches on
* @return the list of mapped resource the path matches on
*/
public List<MappedResource<E>> getMatches(String path)
{
boolean isRootPath = "/".equals(path);
List<MappedResource<E>> ret = new ArrayList<>();
for (MappedResource<E> mr : _mappings)
{
switch (mr.getPathSpec().getGroup())
{
case ROOT:
if (isRootPath)
ret.add(mr);
break;
case DEFAULT:
if (isRootPath || mr.getPathSpec().matches(path))
ret.add(mr);
break;
default:
if (mr.getPathSpec().matches(path))
ret.add(mr);
break;
}
}
return ret;
}
public MappedResource<E> getMatch(String path)
{
PathSpecGroup lastGroup = null;
// Search all the mappings
for (MappedResource<E> mr : _mappings)
{
PathSpecGroup group = mr.getPathSpec().getGroup();
if (group != lastGroup)
{
// New group in list, so let's look for an optimization
switch (group)
{
case EXACT:
{
int i = path.length();
final Trie<MappedResource<E>> exact_map = _exactMap;
while (i >= 0)
{
MappedResource<E> candidate = exact_map.getBest(path, 0, i);
if (candidate == null)
break;
if (candidate.getPathSpec().matches(path))
return candidate;
i = candidate.getPathSpec().getPrefix().length() - 1;
}
break;
}
case PREFIX_GLOB:
{
int i = path.length();
final Trie<MappedResource<E>> prefix_map = _prefixMap;
while (i >= 0)
{
MappedResource<E> candidate = prefix_map.getBest(path, 0, i);
if (candidate == null)
break;
if (candidate.getPathSpec().matches(path))
return candidate;
i = candidate.getPathSpec().getPrefix().length() - 1;
}
break;
}
case SUFFIX_GLOB:
{
int i = 0;
final Trie<MappedResource<E>> suffix_map = _suffixMap;
while ((i = path.indexOf('.', i + 1)) > 0)
{
MappedResource<E> candidate = suffix_map.get(path, i + 1, path.length() - i - 1);
if (candidate != null && candidate.getPathSpec().matches(path))
return candidate;
}
break;
}
default:
}
}
if (mr.getPathSpec().matches(path))
return mr;
lastGroup = group;
}
return null;
}
@Override
public Iterator<MappedResource<E>> iterator()
{
return _mappings.iterator();
}
public static PathSpec asPathSpec(String pathSpecString)
{
if (pathSpecString == null)
throw new RuntimeException("Path Spec String must start with '^', '/', or '*.': got [" + pathSpecString + "]");
if (pathSpecString.length() == 0)
return new ServletPathSpec("");
return pathSpecString.charAt(0) == '^' ? new RegexPathSpec(pathSpecString) : new ServletPathSpec(pathSpecString);
}
public E get(PathSpec spec)
{
Optional<E> optionalResource = _mappings.stream()
.filter(mappedResource -> mappedResource.getPathSpec().equals(spec))
.map(mappedResource -> mappedResource.getResource())
.findFirst();
if (!optionalResource.isPresent())
return null;
return optionalResource.get();
}
public boolean put(String pathSpecString, E resource)
{
return put(asPathSpec(pathSpecString), resource);
}
public boolean put(PathSpec pathSpec, E resource)
{
MappedResource<E> entry = new MappedResource<>(pathSpec, resource);
switch (pathSpec.getGroup())
{
case EXACT:
String exact = pathSpec.getPrefix();
while (exact != null && !_exactMap.put(exact, entry))
{
_exactMap = new ArrayTernaryTrie<>((ArrayTernaryTrie<MappedResource<E>>)_exactMap, 1.5);
}
break;
case PREFIX_GLOB:
String prefix = pathSpec.getPrefix();
while (prefix != null && !_prefixMap.put(prefix, entry))
{
_prefixMap = new ArrayTernaryTrie<>((ArrayTernaryTrie<MappedResource<E>>)_prefixMap, 1.5);
}
break;
case SUFFIX_GLOB:
String suffix = pathSpec.getSuffix();
while (suffix != null && !_suffixMap.put(suffix, entry))
{
_suffixMap = new ArrayTernaryTrie<>((ArrayTernaryTrie<MappedResource<E>>)_prefixMap, 1.5);
}
break;
default:
}
boolean added = _mappings.add(entry);
if (LOG.isDebugEnabled())
LOG.debug("{} {} to {}", added ? "Added" : "Ignored", entry, this);
return added;
}
@SuppressWarnings("incomplete-switch")
public boolean remove(PathSpec pathSpec)
{
String prefix = pathSpec.getPrefix();
String suffix = pathSpec.getSuffix();
switch (pathSpec.getGroup())
{
case EXACT:
if (prefix != null)
_exactMap.remove(prefix);
break;
case PREFIX_GLOB:
if (prefix != null)
_prefixMap.remove(prefix);
break;
case SUFFIX_GLOB:
if (suffix != null)
_suffixMap.remove(suffix);
break;
}
Iterator<MappedResource<E>> iter = _mappings.iterator();
boolean removed = false;
while (iter.hasNext())
{
if (iter.next().getPathSpec().equals(pathSpec))
{
removed = true;
iter.remove();
break;
}
}
if (LOG.isDebugEnabled())
LOG.debug("{} {} to {}", removed ? "Removed" : "Ignored", pathSpec, this);
return removed;
}
@Override
public String toString()
{
return String.format("%s[size=%d]", this.getClass().getSimpleName(), _mappings.size());
}
}