/**
 * Copyright 2017 Carl-Philipp Harmant
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 fr.cph.stock.web.servlet.user;

import fr.cph.stock.business.UserBusiness;
import fr.cph.stock.entities.User;
import fr.cph.stock.guice.GuiceInjector;
import fr.cph.stock.web.servlet.CookieManagement;
import lombok.extern.log4j.Log4j2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static fr.cph.stock.util.Constants.*;

/**
 * This servlet is called when the user want to login
 *
 * @author Carl-Philipp Harmant
 */
@Log4j2
@WebServlet(name = "AuthServlet", urlPatterns = {"/auth"})
public class AuthServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static final int ONE_YEAR_COOKIE = 60 * 60 * 24 * 365;
	private UserBusiness userBusiness;
	private List<String> lcookies;

	@Override
	public final void init() {
		userBusiness = GuiceInjector.INSTANCE.getUserBusiness();
		lcookies = new ArrayList<>();
		lcookies.add(QUOTE);
		lcookies.add(CURRENCY);
		lcookies.add(PARITY);
		lcookies.add(STOP_LOSS);
		lcookies.add(OBJECTIVE);
		lcookies.add(YIELD_1);
		lcookies.add(YIELD_2);
		lcookies.add(AUTO_UPDATE);
	}

	@Override
	protected final void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
		try {
			request.getSession().invalidate();
			final String login = request.getParameter(LOGIN);
			final String password = request.getParameter(PASSWORD);
			final Optional<User> userOptional = userBusiness.checkUser(login, password);
			if (userOptional.isPresent()) {
				final User user = userOptional.get();
				if (!user.getAllow()) {
					request.getSession().setAttribute(ERROR, "Account not confirmed. Check your email!");
					request.getRequestDispatcher("index.jsp").forward(request, response);
				} else {
					request.getSession().setAttribute(USER, user);
					if (request.getCookies() != null) {
						final List<Cookie> cookies = Arrays.asList(request.getCookies());
						lcookies.stream().filter(cookieName -> CookieManagement.notContainsCookie(cookies, cookieName))
							.forEach(cookieName -> addCookieToResponse(response, cookieName, CHECKED));
						if (CookieManagement.notContainsCookie(cookies, LANGUAGE)) {
							addCookieToResponse(response, LANGUAGE, ENGLISH);
						}
					} else {
						lcookies.forEach(cookieName -> addCookieToResponse(response, cookieName, CHECKED));
						addCookieToResponse(response, LANGUAGE, ENGLISH);
					}
					log.info("User logged in [{}]", login);
					response.sendRedirect(HOME);
				}
			} else {
				request.getRequestDispatcher("/jsp/error.jsp").forward(request, response);
			}
		} catch (final Throwable t) {
			log.error("Error: {}", t.getMessage(), t);
			throw new ServletException("Error: " + t.getMessage(), t);
		}
	}

	private void addCookieToResponse(final HttpServletResponse response, final String name, final String value) {
		final Cookie cookie = new Cookie(name, value);
		cookie.setMaxAge(ONE_YEAR_COOKIE);
		response.addCookie(cookie);
	}
}

@@ -1635,18 +1635,24 @@
 String%3E 
-lc
+defaultC
 ookies;%0A
@@ -1748,26 +1748,32 @@
 siness();%0A%09%09
-lc
+defaultC
 ookies = new
@@ -1786,26 +1786,32 @@
 List%3C%3E();%0A%09%09
-lc
+defaultC
 ookies.add(Q
@@ -1815,26 +1815,32 @@
 d(QUOTE);%0A%09%09
-lc
+defaultC
 ookies.add(C
@@ -1847,26 +1847,32 @@
 URRENCY);%0A%09%09
-lc
+defaultC
 ookies.add(P
@@ -1877,26 +1877,32 @@
 (PARITY);%0A%09%09
-lc
+defaultC
 ookies.add(S
@@ -1910,26 +1910,32 @@
 OP_LOSS);%0A%09%09
-lc
+defaultC
 ookies.add(O
@@ -1943,26 +1943,32 @@
 JECTIVE);%0A%09%09
-lc
+defaultC
 ookies.add(Y
@@ -1974,26 +1974,32 @@
 YIELD_1);%0A%09%09
-lc
+defaultC
 ookies.add(Y
@@ -2009,18 +2009,24 @@
 D_2);%0A%09%09
-lc
+defaultC
 ookies.a
@@ -2871,26 +2871,32 @@
 s());%0A%09%09%09%09%09%09
-lc
+defaultC
 ookies.strea
@@ -3216,10 +3216,16 @@
 %09%09%09%09
-lc
+defaultC
 ooki

rename cookie list