All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.matomo.java.tracking.servlet.JakartaHttpServletWrapper Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
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();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy