KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > protocol > ws > client > EJBCAWSRABaseCommand


1 package org.ejbca.core.protocol.ws.client;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileNotFoundException JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStreamReader JavaDoc;
8 import java.io.PrintStream JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import javax.xml.namespace.QName JavaDoc;
13
14 import org.ejbca.core.model.ca.crl.RevokedCertInfo;
15 import org.ejbca.core.protocol.ws.client.gen.EjbcaWSService;
16
17
18 import org.ejbca.util.CertTools;
19
20 /**
21  * Base class inherited by all EJBCA RA WS cli commands.
22  * Checks the property file and creates a webservice connection.
23  *
24  * @author Philip Vendil
25  * $Id: EJBCAWSRABaseCommand.java,v 1.2 2006/10/08 22:53:26 herrvendil Exp $
26  */

27
28 public abstract class EJBCAWSRABaseCommand {
29     
30     protected String JavaDoc[] args = null;
31     private org.ejbca.core.protocol.ws.client.gen.EjbcaWS ejbcaraws = null;
32     private Properties JavaDoc props = null;
33     private String JavaDoc password = null;
34     
35     
36     protected static final String JavaDoc[] REASON_TEXTS ={"NOT REVOKED","UNSPECIFIED","KEYCOMPROMISE","CACOMPROMISE",
37         "AFFILIATIONCHANGED","SUPERSEDED","CESSATIONOFOPERATION",
38         "CERTIFICATEHOLD","REMOVEFROMCRL","PRIVILEGESWITHDRAWN",
39     "AACOMPROMISE"};
40     
41     public static final int NOT_REVOKED = RevokedCertInfo.NOT_REVOKED;
42     public static final int REVOKATION_REASON_UNSPECIFIED = RevokedCertInfo.REVOKATION_REASON_UNSPECIFIED;
43     public static final int REVOKATION_REASON_KEYCOMPROMISE = RevokedCertInfo.REVOKATION_REASON_KEYCOMPROMISE;
44     public static final int REVOKATION_REASON_CACOMPROMISE = RevokedCertInfo.REVOKATION_REASON_CACOMPROMISE;
45     public static final int REVOKATION_REASON_AFFILIATIONCHANGED = RevokedCertInfo.REVOKATION_REASON_AFFILIATIONCHANGED;
46     public static final int REVOKATION_REASON_SUPERSEDED = RevokedCertInfo.REVOKATION_REASON_SUPERSEDED;
47     public static final int REVOKATION_REASON_CESSATIONOFOPERATION = RevokedCertInfo.REVOKATION_REASON_CESSATIONOFOPERATION;
48     public static final int REVOKATION_REASON_CERTIFICATEHOLD = RevokedCertInfo.REVOKATION_REASON_CERTIFICATEHOLD;
49     public static final int REVOKATION_REASON_REMOVEFROMCRL = RevokedCertInfo.REVOKATION_REASON_REMOVEFROMCRL;
50     public static final int REVOKATION_REASON_PRIVILEGESWITHDRAWN = RevokedCertInfo.REVOKATION_REASON_PRIVILEGESWITHDRAWN;
51     public static final int REVOKATION_REASON_AACOMPROMISE = RevokedCertInfo.REVOKATION_REASON_AACOMPROMISE;
52     
53     protected static final int[] REASON_VALUES = {NOT_REVOKED,REVOKATION_REASON_UNSPECIFIED,
54          REVOKATION_REASON_KEYCOMPROMISE, REVOKATION_REASON_CACOMPROMISE,
55          REVOKATION_REASON_AFFILIATIONCHANGED, REVOKATION_REASON_SUPERSEDED,
56          REVOKATION_REASON_CESSATIONOFOPERATION, REVOKATION_REASON_CERTIFICATEHOLD,
57          REVOKATION_REASON_REMOVEFROMCRL, REVOKATION_REASON_PRIVILEGESWITHDRAWN,
58          REVOKATION_REASON_AACOMPROMISE};
59     
60     EJBCAWSRABaseCommand(String JavaDoc[] args){
61         this.args = args;
62     }
63     
64     /**
65      * Method creating a connection to the webservice
66      * using the information stored in the property files.
67      * @throws ServiceException
68      * @throws IOException
69      * @throws FileNotFoundException
70      */

71     protected org.ejbca.core.protocol.ws.client.gen.EjbcaWS getEjbcaRAWS() throws FileNotFoundException JavaDoc, IOException JavaDoc{
72         if(ejbcaraws == null){
73             CertTools.installBCProvider();
74                         
75             System.setProperty("javax.net.ssl.trustStore",getKeyStorePath());
76             System.setProperty("javax.net.ssl.trustStorePassword",getKeyStorePassword());
77             
78             System.setProperty("javax.net.ssl.keyStore",getKeyStorePath());
79             System.setProperty("javax.net.ssl.keyStorePassword",getKeyStorePassword());
80         
81
82             QName JavaDoc qname = new QName JavaDoc("http://ws.protocol.core.ejbca.org/", "EjbcaWSService");
83             EjbcaWSService service = new EjbcaWSService(new URL JavaDoc(getWebServiceURL()),qname);
84             ejbcaraws = service.getEjbcaWSPort();
85             
86
87         }
88                 
89         return ejbcaraws;
90         
91     }
92
93     private String JavaDoc getKeyStorePassword() throws FileNotFoundException JavaDoc, IOException JavaDoc {
94         if(password == null){
95             if(getProperties().getProperty("ejbcawsracli.keystore.password") == null){
96                BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(System.in));
97                System.out.print("Enter keystore password :");
98                password = reader.readLine();
99             }else{
100                 password = getProperties().getProperty("ejbcawsracli.keystore.password");
101             }
102         }
103         return password;
104     }
105
106     private String JavaDoc getKeyStorePath() throws FileNotFoundException JavaDoc, IOException JavaDoc {
107         return getProperties().getProperty("ejbcawsracli.keystore.path", "keystore.jks");
108     }
109
110     private String JavaDoc getWebServiceURL() throws FileNotFoundException JavaDoc, IOException JavaDoc {
111         return getProperties().getProperty("ejbcawsracli.url", "https://localhost:8443/ejbca/ejbcaws/ejbcaws") + "?wsdl";
112     }
113
114     private Properties JavaDoc getProperties() throws FileNotFoundException JavaDoc, IOException JavaDoc {
115         if(props == null){
116           props = new Properties JavaDoc();
117           try {
118             props.load(new FileInputStream JavaDoc("ejbcawsracli.properties"));
119           } catch (FileNotFoundException JavaDoc e) {
120             // Try in parent directory
121
props.load(new FileInputStream JavaDoc("../ejbcawsracli.properties"));
122           }
123         }
124         return props;
125     }
126     
127     protected PrintStream JavaDoc getPrintStream(){
128         return System.out;
129     }
130     
131     protected int getRevokeReason(String JavaDoc reason) throws Exception JavaDoc{
132         for(int i=0;i<REASON_TEXTS.length;i++){
133            if(REASON_TEXTS[i].equalsIgnoreCase(reason)){
134                return REASON_VALUES[i];
135            }
136         }
137         getPrintStream().println("Error : Unsupported reason " + reason);
138         usage();
139         System.exit(-1);
140         return 0;
141     }
142     
143     protected String JavaDoc getRevokeReason(int reason) {
144         for(int i=0;i<REASON_VALUES.length;i++){
145                if(REASON_VALUES[i]==reason){
146                    return REASON_TEXTS[i];
147                }
148             }
149         getPrintStream().println("Error : Unsupported reason " + reason);
150         usage();
151         System.exit(-1);
152         return null;
153     }
154     
155     protected abstract void usage();
156
157 }
158
Popular Tags