Coverage Report - com.jcabi.ssl.maven.plugin.Cacerts
 
Classes in this File Line Coverage Branch Coverage Complexity
Cacerts
100%
14/14
0%
0/10
1
 
 1  2
 /**
 2  
  * Copyright (c) 2012-2013, JCabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.ssl.maven.plugin;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.log.Logger;
 34  
 import java.io.File;
 35  
 import java.io.IOException;
 36  
 import javax.validation.constraints.NotNull;
 37  
 import lombok.EqualsAndHashCode;
 38  
 import lombok.ToString;
 39  
 import org.apache.commons.io.FileUtils;
 40  
 
 41  
 /**
 42  
  * Abstraction of {@code java.home/lib/security/cacerts} file.
 43  
  *
 44  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 45  
  * @version $Id$
 46  
  * @since 0.5
 47  
  */
 48  
 @Immutable
 49  
 @ToString
 50  
 @EqualsAndHashCode(of = "store")
 51  
 final class Cacerts {
 52  
 
 53  
     /**
 54  
      * Constant {@code javax.net.ssl.trustStore}.
 55  
      */
 56  
     private static final String TRUST = "javax.net.ssl.trustStore";
 57  
 
 58  
     /**
 59  
      * Constant {@code javax.net.ssl.trustStorePassword}.
 60  
      */
 61  
     private static final String TRUST_PWD = "javax.net.ssl.trustStorePassword";
 62  
 
 63  
     /**
 64  
      * Standard password of {@code cacerts} file.
 65  
      */
 66  
     private static final String STD_PWD = "changeit";
 67  
 
 68  
     /**
 69  
      * New location of the trust store.
 70  
      */
 71  
     private final transient String store;
 72  
 
 73  
     /**
 74  
      * Public ctor.
 75  
      * @param file New location
 76  
      * @throws IOException If fails
 77  
      */
 78  1
     public Cacerts(@NotNull final File file) throws IOException {
 79  1
         this.store = file.getAbsolutePath();
 80  1
         final File prev = new File(
 81  
             String.format(
 82  
                 "%s/lib/security/cacerts",
 83  
                 System.getProperty("java.home")
 84  
             )
 85  
         );
 86  1
         FileUtils.copyFile(prev, file);
 87  1
         Logger.info(
 88  
             this,
 89  
             "Existing cacerts '%s' copied to '%s' (%s)",
 90  
             prev,
 91  
             this.store,
 92  
             FileUtils.byteCountToDisplaySize(this.store.length())
 93  
         );
 94  1
     }
 95  
 
 96  
     /**
 97  
      * Import existing keystore content into this trust store.
 98  
      * @throws IOException If fails
 99  
      */
 100  
     public void imprt() throws IOException {
 101  1
         final File keystore = new File(System.getProperty(Keystore.KEY));
 102  1
         final String pwd = System.getProperty(Keystore.KEY_PWD);
 103  1
         new Keytool(new File(this.store), Cacerts.STD_PWD).imprt(keystore, pwd);
 104  1
         System.setProperty(Cacerts.TRUST, this.store);
 105  1
         System.setProperty(Cacerts.TRUST_PWD, Cacerts.STD_PWD);
 106  1
         Logger.info(
 107  
             this,
 108  
             "keyStore '%s' imported into trustStore '%s'",
 109  
             keystore,
 110  
             this.store
 111  
         );
 112  1
     }
 113  
 
 114  
 }