View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.jarsigner;
20  
21  import java.io.File;
22  
23  import org.apache.maven.shared.utils.StringUtils;
24  import org.apache.maven.shared.utils.cli.Arg;
25  import org.apache.maven.shared.utils.cli.Commandline;
26  import org.slf4j.Logger;
27  
28  /**
29   * To build the command line for a given {@link JarSignerRequest}.
30   *
31   * @author Tony Chemit
32   * @since 1.0
33   */
34  public class JarSignerCommandLineBuilder {
35  
36      private String jarSignerFile;
37  
38      public Commandline build(JarSignerRequest request) throws CommandLineConfigurationException {
39  
40          checkRequiredState();
41  
42          Commandline cli = new Commandline();
43  
44          cli.setExecutable(jarSignerFile);
45  
46          cli.setWorkingDirectory(request.getWorkingDirectory());
47  
48          if (request.isVerbose()) {
49              cli.createArg().setValue("-verbose");
50          }
51  
52          String keystore = request.getKeystore();
53          if (!(keystore == null || keystore.isEmpty())) {
54              cli.createArg().setValue("-keystore");
55              cli.createArg().setValue(keystore);
56          }
57  
58          String storepass = request.getStorepass();
59          if (!(storepass == null || storepass.isEmpty())) {
60              cli.createArg().setValue("-storepass");
61              Arg arg = cli.createArg();
62              arg.setValue(storepass);
63              arg.setMask(true);
64          }
65  
66          String storetype = request.getStoretype();
67          if (!(storetype == null || storetype.isEmpty())) {
68              cli.createArg().setValue("-storetype");
69              cli.createArg().setValue(storetype);
70          }
71  
72          String providerName = request.getProviderName();
73          if (!(providerName == null || providerName.isEmpty())) {
74              cli.createArg().setValue("-providerName");
75              cli.createArg().setValue(providerName);
76          }
77  
78          String providerClass = request.getProviderClass();
79          if (!(providerClass == null || providerClass.isEmpty())) {
80              cli.createArg().setValue("-providerClass");
81              cli.createArg().setValue(providerClass);
82          }
83  
84          String providerArg = request.getProviderArg();
85          if (!(providerArg == null || providerArg.isEmpty())) {
86              cli.createArg().setValue("-providerArg");
87              cli.createArg().setValue(providerArg);
88          }
89  
90          if (request.isProtectedAuthenticationPath()) {
91              cli.createArg().setValue("-protected");
92          }
93  
94          String maxMemory = request.getMaxMemory();
95          if (maxMemory != null && !maxMemory.isEmpty()) {
96              cli.createArg().setValue("-J-Xmx" + maxMemory);
97          }
98  
99          String[] arguments = request.getArguments();
100         if (arguments != null) {
101             cli.addArguments(arguments);
102         }
103 
104         if (request instanceof JarSignerSignRequest) {
105             build((JarSignerSignRequest) request, cli);
106         }
107 
108         if (request instanceof JarSignerVerifyRequest) {
109             build((JarSignerVerifyRequest) request, cli);
110         }
111 
112         cli.createArg().setFile(request.getArchive());
113 
114         String alias = request.getAlias();
115         if (!(alias == null || alias.isEmpty())) {
116             cli.createArg().setValue(alias);
117         }
118 
119         return cli;
120     }
121 
122     /**
123      * @deprecated logger is not used by this class
124      * @param logger a logger
125      */
126     @Deprecated
127     public void setLogger(Logger logger) {
128         // not used
129     }
130 
131     public void setJarSignerFile(String jarSignerFile) {
132         this.jarSignerFile = jarSignerFile;
133     }
134 
135     protected void checkRequiredState() throws CommandLineConfigurationException {
136         if (jarSignerFile == null) {
137             throw new CommandLineConfigurationException("A jarSigner file is required.");
138         }
139     }
140 
141     protected void build(JarSignerSignRequest request, Commandline cli) {
142 
143         String keypass = request.getKeypass();
144         if (!(keypass == null || keypass.isEmpty())) {
145             cli.createArg().setValue("-keypass");
146             Arg arg = cli.createArg();
147             arg.setValue(keypass);
148             arg.setMask(true);
149         }
150 
151         String sigfile = request.getSigfile();
152         if (!(sigfile == null || sigfile.isEmpty())) {
153             cli.createArg().setValue("-sigfile");
154             cli.createArg().setValue(sigfile);
155         }
156 
157         String tsaLocation = request.getTsaLocation();
158         if (StringUtils.isNotBlank(tsaLocation)) {
159             cli.createArg().setValue("-tsa");
160             cli.createArg().setValue(tsaLocation);
161         }
162 
163         String tsaAlias = request.getTsaAlias();
164         if (StringUtils.isNotBlank(tsaAlias)) {
165             cli.createArg().setValue("-tsacert");
166             cli.createArg().setValue(tsaAlias);
167         }
168 
169         String tsapolicyid = request.getTsapolicyid();
170         if (StringUtils.isNotBlank(tsapolicyid)) {
171             cli.createArg().setValue("-tsapolicyid");
172             cli.createArg().setValue(tsapolicyid);
173         }
174 
175         String tsadigestalg = request.getTsadigestalg();
176         if (StringUtils.isNotBlank(tsadigestalg)) {
177             cli.createArg().setValue("-tsadigestalg");
178             cli.createArg().setValue(tsadigestalg);
179         }
180 
181         File signedjar = request.getSignedjar();
182         if (signedjar != null) {
183             cli.createArg().setValue("-signedjar");
184             cli.createArg().setValue(signedjar.getAbsolutePath());
185         }
186 
187         final File certchain = request.getCertchain();
188         if (certchain != null) {
189             cli.createArg().setValue("-certchain");
190             cli.createArg().setValue(certchain.getAbsolutePath());
191         }
192     }
193 
194     protected void build(JarSignerVerifyRequest request, Commandline cli) {
195         cli.createArg(true).setValue("-verify");
196 
197         if (request.isCerts()) {
198             cli.createArg().setValue("-certs");
199         }
200     }
201 }