KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > view > RedirectView


1 /*
2  * $Id: RedirectView.java,v 1.7 2003/10/27 11:00:58 thusted Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/view/RedirectView.java,v $
4  */

5
6 package org.infohazard.maverick.view;
7
8 import java.io.IOException JavaDoc;
9 import java.net.URLEncoder JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import javax.servlet.ServletException JavaDoc;
14
15 import org.infohazard.maverick.flow.View;
16 import org.infohazard.maverick.flow.ViewContext;
17
18 /**
19  * This view causes a client redirect. If the model is a String,
20  * that overrides the path. ControllerContext params become query
21  * parameters. If the model is a Map, the key/value pairs are
22  * converted into parameters for the target URL, but this behavior
23  * is deprecated.
24  *
25  * The key "#" in context params (or Map model) is appended to
26  * the end of the redirect URL as a named anchor.
27  */

28 public class RedirectView implements View
29 {
30     /**
31      * Redirect string.
32      */

33     protected String JavaDoc target;
34
35     /**
36      * @param target is the URL for the redirect.
37      */

38     RedirectView(String JavaDoc target)
39     {
40         this.target = target;
41     }
42
43     /**
44      * Produces an http redirect.
45      *
46      * If the model property of the given ViewContext is an instance of
47      * java.util.Map, the entries are added as URL parameters.
48      * If the model property is an instance of String, it is used as-is for
49      * the target instead of the configured parameter.
50      *
51      * @param vctx The ViewContext containing a Map or String based model
52      * property
53      *
54      * @see View#go
55      */

56     public void go(ViewContext vctx) throws IOException JavaDoc, ServletException JavaDoc
57     {
58         String JavaDoc result = this.target;
59
60         if (vctx.getModel() instanceof Map JavaDoc)
61         {
62             result = this.addQueryParams(result, (Map JavaDoc)vctx.getModel());
63         }
64         else if (vctx.getModel() instanceof String JavaDoc)
65         {
66             result = (String JavaDoc)vctx.getModel();
67         }
68         
69         // Now, a separate step
70
result = this.addQueryParams(result, vctx.getViewParams());
71
72         // Just in case we need a session id
73
result = vctx.getRealResponse().encodeRedirectURL(result);
74         
75         vctx.getRealResponse().sendRedirect(result);
76     }
77     
78     /**
79      */

80     protected String JavaDoc addQueryParams(String JavaDoc start, Map JavaDoc params)
81     {
82         if (params == null || params.isEmpty())
83             return start;
84             
85         StringBuffer JavaDoc url = new StringBuffer JavaDoc(start);
86         
87         // If there is not already some parameters, we need a ?
88
boolean first = (start.indexOf('?') < 0);
89         
90         Iterator JavaDoc entries = params.entrySet().iterator();
91         while (entries.hasNext())
92         {
93             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
94             String JavaDoc key = entry.getKey().toString();
95
96             // The special key "#" is always tacked onto the end
97
if ("#".equals(key))
98                 continue;
99                 
100             if (first)
101             {
102                 url.append("?");
103                 first = false;
104             }
105             else
106                 url.append("&");
107
108             if (entry.getValue() instanceof Object JavaDoc[])
109             {
110                 Object JavaDoc[] values = (Object JavaDoc[])entry.getValue();
111                 for (int i = 0; i < values.length; i++)
112                 {
113                     if (i > 0)
114                         url.append("&");
115
116                     addQueryParam(url, key, values[i]);
117                 }
118             }
119             else
120             {
121                 addQueryParam(url, key, entry.getValue());
122             }
123         }
124         
125         Object JavaDoc namedAnchor = params.get("#");
126         if (namedAnchor != null)
127         {
128             url.append("#");
129             url.append(URLEncoder.encode(namedAnchor.toString()));
130             // :FIXME: encode is deprecated in Java 1.4 [thusted 2003-10-27]
131
}
132         
133         return url.toString();
134     }
135
136     protected void addQueryParam(StringBuffer JavaDoc url, String JavaDoc key, Object JavaDoc value) {
137         url.append(URLEncoder.encode(key));
138         // :FIXME: encode is deprecated in Java 1.4 [thusted 2003-10-27]
139
url.append("=");
140         url.append(URLEncoder.encode(value.toString()));
141         // :FIXME: encode is deprecated in Java 1.4 [thusted 2003-10-27]
142
}
143 }
Popular Tags