io.quarkus.resteasy.runtime.standalone.VertxUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-resteasy Show documentation
Show all versions of quarkus-resteasy Show documentation
REST endpoint framework implementing JAX-RS and more
package io.quarkus.resteasy.runtime.standalone;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import jakarta.ws.rs.core.Cookie;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import org.jboss.resteasy.core.Headers;
import org.jboss.resteasy.specimpl.ResteasyHttpHeaders;
import org.jboss.resteasy.specimpl.ResteasyUriInfo;
import org.jboss.resteasy.util.CookieParser;
import org.jboss.resteasy.util.HttpHeaderNames;
import org.jboss.resteasy.util.MediaTypeHelper;
import io.vertx.core.http.HttpServerRequest;
/**
* @author Bill Burke
* @version $Revision: 1 $
*/
public class VertxUtil {
private static final Pattern COMMA_PATTERN = Pattern.compile(",");
public static ResteasyUriInfo extractUriInfo(HttpServerRequest req, String contextPath) {
String uri = req.absoluteURI();
String protocol = req.scheme();
String uriString;
// If we appear to have an absolute URL, don't try to recreate it from the host and request line.
if (uri.startsWith(protocol + "://")) {
uriString = uri;
} else {
var authority = req.authority();
if (authority == null) {
uriString = protocol + "//unknown" + uri;
} else {
uriString = protocol + "://" + authority + uri;
}
}
// ResteasyUriInfo expects a context path to start with a "/" character
if (!contextPath.startsWith("/")) {
contextPath = "/" + contextPath;
}
return new ResteasyUriInfo(uriString, contextPath);
}
public static ResteasyHttpHeaders extractHttpHeaders(HttpServerRequest request) {
MultivaluedMap requestHeaders = extractRequestHeaders(request);
ResteasyHttpHeaders headers = new ResteasyHttpHeaders(requestHeaders);
Map cookies = extractCookies(requestHeaders);
headers.setCookies(cookies);
return headers;
}
static Map extractCookies(MultivaluedMap headers) {
Map cookies = new HashMap();
List cookieHeaders = headers.get("Cookie");
if (cookieHeaders == null)
return cookies;
for (String cookieHeader : cookieHeaders) {
for (Cookie cookie : CookieParser.parseCookies(cookieHeader)) {
cookies.put(cookie.getName(), cookie);
}
}
return cookies;
}
public static List extractAccepts(MultivaluedMap requestHeaders) {
List acceptableMediaTypes = new ArrayList();
List accepts = requestHeaders.get(HttpHeaderNames.ACCEPT);
if (accepts == null)
return acceptableMediaTypes;
for (String accept : accepts) {
acceptableMediaTypes.addAll(MediaTypeHelper.parseHeader(accept));
}
return acceptableMediaTypes;
}
public static List extractLanguages(MultivaluedMap requestHeaders) {
List acceptable = new ArrayList();
List accepts = requestHeaders.get(HttpHeaderNames.ACCEPT_LANGUAGE);
if (accepts == null)
return acceptable;
for (String accept : accepts) {
String[] splits = COMMA_PATTERN.split(accept);
for (String split : splits)
acceptable.add(split.trim());
}
return acceptable;
}
public static MultivaluedMap extractRequestHeaders(HttpServerRequest request) {
Headers requestHeaders = new Headers();
for (Map.Entry header : request.headers()) {
requestHeaders.add(header.getKey(), header.getValue());
}
return requestHeaders;
}
}