KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > common > actions > ExtendedRedirect


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.common.actions;
25
26 import java.util.StringTokenizer JavaDoc;
27
28 import org.infoglue.cms.exception.ConfigurationError;
29
30 import webwork.util.ServletValueStack;
31
32 /**
33  *
34  * @author Mattias Bogeblad
35  */

36
37 public class ExtendedRedirect extends InfoGlueAbstractAction
38 {
39
40     private static final long serialVersionUID = -8254805372363786462L;
41     
42     private static final String JavaDoc UNPARSED_PARAMETER_DELIMITER = "#";
43     
44     private String JavaDoc unparsedURL;
45
46     /**
47      *
48      */

49
50     public void setUrl(String JavaDoc unparsedURL)
51     {
52         this.unparsedURL = unparsedURL;
53     }
54
55     /**
56      *
57      */

58     
59     public String JavaDoc doExecute() throws Exception JavaDoc
60     {
61         validateUnparsedURL();
62         redirect();
63         return NONE;
64     }
65
66
67     /**
68      *
69      */

70     
71     private void validateUnparsedURL()
72     {
73         if (this.unparsedURL == null || this.unparsedURL.trim().length() == 0)
74         {
75             throw new ConfigurationError("No url/empty url specified for ExtendedRedirect.action");
76         }
77     }
78     
79     
80     /**
81      *
82      */

83     
84     private void redirect() throws Exception JavaDoc
85     {
86         final String JavaDoc url = getResponse().encodeRedirectURL(parse(this.unparsedURL));
87         getResponse().sendRedirect(url);
88     }
89     
90     /**
91      *
92      */

93     
94     private String JavaDoc parse(String JavaDoc unparsedURL)
95     {
96         final StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(unparsedURL.trim(), UNPARSED_PARAMETER_DELIMITER);
97         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc(st.nextToken()); // action
98
sb.append(st.hasMoreTokens() ? "?" : "");
99         while (st.hasMoreTokens())
100         {
101             sb.append(createParameterString(st.nextToken()));
102             sb.append(st.hasMoreTokens() ? "&" : "");
103         }
104         return sb.toString();
105     }
106     
107     /**
108      *
109      */

110     
111     private String JavaDoc createParameterString(String JavaDoc name)
112     {
113         return name + "=" + getValueFromCallingAction(name);
114     }
115     
116     /**
117      *
118      */

119
120     private String JavaDoc getValueFromCallingAction(String JavaDoc fieldName)
121     {
122         Object JavaDoc value = ServletValueStack.getStack(getRequest()).findValue(fieldName);
123         //Object value = ValueStack.getStack(getRequest()).findValue(fieldName);
124

125         if (value==null)
126             value = getRequest().getParameter(fieldName);
127         
128         if (value == null)
129         {
130             throw new ConfigurationError("Unable to find the value for the parameter [" + fieldName + "].");
131         }
132         
133         
134         return value.toString();
135     }
136
137
138 }
139
Popular Tags