1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

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 /**
29  * Encapsulation of session cookie configuration. This removes the need for the session manager to
30  * know about cookie configuration.
31  *
32  * @author Stuart Douglas
33  */

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