org.cacheonix.impl.cache.web.CookieSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cacheonix-core Show documentation
Show all versions of cacheonix-core Show documentation
Cacheonix is an open source distributed cache for Java that allows its users to scale Java applications in a cluster while preserving the simplicity of design and coding in a single Java VM.
The newest version!
/*
* Cacheonix Systems licenses this file to You under the LGPL 2.1
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.cacheonix.org/products/cacheonix/license-lgpl-2.1.htm
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.cacheonix.impl.cache.web;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.servlet.http.Cookie;
import static org.cacheonix.impl.net.serializer.SerializerUtils.readString;
import static org.cacheonix.impl.net.serializer.SerializerUtils.writeString;
/**
* A Cookie serializer.
*/
final class CookieSerializer {
private CookieSerializer() {
}
/**
* Writes a Cookie to a DataOutputStream
.
*
* @param cookie a Cookie to write.
* @param out the DataOutputStream
.
* @throws IOException if there was an error while writing to the DataOutputStream
.
*/
static void writeCookie(final Cookie cookie, final DataOutputStream out) throws IOException {
writeString(cookie.getName(), out);
writeString(cookie.getValue(), out);
writeString(cookie.getComment(), out);
writeString(cookie.getPath(), out);
out.writeBoolean(cookie.getSecure());
out.writeInt(cookie.getVersion());
out.writeInt(cookie.getMaxAge());
writeString(cookie.getDomain(), out);
}
/**
* Reads a Cookie from a DataInputStream
.
*
* @param in the DataInputStream
to read from.
* @throws IOException if there was an error while reading from the DataInputStream
.
*/
static Cookie readCookie(final DataInputStream in) throws IOException {
final String name = readString(in);
final String value = readString(in);
assert name != null;
final Cookie cookie = new Cookie(name, value);
cookie.setComment(readString(in));
cookie.setPath(readString(in));
cookie.setSecure(in.readBoolean());
cookie.setVersion(in.readInt());
cookie.setMaxAge(in.readInt());
final String domain = readString(in);
if (domain != null) {
cookie.setDomain(domain);
}
return cookie;
}
@SuppressWarnings("RedundantIfStatement")
static boolean equals(final Cookie thisCookie, final Cookie thatCookie) {
if (thisCookie.getMaxAge() != thatCookie.getMaxAge()) {
return false;
}
if (thisCookie.getSecure() != thatCookie.getSecure()) {
return false;
}
if (thisCookie.getVersion() != thatCookie.getVersion()) {
return false;
}
if (thisCookie.getName() != null ? !thisCookie.getName().equals(
thatCookie.getName()) : thatCookie.getName() != null) {
return false;
}
if (thisCookie.getValue() != null ? !thisCookie.getValue().equals(
thatCookie.getValue()) : thatCookie.getValue() != null) {
return false;
}
if (thisCookie.getComment() != null ? !thisCookie.getComment().equals(
thatCookie.getComment()) : thatCookie.getComment() != null) {
return false;
}
if (thisCookie.getDomain() != null ? !thisCookie.getDomain().equals(
thatCookie.getDomain()) : thatCookie.getDomain() != null) {
return false;
}
if (thisCookie.getPath() != null ? !thisCookie.getPath().equals(
thatCookie.getPath()) : thatCookie.getPath() != null) {
return false;
}
return true;
}
}