org.matomo.java.tracking.servlet.JakartaHttpServletWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of matomo-java-tracker-servlet-jakarta Show documentation
Show all versions of matomo-java-tracker-servlet-jakarta Show documentation
Integrates Matomo Java Tracker into your Jakarta servlet based application
package org.matomo.java.tracking.servlet;
import jakarta.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.NonNull;
/**
* Converts a Jakarta {@link HttpServletRequest} to a {@link HttpServletRequestWrapper}.
*/
public final class JakartaHttpServletWrapper {
private JakartaHttpServletWrapper() {
// utility
}
/**
* Takes a Jakarta {@link HttpServletRequest} and converts it to a
* {@link HttpServletRequestWrapper}.
*
* @param request the request to convert to a wrapper object (must not be {@code null}).
* @return the wrapper object (never {@code null}).
*/
@edu.umd.cs.findbugs.annotations.NonNull
public static HttpServletRequestWrapper fromHttpServletRequest(@NonNull HttpServletRequest request) {
Map headers = new LinkedHashMap<>();
request.getHeaderNames()
.asIterator()
.forEachRemaining(name -> headers.put(name.toLowerCase(Locale.ROOT), request.getHeader(name)));
List cookies = null;
if (request.getCookies() != null) {
cookies = Stream.of(request.getCookies())
.map(cookie -> new CookieWrapper(cookie.getName(), cookie.getValue()))
.collect(Collectors.toList());
}
return HttpServletRequestWrapper
.builder()
.requestURL(request.getRequestURL())
.remoteAddr(request.getRemoteAddr())
.remoteUser(request.getRemoteUser())
.headers(headers)
.cookies(cookies == null ? null : cookies.toArray(new CookieWrapper[0]))
.build();
}
}