001: /*
002: * NEMESIS-FORUM.
003: * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
004: *
005: * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
006: *
007: * Copyright (C) 2001 Yasna.com. All rights reserved.
008: *
009: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
010: *
011: * NEMESIS-FORUM. is free software; you can redistribute it and/or
012: * modify it under the terms of the Apache Software License, Version 1.1,
013: * or (at your option) any later version.
014: *
015: * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
016: * application are parts of NEMESIS-FORUM and are distributed under
017: * same terms of licence.
018: *
019: *
020: * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
021: * and software developed by CoolServlets.com (http://www.coolservlets.com).
022: * and software developed by Yasna.com (http://www.yasna.com).
023: *
024: */
025: package org.nemesis.forum.util;
026:
027: import javax.servlet.http.Cookie;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: /**
032: * @author dlaurent
033: *
034: * :TODO:passer en config
035: */
036: public class CookieManager {
037:
038: private static final long SECOND = 1000;
039: private static final long MINUTE = 60 * SECOND;
040: private static final long HOUR = 60 * MINUTE;
041: private static final long DAY = 24 * HOUR;
042: private static final long WEEK = 7 * DAY;
043: public static final int MAX_COOKIE_AGE = (int) (WEEK / 1000) * 8;
044:
045: private final static int ENCODE_XORMASK = 0x5A;
046: private final static char ENCODE_DELIMETER = '\002';
047: private final static char ENCODE_CHAR_OFFSET1 = 'A';
048: private final static char ENCODE_CHAR_OFFSET2 = 'h';
049:
050: public static void setCookie(HttpServletResponse res, String name,
051: String value, int maxAge) {
052: Cookie oneCookie = new Cookie(name, value);
053: oneCookie.setMaxAge(maxAge);
054: oneCookie.setPath("/");
055: res.addCookie(oneCookie);
056: }
057:
058: /**
059: * Returns the specified Cookie object, or null if the cookie does not exist.
060: *
061: * @param request The HttpServletRequest object, known as "request" in a
062: * JSP page.
063: * @param name the name of the cookie.
064: * @return the Cookie object if it exists, otherwise null.
065: */
066: public static Cookie getCookie(HttpServletRequest request,
067: String name) {
068: Cookie cookies[] = request.getCookies();
069: if (cookies == null || name == null || name.length() == 0) {
070: return null;
071: }
072: //Otherwise, we have to do a linear scan for the cookie.
073: for (int i = 0; i < cookies.length; i++) {
074: if (cookies[i].getName().equals(name)) {
075: return cookies[i];
076: }
077: }
078: return null;
079: }
080:
081: /**
082: * Returns the value of the specified cookie as a String. If the cookie
083: * does not exist, the method returns null.
084: *
085: * @param request the HttpServletRequest object, known as "request" in a
086: * JSP page.
087: * @param name the name of the cookie
088: * @return the value of the cookie, or null if the cookie does not exist.
089: */
090: public static String getCookieValue(HttpServletRequest request,
091: String name) {
092: Cookie cookie = getCookie(request, name);
093: if (cookie != null) {
094: return cookie.getValue();
095: }
096: return null;
097: }
098:
099: /**
100: * Invalidate the specified cookie and delete it from the response object.
101: *
102: * @param request The HttpServletRequest object, known as "request" in a JSP page.
103: * @param response The HttpServletResponse object, known as "response" in a JSP page.
104: * @param cookieName The name of the cookie you want to delete.
105: */
106: public static void invalidateCookie(HttpServletRequest request,
107: HttpServletResponse response, String cookieName) {
108: Cookie cookie = new Cookie(cookieName, null); // invalidate cookie
109: cookie.setMaxAge(0); // deletes cookie
110: cookie.setPath("/");
111: response.addCookie(cookie);
112: }
113:
114: /**
115: * Builds a cookie string containing a username and password.<p>
116: *
117: * Note: with open source this is not really secure, but it prevents users
118: * from snooping the cookie file of others and by changing the XOR mask and
119: * character offsets, you can easily tweak results.
120: *
121: * @param username The username.
122: * @param password The password.
123: * @return String encoding the input parameters, an empty string if one of
124: * the arguments equals <code>null</code>.
125: */
126: public static String encodePasswordCookie(String username,
127: String password) {
128: StringBuffer buf = new StringBuffer();
129: if (username != null && password != null) {
130: byte[] bytes = (username + ENCODE_DELIMETER + password)
131: .getBytes();
132: int b;
133:
134: for (int n = 0; n < bytes.length; n++) {
135: b = bytes[n] ^ (ENCODE_XORMASK + n);
136: buf.append((char) (ENCODE_CHAR_OFFSET1 + (b & 0x0F)));
137: buf
138: .append((char) (ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F)));
139: }
140: }
141: return buf.toString();
142: }
143:
144: /**
145: * Unrafels a cookie string containing a username and password.
146: * @param value The cookie value.
147: * @return String[] containing the username at index 0 and the password at
148: * index 1, or <code>{ null, null }</code> if cookieVal equals
149: * <code>null</code> or the empty string.
150: */
151: public static String[] decodePasswordCookie(String cookieVal) {
152:
153: // check that the cookie value isn't null or zero-length
154: if (cookieVal == null || cookieVal.length() <= 0) {
155: return null;
156: }
157:
158: // unrafel the cookie value
159: char[] chars = cookieVal.toCharArray();
160: byte[] bytes = new byte[chars.length / 2];
161: int b;
162: for (int n = 0, m = 0; n < bytes.length; n++) {
163: b = chars[m++] - ENCODE_CHAR_OFFSET1;
164: b |= (chars[m++] - ENCODE_CHAR_OFFSET2) << 4;
165: bytes[n] = (byte) (b ^ (ENCODE_XORMASK + n));
166: }
167: cookieVal = new String(bytes);
168: int pos = cookieVal.indexOf(ENCODE_DELIMETER);
169: String username = (pos < 0) ? "" : cookieVal.substring(0, pos);
170: String password = (pos < 0) ? "" : cookieVal.substring(pos + 1);
171:
172: return new String[] { username, password };
173: }
174: }
|