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 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
public final class CertIntoLdifUtil { |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
static final int BUFFER_SIZE = 1024; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
static final int ARGUMENT_NUMBER = 3; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
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 | |
|
61 | |
|
62 | 0 | private CertIntoLdifUtil () { |
63 | |
|
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
private static String substitutePlaceholderWithCertificate ( |
70 | |
final String inputStr, final String patternStr, final String replacementStr |
71 | |
) { |
72 | |
|
73 | 0 | Pattern pattern = Pattern.compile(patternStr); |
74 | |
|
75 | 0 | Matcher matcher = pattern.matcher(inputStr); |
76 | 0 | String output = matcher.replaceAll(replacementStr); |
77 | 0 | return output; |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
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 | |
|
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 | |
|
96 | 0 | byte[] certificateByteArray = new byte[(int) length]; |
97 | |
|
98 | |
|
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 | |
|
108 | 0 | if (offset < certificateByteArray.length) { |
109 | 0 | throw new IOException("Could not completely read file " + certificateFile.getName()); |
110 | |
} |
111 | |
|
112 | |
|
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 | |
|
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 | |
|
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 | |
} |