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