io.muserver.rest.JaxRsHttpHeadersAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mu-server Show documentation
Show all versions of mu-server Show documentation
A simple but powerful web server framework
package io.muserver.rest;
import io.muserver.HeaderNames;
import io.muserver.Headers;
import javax.ws.rs.core.*;
import java.util.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class JaxRsHttpHeadersAdapter implements HttpHeaders {
private static final List WILDCARD_LOCALES = Collections.unmodifiableList(Collections.singletonList(new Locale("*")));
private static final List WILDCARD_MEDIA_TYPES = Collections.unmodifiableList(Collections.singletonList(MediaType.WILDCARD_TYPE));
private final Headers muHeaders;
private final Set muCookies;
private MultivaluedMap copy;
JaxRsHttpHeadersAdapter(Headers headers, Set cookies) {
muHeaders = headers;
muCookies = cookies;
}
@Override
public List getRequestHeader(String name) {
return getRequestHeaders().get(name);
}
@Override
public String getHeaderString(String name) {
List vals = getRequestHeader(name);
if (vals == null) {
return null;
}
return vals.stream().collect(joining(","));
}
@Override
public MultivaluedMap getRequestHeaders() {
if (copy == null) {
MultivaluedMap c = new MultivaluedHashMap<>();
for (Map.Entry entry : muHeaders) {
c.add(entry.getKey(), entry.getValue());
}
copy = ReadOnlyMultivaluedMap.readOnly(c);
}
return copy;
}
@Override
public List getAcceptableMediaTypes() {
List mediaTypes = MediaTypeDeterminer.parseAcceptHeaders(muHeaders.getAll(HeaderNames.ACCEPT));
return mediaTypes.isEmpty() ? WILDCARD_MEDIA_TYPES : mediaTypes;
}
@Override
public List getAcceptableLanguages() {
return getLocalesFromHeader(HeaderNames.ACCEPT_LANGUAGE, WILDCARD_LOCALES);
}
private List getLocalesFromHeader(CharSequence headerName, List defaultLocales) {
List all = muHeaders.getAll(headerName);
if (all.isEmpty()) {
return defaultLocales;
}
return Locale.LanguageRange.parse(all.stream().collect(joining(","))).stream()
.map(lr -> {
String[] range = lr.getRange().split("-");
switch (range.length) {
case 3:
return new Locale(range[0], range[1], range[2]);
case 2:
return new Locale(range[0], range[1]);
case 1:
return new Locale(range[0]);
}
return null;
})
.filter(Objects::nonNull)
.collect(toList());
}
@Override
public MediaType getMediaType() {
String type = muHeaders.get(HeaderNames.CONTENT_TYPE);
return type == null ? null : MediaType.valueOf(type);
}
@Override
public Locale getLanguage() {
List localesFromHeader = getLocalesFromHeader(HeaderNames.CONTENT_LANGUAGE, null);
return localesFromHeader == null || localesFromHeader.isEmpty() ? null : localesFromHeader.get(0);
}
@Override
public Map getCookies() {
Map all = new HashMap<>();
for (io.muserver.Cookie cookie : muCookies) {
all.put(cookie.name(), new Cookie(cookie.name(), cookie.value()));
}
return all;
}
@Override
public Date getDate() {
long timeMillis = muHeaders.getTimeMillis(HeaderNames.DATE, -1);
return timeMillis < 0 ? null : new Date(timeMillis);
}
@Override
public int getLength() {
return muHeaders.getInt(HeaderNames.CONTENT_LENGTH, -1);
}
}