1
18
19 package io.undertow.server.session;
20
21 import java.util.Map;
22
23 import io.undertow.UndertowLogger;
24 import io.undertow.server.HttpServerExchange;
25 import io.undertow.server.handlers.Cookie;
26 import io.undertow.server.handlers.CookieImpl;
27
28
34 public class SessionCookieConfig implements SessionConfig {
35
36 public static final String DEFAULT_SESSION_ID = "JSESSIONID";
37
38 private String cookieName = DEFAULT_SESSION_ID;
39 private String path = "/";
40 private String domain;
41 private boolean discard;
42 private boolean secure;
43 private boolean httpOnly;
44 private int maxAge = -1;
45 private String comment;
46
47
48 @Override
49 public String rewriteUrl(final String originalUrl, final String sessionId) {
50 return originalUrl;
51 }
52
53 @Override
54 public void setSessionId(final HttpServerExchange exchange, final String sessionId) {
55 Cookie cookie = new CookieImpl(cookieName, sessionId)
56 .setPath(path)
57 .setDomain(domain)
58 .setDiscard(discard)
59 .setSecure(secure)
60 .setHttpOnly(httpOnly)
61 .setComment(comment);
62 if (maxAge > 0) {
63 cookie.setMaxAge(maxAge);
64 }
65 exchange.setResponseCookie(cookie);
66 UndertowLogger.SESSION_LOGGER.tracef("Setting session cookie session id %s on %s", sessionId, exchange);
67 }
68
69 @Override
70 public void clearSession(final HttpServerExchange exchange, final String sessionId) {
71 Cookie cookie = new CookieImpl(cookieName, sessionId)
72 .setPath(path)
73 .setDomain(domain)
74 .setDiscard(discard)
75 .setSecure(secure)
76 .setHttpOnly(httpOnly)
77 .setMaxAge(0);
78 exchange.setResponseCookie(cookie);
79 UndertowLogger.SESSION_LOGGER.tracef("Clearing session cookie session id %s on %s", sessionId, exchange);
80 }
81
82 @Override
83 public String findSessionId(final HttpServerExchange exchange) {
84 Map<String, Cookie> cookies = exchange.getRequestCookies();
85 if (cookies != null) {
86 Cookie sessionId = cookies.get(cookieName);
87 if (sessionId != null) {
88 UndertowLogger.SESSION_LOGGER.tracef("Found session cookie session id %s on %s", sessionId, exchange);
89 return sessionId.getValue();
90 }
91 }
92 return null;
93 }
94
95 @Override
96 public SessionCookieSource sessionCookieSource(HttpServerExchange exchange) {
97 return findSessionId(exchange) != null ? SessionCookieSource.COOKIE : SessionCookieSource.NONE;
98 }
99
100 public String getCookieName() {
101 return cookieName;
102 }
103
104 public SessionCookieConfig setCookieName(final String cookieName) {
105 this.cookieName = cookieName;
106 return this;
107 }
108
109 public String getPath() {
110 return path;
111 }
112
113 public SessionCookieConfig setPath(final String path) {
114 this.path = path;
115 return this;
116 }
117
118 public String getDomain() {
119 return domain;
120 }
121
122 public SessionCookieConfig setDomain(final String domain) {
123 this.domain = domain;
124 return this;
125 }
126
127 public boolean isDiscard() {
128 return discard;
129 }
130
131 public SessionCookieConfig setDiscard(final boolean discard) {
132 this.discard = discard;
133 return this;
134 }
135
136 public boolean isSecure() {
137 return secure;
138 }
139
140 public SessionCookieConfig setSecure(final boolean secure) {
141 this.secure = secure;
142 return this;
143 }
144
145 public boolean isHttpOnly() {
146 return httpOnly;
147 }
148
149 public SessionCookieConfig setHttpOnly(final boolean httpOnly) {
150 this.httpOnly = httpOnly;
151 return this;
152 }
153
154 public int getMaxAge() {
155 return maxAge;
156 }
157
158 public SessionCookieConfig setMaxAge(final int maxAge) {
159 this.maxAge = maxAge;
160 return this;
161 }
162
163 public String getComment() {
164 return comment;
165 }
166
167 public SessionCookieConfig setComment(final String comment) {
168 this.comment = comment;
169 return this;
170 }
171 }
172