KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > approval > approvalrequests > KeyRecoveryApprovalRequest


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 package org.ejbca.core.model.approval.approvalrequests;
14
15 import java.io.IOException JavaDoc;
16 import java.io.ObjectInput JavaDoc;
17 import java.io.ObjectOutput JavaDoc;
18 import java.security.cert.CertificateEncodingException JavaDoc;
19 import java.security.cert.CertificateException JavaDoc;
20 import java.security.cert.X509Certificate JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.ejb.CreateException JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26
27 import org.apache.log4j.Logger;
28 import org.ejbca.core.ejb.ServiceLocator;
29 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionLocal;
30 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionLocalHome;
31 import org.ejbca.core.model.approval.ApprovalDataText;
32 import org.ejbca.core.model.approval.ApprovalDataVO;
33 import org.ejbca.core.model.approval.ApprovalException;
34 import org.ejbca.core.model.approval.ApprovalRequest;
35 import org.ejbca.core.model.approval.ApprovalRequestExecutionException;
36 import org.ejbca.core.model.approval.WaitingForApprovalException;
37 import org.ejbca.core.model.authorization.AuthorizationDeniedException;
38 import org.ejbca.core.model.log.Admin;
39 import org.ejbca.util.Base64;
40 import org.ejbca.util.CertTools;
41
42 /**
43  * Approval Request created when an adminsitrator wants
44  * to recovery a end entities keyset
45  *
46  *
47  *
48  * @author Philip Vendil
49  * @version $Id: KeyRecoveryApprovalRequest.java,v 1.2 2006/09/29 13:46:21 anatom Exp $
50  */

51 public class KeyRecoveryApprovalRequest extends ApprovalRequest {
52
53     private static final long serialVersionUID = -1L;
54
55     private static final Logger log = Logger.getLogger(KeyRecoveryApprovalRequest.class);
56     
57     private static final int LATEST_VERSION = 1;
58         
59     private String JavaDoc username;
60     private X509Certificate JavaDoc cert;
61     
62     
63     private boolean recoverNewestCert = false;
64     
65     
66     
67     /**
68      * Constuctor used in externaliziation only
69      */

70     public KeyRecoveryApprovalRequest() {}
71
72
73     public KeyRecoveryApprovalRequest(X509Certificate JavaDoc cert, String JavaDoc username, boolean recoverNewestCert, Admin requestAdmin, String JavaDoc requestSignature, int numOfReqApprovals, int cAId, int endEntityProfileId) {
74         super(requestAdmin, requestSignature, REQUESTTYPE_SIMPLE,
75                 numOfReqApprovals, cAId, endEntityProfileId);
76         this.username = username;
77         this.cert = cert;
78         this.recoverNewestCert = recoverNewestCert;
79     }
80
81
82     public void execute() throws ApprovalRequestExecutionException {
83         log.debug("Executing mark for recovery for user:" + username);
84         try{
85             ServiceLocator locator = ServiceLocator.getInstance();
86             IKeyRecoverySessionLocalHome keyrechome = (IKeyRecoverySessionLocalHome) locator.getLocalHome(IKeyRecoverySessionLocalHome.COMP_NAME);
87             IKeyRecoverySessionLocal keyrecsession = keyrechome.create();
88
89             if(recoverNewestCert){
90                 keyrecsession.markNewestAsRecoverable(getRequestAdmin(), username, getEndEntityProfileId());
91             }else{
92                 keyrecsession.markAsRecoverable(getRequestAdmin(), cert, getEndEntityProfileId());
93             }
94  
95             
96         }catch (CreateException JavaDoc e) {
97             throw new ApprovalRequestExecutionException("Error creating new userdata session", e);
98         } catch (AuthorizationDeniedException e) {
99             throw new ApprovalRequestExecutionException("Authorization Denied :" + e.getMessage(), e);
100         } catch (ApprovalException e) {
101             throw new EJBException JavaDoc("This should never happen",e);
102         } catch (WaitingForApprovalException e) {
103             throw new EJBException JavaDoc("This should never happen",e);
104         }
105
106     }
107
108     /**
109      * Approval Id is genereated of This approval type (i.e AddEndEntityApprovalRequest) and UserName
110      */

111     public int generateApprovalId() {
112         return new String JavaDoc(getApprovalType() + ";" + username).hashCode();
113     }
114
115
116     public int getApprovalType() {
117         return ApprovalDataVO.APPROVALTYPE_KEYRECOVERY;
118     }
119
120
121     public List JavaDoc getNewRequestDataAsText(Admin admin) {
122         ArrayList JavaDoc retval = new ArrayList JavaDoc();
123         retval.add(new ApprovalDataText("USERNAME",username,true,false));
124         retval.add(new ApprovalDataText("CERTSERIALNUMBER",cert.getSerialNumber().toString(16),true,false));
125         retval.add(new ApprovalDataText("SUBJECTDN",cert.getSubjectDN().toString(),true,false));
126         retval.add(new ApprovalDataText("ISSUERDN",cert.getIssuerDN().toString(),true,false));
127         return retval;
128     }
129     
130     public List JavaDoc getOldRequestDataAsText(Admin admin) {
131         return null;
132     }
133
134
135     public boolean isExecutable() {
136         return true;
137     }
138     
139     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
140         super.writeExternal(out);
141         out.writeInt(LATEST_VERSION);
142         out.writeObject(username);
143         out.writeBoolean(recoverNewestCert);
144         try {
145             String JavaDoc certString = new String JavaDoc(Base64.encode(cert.getEncoded()),"UTF8");
146             out.writeObject(certString);
147         } catch (CertificateEncodingException JavaDoc e) {
148             log.debug("Error serializing certificate", e);
149             throw new IOException JavaDoc(e.getMessage());
150         }
151         
152     }
153
154     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
155         super.readExternal(in);
156         int version = in.readInt();
157         if(version == 1){
158             username = (String JavaDoc) in.readObject();
159             recoverNewestCert = in.readBoolean();
160             String JavaDoc certString = (String JavaDoc) in.readObject();
161             try {
162                 cert = CertTools.getCertfromByteArray(Base64.decode(certString.getBytes("UTF8")));
163             } catch (CertificateException JavaDoc e) {
164                 log.debug("Error deserializing certificate", e);
165                 throw new IOException JavaDoc(e.getMessage());
166             }
167         }
168
169     }
170
171 }
172
Popular Tags