/
MediaTypeMap.java
executable file
·433 lines (382 loc) · 14 KB
/
MediaTypeMap.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package org.jboss.resteasy.core;
import org.jboss.resteasy.util.MediaTypeHelper;
import javax.ws.rs.core.MediaType;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Efficient MediaType index of T.
*
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class MediaTypeMap<T>
{
public interface Typed
{
Class<?> getType();
}
private static class TypedEntryComparator implements Comparator<Entry<?>>, Serializable
{
private static final long serialVersionUID = -8815419198743440920L;
private Class<?> type;
TypedEntryComparator(final Class<?> type)
{
this.type = type;
}
private boolean isAssignableFrom(Typed typed)
{
if (typed.getType() == null) return false;
return typed.getType().isAssignableFrom(type);
}
private int compareTypes(Entry<?> entry, Entry<?> entry1)
{
int val = 0;
if (entry.object instanceof Typed && entry1.object instanceof Typed && type != null)
{
Typed one = (Typed) entry.object;
Typed two = (Typed) entry1.object;
boolean oneTyped = isAssignableFrom(one);
boolean twoTyped = isAssignableFrom(two);
if (oneTyped == twoTyped && (!oneTyped && !twoTyped))
{
// both are false
val = 0;
}
else if (oneTyped == twoTyped)
{
// both are true.
// test for better assignability
if (one.getType().equals(two.getType()))
{
val = 0;
}
else if (one.getType().isAssignableFrom(two.getType()))
{
val = 1;
}
else
{
val = -1;
}
}
else if (oneTyped) val = -1;
else val = 1;
}
return val;
}
public int compare(Entry<?> entry, Entry<?> entry1)
{
int val = compareTypes(entry, entry1);
if (val == 0) val = entry.compareTo(entry1);
return val;
}
}
private static class Entry<T> implements Comparable<Entry<?>>
{
public MediaType mediaType;
public T object;
private Entry(final MediaType mediaType, final T object)
{
this.mediaType = mediaType;
this.object = object;
}
@SuppressWarnings({"rawtypes", "unchecked"})
public int compareTo(Entry<?> entry)
{
int val = MediaTypeHelper.compareWeight(mediaType, entry.mediaType);
if (val == 0 && object instanceof Comparable && entry.object instanceof Comparable)
{
return ((Comparable) object).compareTo(entry.object);
}
return val;
}
}
private static final Pattern COMPOSITE_PATTERN = Pattern.compile("([^\\+]+)\\+(.+)");
// Composite subtypes are of the pattern *+subtype i.e. *+xml, *+json
public static final Pattern COMPOSITE_SUBTYPE_WILDCARD_PATTERN = Pattern.compile("\\*\\+(.+)");
// This composite is subtype+* i.e. atom+* rss+*
public static final Pattern WILD_SUBTYPE_COMPOSITE_PATTERN = Pattern.compile("([^\\+]+)\\+\\*");
private static class SubtypeMap<T>
{
private Map<String, List<Entry<T>>> index = new ConcurrentHashMap<String, java.util.List<Entry<T>>>();
private Map<String, List<Entry<T>>> compositeIndex = new ConcurrentHashMap<String, List<Entry<T>>>();
private Map<String, List<Entry<T>>> wildCompositeIndex = new ConcurrentHashMap<String, List<Entry<T>>>();
private List<Entry<T>> wildcards = new CopyOnWriteArrayList<Entry<T>>();
private List<Entry<T>> all = new CopyOnWriteArrayList<Entry<T>>();
public SubtypeMap<T> clone()
{
SubtypeMap<T> clone = new SubtypeMap<T>();
for (Map.Entry<String, List<Entry<T>>> entry : index.entrySet())
{
List<Entry<T>> newList = new CopyOnWriteArrayList<Entry<T>>();
newList.addAll(entry.getValue());
clone.index.put(entry.getKey(), newList);
}
for (Map.Entry<String, List<Entry<T>>> entry : compositeIndex.entrySet())
{
List<Entry<T>> newList = new CopyOnWriteArrayList<Entry<T>>();
newList.addAll(entry.getValue());
clone.compositeIndex.put(entry.getKey(), newList);
}
for (Map.Entry<String, List<Entry<T>>> entry : wildCompositeIndex.entrySet())
{
List<Entry<T>> newList = new CopyOnWriteArrayList<Entry<T>>();
newList.addAll(entry.getValue());
clone.wildCompositeIndex.put(entry.getKey(), newList);
}
clone.wildcards.addAll(wildcards);
clone.all.addAll(all);
return clone;
}
public void add(MediaType type, T obj)
{
Entry<T> entry = new Entry<T>(type, obj);
all.add(entry);
Matcher matcher = COMPOSITE_SUBTYPE_WILDCARD_PATTERN.matcher(type.getSubtype());
Matcher wildCompositeMatcher = WILD_SUBTYPE_COMPOSITE_PATTERN.matcher(type.getSubtype());
if (type.isWildcardSubtype()) wildcards.add(entry);
else if (matcher.matches())
{
String main = matcher.group(1);
List<Entry<T>> list = compositeIndex.get(main);
if (list == null)
{
list = new CopyOnWriteArrayList<Entry<T>>();
compositeIndex.put(main, list);
}
list.add(entry);
}
else if (wildCompositeMatcher.matches())
{
String main = wildCompositeMatcher.group(1);
List<Entry<T>> list = wildCompositeIndex.get(main);
if (list == null)
{
list = new CopyOnWriteArrayList<Entry<T>>();
wildCompositeIndex.put(main, list);
}
list.add(entry);
}
else
{
List<Entry<T>> list = index.get(type.getSubtype());
if (list == null)
{
list = new CopyOnWriteArrayList<Entry<T>>();
index.put(type.getSubtype(), list);
}
list.add(entry);
}
}
public List<Entry<T>> getPossible(MediaType accept)
{
if (accept.isWildcardSubtype())
{
return all;
}
else
{
List<Entry<T>> matches = new ArrayList<Entry<T>>();
List<Entry<T>> indexed = index.get(accept.getSubtype());
if (indexed != null) matches.addAll(indexed);
Matcher matcher = COMPOSITE_PATTERN.matcher(accept.getSubtype());
String compositeKey = accept.getSubtype();
if (matcher.matches())
{
String wildCompositeKey = matcher.group(1);
List<Entry<T>> windex = wildCompositeIndex.get(wildCompositeKey);
if (windex != null) matches.addAll(windex);
compositeKey = matcher.group(2);
}
List<Entry<T>> indexed2 = compositeIndex.get(compositeKey);
if (indexed2 != null) matches.addAll(indexed2);
matches.addAll(wildcards);
return matches;
}
}
}
private Map<String, SubtypeMap<T>> index = new ConcurrentHashMap<String, MediaTypeMap.SubtypeMap<T>>();
private volatile List<Entry<T>> wildcards = new ArrayList<Entry<T>>();
private volatile List<Entry<T>> all = new ArrayList<Entry<T>>();
private volatile List<T> everything = new ArrayList<T>();
private Map<CachedMediaTypeAndClass, List<T>> classCache = new ConcurrentHashMap<CachedMediaTypeAndClass, List<T>>();
public MediaTypeMap<T> clone()
{
MediaTypeMap<T> clone = new MediaTypeMap<T>();
for (Map.Entry<String, SubtypeMap<T>> entry : index.entrySet())
{
clone.index.put(entry.getKey(), entry.getValue().clone());
}
clone.wildcards.addAll(wildcards);
clone.all.addAll(all);
clone.everything.addAll(everything);
// don't clone class cache
return clone;
}
private static class CachedMediaTypeAndClass
{
// we need a weak reference because of possible hot deployment
// Although, these reference should get cleared up with any add() invocation
private WeakReference<Class<?>> clazz;
private MediaType mediaType;
private final int hash;
@SuppressWarnings({"rawtypes", "unchecked"})
private CachedMediaTypeAndClass(final Class clazz, final MediaType mediaType)
{
this.clazz = new WeakReference(clazz);
this.mediaType = mediaType;
int result = clazz.hashCode();
result = 31 * result + (mediaType.getType() != null ? mediaType.getType().hashCode() : 0) + (mediaType.getSubtype() != null ? mediaType.getSubtype().hashCode() : 0);
hash = result;
}
private Class<?> getClazz()
{
return clazz.get();
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CachedMediaTypeAndClass that = (CachedMediaTypeAndClass) o;
// WeakReference may have GC'd
Class<?> clazz = getClazz();
if (clazz == null || that.getClazz() == null) return false;
if (!clazz.equals(that.getClazz())) return false;
if (mediaType.getType() != null)
{
if (!mediaType.getType().equals(that.mediaType.getType())) return false;
}
else if ((mediaType.getType() != that.mediaType.getType())) return false;
if (mediaType.getSubtype() != null)
{
if (!mediaType.getSubtype().equals(that.mediaType.getSubtype())) return false;
}
else if ((mediaType.getSubtype() != that.mediaType.getSubtype())) return false;
return true;
}
@Override
public int hashCode()
{
return hash;
}
}
/**
* Add an object to the media type map. This is synchronized to serialize adds.
*
* @param type media type
* @param obj object
*/
public synchronized void add(MediaType type, T obj)
{
classCache.clear();
type = new MediaType(type.getType().toLowerCase(), type.getSubtype().toLowerCase(), type.getParameters());
Entry<T> entry = new Entry<T>(type, obj);
List<Entry<T>> newall = new ArrayList<Entry<T>>(all.size() + 1);
newall.addAll(all);
newall.add(entry);
Collections.sort(newall);
all = newall;
everything = convert(newall);
if (type.isWildcardType())
{
List<Entry<T>> newwildcards = new ArrayList<Entry<T>>(wildcards.size() + 1);
newwildcards.addAll(wildcards);
newwildcards.add(entry);
wildcards = newwildcards;
}
else
{
SubtypeMap<T> subtype = index.get(type.getType());
if (subtype == null)
{
subtype = new SubtypeMap<T>();
index.put(type.getType(), subtype);
}
subtype.add(type, obj);
}
}
private static <T> List<T> convert(List<Entry<T>> list)
{
List<T> newList = new ArrayList<T>(list.size());
for (Entry<T> entry : list)
{
newList.add(entry.object);
}
return newList;
}
/**
* Returns a list of objects sorted based on their media type where the first in the list
* is the best match
*
* @param accept mime to match
* @return list of objects
*/
public List<T> getPossible(MediaType accept)
{
accept = new MediaType(accept.getType().toLowerCase(), accept.getSubtype().toLowerCase(), accept.getParameters());
List<Entry<T>> matches = new ArrayList<Entry<T>>();
if (accept.isWildcardType())
{
ArrayList<T> copy = new ArrayList<T>();
copy.addAll(everything);
return copy;
}
else
{
matches.addAll(wildcards);
SubtypeMap<T> indexed = index.get(accept.getType());
if (indexed != null)
{
matches.addAll(indexed.getPossible(accept));
}
}
Collections.sort(matches);
return convert(matches);
}
/**
* By default, MediaTypeMap will cache possible MediaType/Class matches. Set this to false to turn off
* caching
*
*/
public static boolean useCache = true;
public List<T> getPossible(MediaType accept, Class<?> type)
{
List<T> cached = null;
CachedMediaTypeAndClass cacheEntry = null;
if (useCache)
{
cacheEntry = new CachedMediaTypeAndClass(type, accept);
cached = classCache.get(cacheEntry);
if (cached != null) return cached;
}
accept = new MediaType(accept.getType().toLowerCase(), accept.getSubtype().toLowerCase(), accept.getParameters());
List<Entry<T>> matches = new ArrayList<Entry<T>>();
if (accept.isWildcardType())
{
matches.addAll(all);
}
else
{
matches.addAll(wildcards);
SubtypeMap<T> indexed = index.get(accept.getType());
if (indexed != null)
{
matches.addAll(indexed.getPossible(accept));
}
}
Collections.sort(matches, new TypedEntryComparator(type));
cached = convert(matches);
if (useCache) classCache.put(cacheEntry, cached);
return cached;
}
}