001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: * $Id: PageRedirectException.java,v 1.2 2006-06-15 13:40:47 sinisa Exp $
022: */
023:
024: package com.lutris.appserver.server.httpPresentation;
025:
026: import java.net.URLEncoder;
027:
028: /**
029: * Base class of page redirection classes. It is a subclass of Error
030: * rather than exception, as we only want the presentation manager
031: * to catch it.
032: */
033: abstract public class PageRedirectException extends Error {
034: /**
035: * URL that is the target of the redirect.
036: */
037: private String url;
038:
039: /**
040: * Are there any arguments associated with the URL?
041: */
042: private boolean urlHasArgs = false;
043:
044: /**
045: * Character encoding that will be used.
046: */
047: protected String encoding = "";
048:
049: /**
050: * Constructor a redirect, saving the URL.
051: *
052: * @param url The url where the request should be redirected.
053: * It may have arguments encoded and/or they may be added with
054: * <CODE>addArgument</CODE>.
055: * @see PageRedirectException#addArgument
056: */
057: protected PageRedirectException(String url) {
058: this .url = url;
059: urlHasArgs = (url.lastIndexOf('?') >= 0);
060: this .encoding = "iso-8859-1"; //use default encoding
061: }
062:
063: /**
064: * Constructor a redirect, saving the URL.
065: *
066: * @param url The url where the request should be redirected.
067: * It may have arguments encoded and/or they may be added with
068: * <CODE>addArgument</CODE>.
069: * @param encoding encoding to use in this redirect
070: * @see PageRedirectException#addArgument
071: */
072: protected PageRedirectException(String url, String encoding) {
073: this .url = url;
074: urlHasArgs = (url.lastIndexOf('?') >= 0);
075: this .encoding = encoding;
076: }
077:
078: /**
079: * Add an argument to the URL.
080: *
081: * @param name Argument name.
082: * @param value Argument value.
083: */
084: public void addArgument(String name, String value) {
085: try {
086: StringBuffer argStr = new StringBuffer();
087: if (urlHasArgs) {
088: argStr.append("&");
089: } else {
090: argStr.append("?");
091: }
092: argStr.append(URLEncoder.encode(name, encoding));
093: if (value != null) {
094: argStr.append("=");
095: argStr.append(URLEncoder.encode(value, encoding));
096: }
097: url += argStr;
098: urlHasArgs = true;
099: } catch (Exception e) {
100: e.printStackTrace();
101: }
102:
103: }
104:
105: /**
106: * Get the redirection URL associated with this redirect.
107: *
108: * @return The URL string.
109: */
110: public String getUrl() {
111: return url;
112: }
113:
114: /**
115: * Get encoding for this redirect.
116: * @return encoding
117: */
118: public String getEncoding() {
119: return encoding;
120: }
121:
122: }
|