KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > security > web > HttpRequestUserSetup


1 /*
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */

16 package com.jdon.security.web;
17
18 import java.security.Principal JavaDoc;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21
22 import org.apache.log4j.Logger;
23
24 import com.jdon.container.visitor.data.SessionContext;
25 import com.jdon.container.visitor.data.SessionContextSetup;
26
27 /**
28  * load the informations about the login user, and
29  * save into sessioncontext that can be accessed in container.
30  *
31  * this class is confiured in container.xml, so it
32  * can be replcaed. if you want get more informations
33  * from the HttpServletRequest, you can extends this class.
34  * so , you can get these informations in the services
35  *
36  *
37  * @author <a HREF="mailto:banqiao@jdon.com">banq</a>
38  *
39  */

40
41 public class HttpRequestUserSetup implements SessionContextSetup {
42
43     private final static Logger logger = Logger.getLogger(HttpRequestUserSetup.class);
44
45     public void setup(SessionContext sessionContext, HttpServletRequest JavaDoc request) {
46         String JavaDoc remoteAddress = request.getRemoteAddr();
47         saveSessionContext(REMOTE_ADDRESS, remoteAddress, sessionContext);
48
49         Principal JavaDoc principal = request.getUserPrincipal();
50         if (principal == null)
51             return;
52         String JavaDoc username = principal.getName();
53         saveSessionContext(PRINCIPAL_NAME, username, sessionContext);
54
55         logger.debug("[JdonFramework] set principal name:" + username);
56     }
57
58     public String JavaDoc getPrincipalName(SessionContext sessionContext) {
59         return (String JavaDoc) getArrtibute(PRINCIPAL_NAME, sessionContext);
60     }
61
62     public void saveSessionContext(String JavaDoc arrtibuteName, String JavaDoc arrtibuteValue, SessionContext sessionContext) {
63         sessionContext.setArrtibute(arrtibuteName, arrtibuteValue);
64         logger.debug("[JdonFramework] setArrtibute:" + arrtibuteName + "=" + arrtibuteValue);
65     }
66
67     public Object JavaDoc getArrtibute(String JavaDoc arrtibuteName, SessionContext sessionContext) {
68         if (sessionContext == null) {
69             logger.debug("[JdonFramework] sessionContext not be setup, and is null");
70             return null;
71         }
72         return sessionContext.getArrtibute(arrtibuteName);
73     }
74
75 }
76
Popular Tags