KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > stringreplacement > SessionInfoReplacer


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.core.stringreplacement;
21
22 import java.util.Locale JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 import org.apache.struts.Globals;
27
28 import com.sslexplorer.agent.DefaultAgentManager;
29 import com.sslexplorer.boot.PropertyClass;
30 import com.sslexplorer.boot.PropertyClassManager;
31 import com.sslexplorer.core.CoreUtil;
32 import com.sslexplorer.properties.Property;
33 import com.sslexplorer.properties.attributes.AttributeDefinition;
34 import com.sslexplorer.properties.impl.profile.ProfilePropertyKey;
35 import com.sslexplorer.properties.impl.userattributes.UserAttributeKey;
36 import com.sslexplorer.properties.impl.userattributes.UserAttributes;
37 import com.sslexplorer.security.AuthenticationScheme;
38 import com.sslexplorer.security.Constants;
39 import com.sslexplorer.security.LogonControllerFactory;
40 import com.sslexplorer.security.SessionInfo;
41
42 public class SessionInfoReplacer extends AbstractReplacementVariableReplacer {
43     
44     private SessionInfo sessionInfo;
45     
46     public SessionInfoReplacer(SessionInfo sessionInfo) {
47         super();
48         this.sessionInfo = sessionInfo;
49     }
50     
51     public static String JavaDoc replace(SessionInfo session, String JavaDoc input) {
52         VariableReplacement r = new VariableReplacement();
53         r.setSession(session);
54         return r.replace(input);
55     }
56
57     @Override JavaDoc
58     public String JavaDoc processReplacementVariable(Pattern JavaDoc pattern, Matcher JavaDoc matcher, String JavaDoc replacementPattern, String JavaDoc type, String JavaDoc key) throws Exception JavaDoc {
59         if (type.equalsIgnoreCase("property")) {
60             if (sessionInfo == null) {
61                 return null;
62             }
63             return Property.getProperty(new ProfilePropertyKey(CoreUtil.getCurrentPropertyProfileId(sessionInfo.getHttpSession()),
64                             sessionInfo.getUser().getPrincipalName(),
65                             key, sessionInfo.getUser().getRealm().getResourceId()));
66         } else if (type.equalsIgnoreCase("session")) {
67             if (sessionInfo == null) {
68                 return null;
69             }
70             if (key.equals("username")) {
71                 return sessionInfo.getUser().getPrincipalName();
72             } else if (key.equals("email")) {
73                 return sessionInfo.getUser().getEmail();
74             } else if (key.equals("fullname")) {
75                 return sessionInfo.getUser().getFullname();
76             } else if (key.equals("locale")) {
77                 Locale JavaDoc l = (Locale JavaDoc) sessionInfo.getHttpSession().getAttribute(Globals.LOCALE_KEY);
78                 ;
79                 return l == null ? Locale.getDefault().toString() : l.toString();
80             } else if (key.equals("clientProxyURL")) {
81                 String JavaDoc proxyURL = CoreUtil.getProxyURL(sessionInfo.getUser(),
82                     CoreUtil.getCurrentPropertyProfileId(sessionInfo.getHttpSession()));
83                 return proxyURL == null ? "" : proxyURL;
84             } else if (key.equals("password")) {
85                 /**
86                  * LDP - This is broken, I'm guessing that the VPN
87                  * client session is different from the browser
88                  * session so the scheme is not being found
89                  */

90                 AuthenticationScheme scheme = (AuthenticationScheme) sessionInfo.getHttpSession()
91                                 .getAttribute(Constants.AUTH_SESSION);
92                 if (scheme != null) {
93                     char[] pw = LogonControllerFactory.getInstance().getPasswordFromCredentials(scheme);
94                     return pw == null ? "" : new String JavaDoc(pw);
95                 } else {
96                     return "";
97                 }
98             } else if (key.equals("userAgent")) {
99                 return sessionInfo.getUserAgent();
100             } else {
101                 throw new Exception JavaDoc("Unknown key " + key + " for type " + type + ".");
102             }
103         } else if (type.equalsIgnoreCase("attr") || type.equals(UserAttributes.NAME)) {
104             if (sessionInfo == null) {
105                 return null;
106             }
107             PropertyClass propertyClass = PropertyClassManager.getInstance().getPropertyClass(UserAttributes.NAME);
108             AttributeDefinition def = (AttributeDefinition) propertyClass.getDefinition(key);
109             if (def == null) {
110                 VariableReplacement.log.warn("Invalid user attribute '" + key + "'");
111                 return null;
112             } else {
113                 return Property.getProperty(new UserAttributeKey(sessionInfo.getUser(), key));
114             }
115         } else if (type.equalsIgnoreCase("ticket")) {
116             if (sessionInfo == null) {
117                 return null;
118             }
119             if (key.equals("id")) {
120                 return (String JavaDoc) sessionInfo.getHttpSession().getAttribute(Constants.VPN_AUTHORIZATION_TICKET);
121
122             } if(key.equals("new")) {
123                 String JavaDoc agentAuthenticationTicket = DefaultAgentManager.getInstance().registerPendingAgent(sessionInfo);
124                 return agentAuthenticationTicket;
125             } else {
126                 throw new Exception JavaDoc("String replacement pattern for ticket only supports the 'id' key. I.e. ${ticket:id}");
127             }
128         }
129         return null;
130     }
131     
132 }
Popular Tags