1
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
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
163 public static Cookie createSessionCookie(Context context,
164 String sessionId, boolean secure) {
165
166 SessionCookieConfig scc =
167 context.getServletContext().getSessionCookieConfig();
168
169
170
171
172
173
174 Cookie cookie = new Cookie(
175 SessionConfig.getSessionCookieName(context), sessionId);
176
177
178 cookie.setMaxAge(scc.getMaxAge());
179 cookie.setComment(scc.getComment());
180
181 if (context.getSessionCookieDomain() == null) {
182
183 if (scc.getDomain() != null) {
184 cookie.setDomain(scc.getDomain());
185 }
186 } else {
187 cookie.setDomain(context.getSessionCookieDomain());
188 }
189
190
191 if (scc.isSecure() || secure) {
192 cookie.setSecure(true);
193 }
194
195
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