View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers;
12  
13  import java.text.SimpleDateFormat;
14  import java.util.Date;
15  
16  import javax.servlet.http.HttpServletRequest;
17  import javax.servlet.http.HttpServletResponse;
18  
19  import org.springframework.stereotype.Controller;
20  import org.springframework.web.bind.ServletRequestUtils;
21  import org.springframework.web.bind.annotation.RequestMapping;
22  import org.springframework.web.servlet.ModelAndView;
23  import org.springframework.web.servlet.mvc.AbstractController;
24  
25  import psiprobe.jsp.Functions;
26  
27  /**
28   * The Class RememberVisibilityController.
29   */
30  @Controller
31  public class RememberVisibilityController extends AbstractController {
32  
33    /** The sdf. */
34    private final SimpleDateFormat sdf = new SimpleDateFormat("E, d-MMM-yyyy HH:mm:ss zz");
35  
36    @RequestMapping(path = "/remember.ajax")
37    @Override
38    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
39        throws Exception {
40      return super.handleRequest(request, response);
41    }
42  
43    @Override
44    protected ModelAndView handleRequestInternal(HttpServletRequest request,
45        HttpServletResponse response) throws Exception {
46  
47      String cookieName = ServletRequestUtils.getStringParameter(request, "cn");
48      String state = ServletRequestUtils.getStringParameter(request, "state");
49      if (cookieName != null && state != null) {
50        cookieName = Functions.safeCookieName(cookieName);
51        // expire the cookies at the current date + 10years (roughly, nevermind leap years)
52        response.addHeader("Set-Cookie",
53            cookieName + '=' + state + "; Expires="
54                + sdf.format(new Date(System.currentTimeMillis() + 315360000000L))
55                + "; Secure=true; HttpOnly=true");
56      }
57      return null;
58    }
59  }