Sync Integ to Master
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / http / config / ClientCertificate.java
1 package org.openecomp.sdc.common.http.config;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.openecomp.sdc.security.SecurityUtil;
5
6 import fj.data.Either;
7
8 public class ClientCertificate {
9     private String keyStore;
10     private String keyStorePassword;
11
12     public ClientCertificate() {
13     }
14
15     public ClientCertificate(ClientCertificate clientCertificate) {
16         setKeyStore(clientCertificate.getKeyStore());
17         setKeyStorePassword(clientCertificate.getKeyStorePassword(), false);
18     }
19     
20     public void setKeyStore(String keyStore) {
21         validate(keyStore);
22         this.keyStore = keyStore;
23     }
24
25     public void setKeyStorePassword(String keyStorePassword) {
26         setKeyStorePassword(keyStorePassword, true);
27     }
28
29     private void setKeyStorePassword(String keyStorePassword, boolean isEncoded) {
30         validate(keyStorePassword);
31         if(isEncoded) {
32             Either<String, String> passkey = SecurityUtil.INSTANCE.decrypt(keyStorePassword);
33             if (passkey.isLeft()) {
34                 this.keyStorePassword = passkey.left().value();
35             }
36             else {
37                 throw new IllegalArgumentException(passkey.right().value());
38             }
39         }
40         else {
41             this.keyStorePassword = keyStorePassword;
42         }
43     }
44
45     public String getKeyStore() {
46         return keyStore;
47     }
48
49     public String getKeyStorePassword() {
50         return keyStorePassword;
51     }
52     
53     @Override
54     public int hashCode() {
55         final int prime = 31;
56         int result = 1;
57         result = prime * result + ((keyStore == null) ? 0 : keyStore.hashCode());
58         result = prime * result + ((keyStorePassword == null) ? 0 : keyStorePassword.hashCode());
59         return result;
60     }
61
62     @Override
63     public boolean equals(Object obj) {
64         if (this == obj)
65             return true;
66         if (obj == null)
67             return false;
68         if (getClass() != obj.getClass())
69             return false;
70         ClientCertificate other = (ClientCertificate) obj;
71         if (keyStore == null) {
72             if (other.keyStore != null)
73                 return false;
74         }
75         else if (!keyStore.equals(other.keyStore))
76             return false;
77         if (keyStorePassword == null) {
78             if (other.keyStorePassword != null)
79                 return false;
80         }
81         else if (!keyStorePassword.equals(other.keyStorePassword))
82             return false;
83         return true;
84     }
85
86     @Override
87     public String toString() {
88         StringBuilder builder = new StringBuilder();
89         builder.append("ClientCertificate [keyStore=");
90         builder.append(keyStore);
91         builder.append("]");
92         return builder.toString();
93     }
94     
95     private void validate(String str) {
96         if(StringUtils.isEmpty(str)) {
97             throw new IllegalArgumentException("ClientCertificate keystore and/or kestorePassword cannot be empty");
98         }
99     }
100 }