}
@Override
public void filter(ContainerRequestContext rc) throws IOException {
UriInfo uriInfo = rc.getUriInfo();
// Quick check for a '.' character
String path = uriInfo.getRequestUri().getRawPath();
if (path.indexOf('.') == -1) {
return;
}
List<PathSegment> l = uriInfo.getPathSegments(false);
if (l.isEmpty()) {
return;
}
// Get the last non-empty path segment
PathSegment segment = null;
for (int i = l.size() - 1; i >= 0; i--) {
segment = l.get(i);
if (segment.getPath().length() > 0) {
break;
}
}
if (segment == null) {
return;
}
final int length = path.length();
// Get the suffixes
final String[] suffixes = segment.getPath().split("\\.");
for (int i = suffixes.length - 1; i >= 1; i--) {
final String suffix = suffixes[i];
if (suffix.length() == 0) {
continue;
}
final MediaType accept = mediaTypeMappings.get(suffix);
if (accept != null) {
rc.getHeaders().putSingle(HttpHeaders.ACCEPT, accept.toString());
final int index = path.lastIndexOf('.' + suffix);
path = new StringBuilder(path).delete(index, index + suffix.length() + 1).toString();
suffixes[i] = "";
break;
}
}
for (int i = suffixes.length - 1; i >= 1; i--) {
final String suffix = suffixes[i];
if (suffix.length() == 0)
continue;
final String acceptLanguage = languageMappings.get(suffix);
if (acceptLanguage != null) {
rc.getHeaders().putSingle(HttpHeaders.ACCEPT_LANGUAGE, acceptLanguage);
final int index = path.lastIndexOf('.' + suffix);
path = new StringBuilder(path).delete(index, index + suffix.length() + 1).toString();
suffixes[i] = "";
break;
}
}
if (length != path.length()) {
rc.setRequestUri(uriInfo.getRequestUriBuilder().replacePath(path).build());
}
}