Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

As a System I want to save login and password in cookies

  • Loading branch information...
commit b9a419161871b47821171790f443cf92869ac631 1 parent c5e91b3
Tomasz Dziurko authored September 14, 2012
6  pom.xml
@@ -77,6 +77,12 @@
77 77
             <artifactId>guava</artifactId>
78 78
             <version>11.0</version>
79 79
         </dependency>
  80
+        <dependency>
  81
+            <groupId>javax.servlet</groupId>
  82
+            <artifactId>servlet-api</artifactId>
  83
+            <version>2.5</version>
  84
+            <scope>provided</scope>
  85
+        </dependency>
80 86
 
81 87
 
82 88
     </dependencies>
6  src/main/java/pl/tomaszdziurko/wicket/WicketApplication.java
@@ -4,6 +4,7 @@
4 4
 import org.apache.wicket.protocol.http.WebApplication;
5 5
 import org.apache.wicket.request.Request;
6 6
 import org.apache.wicket.request.Response;
  7
+import pl.tomaszdziurko.wicket.service.CookieService;
7 8
 import pl.tomaszdziurko.wicket.service.UserService;
8 9
 import pl.tomaszdziurko.wicket.view.HomePage;
9 10
 import pl.tomaszdziurko.wicket.view.LoginPage;
@@ -11,6 +12,7 @@
11 12
 public class WicketApplication extends WebApplication {
12 13
 
13 14
     private UserService userService = new UserService();
  15
+    private CookieService cookieService = new CookieService();
14 16
 
15 17
     @Override
16 18
     public Class<HomePage> getHomePage() {
@@ -39,5 +41,9 @@ public UserService getUserService() {
39 41
         return userService;
40 42
     }
41 43
 
  44
+    public CookieService getCookieService() {
  45
+        return cookieService;
  46
+    }
  47
+
42 48
 
43 49
 }
44  src/main/java/pl/tomaszdziurko/wicket/service/CookieService.java
... ...
@@ -0,0 +1,44 @@
  1
+package pl.tomaszdziurko.wicket.service;
  2
+
  3
+import org.apache.wicket.request.Request;
  4
+import org.apache.wicket.request.Response;
  5
+import org.apache.wicket.request.http.WebRequest;
  6
+import org.apache.wicket.request.http.WebResponse;
  7
+
  8
+import javax.servlet.http.Cookie;
  9
+import java.util.List;
  10
+import java.util.concurrent.TimeUnit;
  11
+
  12
+public class CookieService {
  13
+
  14
+    public Cookie loadCookie(Request request, String cookieName) {
  15
+
  16
+        List<Cookie> cookies = ((WebRequest) request).getCookies();
  17
+
  18
+        if (cookies == null) {
  19
+            return null;
  20
+        }
  21
+
  22
+        for (Cookie cookie : cookies) {
  23
+            if(cookie.getName().equals(cookieName)) {
  24
+                return cookie;
  25
+            }
  26
+        }
  27
+
  28
+        return null;
  29
+    }
  30
+
  31
+    public void saveCookie(Response response, String cookieName, String cookieValue, int expiryTimeInDays) {
  32
+        Cookie cookie = new Cookie(cookieName, cookieValue);
  33
+        cookie.setMaxAge((int) TimeUnit.DAYS.toSeconds(expiryTimeInDays));
  34
+        ((WebResponse)response).addCookie(cookie);
  35
+    }
  36
+
  37
+    public void removeCookieIfPresent(Request request, Response response, String cookieName) {
  38
+        Cookie cookie = loadCookie(request, cookieName);
  39
+
  40
+        if(cookie != null) {
  41
+            ((WebResponse)response).clearCookie(cookie);
  42
+        }
  43
+    }
  44
+}
12  src/main/java/pl/tomaszdziurko/wicket/view/LoginPage.java
@@ -10,10 +10,15 @@
10 10
 import pl.tomaszdziurko.wicket.UserSession;
11 11
 import pl.tomaszdziurko.wicket.WicketApplication;
12 12
 import pl.tomaszdziurko.wicket.model.User;
  13
+import pl.tomaszdziurko.wicket.service.CookieService;
13 14
 import pl.tomaszdziurko.wicket.service.UserService;
14 15
 
15 16
 public class LoginPage extends BasePage {
16 17
 
  18
+    public static final int REMEMBER_ME_DURATION_IN_DAYS = 30;
  19
+    private static final String REMEMBER_ME_LOGIN_COOKIE = "loginCookie";
  20
+    private static final String REMEMBER_ME_PASSWORD_COOKIE = "passwordCookie";
  21
+
17 22
     private String login;
18 23
     private String password;
19 24
     private boolean rememberMe;
@@ -40,6 +45,13 @@ public void onSubmit() {
40 45
                 }
41 46
                 else {
42 47
                     UserSession.get().setUser(user);
  48
+
  49
+                    if(rememberMe) {
  50
+                        CookieService cookieService = WicketApplication.get().getCookieService();
  51
+                        cookieService.saveCookie(getResponse(), REMEMBER_ME_LOGIN_COOKIE, user.getLogin(), REMEMBER_ME_DURATION_IN_DAYS);
  52
+                        cookieService.saveCookie(getResponse(), REMEMBER_ME_PASSWORD_COOKIE, user.getPassword(), REMEMBER_ME_DURATION_IN_DAYS);
  53
+                    }
  54
+
43 55
                     setResponsePage(HomePage.class);
44 56
                 }
45 57
             }

0 notes on commit b9a4191

Please sign in to comment.
Something went wrong with that request. Please try again.