home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-04-03 | 7.3 KB | 259 lines |
- /*
- ** Sambar Server JAVA Engine Implementation
- **
- ** Confidential Property of Tod Sambar
- ** (c) Copyright Tod Sambar 1999
- ** All rights reserved.
- */
- package com.sambar.javaeng;
-
- import java.net.URLEncoder;
- import java.util.Date;
- import java.util.Vector;
- import java.util.StringTokenizer;
- import java.util.Locale;
- import java.util.TimeZone;
- import java.util.NoSuchElementException;
- import java.text.SimpleDateFormat;
- import javax.servlet.http.Cookie;
-
- public final class SambarUtils
- {
- public final static String URLEncode(String str)
- {
- if (str == null)
- return null;
- return URLEncoder.encode(str);
- }
-
- /*
- ** This method decodes the given urlencoded string.
- **
- ** @param str the url-encoded string
- ** @return the decoded string
- ** @exception IllegalArgumentException If a '%' is not
- ** followed by a valid 2-digit hex number.
- */
- public final static String URLDecode(String str)
- throws IllegalArgumentException
- {
- if (str == null)
- return null;
-
- StringBuffer dec = new StringBuffer(); // decoded string output
- int strPos = 0;
- int strLen = str.length();
-
- dec.ensureCapacity(str.length());
- while (strPos < strLen)
- {
- int laPos; // lookahead position
-
- // look ahead to next URLencoded metacharacter, if any
- for (laPos = strPos; laPos < strLen; laPos++)
- {
- char laChar = str.charAt(laPos);
- if ((laChar == '+') || (laChar == '%'))
- break;
- }
-
- // if there were non-metacharacters, copy them all as a block
- if (laPos > strPos)
- {
- dec.append(str.substring(strPos,laPos));
- strPos = laPos;
- }
-
- // shortcut out of here if we're at the end of the string
- if (strPos >= strLen)
- break;
-
- // process next metacharacter
- char metaChar = str.charAt(strPos);
- if (metaChar == '+')
- {
- dec.append(' ');
- strPos++;
- continue;
- }
- else if (metaChar == '%')
- {
- try
- {
- dec.append((char)Integer.parseInt(str.substring(strPos + 1,
- strPos + 3), 16));
- }
- catch (NumberFormatException e)
- {
- throw new IllegalArgumentException("invalid hexadecimal "
- + str.substring(strPos + 1, strPos + 3)
- + " in URLencoded string (illegal unescaped '%'?)" );
- }
- catch (StringIndexOutOfBoundsException e)
- {
- throw new IllegalArgumentException("illegal unescaped '%' "
- + " in URLencoded string" );
- }
- strPos += 3;
- }
- }
-
- return dec.toString();
- }
-
- /*
- ** Parse a cookie header into an array of cookies as per
- ** RFC2109 - HTTP Cookies
- **
- ** @param cookieHdr The Cookie header value.
- */
- public static Cookie[] parseCookies(String cookieHdr)
- {
- Vector cookieJar = new Vector();
-
- if(cookieHdr == null || cookieHdr.length() == 0)
- return new Cookie[0];
-
- StringTokenizer stok = new StringTokenizer(cookieHdr, "; ");
- while (stok.hasMoreTokens())
- {
- try
- {
- String tok = stok.nextToken();
- int equals_pos = tok.indexOf('=');
- if (equals_pos > 0)
- {
- String name = URLDecode(tok.substring(0, equals_pos));
- String value = URLDecode(tok.substring(equals_pos + 1));
- cookieJar.addElement(new Cookie(name, value));
- }
- else if ( tok.length() > 0 && equals_pos == -1 )
- {
- String name = URLDecode(tok);
- cookieJar.addElement(new Cookie(name, ""));
- }
- }
- catch (IllegalArgumentException badcookie)
- {
- }
- catch (NoSuchElementException badcookie)
- {
- }
- }
-
- Cookie[] cookies = new Cookie[cookieJar.size()];
- cookieJar.copyInto(cookies);
- return cookies;
- }
-
- private static SimpleDateFormat cookieDate =
- new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss zz", Locale.US );
-
- static
- {
- cookieDate.setTimeZone(TimeZone.getTimeZone("GMT"));
- }
-
- /*
- ** Encode a cookie as per the Netscape Cookies specification. The
- ** resulting string can be used in a Set-Cookie header.
- **
- ** @param cookie The cookie to encode.
- ** @return A string following Netscape Cookies specification.
- */
- public static String encodeCookie(Cookie cookie)
- {
- StringBuffer buf = new StringBuffer( cookie.getName() );
- buf.append('=');
- buf.append(cookie.getValue());
-
- long age = cookie.getMaxAge();
- if (age > 0)
- {
- buf.append("; expires=");
- buf.append(cookieDate.format(
- new Date(System.currentTimeMillis() + (long)age * 1000 )));
- }
- else if (age == 0)
- {
- buf.append("; expires=");
- // Set expiration to the epoch to delete the cookie
- buf.append(cookieDate.format(new Date(0)));
- }
-
- if (cookie.getDomain() != null)
- {
- buf.append("; domain=");
- buf.append(cookie.getDomain());
- }
-
- if (cookie.getPath() != null)
- {
- buf.append("; path=");
- buf.append(cookie.getPath());
- }
-
- if (cookie.getSecure())
- buf.append("; secure");
-
- return buf.toString();
- }
-
- /*
- ** Parse a content-type header for the character encoding. If the
- ** content-type is null or there is no explicit character encoding,
- ** ISO-8859-1 is returned.
- **
- ** @param contentType a content type header.
- */
- public static String parseCharacterEncoding(String contentType)
- {
- int start;
- int end;
-
- if ((contentType == null) ||
- ((start = contentType.indexOf("charset="))) == -1 )
- {
- return "ISO-8859-1";
- }
-
- String encoding = contentType.substring(start + 8);
-
- if ((end = encoding.indexOf(";")) > -1)
- return encoding.substring(0, end);
- else
- return encoding;
- }
-
- /*
- ** Concatenate 2 paths, dealing with ..
- ** ( /a/b/c + d = /a/b/d, /a/b/c + ../d = /a/d )
- */
- public static String catPath(String lookupPath, String path)
- {
- // Cut off the last slash and everything beyond
- int index = lookupPath.lastIndexOf("/");
- lookupPath = lookupPath.substring(0, index);
-
- // Deal with .. by chopping dirs off the lookup path
- while (path.startsWith("../"))
- {
- if (lookupPath.length() > 0)
- {
- index = lookupPath.lastIndexOf("/");
- lookupPath = lookupPath.substring(0, index);
- }
- else
- {
- // More ..'s than dirs, return null
- return null;
- }
-
- index = path.indexOf("../") + 3;
- path = path.substring(index);
- }
-
- return lookupPath + "/" + path;
- }
- }
-