-
Notifications
You must be signed in to change notification settings - Fork 888
/
AcceptHeaderByFileSuffixFilter.java
158 lines (140 loc) · 4.79 KB
/
AcceptHeaderByFileSuffixFilter.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
package org.jboss.resteasy.core;
import org.jboss.resteasy.specimpl.PathSegmentImpl;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Modifies Accept and Accept-Language headers by looking at path file suffix i.e. .xml means Accept application/xml
*/
@Provider
@PreMatching
public class AcceptHeaderByFileSuffixFilter implements ContainerRequestFilter
{
private final Map<String, String> mediaTypeMappings = new HashMap<String, String>();
private final Map<String, String> languageMappings = new HashMap<String, String>();
public void setMediaTypeMappings(Map<String, MediaType> mediaTypeMappings)
{
this.mediaTypeMappings.clear();
for (Map.Entry<String, MediaType> entry : mediaTypeMappings.entrySet())
{
this.mediaTypeMappings.put(entry.getKey(), entry.getValue().toString());
}
}
public void setLanguageMappings(Map<String, String> languageMappings)
{
this.languageMappings.clear();
for (Map.Entry<String, String> entry : languageMappings.entrySet())
{
this.languageMappings.put(entry.getKey(), entry.getValue());
}
}
@Override
public void filter(ContainerRequestContext requestContext) throws IOException
{
if ((mediaTypeMappings == null || mediaTypeMappings.isEmpty()) && (languageMappings == null || languageMappings.isEmpty()))
{
return;
}
URI uri = requestContext.getUriInfo().getRequestUri();
String rawPath = uri.getRawPath();
int lastSegment = rawPath.lastIndexOf('/');
if (lastSegment < 0)
{
lastSegment = 0;
}
int index = rawPath.indexOf('.', lastSegment);
if (index < 0)
{
return;
}
boolean preprocessed = false;
String extension = rawPath.substring(index + 1);
String[] extensions = extension.split("\\.");
StringBuilder rebuilt = new StringBuilder();
for (String ext : extensions)
{
if (mediaTypeMappings != null)
{
String match = mediaTypeMappings.get(ext);
if (match != null)
{
requestContext.getHeaders().addFirst(HttpHeaders.ACCEPT, match);
preprocessed = true;
continue;
}
}
if (languageMappings != null)
{
String match = languageMappings.get(ext);
if (match != null)
{
requestContext.getHeaders().add(HttpHeaders.ACCEPT_LANGUAGE, match);
preprocessed = true;
continue;
}
}
rebuilt.append(".").append(ext);
}
if (!preprocessed) return;
rawPath = rawPath.substring(0, index) + rebuilt.toString();
URI newUri = requestContext.getUriInfo().getBaseUriBuilder().replacePath(rawPath).replaceQuery(uri.getRawQuery()).build();
requestContext.setRequestUri(newUri);
}
private List<PathSegment> process(ContainerRequestContext in)
{
String path = in.getUriInfo().getPath(false);
int lastSegment = path.lastIndexOf('/');
if (lastSegment < 0)
{
lastSegment = 0;
}
int index = path.indexOf('.', lastSegment);
if (index < 0)
{
return null;
}
boolean preprocessed = false;
String extension = path.substring(index + 1);
String[] extensions = extension.split("\\.");
StringBuilder rebuilt = new StringBuilder(path.substring(0, index));
for (String ext : extensions)
{
if (mediaTypeMappings != null)
{
String match = mediaTypeMappings.get(ext);
if (match != null)
{
in.getHeaders().addFirst(HttpHeaders.ACCEPT, match);
preprocessed = true;
continue;
}
}
if (languageMappings != null)
{
String match = languageMappings.get(ext);
if (match != null)
{
in.getHeaders().add(HttpHeaders.ACCEPT_LANGUAGE, match);
preprocessed = true;
continue;
}
}
rebuilt.append(".").append(ext);
}
List<PathSegment> segments = null;
if (preprocessed)
{
segments = PathSegmentImpl.parseSegments(rebuilt.toString(), false);
}
return segments;
}
}