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

com.github.t3t5u.common.http.Cookie Maven / Gradle / Ivy

The newest version!
package com.github.t3t5u.common.http;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("serial")
public class Cookie implements Serializable {
	public static final String REQUEST = "Cookie";
	public static final String RESPONSE = "Set-Cookie";
	private static final Logger LOGGER = LoggerFactory.getLogger(Cookie.class);
	private final String name;
	private final String value;

	public Cookie(final String name, final String value) {
		this.name = name;
		this.value = value;
	}

	public String getName() {
		return name;
	}

	public String getValue() {
		return value;
	}

	@Override
	public boolean equals(final Object o) {
		return (o == this) || ((o instanceof Cookie) && new EqualsBuilder().append(getName(), ((Cookie) o).getName()).append(getValue(), ((Cookie) o).getValue()).build());
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(getName()).append(getValue()).build();
	}

	@Override
	public String toString() {
		return name + "=" + value;
	}

	public static List parse(final Map> headerFields) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("headerFields: " + headerFields);
		}
		if ((headerFields == null) || headerFields.isEmpty()) {
			return null;
		}
		return parse(headerFields.get(StringUtils.lowerCase(RESPONSE)));
	}

	public static List parse(final List headerList) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("headerList: " + headerList);
		}
		if ((headerList == null) || headerList.isEmpty()) {
			return null;
		}
		final Map cookieMap = new LinkedHashMap();
		for (final String header : headerList) {
			put(cookieMap, header);
		}
		final List cookieList = new ArrayList(cookieMap.values());
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("cookieList: " + cookieList);
		}
		return cookieList;
	}

	public static List format(final List cookieList) {
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("cookieList: " + cookieList);
		}
		if ((cookieList == null) || cookieList.isEmpty()) {
			return null;
		}
		final Map headerMap = new LinkedHashMap();
		for (final Cookie cookie : cookieList) {
			put(headerMap, cookie);
		}
		final List headerList = new ArrayList(headerMap.values());
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("headerList: " + headerList);
		}
		return headerList;
	}

	private static void put(final Map cookieMap, final String header) {
		if (header == null) {
			return;
		}
		final Cookie cookie = new Cookie(header.replaceFirst("=.*$", "").trim(), header.replaceFirst("^[^=]+=", "").replaceFirst(";.*$", "").trim());
		cookieMap.put(cookie.getName(), cookie);
	}

	private static void put(final Map headerMap, final Cookie cookie) {
		if (cookie == null) {
			return;
		}
		headerMap.put(cookie.getName(), cookie.toString());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy