KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > interceptor > LoginInterceptor


1 package org.javabb.interceptor;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import org.javabb.infra.UserContext;
7 import org.javabb.vo.User;
8
9 import com.opensymphony.xwork.Action;
10 import com.opensymphony.xwork.ActionContext;
11 import com.opensymphony.xwork.ActionInvocation;
12 import com.opensymphony.xwork.interceptor.AroundInterceptor;
13
14 /*
15  * Copyright 2004 JavaFree.org
16  *
17  * Licensed under the Apache License, Version 2.0 (the "License");
18  * you may not use this file except in compliance with the License.
19  * You may obtain a copy of the License at
20  *
21  * http://www.apache.org/licenses/LICENSE-2.0
22  *
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  */

29
30 /**
31  * $Id: LoginInterceptor.java,v 1.8.4.1.4.2 2006/04/17 17:47:08 daltoncamargo Exp $
32  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org </a> <br>
33  */

34 public class LoginInterceptor extends AroundInterceptor {
35
36     /**
37      * @param invocation
38      * @throws Exception
39      * @see com.opensymphony.xwork.interceptor.AroundInterceptor#before(com.opensymphony.xwork.ActionInvocation)
40      */

41     protected void before(ActionInvocation invocation) throws Exception JavaDoc {
42     
43     }
44
45     /**
46      * @param invocation
47      * @param result
48      * @throws Exception
49      * @see com.opensymphony.xwork.interceptor.AroundInterceptor#after(com.opensymphony.xwork.ActionInvocation,
50      * java.lang.String)
51      */

52     protected void after(ActionInvocation invocation, String JavaDoc result) throws Exception JavaDoc {
53         //
54
}
55
56     /**
57      * @see com.opensymphony.xwork.interceptor.Interceptor#intercept(com.opensymphony.xwork.ActionInvocation)
58      */

59     public String JavaDoc intercept(ActionInvocation invocation) throws Exception JavaDoc {
60         
61         boolean loggedIn = false;
62         User user = null;
63         ActionContext ctx = ActionContext.getContext();
64         Map JavaDoc session = ctx.getSession();
65         user = UserContext.getContext().getUser();
66
67         String JavaDoc guest = (String JavaDoc) session.get("jbbguest");
68
69         if ((guest != null) && (user != null)
70             && (user.getUser() != null)
71             && !"".equals(user.getUser())) {
72             loggedIn = true;
73             session.remove("jbbUrlBeforeLogin");
74         } else {
75             loggedIn = false;
76             if(!"login".equals(ctx.getName())){
77                 Map JavaDoc parameters = ctx.getParameters();
78                 String JavaDoc lastURL = "";
79     
80                 for (Iterator JavaDoc i = parameters.keySet().iterator(); i.hasNext();) {
81                     String JavaDoc param = (String JavaDoc) i.next();
82                     String JavaDoc[] value = (String JavaDoc[]) parameters.get(param);
83                     lastURL += (param + "=" + value[0] + "&");
84                 }
85     
86                 int lastLength = Math.max(0, lastURL.length() - 1);
87                 lastURL = lastURL.substring(0, lastLength);
88     
89                 lastURL = ctx.getName() + ".jbb?" + lastURL;
90                 //Insert the url on the session
91
session.put("jbbUrlBeforeLogin", lastURL);
92             }
93         }
94
95         if (loggedIn == false) {
96             // Envia o usuário para a tela de login
97
return Action.LOGIN;
98         }
99
100         // Usuário logado, deixa ele acessar o link desejado
101
String JavaDoc result = invocation.invoke();
102         after(invocation, result);
103
104         return result;
105     }
106 }
107
Popular Tags