View Javadoc
1   package org.codehaus.mojo.keytool;
2   
3   /*
4    * Copyright 2005-2013 The Codehaus
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License" );
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.maven.shared.utils.cli.Commandline;
20  import org.codehaus.mojo.keytool.requests.KeyToolChangeAliasRequest;
21  import org.codehaus.mojo.keytool.requests.KeyToolChangeKeyPasswordRequest;
22  import org.codehaus.mojo.keytool.requests.KeyToolChangeStorePasswordRequest;
23  import org.codehaus.mojo.keytool.requests.KeyToolDeleteRequest;
24  import org.codehaus.mojo.keytool.requests.KeyToolExportCertificateRequest;
25  import org.codehaus.mojo.keytool.requests.KeyToolGenerateCertificateRequest;
26  import org.codehaus.mojo.keytool.requests.KeyToolGenerateCertificateRequestRequest;
27  import org.codehaus.mojo.keytool.requests.KeyToolGenerateKeyPairRequest;
28  import org.codehaus.mojo.keytool.requests.KeyToolGenerateSecretKeyRequest;
29  import org.codehaus.mojo.keytool.requests.KeyToolImportCertificateRequest;
30  import org.codehaus.mojo.keytool.requests.KeyToolImportKeystoreRequest;
31  import org.codehaus.mojo.keytool.requests.KeyToolListRequest;
32  import org.codehaus.mojo.keytool.requests.KeyToolPrintCRLFileRequest;
33  import org.codehaus.mojo.keytool.requests.KeyToolPrintCertificateRequest;
34  import org.codehaus.mojo.keytool.requests.KeyToolPrintCertificateRequestRequest;
35  import org.codehaus.plexus.component.annotations.Component;
36  
37  import java.util.HashSet;
38  import java.util.Set;
39  
40  /**
41   * To build the command line for a given {@link KeyToolRequest}.
42   *
43   * @author tchemit <chemit@codelutin.com>
44   * @version $Id$
45   * @since 1.1
46   */
47  @Component( role = KeyToolCommandLineBuilder.class, hint = "default" )
48  public class DefaultKeyToolCommandLineBuilder
49      extends AbstractKeyToolCommandLineBuilder
50  {
51  
52      /**
53       * Unsupported request types.
54       *
55       * @since 1.3
56       */
57      final Set<Class<? extends KeyToolRequest>> unsupportedRequestTypes;
58  
59      public DefaultKeyToolCommandLineBuilder()
60      {
61          this.unsupportedRequestTypes = new HashSet<Class<? extends KeyToolRequest>>();
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      public <R extends KeyToolRequest> boolean supportRequestType( Class<R> requestType )
68      {
69          return !unsupportedRequestTypes.contains( requestType );
70      }
71  
72      /**
73       * {@inheritDoc}
74       */
75      public Commandline build( KeyToolRequest request )
76          throws CommandLineConfigurationException, UnsupportedKeyToolRequestException
77      {
78          checkRequiredState();
79          checkSupportedRequest( request );
80  
81          Commandline cli = new Commandline();
82  
83          cli.setExecutable( getKeyToolFile() );
84  
85          cli.setWorkingDirectory( request.getWorkingDirectory() );
86  
87          if ( request instanceof KeyToolChangeAliasRequest )
88          {
89              build( (KeyToolChangeAliasRequest) request, cli );
90          }
91          if ( request instanceof KeyToolChangeKeyPasswordRequest )
92          {
93              build( (KeyToolChangeKeyPasswordRequest) request, cli );
94          }
95          if ( request instanceof KeyToolChangeStorePasswordRequest )
96          {
97              build( (KeyToolChangeStorePasswordRequest) request, cli );
98          }
99          if ( request instanceof KeyToolDeleteRequest )
100         {
101             build( (KeyToolDeleteRequest) request, cli );
102         }
103         if ( request instanceof KeyToolExportCertificateRequest )
104         {
105             build( (KeyToolExportCertificateRequest) request, cli );
106         }
107         if ( request instanceof KeyToolGenerateCertificateRequest )
108         {
109             build( (KeyToolGenerateCertificateRequest) request, cli );
110         }
111         if ( request instanceof KeyToolGenerateCertificateRequestRequest )
112         {
113             build( (KeyToolGenerateCertificateRequestRequest) request, cli );
114         }
115         if ( request instanceof KeyToolGenerateKeyPairRequest )
116         {
117             build( (KeyToolGenerateKeyPairRequest) request, cli );
118         }
119         if ( request instanceof KeyToolGenerateSecretKeyRequest )
120         {
121             build( (KeyToolGenerateSecretKeyRequest) request, cli );
122         }
123         if ( request instanceof KeyToolImportCertificateRequest )
124         {
125             build( (KeyToolImportCertificateRequest) request, cli );
126         }
127         if ( request instanceof KeyToolImportKeystoreRequest )
128         {
129             build( (KeyToolImportKeystoreRequest) request, cli );
130         }
131         if ( request instanceof KeyToolListRequest )
132         {
133             build( (KeyToolListRequest) request, cli );
134         }
135         if ( request instanceof KeyToolPrintCertificateRequest )
136         {
137             build( (KeyToolPrintCertificateRequest) request, cli );
138         }
139         if ( request instanceof KeyToolPrintCertificateRequestRequest )
140         {
141             build( (KeyToolPrintCertificateRequestRequest) request, cli );
142         }
143         if ( request instanceof KeyToolPrintCRLFileRequest )
144         {
145             build( (KeyToolPrintCRLFileRequest) request, cli );
146         }
147 
148         String[] arguments = request.getArguments();
149         if ( arguments != null )
150         {
151             cli.addArguments( arguments );
152         }
153 
154         return cli;
155     }
156 
157     /**
158      * Fill the commandline client for the given {@code request}.
159      *
160      * @param request the keytool request to consume
161      * @param cli     the commandline client to prepare
162      */
163     protected void build( KeyToolChangeAliasRequest request, Commandline cli )
164     {
165         addKeytoolCommandAndDefaultoptions( cli, "-changealias", request );
166         addArgIfNotEmpty( cli, "-destalias", request.getDestalias() );
167         addArgIfNotEmpty( cli, "-keypass", request.getKeypass() );
168     }
169 
170     /**
171      * Fill the commandline client for the given {@code request}.
172      *
173      * @param request the keytool request to consume
174      * @param cli     the commandline client to prepare
175      */
176     protected void build( KeyToolChangeKeyPasswordRequest request, Commandline cli )
177     {
178         addKeytoolCommandAndDefaultoptions( cli, "-keypasswd", request );
179         addArgIfNotEmpty( cli, "-keypass", request.getKeypass() );
180         addArgIfNotEmpty( cli, "-new", request.getNewPassword() );
181     }
182 
183     /**
184      * Fill the commandline client for the given {@code request}.
185      *
186      * @param request the keytool request to consume
187      * @param cli     the commandline client to prepare
188      */
189     protected void build( KeyToolChangeStorePasswordRequest request, Commandline cli )
190     {
191         addKeytoolCommandAndDefaultoptions( cli, "-storepasswd", request );
192         addArgIfNotEmpty( cli, "-new", request.getNewPassword() );
193     }
194 
195     /**
196      * Fill the commandline client for the given {@code request}.
197      *
198      * @param request the keytool request to consume
199      * @param cli     the commandline client to prepare
200      */
201     protected void build( KeyToolDeleteRequest request, Commandline cli )
202     {
203         addKeytoolCommandAndDefaultoptions( cli, "-delete", request );
204     }
205 
206     /**
207      * Fill the commandline client for the given {@code request}.
208      *
209      * @param request the keytool request to consume
210      * @param cli     the commandline client to prepare
211      */
212     protected void build( KeyToolExportCertificateRequest request, Commandline cli )
213     {
214         addKeytoolCommandAndDefaultoptions( cli, "-export", request );
215         addArgIfTrue( cli, "-rfc", request.isRfc() );
216         addArgIfNotEmpty( cli, "-file", request.getFile() );
217     }
218 
219     /**
220      * Fill the commandline client for the given {@code request}.
221      *
222      * @param request the keytool request to consume
223      * @param cli     the commandline client to prepare
224      */
225     protected void build( KeyToolGenerateCertificateRequest request, Commandline cli )
226     {
227         addKeytoolCommandAndDefaultoptions( cli, "-gencert", request );
228         addArgIfTrue( cli, "-rfc", request.isRfc() );
229         addArgIfNotEmpty( cli, "-infile", request.getInfile() );
230         addArgIfNotEmpty( cli, "-outfile", request.getOutfile() );
231         addArgIfNotEmpty( cli, "-sigalg", request.getSigalg() );
232         addArgIfNotEmpty( cli, "-dname", request.getDname() );
233         addArgIfNotEmpty( cli, "-startdate", request.getStartdate() );
234         addArgIfNotEmpty( cli, "-ext", request.getExt() );
235         addArgIfNotEmpty( cli, "-validity", request.getValidity() );
236         addArgIfNotEmpty( cli, "-keypass", request.getKeypass() );
237     }
238 
239     /**
240      * Fill the commandline client for the given {@code request}.
241      *
242      * @param request the keytool request to consume
243      * @param cli     the commandline client to prepare
244      */
245     protected void build( KeyToolGenerateCertificateRequestRequest request, Commandline cli )
246     {
247         addKeytoolCommandAndDefaultoptions( cli, "-certreq", request );
248         addArgIfNotEmpty( cli, "-sigalg", request.getSigalg() );
249         addArgIfNotEmpty( cli, "-file", request.getFile() );
250         addArgIfNotEmpty( cli, "-keypass", request.getKeypass() );
251         addArgIfNotEmpty( cli, "-dname", request.getDname() );
252     }
253 
254     /**
255      * Fill the commandline client for the given {@code request}.
256      *
257      * @param request the keytool request to consume
258      * @param cli     the commandline client to prepare
259      */
260     protected void build( KeyToolGenerateKeyPairRequest request, Commandline cli )
261     {
262         addKeytoolCommandAndDefaultoptions( cli, "-genkeypair", request );
263         addArgIfNotEmpty( cli, "-dname", request.getDname() );
264         addArgIfNotEmpty( cli, "-keypass", request.getKeypass() );
265         addArgIfNotEmpty( cli, "-validity", request.getValidity() );
266         addArgIfNotEmpty( cli, "-keyalg", request.getKeyalg() );
267         addArgIfNotEmpty( cli, "-keysize", request.getKeysize() );
268         addArgIfNotEmpty( cli, "-sigalg", request.getSigalg() );
269         addArgIfNotEmpty( cli, "-startdate", request.getStartdate() );
270         addArgIfNotEmpty( cli, "-ext", request.getExt() );
271     }
272 
273     /**
274      * Fill the commandline client for the given {@code request}.
275      *
276      * @param request the keytool request to consume
277      * @param cli     the commandline client to prepare
278      */
279     protected void build( KeyToolGenerateSecretKeyRequest request, Commandline cli )
280     {
281         addKeytoolCommandAndDefaultoptions( cli, "-genseckey", request );
282         addArgIfNotEmpty( cli, "-keypass", request.getKeypass() );
283         addArgIfNotEmpty( cli, "-keyalg", request.getKeyalg() );
284         addArgIfNotEmpty( cli, "-keysize", request.getKeysize() );
285     }
286 
287     /**
288      * Fill the commandline client for the given {@code request}.
289      *
290      * @param request the keytool request to consume
291      * @param cli     the commandline client to prepare
292      */
293     protected void build( KeyToolImportCertificateRequest request, Commandline cli )
294     {
295         addKeytoolCommandAndDefaultoptions( cli, "-importcert", request );
296         addArgIfTrue( cli, "-noprompt", request.isNoprompt() );
297         addArgIfTrue( cli, "-trustcacerts", request.isTrustcacerts() );
298         addArgIfNotEmpty( cli, "-file", request.getFile() );
299         addArgIfNotEmpty( cli, "-keypass", request.getKeypass() );
300     }
301 
302     /**
303      * Fill the commandline client for the given {@code request}.
304      *
305      * @param request the keytool request to consume
306      * @param cli     the commandline client to prepare
307      */
308     protected void build( KeyToolImportKeystoreRequest request, Commandline cli )
309     {
310         addKeytoolCommandAndDefaultoptions( cli, "-importkeystore", request );
311         addArgIfTrue( cli, "-noprompt", request.isNoprompt() );
312         addArgIfNotEmpty( cli, "-srcprotected", request.isSrcprotected() ? Boolean.TRUE.toString() : "" );
313         addArgIfNotEmpty( cli, "-srckeystore", request.getSrckeystore() );
314         addArgIfNotEmpty( cli, "-destkeystore", request.getDestkeystore() );
315         addArgIfNotEmpty( cli, "-srcstoretype", request.getSrcstoretype() );
316         addArgIfNotEmpty( cli, "-deststoretype", request.getDeststoretype() );
317         addArgIfNotEmpty( cli, "-srcstorepass", request.getSrcstorepass() );
318         addArgIfNotEmpty( cli, "-deststorepass", request.getDeststorepass() );
319         addArgIfNotEmpty( cli, "-srcprovidername", request.getSrcprovidername() );
320         addArgIfNotEmpty( cli, "-destprovidername", request.getDestprovidername() );
321         addArgIfNotEmpty( cli, "-srcalias", request.getSrcalias() );
322         addArgIfNotEmpty( cli, "-destalias", request.getDestalias() );
323         addArgIfNotEmpty( cli, "-srckeypass", request.getSrckeypass() );
324         addArgIfNotEmpty( cli, "-destkeypass", request.getDestkeypass() );
325         addArgIfNotEmpty( cli, "-providerclass", request.getProviderclass() );
326         addArgIfNotEmpty( cli, "-providerarg", request.getProviderarg() );
327         addArgIfNotEmpty( cli, "-providerpath", request.getProviderpath() );
328     }
329 
330     /**
331      * Fill the commandline client for the given {@code request}.
332      *
333      * @param request the keytool request to consume
334      * @param cli     the commandline client to prepare
335      */
336     protected void build( KeyToolListRequest request, Commandline cli )
337     {
338         addKeytoolCommandAndDefaultoptions( cli, "-list", request );
339         addArgIfTrue( cli, "-rfc", request.isRfc() );
340     }
341 
342     /**
343      * Fill the commandline client for the given {@code request}.
344      *
345      * @param request the keytool request to consume
346      * @param cli     the commandline client to prepare
347      */
348     protected void build( KeyToolPrintCertificateRequest request, Commandline cli )
349     {
350         addKeytoolCommandAndDefaultoptions( cli, "-printcert", request );
351         addArgIfTrue( cli, "-rfc", request.isRfc() );
352         addArgIfNotEmpty( cli, "-file", request.getFile() );
353         addArgIfNotEmpty( cli, "-sslserver", request.getSslserver() );
354         addArgIfNotEmpty( cli, "-jarfile", request.getJarfile() );
355     }
356 
357     /**
358      * Fill the commandline client for the given {@code request}.
359      *
360      * @param request the keytool request to consume
361      * @param cli     the commandline client to prepare
362      */
363     protected void build( KeyToolPrintCertificateRequestRequest request, Commandline cli )
364     {
365         addKeytoolCommandAndDefaultoptions( cli, "-printcertreq", request );
366         addArgIfNotEmpty( cli, "-file", request.getFile() );
367     }
368 
369     /**
370      * Fill the commandline client for the given {@code request}.
371      *
372      * @param request the keytool request to consume
373      * @param cli     the commandline client to prepare
374      */
375     protected void build( KeyToolPrintCRLFileRequest request, Commandline cli )
376     {
377         addKeytoolCommandAndDefaultoptions( cli, "-printcrl", request );
378         addArgIfNotEmpty( cli, "-file", request.getFile() );
379     }
380 
381 }