1 package org.codehaus.mojo.keytool;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
42
43
44
45
46
47 @Component( role = KeyToolCommandLineBuilder.class, hint = "default" )
48 public class DefaultKeyToolCommandLineBuilder
49 extends AbstractKeyToolCommandLineBuilder
50 {
51
52
53
54
55
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
66
67 public <R extends KeyToolRequest> boolean supportRequestType( Class<R> requestType )
68 {
69 return !unsupportedRequestTypes.contains( requestType );
70 }
71
72
73
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
159
160
161
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
172
173
174
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
185
186
187
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
197
198
199
200
201 protected void build( KeyToolDeleteRequest request, Commandline cli )
202 {
203 addKeytoolCommandAndDefaultoptions( cli, "-delete", request );
204 }
205
206
207
208
209
210
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
221
222
223
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
241
242
243
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
256
257
258
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
275
276
277
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
289
290
291
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
304
305
306
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
332
333
334
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
344
345
346
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
359
360
361
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
371
372
373
374
375 protected void build( KeyToolPrintCRLFileRequest request, Commandline cli )
376 {
377 addKeytoolCommandAndDefaultoptions( cli, "-printcrl", request );
378 addArgIfNotEmpty( cli, "-file", request.getFile() );
379 }
380
381 }