KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > web > admin > hardtokeninterface > HardTokenInterfaceBean


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.ui.web.admin.hardtokeninterface;
15
16 import java.security.cert.X509Certificate JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21
22 import org.ejbca.core.ejb.ServiceLocator;
23 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocal;
24 import org.ejbca.core.ejb.authorization.IAuthorizationSessionLocalHome;
25 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocal;
26 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocalHome;
27 import org.ejbca.core.ejb.hardtoken.IHardTokenBatchJobSessionLocal;
28 import org.ejbca.core.ejb.hardtoken.IHardTokenBatchJobSessionLocalHome;
29 import org.ejbca.core.ejb.hardtoken.IHardTokenSessionLocal;
30 import org.ejbca.core.ejb.hardtoken.IHardTokenSessionLocalHome;
31 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionLocal;
32 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionLocalHome;
33 import org.ejbca.core.ejb.ra.IUserAdminSessionLocal;
34 import org.ejbca.core.ejb.ra.IUserAdminSessionLocalHome;
35 import org.ejbca.core.model.authorization.AdminGroup;
36 import org.ejbca.core.model.hardtoken.HardTokenData;
37 import org.ejbca.core.model.hardtoken.HardTokenIssuer;
38 import org.ejbca.core.model.hardtoken.HardTokenIssuerData;
39 import org.ejbca.core.model.hardtoken.HardTokenIssuerDoesntExistsException;
40 import org.ejbca.core.model.hardtoken.HardTokenIssuerExistsException;
41 import org.ejbca.core.model.log.Admin;
42 import org.ejbca.ui.web.admin.configuration.EjbcaWebBean;
43 import org.ejbca.ui.web.admin.configuration.InformationMemory;
44 import org.ejbca.ui.web.admin.rainterface.RAInterfaceBean;
45
46 /**
47  * A java bean handling the interface between EJBCA hard token module and JSP pages.
48  *
49  * @author Philip Vendil
50  * @version $Id: HardTokenInterfaceBean.java,v 1.1 2006/01/17 20:26:30 anatom Exp $
51  */

