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.handlers;
20
21 import java.util.Arrays;
22 import java.util.Date;
23
24 import io.undertow.UndertowLogger;
25 import io.undertow.UndertowMessages;
26
27 /**
28  * @author Stuart Douglas
29  */

30 public class CookieImpl implements Cookie {
31
32     private final String name;
33     private String value;
34     private String path;
35     private String domain;
36     private Integer maxAge;
37     private Date expires;
38     private boolean discard;
39     private boolean secure;
40     private boolean httpOnly;
41     private int version = 0;
42     private String comment;
43     private boolean sameSite;
44     private String sameSiteMode;
45
46     public CookieImpl(final String name, final String value) {
47         this.name = name;
48         this.value = value;
49     }
50
51     public CookieImpl(final String name) {
52         this.name = name;
53     }
54
55     public String getName() {
56         return name;
57     }
58
59     public String getValue() {
60         return value;
61     }
62
63     public CookieImpl setValue(final String value) {
64         this.value = value;
65         return this;
66     }
67
68     public String getPath() {
69         return path;
70     }
71
72     public CookieImpl setPath(final String path) {
73         this.path = path;
74         return this;
75     }
76
77     public String getDomain() {
78         return domain;
79     }
80
81     public CookieImpl setDomain(final String domain) {
82         this.domain = domain;
83         return this;
84     }
85
86     public Integer getMaxAge() {
87         return maxAge;
88     }
89
90     public CookieImpl setMaxAge(final Integer maxAge) {
91         this.maxAge = maxAge;
92         return this;
93     }
94
95     public boolean isDiscard() {
96         return discard;
97     }
98
99     public CookieImpl setDiscard(final boolean discard) {
100         this.discard = discard;
101         return this;
102     }
103
104     public boolean isSecure() {
105         return secure;
106     }
107
108     public CookieImpl setSecure(final boolean secure) {
109         this.secure = secure;
110         return this;
111     }
112
113     public int getVersion() {
114         return version;
115     }
116
117     public CookieImpl setVersion(final int version) {
118         this.version = version;
119         return this;
120     }
121
122     public boolean isHttpOnly() {
123         return httpOnly;
124     }
125
126     public CookieImpl setHttpOnly(final boolean httpOnly) {
127         this.httpOnly = httpOnly;
128         return this;
129     }
130
131     public Date getExpires() {
132         return expires;
133     }
134
135     public CookieImpl setExpires(final Date expires) {
136         this.expires = expires;
137         return this;
138     }
139
140     public String getComment() {
141         return comment;
142     }
143
144     public Cookie setComment(final String comment) {
145         this.comment = comment;
146         return this;
147     }
148
149     @Override
150     public boolean isSameSite() {
151         return sameSite;
152     }
153
154     @Override
155     public Cookie setSameSite(final boolean sameSite) {
156         this.sameSite = sameSite;
157         return this;
158     }
159
160     @Override
161     public String getSameSiteMode() {
162         return sameSiteMode;
163     }
164
165     @Override
166     public Cookie setSameSiteMode(final String mode) {
167         final String m = CookieSameSiteMode.lookupModeString(mode);
168         if (m != null) {
169             UndertowLogger.REQUEST_LOGGER.tracef("Setting SameSite mode to [%s] for cookie [%s]", m, this.name);
170             this.sameSiteMode = m;
171             this.setSameSite(true);
172         } else {
173             UndertowLogger.REQUEST_LOGGER.warnf(UndertowMessages.MESSAGES.invalidSameSiteMode(mode, Arrays.toString(CookieSameSiteMode.values())), "Ignoring specified SameSite mode [%s] for cookie [%s]", mode, this.name);
174         }
175         return this;
176     }
177 }
178