KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > vr > VRAction


1 /*
2  * eAdmin/OWX
3  * Copyright (C) 1996-2003 OWX-Project Team <owx-team@gmx.net>
4  */

5  
6 package com.raptus.vr;
7
8 import java.io.IOException JavaDoc;
9 import java.util.Locale JavaDoc;
10
11 import javax.servlet.ServletException JavaDoc;
12 import javax.servlet.http.*;
13
14 import org.apache.struts.Globals;
15 import org.apache.struts.action.*;
16
17 import com.raptus.owxv3.*;
18
19 /**
20  *
21  * <hr>
22  * <table width="100%" border="0">
23  * <tr>
24  * <td width="24%"><b>Filename</b></td><td width="76%">VRAction.java</td>
25  * </tr>
26  * <tr>
27  * <td width="24%"><b>Author</b></td><td width="76%">Guy Zürcher (gzuercher@raptus.com)</td>
28  * </tr>
29  * <tr>
30  * <td width="24%"><b>Date</b></td><td width="76%">5th of March 2002</td>
31  * </tr>
32  * </table>
33  * <hr>
34  * <table width="100%" border="0">
35  * <tr
36  * <td width="24%"><b>Date / Author</b></td><td width="76%"><b>Changes</b></td>
37  * </tr>
38  * </table>
39  * <hr>
40  */

41 public class VRAction extends org.apache.struts.action.Action
42 {
43     public final static String JavaDoc VRLOCALE = Globals.LOCALE_KEY;
44     public final static String JavaDoc VRCURRENT = "_vr_current";
45     public final static String JavaDoc VRRESOLUTION = "_vr_res";
46     public final static String JavaDoc VRBROWSER = "_vr_browser";
47     public final static String JavaDoc VRJSCRIPT = "_vr_jscript";
48     public final static String JavaDoc VRFLASH = "_vr_flash";
49
50     /***
51      * Main VR backend to jsp's
52      *
53      * CMD can take the following values: swlocale
54      * swactive
55      *
56      * VALUE is dependent of CMD. Either a locale or some ID (for use in JSPs)
57      *
58      * NEXT is used to ovverride the forwarding to the default screen.
59      *
60      */

61     public ActionForward perform(ActionMapping mapping,
62                                  ActionForm form,
63                                  HttpServletRequest request,
64                                  HttpServletResponse response)
65                          throws IOException JavaDoc, ServletException JavaDoc
66     {
67         //
68
// Possible URL combinations:
69
//
70
// .../vrmenu/iface.do?cmd=swactive&value=home&next=/vrcontent/home.jsp
71
// .../vrmenu/iface.do?cmd=swlocale&value=de_CH&next=/vrcontent/home.jsp
72
// .../vrmenu/iface.do?res=big&flash=true&js=true&next=/vrcontent/home.jsp
73
// .../vrmenu/iface.do?locale=fr_CH&next=/vrcontent/home.jsp
74
// .../vrmenu/iface.do?cmd=csd&res=small&flash=false&js=false&next=...
75
//
76

77         VRBean myform = (VRBean) form;
78         String JavaDoc ovrloc = myform.getLocale();
79         String JavaDoc cmd = myform.getCmd();
80         String JavaDoc val = myform.getValue();
81         String JavaDoc nex = myform.getNext();
82         String JavaDoc res = myform.getRes();
83         boolean csd = myform.getCsdetect();
84         boolean js = myform.getJscript();
85         boolean fla = myform.getFlash();
86         boolean redirForward = true;
87
88         HttpSession session = request.getSession();
89
90         // explicitly switching locale. a correct locale for <value> is required.
91
if(cmd.equalsIgnoreCase("swlocale"))
92         {
93             if(val != null && verifyLocaleString(val))
94             {
95                 setLocale(session, val);
96                 LoggingManager.log("switched locale to " + val, this);
97             }
98             else
99             {
100                 setDefaultLocale(session);
101                 LoggingManager.log("invalid locale: " + val +
102                                    ". switched to default locale.", this);
103             }
104         }
105         else if(ovrloc != null)
106         {
107             // override locale if given together with swactive!!!
108
if(verifyLocaleString(ovrloc))
109             {
110                 setLocale(session, ovrloc);
111                 LoggingManager.log("switched locale to " + ovrloc + " !override mode!", this);
112             }
113             else
114                 LoggingManager.log("invalid override locale <" + ovrloc + "> doing nothing.", this);
115
116         }
117
118         // current
119
if(cmd.equalsIgnoreCase("swactive"))
120         {
121
122             if(val != null)
123             {
124                 session.setAttribute(VRCURRENT, val);
125                 LoggingManager.log("new current id is " + val, this);
126             }
127             else
128                 LoggingManager.log("invalid value for <swactive>: " + val, this);
129         }
130         
131         // return values from csd
132
if(cmd.equalsIgnoreCase("csd"))
133         {
134             myform.setCsdetect(false);
135             csd = false;
136             redirForward = false;
137             LoggingManager.log("client detection: res: " + res, this);
138             LoggingManager.log("client detection: flash: " + fla, this);
139             LoggingManager.log("client detection: js: " + js, this);
140             
141             // resolution
142
if(res != null && res.equalsIgnoreCase("big"))
143                 session.setAttribute(VRRESOLUTION, res);
144             else
145                 session.setAttribute(VRRESOLUTION, "small");
146             
147             session.setAttribute(VRFLASH, new Boolean JavaDoc(fla));
148             session.setAttribute(VRJSCRIPT, new Boolean JavaDoc(js));
149         }
150
151         // browser detection
152
Object JavaDoc o = session.getAttribute(VRBROWSER);
153         if(o == null)
154         {
155             String JavaDoc bro = detectBrowser(request.getHeader("User-Agent"));
156             session.setAttribute(VRBROWSER, bro);
157
158             LoggingManager.log("server detection: browser: " + bro, this);
159         }
160
161         // locale verify
162
String JavaDoc loc = ((Locale JavaDoc) session.getAttribute(VRLOCALE)).toString();
163         if(!verifyLocaleString(loc))
164         {
165             setDefaultLocale(session);
166             LoggingManager.log("switched to default locale!", this);
167         }
168         else
169             LoggingManager.log("current locale: " + loc, this);
170                 
171         // if clientside-detection is wanted
172
if(csd == true && nex != null)
173         {
174             // javascript & flash detection
175
LoggingManager.log("invoking clientside detection. (Next: " + nex + ")", this);
176             String JavaDoc enc = "ISO-8859-1";
177             String JavaDoc csdurl = "/owxv3/vrmenu/clientside-detection.jsp?next=" +
178                             java.net.URLEncoder.encode(nex, enc);
179             response.sendRedirect(csdurl);
180             return null; // quit here!
181
}
182
183         // next screen
184
if(nex != null && nex.length() > 0)
185         {
186             LoggingManager.log("redirecting to " + nex, this);
187             if(redirForward)
188                 request.getRequestDispatcher(nex).forward(request, response);
189             else
190                 response.sendRedirect(nex);
191         }
192         else
193             LoggingManager.log("no redirection", this);
194
195         return null;
196     }
197
198     /**
199      *
200      */

201     protected String JavaDoc detectBrowser(String JavaDoc ua)
202     {
203         String JavaDoc val = "msie";
204         if(ua != null && ua.length() > 0)
205         {
206             if(ua.toLowerCase().indexOf("msie") != -1)
207             {
208                 val = "msie";
209             }
210             else if(ua.toLowerCase().indexOf("gecko") != -1)
211             {
212                 val = "ns6";
213             }
214             else if(ua.toLowerCase().indexOf("mozilla") != -1)
215             {
216                 val = "ns4";
217             }
218         }
219         
220         return val;
221     }
222     
223     /**
224      *
225      */

226     protected boolean verifyLocaleString(String JavaDoc locale)
227     {
228         return( locale.equals("de_CH") ||
229                 locale.equals("fr_CH") ||
230                 locale.equals("en_GB") ||
231                 locale.equals("it_CH") );
232     }
233
234     /**
235      *
236      */

237     protected void setLocale(HttpSession session, String JavaDoc locale)
238     {
239         PairOfObjects po = LocaleManager.stripLocaleString(locale);
240         Locale JavaDoc l = new Locale JavaDoc((String JavaDoc) po.getObjectOne(),
241                               (String JavaDoc) po.getObjectTwo());
242         session.setAttribute(VRLOCALE, l);
243     }
244
245     /**
246      *
247      */

248     protected void setDefaultLocale(HttpSession session)
249     {
250         Locale JavaDoc l = new Locale JavaDoc("de", "CH");
251         session.setAttribute(VRLOCALE, l);
252     }
253
254 }
255
256 // eof
257
Popular Tags