Added oparent to sdc main
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / http / config / ClientCertificate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.common.http.config;
22
23 import fj.data.Either;
24 import org.apache.commons.lang3.StringUtils;
25 import org.openecomp.sdc.security.SecurityUtil;
26
27 public class ClientCertificate {
28     private String keyStore;
29     private String keyStorePassword;
30
31     public ClientCertificate() {
32     }
33
34     public ClientCertificate(ClientCertificate clientCertificate) {
35         setKeyStore(clientCertificate.getKeyStore());
36         setKeyStorePassword(clientCertificate.getKeyStorePassword(), false);
37     }
38     
39     public void setKeyStore(String keyStore) {
40         validate(keyStore);
41         this.keyStore = keyStore;
42     }
43
44     public void setKeyStorePassword(String keyStorePassword) {
45         setKeyStorePassword(keyStorePassword, true);
46     }
47
48     private void setKeyStorePassword(String keyStorePassword, boolean isEncoded) {
49         validate(keyStorePassword);
50         if(isEncoded) {
51             Either<String, String> passkey = SecurityUtil.INSTANCE.decrypt(keyStorePassword);
52             if (passkey.isLeft()) {
53                 this.keyStorePassword = passkey.left().value();
54             }
55             else {
56                 throw new IllegalArgumentException(passkey.right().value());
57             }
58         }
59         else {
60             this.keyStorePassword = keyStorePassword;
61         }
62     }
63
64     public String getKeyStore() {
65         return keyStore;
66     }
67
68     public String getKeyStorePassword() {
69         return keyStorePassword;
70     }
71     
72     @Override
73     public int hashCode() {
74         final int prime = 31;
75         int result = 1;
76         result = prime * result + ((keyStore == null) ? 0 : keyStore.hashCode());
77         result = prime * result + ((keyStorePassword == null) ? 0 : keyStorePassword.hashCode());
78         return result;
79     }
80
81     @Override
82     public boolean equals(Object obj) {
83         if (this == obj)
84             return true;
85         if (obj == null)
86             return false;
87         if (getClass() != obj.getClass())
88             return false;
89         ClientCertificate other = (ClientCertificate) obj;
90         if (keyStore == null) {
91             if (other.keyStore != null)
92                 return false;
93         }
94         else if (!keyStore.equals(other.keyStore))
95             return false;
96         if (keyStorePassword == null) {
97             if (other.keyStorePassword != null)
98                 return false;
99         }
100         else if (!keyStorePassword.equals(other.keyStorePassword))
101             return false;
102         return true;
103     }
104
105     @Override
106     public String toString() {
107         StringBuilder builder = new StringBuilder();
108         builder.append("ClientCertificate [keyStore=");
109         builder.append(keyStore);
110         builder.append("]");
111         return builder.toString();
112     }
113     
114     private void validate(String str) {
115         if(StringUtils.isEmpty(str)) {
116             throw new IllegalArgumentException("ClientCertificate keystore and/or kestorePassword cannot be empty");
117         }
118     }
119 }