1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.catalina.core;
19
20 import javax.servlet.SessionCookieConfig;
21 import javax.servlet.http.Cookie;
22
23 import org.apache.catalina.Context;
24 import org.apache.catalina.LifecycleState;
25 import org.apache.catalina.util.SessionConfig;
26 import org.apache.tomcat.util.res.StringManager;
27
28 public class ApplicationSessionCookieConfig implements SessionCookieConfig {
29
30     /**
31      * The string manager for this package.
32      */

33     private static final StringManager sm = StringManager
34             .getManager(Constants.Package);
35
36     private boolean httpOnly;
37     private boolean secure;
38     private int maxAge = -1;
39     private String comment;
40     private String domain;
41     private String name;
42     private String path;
43     private StandardContext context;
44
45     public ApplicationSessionCookieConfig(StandardContext context) {
46         this.context = context;
47     }
48
49     @Override
50     public String getComment() {
51         return comment;
52     }
53
54     @Override
55     public String getDomain() {
56         return domain;
57     }
58
59     @Override
60     public int getMaxAge() {
61         return maxAge;
62     }
63
64     @Override
65     public String getName() {
66         return name;
67     }
68
69     @Override
70     public String getPath() {
71         return path;
72     }
73
74     @Override
75     public boolean isHttpOnly() {
76         return httpOnly;
77     }
78
79     @Override
80     public boolean isSecure() {
81         return secure;
82     }
83
84     @Override
85     public void setComment(String comment) {
86         if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
87             throw new IllegalStateException(sm.getString(
88                     "applicationSessionCookieConfig.ise""comment",
89                     context.getPath()));
90         }
91         this.comment = comment;
92     }
93
94     @Override
95     public void setDomain(String domain) {
96         if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
97             throw new IllegalStateException(sm.getString(
98                     "applicationSessionCookieConfig.ise""domain name",
99                     context.getPath()));
100         }
101         this.domain = domain;
102     }
103
104     @Override
105     public void setHttpOnly(boolean httpOnly) {
106         if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
107             throw new IllegalStateException(sm.getString(
108                     "applicationSessionCookieConfig.ise""HttpOnly",
109                     context.getPath()));
110         }
111         this.httpOnly = httpOnly;
112     }
113
114     @Override
115     public void setMaxAge(int maxAge) {
116         if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
117             throw new IllegalStateException(sm.getString(
118                     "applicationSessionCookieConfig.ise""max age",
119                     context.getPath()));
120         }
121         this.maxAge = maxAge;
122     }
123
124     @Override
125     public void setName(String name) {
126         if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
127             throw new IllegalStateException(sm.getString(
128                     "applicationSessionCookieConfig.ise""name",
129                     context.getPath()));
130         }
131         this.name = name;
132     }
133
134     @Override
135     public void setPath(String path) {
136         if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
137             throw new IllegalStateException(sm.getString(
138                     "applicationSessionCookieConfig.ise""path",
139                     context.getPath()));
140         }
141         this.path = path;
142     }
143
144     @Override
145     public void setSecure(boolean secure) {
146         if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
147             throw new IllegalStateException(sm.getString(
148                     "applicationSessionCookieConfig.ise""secure",
149                     context.getPath()));
150         }
151         this.secure = secure;
152     }
153
154     /**
155      * Creates a new session cookie for the given session ID
156      *
157      * @param context     The Context for the web application
158      * @param sessionId   The ID of the session for which the cookie will be
159      *                    created
160      * @param secure      Should session cookie be configured as secure
161      * @return the cookie for the session
162      */

163     public static Cookie createSessionCookie(Context context,
164             String sessionId, boolean secure) {
165
166         SessionCookieConfig scc =
167             context.getServletContext().getSessionCookieConfig();
168
169         // NOTE: The priority order for session cookie configuration is:
170         //       1. Context level configuration
171         //       2. Values from SessionCookieConfig
172         //       3. Defaults
173
174         Cookie cookie = new Cookie(
175                 SessionConfig.getSessionCookieName(context), sessionId);
176
177         // Just apply the defaults.
178         cookie.setMaxAge(scc.getMaxAge());
179         cookie.setComment(scc.getComment());
180
181         if (context.getSessionCookieDomain() == null) {
182             // Avoid possible NPE
183             if (scc.getDomain() != null) {
184                 cookie.setDomain(scc.getDomain());
185             }
186         } else {
187             cookie.setDomain(context.getSessionCookieDomain());
188         }
189
190         // Always set secure if the request is secure
191         if (scc.isSecure() || secure) {
192             cookie.setSecure(true);
193         }
194
195         // Always set httpOnly if the context is configured for that
196         if (scc.isHttpOnly() || context.getUseHttpOnly()) {
197             cookie.setHttpOnly(true);
198         }
199
200         cookie.setPath(SessionConfig.getSessionCookiePath(context));
201
202         return cookie;
203     }
204 }
205