KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > examples > xmlc > RedirectEx1


1 package org.enhydra.barracuda.examples.xmlc;
2
3 import java.io.*;
4 import java.util.*;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7
8 import org.w3c.dom.*;
9 import org.w3c.dom.html.*;
10 import org.enhydra.xml.xmlc.*;
11
12 import org.enhydra.barracuda.core.comp.*;
13 import org.enhydra.barracuda.core.comp.helper.*;
14 import org.enhydra.barracuda.core.view.*;
15 import org.enhydra.barracuda.core.util.dom.*;
16 import org.enhydra.barracuda.plankton.http.*;
17 import org.enhydra.barracuda.examples.xmlc.data.*;
18
19 /**
20  *
21  */

22 public class RedirectEx1 extends ComponentGateway {
23
24     public static boolean DEFAULT_NOBACK = true; //default value
25

26     //-------------------- ComponentGateway ----------------------
27
/**
28      * Handle the default HttpRequest.
29      */

30     public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException {
31         //load the localized DOM template
32
Document page = DefaultDOMLoader.getGlobalInstance().getDOM(RedirectEx1HTML.class);
33
34         //create a template component and bind it to the views
35
BTemplate templateComp = new BTemplate(new SimpleTemplateModel(req, resp));
36         templateComp.setView(new DefaultTemplateView(page.getElementById("TemplateID")));
37         root.addChild(templateComp);
38
39         //return the page
40
return page;
41     }
42
43     /**
44      * <p>Get a DOMWriter. By default, we use a DefaultDOMWriter. If
45      * you'd like to use something else, override this method.
46      *
47      * @return a DOMWriter to be used to render the DOM
48      */

49     public DOMWriter getDOMWriter() {
50         //return DefaultDOM writer
51
return new DefaultDOMWriter(true);
52     }
53
54     //--------------- UserReportModel ------------------------
55
/**
56      * SimpleTemplateModel
57      */

58     class SimpleTemplateModel extends AbstractTemplateModel {
59         HttpServletRequest req = null;
60         HttpServletResponse resp = null;
61         Integer JavaDoc cntr = null;
62         Boolean JavaDoc noBack = null;
63         String JavaDoc targetURL = "RedirectEx1";
64
65         public SimpleTemplateModel(HttpServletRequest ireq, HttpServletResponse iresp) {
66             req = ireq;
67             resp = iresp;
68
69             //adjust the cntr
70
HttpSession session = req.getSession(true);
71             cntr = (Integer JavaDoc) session.getAttribute("cntr");
72             if (cntr==null || req.getParameter("clear")!=null) cntr = new Integer JavaDoc(0);
73             else cntr = new Integer JavaDoc(cntr.intValue()+1);
74             session.setAttribute("cntr", cntr);
75
76             //figure out whether or not we are allowing back button
77
noBack = (Boolean JavaDoc) session.getAttribute("noback");
78             if (noBack==null) noBack = new Boolean JavaDoc(DEFAULT_NOBACK);
79             if (req.getParameter("noback")!=null) noBack = new Boolean JavaDoc(true);
80             else if (req.getParameter("okback")!=null) noBack = new Boolean JavaDoc(false);
81             session.setAttribute("noback", noBack);
82         }
83
84         //register the model by name
85
public String JavaDoc getName() {
86             return "Rewrite";
87         }
88         
89         //provide items by key
90
public Object JavaDoc getItem(String JavaDoc key) {
91             ViewContext vc = getViewContext();
92         
93             //rewriting val
94
if (key.equals("RewritingVal")) {
95                 return new Boolean JavaDoc(URLRewriter.REWRITE_URLS);
96                 
97             //back button val
98
} else if (key.equals("BackButtonVal")) {
99                 return noBack.booleanValue() ? "disabling" : "enabling";
100                 
101             //servlet encoded url
102
} else if (key.equals("ServletEncode")) {
103 // String url = resp.encodeURL(targetURL+"?cntr="+cntr);
104
// String url = resp.encodeURL(targetURL);
105
//System.out.println("Servlet encoded URL:"+url);
106
String JavaDoc text = "Target URL = "+resp.encodeURL(targetURL);
107                 BLink blink = new BLink(text, targetURL, vc);
108                 blink.setDisableBackButton(noBack.booleanValue());
109                 return blink;
110                 
111             //barracuda encoded url
112
} else if (key.equals("BarracudaEncode")) {
113 // String url = URLRewriter.encodeURL(req, resp, targetURL+"?cntr="+cntr);
114
// String url = URLRewriter.encodeURL(req, resp, targetURL);
115
//System.out.println("Barracuda encoded URL:"+url);
116
String JavaDoc text = "Target URL = "+URLRewriter.encodeURL(req, resp, targetURL);;
117                 BLink blink = new BLink(text, targetURL, vc);
118                 blink.setDisableBackButton(noBack.booleanValue());
119                 return blink;
120                 
121             //barracuda encoded url
122
} else if (key.equals("EncodeForm1")) {
123 // String url = URLRewriter.encodeURL(req, resp, targetURL);
124
// BAction baction = new BAction(url);
125
BAction baction = new BAction(targetURL);
126                 baction.setParam("foo","bar?bar/bar=bar~bar^bar,bar;bar");
127                 baction.setDisableBackButton(noBack.booleanValue());
128                 return baction;
129
130             //barracuda encoded url
131
} else if (key.equals("EncodeForm2")) {
132 // String url = URLRewriter.encodeURL(req, resp, targetURL);
133
// BAction baction = new BAction(url);
134
BAction baction = new BAction(targetURL);
135                 baction.setDisableBackButton(noBack.booleanValue());
136                 return baction;
137
138             //barracuda encoded url
139
} else if (key.equals("EncodeForm3")) {
140 // String url = URLRewriter.encodeURL(req, resp, targetURL);
141
// BAction baction = new BAction(url);
142
BAction baction = new BAction(targetURL);
143                 baction.setDisableBackButton(noBack.booleanValue());
144                 return baction;
145 // BInput binput = new BInput(BInput.BUTTON, "name", "push me!", null, vc);
146
// binput.setDisableBackButton(noBack.booleanValue());
147
// return binput;
148

149             //barracuda encoded url
150
} else if (key.equals("EncodeForm4")) {
151 // String url = URLRewriter.encodeURL(req, resp, targetURL);
152
// BAction baction = new BAction(url);
153
BAction baction = new BAction(targetURL);
154                 baction.setDisableBackButton(noBack.booleanValue());
155                 return baction;
156
157             //session cntr
158
} else if (key.equals("Cntr")) {
159                 return cntr;
160                 
161             //reset cntr link
162
} else if (key.equals("ResetLink")) {
163                 String JavaDoc text = "Reset Cntr";
164 // String url = URLRewriter.encodeURL(req, resp, targetURL+"?clear=true");
165
String JavaDoc url = targetURL+"?clear=true";
166                 BLink blink = new BLink(text, url, vc);
167                 blink.setDisableBackButton(noBack.booleanValue());
168                 return blink;
169                 
170             //toggle back button link
171
} else if (key.equals("ToggleLink")) {
172                 String JavaDoc text = (noBack.booleanValue() ? "Enable back button" : "Disable back button");
173 // String url = URLRewriter.encodeURL(req, resp, targetURL+(noBack.booleanValue() ? "?okback=true" : "?noback=true"));
174
String JavaDoc url = targetURL+(noBack.booleanValue() ? "?okback=true" : "?noback=true");
175                 BLink blink = new BLink(text, url, vc);
176                 blink.setDisableBackButton(noBack.booleanValue());
177                 return blink;
178                 
179             //anything else...
180
} else return super.getItem(key);
181         }
182     }
183
184
185
186     //xmlc factory (ok as static because its threadsafe)
187
// private static XMLCFactory xmlcFactory = null;
188

189     //-------------------- ComponentGateway ----------------------
190
/**
191      * Handle the default HttpRequest.
192      */

193 /*
194     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
195         //adjust the cntr
196         HttpSession session = req.getSession(true);
197         Integer i = (Integer) session.getAttribute("cntr");
198         if (i==null || req.getParameter("clear")!=null) i = new Integer(0);
199         else i = new Integer(i.intValue()+1);
200         session.setAttribute("cntr", i);
201
202         //figure out whether or not we are allowing back button
203         Boolean noBack = (Boolean) session.getAttribute("noback");
204         if (noBack==null || req.getParameter("okback")!=null) noBack = new Boolean(false);
205         else if (req.getParameter("noback")!=null) noBack = new Boolean(true);
206         session.setAttribute("noback", noBack);
207
208         //write a response
209         resp.setContentType("text/html");
210         PrintWriter out = resp.getWriter();
211         out.println("<html>");
212         out.println(" <head><title>URL Rewrite Test</title></head>");
213         out.println(" <body>");
214         out.println(" <h1>URL Rewrite / Back Button Test</h1>");
215         
216         out.println(" <p>This example tests URL Rewriting in conjunction with disabling of the back button. Barracuda URL Rewriting is currently set to <strong>"+URLRewriter.REWRITE_URLS+"</strong>. To modify this value, you will need to adjust the URLRewriter.REWRITE_URLS variable.We are currently <strong>"+(noBack.booleanValue() ? "disabling" : "enabling")+"</strong> the back button");
217
218         String targetURL = "RedirectEx1";
219         String encodedURL1a = resp.encodeURL(targetURL);
220         String encodedURL1b = URLRewriter.encodeURL(req, resp, targetURL);
221         String encodedURL2 = URLRewriter.encodeURL(req, resp, targetURL+"?clear=true");
222         String encodedURL3 = URLRewriter.encodeURL(req, resp, targetURL+(noBack.booleanValue() ? "?okback=true" : "?noback=true"));
223         
224         out.println(" <p>Servlet Encoded URL:<a HREF=\""+encodedURL1a+"\">Target URL = "+encodedURL1a+"</a> (May not always work in older versions of Tomcat or Enhydra 3.x...if this link does not match the link below, you're server probably does not support implement the Servlet URL encoding methods properly.)</p>");
225         out.println(" <p>Barracuda Encoded URL:<a HREF=\""+encodedURL1b+"\">Target URL = "+encodedURL1b+"</a> </p>");
226         out.println(" <p>(Cntr = "+i+")");
227         out.println(" <p><a HREF=\""+encodedURL2+"\">(Reset cntr)</a></p>");
228         out.println(" <p><a HREF=\""+encodedURL3+"\">("+(noBack.booleanValue() ? "Enable back button" : "Disable back button")+")</a></p>");
229         out.println(" </body>");
230         out.println("</html>");
231
232     }
233 */

234
235 }
236
Popular Tags