KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > cli > CaImportProfilesCommand


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.cli;
15
16 import java.beans.XMLDecoder JavaDoc;
17 import java.io.File JavaDoc;
18 import java.io.FileInputStream JavaDoc;
19
20 import org.ejbca.core.model.SecConst;
21 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfile;
22 import org.ejbca.core.model.ca.certificateprofiles.CertificateProfileExistsException;
23 import org.ejbca.core.model.ra.raadmin.EndEntityProfile;
24 import org.ejbca.core.model.ra.raadmin.EndEntityProfileExistsException;
25
26
27
28 /**
29  * Export profiles from the databse to XML-files.
30  *
31  * @version $Id: CaImportProfilesCommand.java,v 1.1 2006/01/17 20:28:05 anatom Exp $
32  */

33 public class CaImportProfilesCommand extends BaseCaAdminCommand {
34     /**
35      * Creates a new instance of CaImportProfilesCommand
36      *
37      * @param args command line arguments
38      */

39     public CaImportProfilesCommand(String JavaDoc[] args) {
40         super(args);
41     }
42
43     /**
44      * Runs the command
45      *
46      * @throws IllegalAdminCommandException Error in command args
47      * @throws ErrorAdminCommandException Error running command
48      */

49     public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {
50         try {
51
52             if (args.length < 2) {
53                 getOutputStream().println("Usage: CA importprofiles <inpath>");
54                 getOutputStream().print("\n");
55                 return;
56             }
57
58             String JavaDoc inpath = args[1];
59
60             getOutputStream().println("Importing certificate and entity profiles: ");
61             File JavaDoc inFile = new File JavaDoc(inpath);
62             // List all filenames in the given directory, we will try to import them all
63
File JavaDoc[] infiles = inFile.listFiles();
64             for (int i = 0; i < infiles.length; i++) {
65                 getOutputStream().println("Filename:"+infiles[i].getName());
66                 if ( infiles[i].isFile() && ((infiles[i].getName().indexOf("certprofile_") > -1) || (infiles[i].getName().indexOf("entityprofile_") > -1)) ) {
67                     boolean entityprofile = false;
68                     if (infiles[i].getName().indexOf("entityprofile_") > -1) {
69                         entityprofile=true;
70                     }
71                     int index1 = infiles[i].getName().indexOf("_");
72                     int index2 = infiles[i].getName().lastIndexOf("-");
73                     int index3 = infiles[i].getName().lastIndexOf(".xml");
74                     if (index1 < 0 || index2 < 0 || index3 < 0) {
75                         getOutputStream().println("Error: Filename not as expected (cert/entityprofile_<name>-<id>.xml).");
76                     } else {
77                         String JavaDoc profilename = infiles[i].getName().substring(index1+1,index2);
78                         //getOutputStream().println("Name:"+profilename);
79
//getOutputStream().println("Id:"+infiles[i].getName().substring(index2+1,index3));
80
int profileid = Integer.parseInt(infiles[i].getName().substring(index2+1,index3));
81                         // We don't add the fixed profiles, EJBCA handles those automagically
82
if ( (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_ENDUSER) ||
83                             (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_SUBCA) ||
84                             (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_ROOTCA) ||
85                             (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_HARDTOKENAUTH) ||
86                             (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_HARDTOKENAUTHENC) ||
87                             (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_HARDTOKENENC) ||
88                             (!entityprofile && profileid == SecConst.CERTPROFILE_FIXED_HARDTOKENSIGN) ) {
89                             getOutputStream().println("Not adding fixed certificate profile '"+profilename+"'.");
90                         } else {
91                             if (entityprofile && profileid == SecConst.EMPTY_ENDENTITYPROFILE) {
92                                 getOutputStream().println("Not adding fixed entity profile '"+profilename+"'.");
93                             } else {
94                                 // Check if the profiles already exist, cause we donšt want to add them if they do
95
boolean error = false;
96                                 if (entityprofile) {
97                                     if (getRaAdminSession().getEndEntityProfileId(administrator, profilename) != SecConst.PROFILE_NO_PROFILE) {
98                                         getOutputStream().println("Error: Entity profile '"+profilename+"' already exist in database.");
99                                         error = true;
100                                     }
101                                     if (getRaAdminSession().getEndEntityProfile(administrator, profileid) != null) {
102                                         getOutputStream().println("Error: Entity profileid '"+profileid+"' already exist in database.");
103                                         error = true;
104                                     }
105                                 } else {
106                                     if (getCertificateStoreSession().getCertificateProfileId(administrator,profilename) != SecConst.PROFILE_NO_PROFILE) {
107                                         getOutputStream().println("Error: Certificate profile '"+profilename+"' already exist in database.");
108                                         error = true;
109                                     }
110                                     if (getCertificateStoreSession().getCertificateProfile(administrator,profileid) != null) {
111                                         getOutputStream().println("Error: Certificate profile id '"+profileid+"' already exist in database.");
112                                         error = true;
113                                     }
114                                 }
115                                 if (!error) {
116                                     CertificateProfile cprofile = null;
117                                     EndEntityProfile eprofile = null;
118                                     FileInputStream JavaDoc is = new FileInputStream JavaDoc(infiles[i]);
119                                     XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc( is );
120                                     if (entityprofile) {
121                                         eprofile = new EndEntityProfile();
122                                         eprofile.loadData(decoder.readObject());
123                                         try{
124                                             getRaAdminSession().addEndEntityProfile(administrator,profileid,profilename,eprofile);
125                                             getOutputStream().println("Added entity profile '"+profilename+"' to database.");
126                                         }catch(EndEntityProfileExistsException eepee){
127                                             getOutputStream().println("Error: Error adding entity profile '"+profilename+"' to database.");
128                                         }
129                                     } else {
130                                         cprofile = new CertificateProfile();
131                                         cprofile.loadData(decoder.readObject());
132                                         try{
133                                             getCertificateStoreSession().addCertificateProfile(administrator,profileid,profilename,cprofile);
134                                             getOutputStream().println("Added certificate profile '"+profilename+"' to database.");
135                                         }catch(CertificateProfileExistsException cpee){
136                                             getOutputStream().println("Error: Error adding certificate profile '"+profilename+"' to database.");
137                                         }
138                                     }
139                                     decoder.close();
140                                     is.close();
141                                 }
142                             }
143                         }
144                     }
145                 }
146             }
147         } catch (Exception JavaDoc e) {
148             throw new ErrorAdminCommandException(e);
149         }
150     }
151 }
152
Popular Tags