All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.netty.handler.codec.http.ServerCookieEncoder Maven / Gradle / Ivy

Go to download

This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up with different versions on classes on the class path).

There is a newer version: 32.0.0.Final
Show newest version
/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you under the Apache License,
 * version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * 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 io.netty.handler.codec.http;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static io.netty.handler.codec.http.CookieEncoderUtil.*;

/**
 * Encodes server-side {@link Cookie}s into HTTP header values.  This encoder can encode
 * the HTTP cookie version 0, 1, and 2.
 * 
 * // Example
 * {@link HttpRequest} req = ...;
 * res.setHeader("Set-Cookie", {@link ServerCookieEncoder}.encode("JSESSIONID", "1234"));
 * 
* * @see CookieDecoder */ public final class ServerCookieEncoder { /** * Encodes the specified cookie into an HTTP header value. */ public static String encode(String name, String value) { return encode(new DefaultCookie(name, value)); } public static String encode(Cookie cookie) { if (cookie == null) { throw new NullPointerException("cookie"); } StringBuilder buf = stringBuilder(); add(buf, cookie.getName(), cookie.getValue()); if (cookie.getMaxAge() != Long.MIN_VALUE) { if (cookie.getVersion() != 0) { add(buf, CookieHeaderNames.MAX_AGE, cookie.getMaxAge()); } addUnquoted(buf, CookieHeaderNames.EXPIRES, HttpHeaderDateFormat.get().format( new Date(System.currentTimeMillis() + cookie.getMaxAge() * 1000L))); } if (cookie.getPath() != null) { if (cookie.getVersion() > 0) { add(buf, CookieHeaderNames.PATH, cookie.getPath()); } else { addUnquoted(buf, CookieHeaderNames.PATH, cookie.getPath()); } } if (cookie.getDomain() != null) { if (cookie.getVersion() > 0) { add(buf, CookieHeaderNames.DOMAIN, cookie.getDomain()); } else { addUnquoted(buf, CookieHeaderNames.DOMAIN, cookie.getDomain()); } } if (cookie.isSecure()) { buf.append(CookieHeaderNames.SECURE); buf.append((char) HttpConstants.SEMICOLON); buf.append((char) HttpConstants.SP); } if (cookie.isHttpOnly()) { buf.append(CookieHeaderNames.HTTPONLY); buf.append((char) HttpConstants.SEMICOLON); buf.append((char) HttpConstants.SP); } if (cookie.getVersion() >= 1) { if (cookie.getComment() != null) { add(buf, CookieHeaderNames.COMMENT, cookie.getComment()); } add(buf, CookieHeaderNames.VERSION, 1); if (cookie.getCommentUrl() != null) { addQuoted(buf, CookieHeaderNames.COMMENTURL, cookie.getCommentUrl()); } if (!cookie.getPorts().isEmpty()) { buf.append(CookieHeaderNames.PORT); buf.append((char) HttpConstants.EQUALS); buf.append((char) HttpConstants.DOUBLE_QUOTE); for (int port: cookie.getPorts()) { buf.append(port); buf.append((char) HttpConstants.COMMA); } buf.setCharAt(buf.length() - 1, (char) HttpConstants.DOUBLE_QUOTE); buf.append((char) HttpConstants.SEMICOLON); buf.append((char) HttpConstants.SP); } if (cookie.isDiscard()) { buf.append(CookieHeaderNames.DISCARD); buf.append((char) HttpConstants.SEMICOLON); buf.append((char) HttpConstants.SP); } } return stripTrailingSeparator(buf); } public static List encode(Cookie... cookies) { if (cookies == null) { throw new NullPointerException("cookies"); } List encoded = new ArrayList(cookies.length); for (Cookie c: cookies) { if (c == null) { break; } encoded.add(encode(c)); } return encoded; } public static List encode(Collection cookies) { if (cookies == null) { throw new NullPointerException("cookies"); } List encoded = new ArrayList(cookies.size()); for (Cookie c: cookies) { if (c == null) { break; } encoded.add(encode(c)); } return encoded; } public static List encode(Iterable cookies) { if (cookies == null) { throw new NullPointerException("cookies"); } List encoded = new ArrayList(); for (Cookie c: cookies) { if (c == null) { break; } encoded.add(encode(c)); } return encoded; } private ServerCookieEncoder() { // Unused } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy