package org.pac4j.jee.util;

import java.security.Principal;
import java.util.Collection;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.profile.ProfileHelper;

/**
 * This wraps the {@link HttpServletRequest} to make it pac4j-aware by with respect to user-related methods.
 * 
 * @author Victor Noel
 * @since 4.0.0
 *
 */
public class Pac4JHttpServletRequestWrapper extends HttpServletRequestWrapper {

    private Collection<CommonProfile> profiles;

    public Pac4JHttpServletRequestWrapper(HttpServletRequest request, Collection<CommonProfile> profiles) {
        super(request);
        this.profiles = profiles;
    }

    @Override
    public String getRemoteUser() {
        return getPrincipal().map(p -> p.getName()).orElse(null);
    }

    private Optional<CommonProfile> getProfile() {
        return ProfileHelper.flatIntoOneProfile(profiles);
    }

    private Optional<Principal> getPrincipal() {
        return getProfile().map(CommonProfile::asPrincipal);
    }

    @Override
    public Principal getUserPrincipal() {
        return getPrincipal().orElse(null);
    }

    @Override
    public boolean isUserInRole(String role) {
        return this.profiles.stream().anyMatch(p -> p.getRoles().contains(role));
    }
}

@@ -241,29 +241,29 @@
 profile.
-Common
 Profile
+Helper
 ;%0Aimport
@@ -286,29 +286,27 @@
 profile.
+User
 Profile
-Helper
 ;%0A%0A/**%0A 
@@ -559,38 +559,36 @@
 vate Collection%3C
-Common
+User
 Profile%3E profile
@@ -672,22 +672,20 @@
 lection%3C
-Common
+User
 Profile%3E
@@ -906,22 +906,20 @@
 ptional%3C
-Common
+User
 Profile%3E
@@ -1085,14 +1085,12 @@
 map(
-Common
+User
 Prof

use UserProfile instead of CommonProfile