Coverage Report - org.openpermis.examples.ldap.CertIntoLdifUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
CertIntoLdifUtil
0%
0/45
0%
0/14
2.667
 
 1  
 package org.openpermis.examples.ldap;
 2  
 
 3  
 import java.io.BufferedReader;
 4  
 import java.io.File;
 5  
 import java.io.FileInputStream;
 6  
 import java.io.FileReader;
 7  
 import java.io.FileWriter;
 8  
 import java.io.IOException;
 9  
 import java.io.InputStream;
 10  
 import java.util.regex.Matcher;
 11  
 import java.util.regex.Pattern;
 12  
 
 13  
 /**
 14  
  * This utility class substitutes in the given ldif file the given place holder with the given 
 15  
  * certificate.
 16  
  * @since 0.3.0
 17  
  */
 18  
 public final class CertIntoLdifUtil {
 19  
         
 20  
         //---- Static
 21  
         
 22  
         /**
 23  
          * @since 0.3.0
 24  
          */
 25  
         static final int BUFFER_SIZE = 1024;
 26  
 
 27  
         /**
 28  
          * @since 0.3.0
 29  
          */
 30  
         static final int ARGUMENT_NUMBER = 3;
 31  
 
 32  
         /**
 33  
          * Substitutes in the given ldif file the given place holder with the given certificate.
 34  
          * 
 35  
          * @param args "#1 argument: path of ldif file, #2 argument: place holder within ldif file, #3 
 36  
          * argument: path of certificate file
 37  
          * @throws IOException if something goes awry.
 38  
          * @since 0.3.0
 39  
          */
 40  
         public static void main (String[] args) throws IOException {
 41  
 
 42  0
                 if (args.length != ARGUMENT_NUMBER) {
 43  0
                         String message = "CertIntoLdifUtil takes three arguments: "
 44  
                                 + "#1 argument: path of ldif file. "
 45  
                                 + "#2 argument: placeholder within ldif file. "
 46  
                                 + "#3 argument: path of certificate file.";
 47  0
                         throw new IllegalArgumentException(message);
 48  
                 }
 49  
 
 50  0
                 writeStringToFile(
 51  
                         substitutePlaceholderWithCertificate(
 52  
                                 readFileAsString(args[0]),
 53  
                                 args[1], getCertificateInBase64(args[2])
 54  
                         ),
 55  
                         args[0]
 56  
                 );
 57  0
         }
 58  
 
 59  
         /**
 60  
          * @since 0.3.0
 61  
          */
 62  0
         private CertIntoLdifUtil () {
 63  
                 // Prevents instantiation.
 64  0
         }
 65  
 
 66  
         /**
 67  
          * @since 0.3.0
 68  
          */
 69  
         private static String substitutePlaceholderWithCertificate (
 70  
                 final String inputStr, final String patternStr, final String replacementStr
 71  
         ) {
 72  
                 // Compile regular expression
 73  0
                 Pattern pattern = Pattern.compile(patternStr);
 74  
                 // Replace all occurrences of pattern in input
 75  0
                 Matcher matcher = pattern.matcher(inputStr);
 76  0
                 String output = matcher.replaceAll(replacementStr);
 77  0
                 return output;
 78  
         }
 79  
 
 80  
         /**
 81  
          * @since 0.3.0
 82  
          */
 83  
         private static String getCertificateInBase64 (String certificateFilePath) throws IOException {
 84  0
                 File certificateFile = new File(certificateFilePath);
 85  
 
 86  0
                 InputStream is = new FileInputStream(certificateFile);
 87  
 
 88  
                 // Get the size of the file
 89  0
                 long length = certificateFile.length();
 90  
 
 91  0
                 if (length > Integer.MAX_VALUE) {
 92  0
                         throw new IOException("File to large.");
 93  
                 }
 94  
 
 95  
                 // Create the byte array to hold the data
 96  0
                 byte[] certificateByteArray = new byte[(int) length];
 97  
 
 98  
                 // Read in the bytes
 99  0
                 int offset = 0;
 100  0
                 int numRead = 0;
 101  
 
 102  
                 do {
 103  0
                         numRead = is.read(certificateByteArray, offset, certificateByteArray.length - offset);
 104  0
                         offset += numRead;
 105  0
                 } while (offset < certificateByteArray.length && numRead >= 0);
 106  
                 
 107  
                 // Ensure all the bytes have been read in
 108  0
                 if (offset < certificateByteArray.length) {
 109  0
                         throw new IOException("Could not completely read file " + certificateFile.getName());
 110  
                 }
 111  
 
 112  
                 // Close the input stream and return bytes
 113  0
                 is.close();
 114  
 
 115  0
                 StringBuilder stringBuilder = new StringBuilder();
 116  0
                 byte[] ab = org.bouncycastle.util.encoders.Base64.encode(certificateByteArray);
 117  0
                 for (byte b : ab) {
 118  0
                         stringBuilder.append((char) b);
 119  
                 }
 120  
 
 121  0
                 return stringBuilder.toString();
 122  
         }
 123  
 
 124  
         /**
 125  
          * @since 0.3.0
 126  
          */
 127  
         private static String readFileAsString (String filePath)
 128  
                 throws java.io.IOException {
 129  0
                 StringBuffer fileData = new StringBuffer(BUFFER_SIZE);
 130  0
                 BufferedReader reader = new BufferedReader(new FileReader(filePath));
 131  0
                 char[] buf = new char[BUFFER_SIZE];
 132  0
                 int numRead = 0;
 133  0
                 while ((numRead = reader.read(buf)) != -1) {
 134  0
                         String readData = String.valueOf(buf, 0, numRead);
 135  0
                         fileData.append(readData);
 136  0
                         buf = new char[BUFFER_SIZE];
 137  0
                 }
 138  0
                 reader.close();
 139  0
                 return fileData.toString();
 140  
         }
 141  
 
 142  
         /**
 143  
          * @since 0.3.0
 144  
          */
 145  
         private static void writeStringToFile (String encodedCertificate, String filePath)
 146  
                 throws IOException {
 147  0
                 FileWriter fw = new FileWriter(new File(filePath));
 148  0
                 fw.write(encodedCertificate);
 149  0
                 fw.close();
 150  0
         }
 151  
 }