fixing warnings from checkstyle in common-app-api
[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             } else {
55                 throw new IllegalArgumentException(passkey.right().value());
56             }
57         } else {
58             this.keyStorePassword = keyStorePassword;
59         }
60     }
61
62     public String getKeyStore() {
63         return keyStore;
64     }
65
66     public String getKeyStorePassword() {
67         return keyStorePassword;
68     }
69
70     @Override
71     public int hashCode() {
72         final int prime = 31;
73         int result = 1;
74         result = prime * result + ((keyStore == null) ? 0 : keyStore.hashCode());
75         result = prime * result + ((keyStorePassword == null) ? 0 : keyStorePassword.hashCode());
76         return result;
77     }
78
79     @Override
80     public boolean equals(Object obj) {
81         if (this == obj) {
82             return true;
83         }
84         if (obj == null) {
85             return false;
86         }
87         if (getClass() != obj.getClass()) {
88             return false;
89         }
90         ClientCertificate other = (ClientCertificate) obj;
91         if (keyStore == null) {
92             if (other.keyStore != null) {
93                 return false;
94             }
95         } else if (!keyStore.equals(other.keyStore)) {
96             return false;
97         }
98         if (keyStorePassword == null) {
99             return other.keyStorePassword == null;
100         } else {
101             return keyStorePassword.equals(other.keyStorePassword);
102         }
103     }
104
105     @Override
106     public String toString() {
107         return "ClientCertificate [keyStore=" + keyStore + "]";
108     }
109
110     private void validate(String str) {
111         if (StringUtils.isEmpty(str)) {
112             throw new IllegalArgumentException("ClientCertificate keystore and/or kestorePassword cannot be empty");
113         }
114     }
115 }