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.plugin.MojoExecutionException;
20  import org.apache.maven.plugins.annotations.Mojo;
21  import org.apache.maven.plugins.annotations.Parameter;
22  import org.apache.maven.shared.utils.cli.Commandline;
23  import org.codehaus.mojo.keytool.requests.KeyToolImportCertificateRequest;
24  import org.codehaus.plexus.util.StringUtils;
25  
26  import java.io.File;
27  
28  /**
29   * To import a certificate into a keystore.
30   * <p/>
31   * Implemented as a wrapper around the SDK {@code keytool -import} (jdk 1.5) or  {@code keytool -importcert} (jdk 1.6)
32   * command.
33   * <p/>
34   * See <a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/keytool.html">keystore documentation</a>.
35   * <p/>
36   * <strong>Since version 1.2, this mojo replace the mojo import.</strong>
37   *
38   * @author tchemit <chemit@codelutin.com>
39   * @since 1.2
40   */
41  @Mojo(name = "importCertificate", requiresProject = true)
42  public class ImportCertificateMojo
43      extends AbstractKeyToolRequestWithKeyStoreAndAliasParametersMojo<KeyToolImportCertificateRequest>
44  {
45  
46      /**
47       * Key password.
48       * <p/>
49       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
50       *
51       * @since 1.2
52       */
53      @Parameter
54      private String keypass;
55  
56      /**
57       * Input file name.
58       * <p/>
59       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
60       *
61       * @since 1.2
62       */
63      @Parameter
64      private String file;
65  
66      /**
67       * Do not prompt.
68       * <p/>
69       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
70       *
71       * @since 1.2
72       */
73      @Parameter
74      private boolean noprompt;
75  
76      /**
77       * Trust certificates from cacerts.
78       * <p/>
79       * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
80       *
81       * @since 1.2
82       */
83      @Parameter
84      private boolean trustcacerts;
85  
86      /**
87       * If value is {@code true}, then will do nothing if keystore already exists.
88       *
89       * @since 1.3
90       */
91      @Parameter
92      private boolean skipIfExist;
93  
94      /**
95       * Default contructor.
96       */
97      public ImportCertificateMojo()
98      {
99          super( KeyToolImportCertificateRequest.class );
100     }
101 
102     @Override
103     public void execute()
104         throws MojoExecutionException
105     {
106 
107         if ( skipIfExist )
108         {
109 
110             // check if keystore already exist
111             File keystoreFile = getKeystoreFile();
112             boolean keystoreFileExists = keystoreFile.exists();
113 
114             if ( keystoreFileExists )
115             {
116                 getLog().info( "Skip execution, keystore already exists at " + keystoreFile );
117                 setSkip( true );
118             }
119 
120         }
121         super.execute();
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     protected KeyToolImportCertificateRequest createKeytoolRequest()
129     {
130         KeyToolImportCertificateRequest request = super.createKeytoolRequest();
131 
132         request.setKeypass( this.keypass );
133         request.setFile( this.file );
134         request.setNoprompt( this.noprompt );
135         request.setTrustcacerts( this.trustcacerts );
136         return request;
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     protected String getCommandlineInfo( Commandline commandLine )
144     {
145         String commandLineInfo = super.getCommandlineInfo( commandLine );
146 
147         commandLineInfo = StringUtils.replace( commandLineInfo, this.keypass, "'*****'" );
148 
149         return commandLineInfo;
150     }
151 }