<commit_before>/*
 * #%L
 * Eureka WebApp
 * %%
 * Copyright (C) 2012 - 2013 Emory University
 * %%
 * 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.
 * #L%
 */
package edu.emory.cci.aiw.cvrg.eureka.servlet.filter;

import java.io.IOException;
import java.security.Principal;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.emory.cci.aiw.cvrg.eureka.common.comm.clients.ServicesClient;
import edu.emory.cci.aiw.cvrg.eureka.common.entity.User;

/**
 * @author hrathod
 */
public class PasswordExpiredFilter implements Filter {

	private static final Logger LOGGER = LoggerFactory.getLogger
			(PasswordExpiredFilter.class);
	private ServicesClient servicesClient;
	private String redirectUrl;
	private String saveUrl;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		String serviceUrl = filterConfig.getServletContext()
				.getInitParameter("eureka-services-url");
		this.servicesClient = new ServicesClient(serviceUrl);
		this.redirectUrl = filterConfig.getInitParameter("redirect-url");
		this.saveUrl = filterConfig.getInitParameter("save-url");
		if (this.redirectUrl == null) {
			throw new ServletException("Parameter redirect-url must be set");
		}
		if (this.saveUrl == null) {
			throw new ServletException("Parameter save-url must be set");
		}
		LOGGER.debug("redirect-url: {}", this.redirectUrl);
		LOGGER.debug("save-url: {}", this.saveUrl);
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, 
			FilterChain chain) throws IOException, ServletException {
		if (request instanceof HttpServletRequest && response instanceof 
				HttpServletResponse) {
			HttpServletRequest servletRequest = (HttpServletRequest) request;
			HttpServletResponse servletResponse = (HttpServletResponse) 
					response;
			Principal principal = servletRequest.getUserPrincipal();
			if (principal != null) {
				LOGGER.debug("username: {}", principal.getName());
				User user = this.servicesClient.getUserByName(principal
						.getName());
				Date now = Calendar.getInstance().getTime();
				Date expiration = user.getPasswordExpiration();
				LOGGER.debug("expiration date: {}", user.getPasswordExpiration
						());
				if (expiration != null && now.after(expiration)) {
					String targetUrl = servletRequest.getRequestURI();
					String fullRedirectUrl = servletRequest.getContextPath() 
							+ this.redirectUrl;
					String fullSaveUrl = servletRequest.getContextPath() + 
							this.saveUrl;
					LOGGER.debug("fullRedirectUrl: {}", fullRedirectUrl);
					LOGGER.debug("fullSaveUrl: {}", fullSaveUrl);
					LOGGER.debug("targetUrl: {}", targetUrl);
					if (!targetUrl.equals(fullRedirectUrl) && !targetUrl
							.equals(fullSaveUrl)) {
						servletResponse.sendRedirect(fullRedirectUrl);<commit_msg>changes to pass the requested URL as a query parameter<commit_after>/*
 * #%L
 * Eureka WebApp
 * %%
 * Copyright (C) 2012 - 2013 Emory University
 * %%
 * 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.
 * #L%
 */
package edu.emory.cci.aiw.cvrg.eureka.servlet.filter;

import java.io.IOException;
import java.security.Principal;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.emory.cci.aiw.cvrg.eureka.common.comm.clients.ServicesClient;
import edu.emory.cci.aiw.cvrg.eureka.common.entity.User;

/**
 * @author hrathod
 */
public class PasswordExpiredFilter implements Filter {

	private static final Logger LOGGER = LoggerFactory.getLogger
			(PasswordExpiredFilter.class);
	private ServicesClient servicesClient;
	private String redirectUrl;
	private String saveUrl;

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		String serviceUrl = filterConfig.getServletContext()
				.getInitParameter("eureka-services-url");
		this.servicesClient = new ServicesClient(serviceUrl);
		this.redirectUrl = filterConfig.getInitParameter("redirect-url");
		this.saveUrl = filterConfig.getInitParameter("save-url");
		if (this.redirectUrl == null) {
			throw new ServletException("Parameter redirect-url must be set");
		}
		if (this.saveUrl == null) {
			throw new ServletException("Parameter save-url must be set");
		}
		LOGGER.debug("redirect-url: {}", this.redirectUrl);
		LOGGER.debug("save-url: {}", this.saveUrl);
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, 
			FilterChain chain) throws IOException, ServletException {
		if (request instanceof HttpServletRequest && response instanceof 
				HttpServletResponse) {
			HttpServletRequest servletRequest = (HttpServletRequest) request;
			HttpServletResponse servletResponse = (HttpServletResponse) 
					response;
			Principal principal = servletRequest.getUserPrincipal();
			if (principal != null) {
				LOGGER.debug("username: {}", principal.getName());
				User user = this.servicesClient.getUserByName(principal
						.getName());
				Date now = Calendar.getInstance().getTime();
				Date expiration = user.getPasswordExpiration();
				LOGGER.debug("expiration date: {}", user.getPasswordExpiration
						());
				if (expiration != null && now.after(expiration)) {
					String targetUrl = servletRequest.getRequestURI();
					String fullRedirectUrl = servletRequest.getContextPath() 
							+ this.redirectUrl;
					String fullSaveUrl = servletRequest.getContextPath() + 
							this.saveUrl;
					LOGGER.debug("fullRedirectUrl: {}", fullRedirectUrl);
					LOGGER.debug("fullSaveUrl: {}", fullSaveUrl);
					LOGGER.debug("targetUrl: {}", targetUrl);
					if (!targetUrl.equals(fullRedirectUrl) && !targetUrl
							.equals(fullSaveUrl)) {
						servletResponse.sendRedirect(fullRedirectUrl+"?redirectURL="+targetUrl);
{'fraction_non_alphanumeric': 0.1065873123224672, 'fraction_numerical': 0.0037873664277018803, 'mean_word_length': 4.635670731707317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4903192', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2073, 'n_words': 532}