// Path: core/src/main/java/org/juiser/model/User.java
// public interface User {
// 
//     /**
//      * Returns {@code true} if the user has been authenticated (proven their identity) or {@code false} if the user
//      * is considered anonymous.
//      *
//      * @return {@code true} if the user has been authenticated (proven their identity) or {@code false} if the user
//      * is considered anonymous.
//      */
//     boolean isAuthenticated();
// 
//     /**
//      * Returns {@code true} if the user identity is unknown, {@code false} if the user is authenticated.
//      *
//      * @return {@code true} if the user identity is unknown, {@code false} if the user is authenticated.
//      */
//     boolean isAnonymous();
// 
//     String getHref();
// 
//     String getId();
// 
//     String getName(); //full name
// 
//     String getGivenName(); //aka 'first name' in Western cultures
// 
//     String getFamilyName(); //aka surname
// 
//     String getMiddleName();
// 
//     String getNickname(); //aka 'Mike' if givenName is 'Michael'
// 
//     String getUsername(); //OIDC 'preferred_username'
// 
//     URL getProfile();
// 
//     URL getPicture();
// 
//     URL getWebsite();
// 
//     String getEmail();
// 
//     boolean isEmailVerified();
// 
//     String getGender();
// 
//     LocalDate getBirthdate();
// 
//     TimeZone getZoneInfo();
// 
//     Locale getLocale();
// 
//     Phone getPhone();
// 
//     String getPhoneNumber();
// 
//     boolean isPhoneNumberVerified();
// 
//     ZonedDateTime getCreatedAt();
// 
//     ZonedDateTime getUpdatedAt();
// }
// 
// Path: servlet/src/main/java/org/juiser/servlet/ForwardedUserFilter.java
// public class ForwardedUserFilter implements Filter {
// 
//     private final String headerName;
//     private final Function<HttpServletRequest, User> userFactory;
//     private final Collection<String> requestAttributeNames;
// 
//     public ForwardedUserFilter(String headerName,
//                                Function<HttpServletRequest, User> userFactory,
//                                Collection<String> requestAttributeNames) {
//         Assert.hasText(headerName, "headerName cannot be null or empty.");
//         Assert.notNull(userFactory, "userFactory function cannot be null.");
// 
//         this.headerName = headerName;
//         this.userFactory = userFactory;
// 
//         //always ensure that the fully qualified interface name is accessible:
//         LinkedHashSet<String> set = new LinkedHashSet<>();
//         set.add(User.class.getName());
//         if (!Collections.isEmpty(requestAttributeNames)) {
//             set.addAll(requestAttributeNames);
//         }
//         this.requestAttributeNames = set;
//     }
// 
//     @Override
//     public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
//         if (isEnabled(req)) {
//             HttpServletRequest request = (HttpServletRequest) req;
//             HttpServletResponse response = (HttpServletResponse) resp;
//             doFilter(request, response, chain);
//         } else {
//             chain.doFilter(req, resp);
//         }
//     }
// 
//     public boolean isEnabled(ServletRequest request) throws ServletException {
//         return request instanceof HttpServletRequest && isEnabled((HttpServletRequest) request);
//     }
// 
//     public boolean isEnabled(HttpServletRequest request) throws ServletException {
//         String headerValue = request.getHeader(headerName);
//         return Strings.hasText(headerValue);
//     }
// 
//     public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
//         throws ServletException, IOException {
// 
//         String value = request.getHeader(headerName);
//         assert Strings.hasText(value) : "request header value cannot be null or empty. Call isEnabled before calling doFilter";
// 
//         User user = userFactory.apply(request);
//         assert user != null : "user instance returned from userFactory function cannot be null.";
// 
//         for (String requestAttributeName : requestAttributeNames) {
//             request.setAttribute(requestAttributeName, user);
//         }
//         try {
//             filterChain.doFilter(request, response);
//         } finally {
//             for (String requestAttributeName : requestAttributeNames) {
//                 Object object = request.getAttribute(requestAttributeName);
//                 if (user.equals(object)) {
//                     //only remove the object if we put it there.  If someone downstream of this filter changed
//                     //it to be something else, we shouldn't touch it:
//                     request.removeAttribute(requestAttributeName);
//                 }
//             }
//         }
//     }
// 
//     @Override
//     public void init(FilterConfig filterConfig) throws ServletException {
//         //no op
//     }
// 
//     @Override
//     public void destroy() {
//         //no op
//     }
// }


import org.juiser.model.User;
import org.juiser.servlet.ForwardedUserFilter;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.function.Function;
/*
 * Copyright 2017 Les Hazlewood and the respective Juiser contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.juiser.spring.web;



/**
 * @since 1.0.0
 */
public class SpringForwardedUserFilter extends OncePerRequestFilter {

    private final ForwardedUserFilter delegate;

    public SpringForwardedUserFilter(String headerName,
// Path: core/src/main/java/org/juiser/model/User.java
// public interface User {
// 
//     /**
//      * Returns {@code true} if the user has been authenticated (proven their identity) or {@code false} if the user
//      * is considered anonymous.
//      *
//      * @return {@code true} if the user has been authenticated (proven their identity) or {@code false} if the user
//      * is considered anonymous.
//      */
//     boolean isAuthenticated();
// 
//     /**
//      * Returns {@code true} if the user identity is unknown, {@code false} if the user is authenticated.
//      *
//      * @return {@code true} if the user identity is unknown, {@code false} if the user is authenticated.
//      */
//     boolean isAnonymous();
// 
//     String getHref();
// 
//     String getId();
// 
//     String getName(); //full name
// 
//     String getGivenName(); //aka 'first name' in Western cultures
// 
//     String getFamilyName(); //aka surname
// 
//     String getMiddleName();
// 
//     String getNickname(); //aka 'Mike' if givenName is 'Michael'
// 
//     String getUsername(); //OIDC 'preferred_username'
// 
//     URL getProfile();
// 
//     URL getPicture();
// 
//     URL getWebsite();
// 
//     String getEmail();
// 
//     boolean isEmailVerified();
// 
//     String getGender();
// 
//     LocalDate getBirthdate();
// 
//     TimeZone getZoneInfo();
// 
//     Locale getLocale();
// 
//     Phone getPhone();
// 
//     String getPhoneNumber();
// 
//     boolean isPhoneNumberVerified();
// 
//     ZonedDateTime getCreatedAt();
// 
//     ZonedDateTime getUpdatedAt();
// }
// 
// Path: servlet/src/main/java/org/juiser/servlet/ForwardedUserFilter.java
// public class ForwardedUserFilter implements Filter {
// 
//     private final String headerName;
//     private final Function<HttpServletRequest, User> userFactory;
//     private final Collection<String> requestAttributeNames;
// 
//     public ForwardedUserFilter(String headerName,
//                                Function<HttpServletRequest, User> userFactory,
//                                Collection<String> requestAttributeNames) {
//         Assert.hasText(headerName, "headerName cannot be null or empty.");
//         Assert.notNull(userFactory, "userFactory function cannot be null.");
// 
//         this.headerName = headerName;
//         this.userFactory = userFactory;
// 
//         //always ensure that the fully qualified interface name is accessible:
//         LinkedHashSet<String> set = new LinkedHashSet<>();
//         set.add(User.class.getName());
//         if (!Collections.isEmpty(requestAttributeNames)) {
//             set.addAll(requestAttributeNames);
//         }
//         this.requestAttributeNames = set;
//     }
// 
//     @Override
//     public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
//         if (isEnabled(req)) {
//             HttpServletRequest request = (HttpServletRequest) req;
//             HttpServletResponse response = (HttpServletResponse) resp;
//             doFilter(request, response, chain);
//         } else {
//             chain.doFilter(req, resp);
//         }
//     }
// 
//     public boolean isEnabled(ServletRequest request) throws ServletException {
//         return request instanceof HttpServletRequest && isEnabled((HttpServletRequest) request);
//     }
// 
//     public boolean isEnabled(HttpServletRequest request) throws ServletException {
//         String headerValue = request.getHeader(headerName);
//         return Strings.hasText(headerValue);
//     }
// 
//     public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
//         throws ServletException, IOException {
// 
//         String value = request.getHeader(headerName);
//         assert Strings.hasText(value) : "request header value cannot be null or empty. Call isEnabled before calling doFilter";
// 
//         User user = userFactory.apply(request);
//         assert user != null : "user instance returned from userFactory function cannot be null.";
// 
//         for (String requestAttributeName : requestAttributeNames) {
//             request.setAttribute(requestAttributeName, user);
//         }
//         try {
//             filterChain.doFilter(request, response);
//         } finally {
//             for (String requestAttributeName : requestAttributeNames) {
//                 Object object = request.getAttribute(requestAttributeName);
//                 if (user.equals(object)) {
//                     //only remove the object if we put it there.  If someone downstream of this filter changed
//                     //it to be something else, we shouldn't touch it:
//                     request.removeAttribute(requestAttributeName);
//                 }
//             }
//         }
//     }
// 
//     @Override
//     public void init(FilterConfig filterConfig) throws ServletException {
//         //no op
//     }
// 
//     @Override
//     public void destroy() {
//         //no op
//     }
// }


// Path: spring/spring-web/src/main/java/org/juiser/spring/web/SpringForwardedUserFilter.java
import org.juiser.model.User;
import org.juiser.servlet.ForwardedUserFilter;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.function.Function;

/*
 * Copyright 2017 Les Hazlewood and the respective Juiser contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.juiser.spring.web;



/**
 * @since 1.0.0
 */
public class SpringForwardedUserFilter extends OncePerRequestFilter {

    private final ForwardedUserFilter delegate;

    public SpringForwardedUserFilter(String headerName,
                                     Function<HttpServletRequest, User> userFactory,