KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lutris > appserver > server > httpPresentation > PageRedirectException


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  * $Id: PageRedirectException.java,v 1.1 2005/07/13 11:09:06 slobodan Exp $
23  */

24
25
26
27
28
29 package com.lutris.appserver.server.httpPresentation;
30 import java.net.URLEncoder JavaDoc;
31
32
33 /**
34  * Base class of page redirection classes. It is a subclass of Error
35  * rather than exception, as we only want the presentation manager
36  * to catch it.
37  */

38 abstract public class PageRedirectException extends Error JavaDoc {
39     /**
40      * URL that is the target of the redirect.
41      */

42     private String JavaDoc url;
43
44     /**
45      * Are there any arguments associated with the URL?
46      */

47     private boolean urlHasArgs = false;
48
49     /**
50      * Character encoding that will be used.
51      */

52     protected String JavaDoc encoding = "";
53
54     /**
55      * Constructor a redirect, saving the URL.
56      *
57      * @param url The url where the request should be redirected.
58      * It may have arguments encoded and/or they may be added with
59      * <CODE>addArgument</CODE>.
60      * @see PageRedirectException#addArgument
61      */

62     protected PageRedirectException(String JavaDoc url) {
63         this.url = url;
64         urlHasArgs = (url.lastIndexOf('?') >= 0);
65         this.encoding = "iso-8859-1"; //use default encoding
66
}
67
68          /**
69      * Constructor a redirect, saving the URL.
70      *
71      * @param url The url where the request should be redirected.
72      * It may have arguments encoded and/or they may be added with
73      * <CODE>addArgument</CODE>.
74      * @param encoding encoding to use in this redirect
75      * @see PageRedirectException#addArgument
76      */

77     protected PageRedirectException(String JavaDoc url, String JavaDoc encoding) {
78         this.url = url;
79         urlHasArgs = (url.lastIndexOf('?') >= 0);
80         this.encoding = encoding;
81     }
82
83
84     /**
85      * Add an argument to the URL.
86      *
87      * @param name Argument name.
88      * @param value Argument value.
89      */

90     public void addArgument( String JavaDoc name,
91                              String JavaDoc value ) {
92       try {
93         StringBuffer JavaDoc argStr = new StringBuffer JavaDoc();
94         if ( urlHasArgs ) {
95           argStr.append( "&" );
96         }
97         else {
98           argStr.append( "?" );
99         }
100       argStr.append( URLEncoder.encode( name, encoding ) );
101       if ( value != null ) {
102         argStr.append( "=" );
103         argStr.append( URLEncoder.encode( value, encoding ) );
104         }
105         url += argStr;
106         urlHasArgs = true;
107       }
108       catch ( Exception JavaDoc e ) {
109         e.printStackTrace();
110       }
111
112     }
113
114    /**
115      * Get the redirection URL associated with this redirect.
116      *
117      * @return The URL string.
118      */

119     public String JavaDoc getUrl() {
120         return url;
121     }
122
123     /**
124      * Get encoding for this redirect.
125      * @return encoding
126      */

127     public String JavaDoc getEncoding() {
128         return encoding;
129     }
130
131 }
132
Popular Tags