com.hfg.webapp.HfgCookie Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.webapp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.Cookie;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
Cookie extension that handles encoding/decoding of cookie values.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
// TODO: Add a toString().
public class HfgCookie extends Cookie
{
private Date mExpires;
private String mDecodedValue;
private static final Pattern COOKIE_NAME_VALUE_PATTERN = Pattern.compile("([^=]+)=(.+)");
// Mon, 09-Dec-2002 13:46:00 GMT
private static final SimpleDateFormat EXPIRES_FORMAT = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public HfgCookie(String inName, String inValue)
{
super(inName, inValue);
setValue(inValue);
}
//---------------------------------------------------------------------------
public HfgCookie(Cookie inCookie)
{
this(inCookie.getName(), inCookie.getValue());
if (inCookie.getPath() != null)
{
setPath(inCookie.getPath());
}
if (inCookie.getComment() != null)
{
setComment(inCookie.getComment());
}
if (inCookie.getDomain() != null)
{
setDomain(inCookie.getDomain());
}
setMaxAge(inCookie.getMaxAge());
setHttpOnly(inCookie.isHttpOnly());
setSecure(inCookie.getSecure());
setVersion(inCookie.getVersion());
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
/**
* Returns one or more cookies from an http response's "Set-Cookie" or
* "Set-Cookie2" header field.
*
* "Set-Cookie" is covered by RFC2109.
* RFC 2965 section 3.2.2 says that one "Set-Cookie2" header line
* can contain more than one cookie definition.
*
* @param inSetCookieValue the value of the "Set-Cookie" or "Set-Cookie2" header
* @return one or more parsed cookies
*/
public static List parse(String inSetCookieValue)
{
List cookies = null;
// Ex: JSESSIONID=3AF5DD38614E80FCCC0632902E3C8E18; Path=/; Secure; HttpOnly
if (StringUtil.isSet(inSetCookieValue))
{
List cookieStrings = splitIntoCookieStrings(inSetCookieValue);
cookies = new ArrayList<>(cookieStrings.size());
for (String cookieString : cookieStrings)
{
HfgCookie currentCookie = null;
String[] pieces = cookieString.split(";");
for (String piece : pieces)
{
piece = piece.trim();
Matcher m = COOKIE_NAME_VALUE_PATTERN.matcher(piece);
if (m.matches())
{
String name = m.group(1).trim();
String value = m.group(2).trim();
if (name.equalsIgnoreCase("Path"))
{
if (currentCookie != null)
{
currentCookie.setPath(value);
}
}
else if (name.equalsIgnoreCase("Domain"))
{
if (currentCookie != null)
{
currentCookie.setDomain(value);
}
}
else if (name.equalsIgnoreCase("Version"))
{
if (currentCookie != null)
{
currentCookie.setVersion(Integer.parseInt(value));
}
}
else if (name.equalsIgnoreCase("Max-Age")) // Seconds to keep the cookie
{
if (currentCookie != null)
{
currentCookie.setMaxAge(Integer.parseInt(value));
}
}
else if (name.equalsIgnoreCase("Expires"))
{
if (currentCookie != null)
{
try
{
// Mon, 09-Dec-2002 13:46:00 GMT
currentCookie.setExpires(EXPIRES_FORMAT.parse(value));
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}
}
else
{
currentCookie = new HfgCookie(name, value);
cookies.add(currentCookie);
}
}
else if (piece.equalsIgnoreCase("Secure"))
{
if (currentCookie != null)
{
currentCookie.setSecure(true);
}
}
else if (piece.equalsIgnoreCase("HttpOnly"))
{
if (currentCookie != null)
{
currentCookie.setHttpOnly(true);
}
}
}
}
}
return cookies;
}
//---------------------------------------------------------------------------
@Override
public String toString()
{
return getName() + "=" + getValue();
}
//---------------------------------------------------------------------------
@Override
public void setValue(String inValue)
{
super.setValue(inValue);
// The incoming value could be encoded or decoded.
if (inValue != null)
{
if (WebappUtil.isCookieEncoded(this))
{
mDecodedValue = WebappUtil.decodeCookieValue(getValue());
// Sometimes we have double-encoded values. Try unwinding further...
while (WebappUtil.isCookieEncoded(mDecodedValue))
{
mDecodedValue = WebappUtil.decodeCookieValue(mDecodedValue);
}
}
else if (WebappUtil.cookieNeedsEncoding(this))
{
mDecodedValue = getValue();
setValue(WebappUtil.encodeCookieValue(getValue()));
}
else
{
mDecodedValue = getValue();
}
}
}
//---------------------------------------------------------------------------
public HfgCookie setExpires(Date inValue)
{
mExpires = inValue;
return this;
}
//---------------------------------------------------------------------------
public Date getExpires()
{
return mExpires;
}
//---------------------------------------------------------------------------
public String getDecodedValue()
{
return mDecodedValue;
}
//###########################################################################
// PRIVATE METHODS
//###########################################################################
//---------------------------------------------------------------------------
private static List splitIntoCookieStrings(String inValue)
{
List cookies;
// Only bother looking closely if the is a comma somewhere in the string
if (inValue.indexOf(",") > 0)
{
cookies = new ArrayList<>(2);
// Make sure the comma isn't inside of quotes
boolean inQuotes = false;
int startIndex = 0;
for (int i = 0; i < inValue.length(); i++)
{
char currentChar = inValue.charAt(i);
if (currentChar == '"')
{
inQuotes = ! inQuotes;
}
else if (currentChar == ','
&& ! inQuotes)
{
cookies.add(inValue.substring(startIndex, i));
startIndex = i + 1;
}
}
// Add whatever is remaining
cookies.add(inValue.substring(startIndex));
}
else
{
cookies = new ArrayList<>(1);
cookies.add(inValue);
}
return cookies;
}
}