001: /*
002: * Created on Mar 12, 2005
003: */
004: package com.sun.portal.wireless.htmlconversion.servlet;
005:
006: import java.io.UnsupportedEncodingException;
007: import java.net.URLEncoder;
008: import java.net.URLDecoder;
009: import java.net.URL;
010: import java.net.MalformedURLException;
011:
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014:
015: /**
016: * Utility class for encoding and decoding URLs
017: *
018: * @author ashwin.mathew@sun.com
019: */
020: public class URLTranscoder {
021:
022: private HttpServletRequest request;
023:
024: private HttpServletResponse response;
025:
026: private URL baseURL;
027:
028: private static final String CHARSET_UTF8 = "UTF-8";
029:
030: public static final String EQUALS = "=";
031: public static final String QUERY = "?";
032:
033: public URLTranscoder(HttpServletRequest request,
034: HttpServletResponse response) {
035: this .request = request;
036: this .response = response;
037:
038: String url = request.getParameter(URLScraper.URL_PARAM);
039:
040: if (url != null) {
041: String refererURLspec = URLTranscoder.decode(url);
042:
043: try {
044: this .baseURL = new URL(refererURLspec);
045: } catch (MalformedURLException e) {
046: // This should never happen,
047: // Since the URL has already been retrieved
048: e.printStackTrace();
049: }
050: }
051: }
052:
053: public static final String encode(String url) {
054: String encodedUrl = url;
055:
056: try {
057: encodedUrl = URLEncoder.encode(url, CHARSET_UTF8);
058: } catch (UnsupportedEncodingException e) {
059: // Do nothing, UTF-8 is supported
060: }
061:
062: return encodedUrl;
063: }
064:
065: public static final String decode(String url) {
066: String decodedUrl = url;
067:
068: try {
069: decodedUrl = URLDecoder.decode(url, CHARSET_UTF8);
070: } catch (UnsupportedEncodingException e) {
071: // Do nothing, UTF-8 is supported
072: }
073:
074: return decodedUrl;
075: }
076:
077: /**
078: * Returns the URL prefix, with the context path added to it.
079: *
080: * @return
081: */
082: public String getURLPrefix() {
083: return getContextPath() + URLScraper.URL_PREFIX;
084: }
085:
086: /**
087: * Returns the request context path.
088: * @return
089: */
090: public String getContextPath() {
091: return request.getContextPath();
092: }
093:
094: /**
095: * Rewrites a URL with the session id, and encodes it. This
096: * method will also prefix the current absolute path to the URL
097: * in case the URL is relative.
098: *
099: * @param url
100: * @return
101: */
102: public String rewriteURL(String url) {
103: url = resolveURL(url);
104:
105: StringBuffer rewriteUrlBuffer = new StringBuffer();
106:
107: rewriteUrlBuffer.append(getURLPrefix());
108:
109: rewriteUrlBuffer.append(URLScraper.URL_PARAM);
110: rewriteUrlBuffer.append(EQUALS);
111: rewriteUrlBuffer.append(URLTranscoder.encode(url));
112:
113: String rewriteUrl = response.encodeURL(rewriteUrlBuffer
114: .toString());
115:
116: return rewriteUrl;
117: }
118:
119: /**
120: * If a URL is relative, resolves it relative to the
121: * referer URL.
122: *
123: * @param url
124: * @return
125: */
126: public String resolveURL(String url) {
127: String resolvedURL = url;
128:
129: if (baseURL != null) {
130: try {
131: // Need to do this, in case url is relative
132: URL fullURL = new URL(baseURL, url);
133: resolvedURL = fullURL.toExternalForm();
134: } catch (MalformedURLException e) {
135: // Ignore - we'll try to proceed with the URL passed in
136: e.printStackTrace();
137: }
138: }
139:
140: return resolvedURL;
141: }
142:
143: public void setBaseURL(String newBaseURL) {
144: try {
145: // Need to do this, in case url is relative
146: baseURL = new URL(baseURL, newBaseURL);
147: } catch (MalformedURLException e) {
148: // Ignore - we'll try to proceed normally
149: e.printStackTrace();
150: }
151: }
152:
153: }
|