52 public class HardTokenInterfaceBean implements java.io.Serializable JavaDoc {
53
54     /** Creates new LogInterfaceBean */
55     public HardTokenInterfaceBean(){
56     }
57     // Public methods.
58
/**
59      * Method that initialized the bean.
60      *
61      * @param request is a reference to the http request.
62      */

63     public void initialize(HttpServletRequest JavaDoc request, EjbcaWebBean ejbcawebbean) throws Exception JavaDoc{
64
65       if(!initialized){
66         admin = new Admin(((X509Certificate JavaDoc[]) request.getAttribute( "javax.servlet.request.X509Certificate" ))[0]);
67             
68         final ServiceLocator locator = ServiceLocator.getInstance();
69         IHardTokenSessionLocalHome hardtokensessionhome = (IHardTokenSessionLocalHome) locator.getLocalHome(IHardTokenSessionLocalHome.COMP_NAME);
70         hardtokensession = hardtokensessionhome.create();
71
72         IHardTokenBatchJobSessionLocalHome hardtokenbatchsessionhome = (IHardTokenBatchJobSessionLocalHome) locator.getLocalHome(IHardTokenBatchJobSessionLocalHome.COMP_NAME);
73         hardtokenbatchsession = hardtokenbatchsessionhome.create();
74         
75         IAuthorizationSessionLocalHome authorizationsessionhome = (IAuthorizationSessionLocalHome) locator.getLocalHome(IAuthorizationSessionLocalHome.COMP_NAME);
76         IAuthorizationSessionLocal authorizationsession = authorizationsessionhome.create();
77
78         IUserAdminSessionLocalHome adminsessionhome = (IUserAdminSessionLocalHome) locator.getLocalHome(IUserAdminSessionLocalHome.COMP_NAME);
79         IUserAdminSessionLocal useradminsession = adminsessionhome.create();
80
81         ICertificateStoreSessionLocalHome certificatestorehome = (ICertificateStoreSessionLocalHome) locator.getLocalHome(ICertificateStoreSessionLocalHome.COMP_NAME);
82         ICertificateStoreSessionLocal certificatesession = certificatestorehome.create();
83
84         IKeyRecoverySessionLocalHome keyrecoverysessionhome = (IKeyRecoverySessionLocalHome) locator.getLocalHome(IKeyRecoverySessionLocalHome.COMP_NAME);
85         keyrecoverysession = keyrecoverysessionhome.create();
86         
87         initialized=true;
88         
89         this.informationmemory = ejbcawebbean.getInformationMemory();
90                       
91         this.hardtokenprofiledatahandler = new HardTokenProfileDataHandler(admin, hardtokensession, certificatesession, authorizationsession , useradminsession, informationmemory);
92         
93       }
94     }
95     
96     /* Returns the first found hard token for the given username. */
97     public HardTokenView getHardTokenViewWithUsername(String JavaDoc username) {
98       this.result=null;
99
100       Collection JavaDoc res = hardtokensession.getHardTokens(admin, username);
101       Iterator JavaDoc iter = res.iterator();
102       if(res.size() > 0) {
103         this.result = new HardTokenView[res.size()];
104         for(int i=0;iter.hasNext();i++) {
105           this.result[i]=new HardTokenView((HardTokenData) iter.next());
106         }
107         
108         if(this.result!= null && this.result.length > 0) {
109             return this.result[0];
110         }
111         
112       }
113       return null;
114     }
115     
116     public HardTokenView getHardTokenViewWithIndex(String JavaDoc username, int index) {
117         HardTokenView returnval=null;
118         
119         if(result == null)
120             getHardTokenViewWithUsername(username);
121         
122         if(result!=null)
123             if(index < result.length)
124                 returnval=result[index];
125         
126         return returnval;
127     }
128     
129     public int getHardTokensInCache() {
130         int returnval = 0;
131         if(result!=null)
132             returnval = result.length;
133         
134         return returnval;
135     }
136     
137     public HardTokenView getHardTokenView(String JavaDoc tokensn) {
138         HardTokenView returnval = null;
139         this.result=null;
140         HardTokenData token = hardtokensession.getHardToken(admin, tokensn);
141         if(token != null)
142             returnval = new HardTokenView(token);
143         
144         return returnval;
145     }
146     
147
148     
149     
150     public String JavaDoc[] getHardTokenIssuerAliases() {
151         return (String JavaDoc[]) hardtokensession.getHardTokenIssuers(admin).keySet().toArray(new String JavaDoc[0]);
152     }
153     
154     /** Returns the alias from id. */
155     public String JavaDoc getHardTokenIssuerAlias(int id) {
156         return hardtokensession.getHardTokenIssuerAlias(admin, id);
157     }
158     
159     public int getHardTokenIssuerId(String JavaDoc alias) {
160         return hardtokensession.getHardTokenIssuerId(admin, alias);
161     }
162     
163     public HardTokenIssuerData getHardTokenIssuerData(String JavaDoc alias) {
164         return hardtokensession.getHardTokenIssuerData(admin, alias);
165     }
166     
167     public HardTokenIssuerData getHardTokenIssuerData(int id) {
168         return hardtokensession.getHardTokenIssuerData(admin, id);
169     }
170     
171     public void addHardTokenIssuer(String JavaDoc alias, int admingroupid) throws HardTokenIssuerExistsException {
172         Iterator JavaDoc iter = this.informationmemory.getHardTokenIssuingAdminGroups().iterator();
173         while(iter.hasNext()){
174             if(((AdminGroup) iter.next()).getAdminGroupId() == admingroupid){
175                 if(!hardtokensession.addHardTokenIssuer(admin, alias, admingroupid, new HardTokenIssuer()))
176                     throw new HardTokenIssuerExistsException();
177                 informationmemory.hardTokenDataEdited();
178             }
179         }
180     }
181     
182     public void changeHardTokenIssuer(String JavaDoc alias, HardTokenIssuer hardtokenissuer) throws HardTokenIssuerDoesntExistsException {
183         if(informationmemory.authorizedToHardTokenIssuer(alias)){
184             if(!hardtokensession.changeHardTokenIssuer(admin, alias, hardtokenissuer))
185                 throw new HardTokenIssuerDoesntExistsException();
186             informationmemory.hardTokenDataEdited();
187         }
188     }
189     
190     /* Returns false if profile is used by any user or in authorization rules. */
191     public boolean removeHardTokenIssuer(String JavaDoc alias) {
192         boolean issuerused = false;
193         if(informationmemory.authorizedToHardTokenIssuer(alias)){
194             int issuerid = hardtokensession.getHardTokenIssuerId(admin, alias);
195             // Check if any users or authorization rule use the profile.
196

197             issuerused = hardtokenbatchsession.checkForHardTokenIssuerId(admin, issuerid);
198             
199             if(!issuerused){
200                 hardtokensession.removeHardTokenIssuer(admin, alias);
201                 informationmemory.hardTokenDataEdited();
202             }
203         }
204         return !issuerused;
205     }
206     
207     public void renameHardTokenIssuer(String JavaDoc oldalias, String JavaDoc newalias, int newadmingroupid) throws HardTokenIssuerExistsException {
208         if(informationmemory.authorizedToHardTokenIssuer(oldalias)){
209             if(!hardtokensession.renameHardTokenIssuer(admin, oldalias, newalias, newadmingroupid))
210                 throw new HardTokenIssuerExistsException();
211             
212             informationmemory.hardTokenDataEdited();
213         }
214     }
215     
216     public void cloneHardTokenIssuer(String JavaDoc oldalias, String JavaDoc newalias, int newadmingroupid) throws HardTokenIssuerExistsException {
217         if(informationmemory.authorizedToHardTokenIssuer(oldalias)){
218             if(!hardtokensession.cloneHardTokenIssuer(admin, oldalias, newalias, newadmingroupid))
219                 throw new HardTokenIssuerExistsException();
220             
221             informationmemory.hardTokenDataEdited();
222         }
223     }
224
225 /**
226  * Method that checks if a token is key recoverable and also check if the administrator is authorized to the action.
227  * @param tokensn
228  * @param rabean
229  * @return
230  */

231     
232     public boolean isTokenKeyRecoverable(String JavaDoc tokensn, String JavaDoc username, RAInterfaceBean rabean) throws Exception JavaDoc{
233       boolean retval = false;
234       X509Certificate JavaDoc keyRecCert = null;
235       
236       Collection JavaDoc result = hardtokensession.findCertificatesInHardToken(admin, tokensn);
237       Iterator JavaDoc iter = result.iterator();
238       while(iter.hasNext()){
239         X509Certificate JavaDoc cert = (X509Certificate JavaDoc) iter.next();
240         if(keyrecoverysession.existsKeys(admin,cert)){
241             keyRecCert = cert;
242         }
243       }
244             
245       if(keyRecCert != null){
246        retval = rabean.keyRecoveryPossible(keyRecCert,username);
247       }
248       
249       return retval;
250     }
251     
252     public void markTokenForKeyRecovery(String JavaDoc tokensn,String JavaDoc username, RAInterfaceBean rabean) throws Exception JavaDoc{
253         Collection JavaDoc result = hardtokensession.findCertificatesInHardToken(admin, tokensn);
254         Iterator JavaDoc iter = result.iterator();
255         while(iter.hasNext()){
256             X509Certificate JavaDoc cert = (X509Certificate JavaDoc) iter.next();
257             if(keyrecoverysession.existsKeys(admin,cert)){
258                 rabean.markForRecovery(username,cert);
259             }
260         }
261     }
262     
263     
264     public HardTokenProfileDataHandler getHardTokenProfileDataHandler() {
265         return hardtokenprofiledatahandler;
266     }
267     // Private fields.
268
private IHardTokenSessionLocal hardtokensession;
269     private IKeyRecoverySessionLocal keyrecoverysession;
270     private IHardTokenBatchJobSessionLocal hardtokenbatchsession;
271     private Admin admin;
272     private InformationMemory informationmemory;
273     private boolean initialized=false;
274     private HardTokenView[] result;
275     private HardTokenProfileDataHandler hardtokenprofiledatahandler;
276     
277 }
278
Popular Tags