Salome HOME
- Fix query
[modules/gde.git] / projects / GDE_App / GDE-war / test / com / edf / gde / test / restapi / providers / FileCertificateProvider.java
1 package gdetester.restapi.providers;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.security.cert.Certificate;
10 import java.security.cert.CertificateException;
11 import java.security.cert.CertificateFactory;
12
13 public class FileCertificateProvider implements CertificateProvider {
14
15     private final File file;
16     private Certificate ca;
17
18     public FileCertificateProvider(File file) {
19         this.file = file;
20     }
21
22     public FileCertificateProvider(String fileName) {
23         file = new File(fileName);
24     }
25
26     @Override
27     public Certificate getCertificate() throws CertificateException, IOException {
28         if (ca != null) {
29             return ca;
30         }
31
32         CertificateFactory cf = CertificateFactory.getInstance("X.509");
33         InputStream caInput = null;
34         try {
35             caInput = new BufferedInputStream(new FileInputStream(file));
36             ca = cf.generateCertificate(caInput);
37             return ca;
38         } catch (FileNotFoundException ex) {
39             throw new IOException(ex);
40         } finally {
41             if (caInput != null) {
42                 caInput.close();
43             }
44         }
45     }
46
47 }