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.servlet.spec;
20
21 import io.undertow.server.HttpServerExchange;
22 import io.undertow.server.session.SessionConfig;
23 import io.undertow.servlet.UndertowServletMessages;
24
25 import javax.servlet.SessionCookieConfig;
26
27 /**
28  * @author Stuart Douglas
29  */

30 public class SessionCookieConfigImpl implements SessionCookieConfig, SessionConfig {
31
32     private final ServletContextImpl servletContext;
33     private final io.undertow.server.session.SessionCookieConfig delegate;
34     private SessionConfig fallback;
35
36     public SessionCookieConfigImpl(final ServletContextImpl servletContext) {
37         this.servletContext = servletContext;
38         this.delegate = new io.undertow.server.session.SessionCookieConfig();
39     }
40
41     @Override
42     public String rewriteUrl(final String originalUrl, final String sessionid) {
43         if(fallback != null) {
44             return fallback.rewriteUrl(originalUrl, sessionid);
45         }
46         return originalUrl;
47     }
48
49     @Override
50     public void setSessionId(final HttpServerExchange exchange, final String sessionId) {
51         delegate.setSessionId(exchange, sessionId);
52     }
53
54     @Override
55     public void clearSession(final HttpServerExchange exchange, final String sessionId) {
56         delegate.clearSession(exchange, sessionId);
57     }
58
59     @Override
60     public String findSessionId(final HttpServerExchange exchange) {
61         String existing = delegate.findSessionId(exchange);
62         if(existing != null) {
63             return existing;
64         }
65         if(fallback != null) {
66             return fallback.findSessionId(exchange);
67         }
68         return null;
69     }
70
71     @Override
72     public SessionCookieSource sessionCookieSource(HttpServerExchange exchange) {
73         String existing = delegate.findSessionId(exchange);
74         if (existing != null) {
75             return SessionCookieSource.COOKIE;
76         }
77         if(fallback != null) {
78             String id =  fallback.findSessionId(exchange);
79             return id != null ? fallback.sessionCookieSource(exchange) : SessionCookieSource.NONE;
80         }
81         return SessionCookieSource.NONE;
82     }
83
84     public String getName() {
85         return delegate.getCookieName();
86     }
87
88     public void setName(final String name) {
89         if(servletContext.isInitialized()) {
90             throw UndertowServletMessages.MESSAGES.servletContextAlreadyInitialized();
91         }
92         delegate.setCookieName(name);
93     }
94
95     public String getDomain() {
96         return delegate.getDomain();
97     }
98
99     public void setDomain(final String domain) {
100         if(servletContext.isInitialized()) {
101             throw UndertowServletMessages.MESSAGES.servletContextAlreadyInitialized();
102         }
103         delegate.setDomain(domain);
104     }
105
106     public String getPath() {
107         return delegate.getPath();
108     }
109
110     public void setPath(final String path) {
111         if(servletContext.isInitialized()) {
112             throw UndertowServletMessages.MESSAGES.servletContextAlreadyInitialized();
113         }
114         delegate.setPath(path);
115     }
116
117     public String getComment() {
118         return delegate.getComment();
119     }
120
121     public void setComment(final String comment) {
122         if(servletContext.isInitialized()) {
123             throw UndertowServletMessages.MESSAGES.servletContextAlreadyInitialized();
124         }
125         delegate.setComment(comment);
126     }
127
128     public boolean isHttpOnly() {
129         return delegate.isHttpOnly();
130     }
131
132     public void setHttpOnly(final boolean httpOnly) {
133         if(servletContext.isInitialized()) {
134             throw UndertowServletMessages.MESSAGES.servletContextAlreadyInitialized();
135         }
136         delegate.setHttpOnly(httpOnly);
137     }
138
139     public boolean isSecure() {
140         return delegate.isSecure();
141     }
142
143     public void setSecure(final boolean secure) {
144         if(servletContext.isInitialized()) {
145             throw UndertowServletMessages.MESSAGES.servletContextAlreadyInitialized();
146         }
147         delegate.setSecure(secure);
148     }
149
150     public int getMaxAge() {
151         return delegate.getMaxAge();
152     }
153
154     public void setMaxAge(final int maxAge) {
155         if(servletContext.isInitialized()) {
156             throw UndertowServletMessages.MESSAGES.servletContextAlreadyInitialized();
157         }
158         this.delegate.setMaxAge(maxAge);
159     }
160
161     public SessionConfig getFallback() {
162         return fallback;
163     }
164
165     public void setFallback(final SessionConfig fallback) {
166         this.fallback = fallback;
167     }
168 }
169