1 18 package sync4j.framework.core; 19 20 import sync4j.framework.tools.Base64; 21 22 29 public final class Authentication { 30 31 private String data = null; 33 private String username = null; 34 private String password = null; 35 private boolean encode = false; 36 private String deviceId = null ; 37 private String syncMLVerProto = null ; 38 private String principalId = null; 39 40 private Meta meta = null; 41 42 44 45 protected Authentication() {} 46 47 54 public Authentication(final Meta meta, final String data) { 55 this.meta = meta; 56 createAuthentication(meta.getType(),data); 57 } 58 59 66 public Authentication(final String type, final String data) { 67 createAuthentication(type, data); 68 } 69 70 78 public Authentication(final String type, 79 final String data, 80 final boolean encode) { 81 82 this.encode = encode; 83 createAuthentication(type, data); 84 } 85 86 94 public Authentication(final String type, 95 final String username, 96 final String password) { 97 if (username == null || password == null) { 98 throw new IllegalArgumentException ( 99 "The authentication username and password cannot be null"); 100 } 101 102 encode = true; 103 createAuthentication(type, username + ":" + password); 104 } 105 106 108 public void createAuthentication(String type, String data) { 109 110 if (Cred.AUTH_SUPPORTED_TYPES.indexOf(type) < 0) { 111 type = Cred.AUTH_TYPE_BASIC; 112 } 113 114 if (Cred.AUTH_TYPE_BASIC.equals(type)) { 115 this.setType(Cred.AUTH_TYPE_BASIC); 116 this.setFormat(Constants.FORMAT_B64); 117 this.setData(data); 118 } else if (Cred.AUTH_TYPE_MD5.equals(type)) { 119 this.setType(Cred.AUTH_TYPE_MD5); 120 this.setData(data); 121 } 122 } 123 124 129 public String getType() { 130 return (meta == null) ? null : meta.getType(); 131 } 132 133 138 public void setType(String type) { 139 if (meta == null) { 140 meta = new Meta(); 141 } 142 meta.setType(type); 143 } 144 145 150 public String getFormat() { 151 return (meta == null) ? null : meta.getFormat(); 152 }; 153 154 159 public void setFormat(String format) { 160 if (meta == null) { 161 meta = new Meta(); 162 } 163 meta.setFormat(format); 164 }; 165 166 171 public final String getData() { 172 return data; 173 } 174 175 181 public void setData(String data) { 182 if (data == null) { 183 throw new IllegalArgumentException ("data cannot be null"); 184 } 185 186 String type = this.getType(); 187 188 if (type.equals(Cred.AUTH_TYPE_BASIC)) { 189 String clearData = null; 190 191 if (encode) { 192 this.data = new String (Base64.encode(data.getBytes())); 193 clearData = data; 194 } else { 195 clearData = new String (Base64.decode(data.getBytes())); 196 this.data = data; 197 } 198 199 int p = clearData.indexOf(':'); 200 201 if (p == -1) { 202 this.setUsername(clearData); 203 this.setPassword(null); 204 } else { 205 this.username = (p>0) ? clearData.substring(0, p) : ""; 206 this.password = (p<data.length()) ? clearData.substring(p+1) : ""; 207 } 208 } 209 210 if (type.equals(Cred.AUTH_TYPE_MD5)) { 211 if (meta.getFormat() == null) { 212 this.setFormat(Constants.FORMAT_B64); 213 } 214 this.username = data; 215 this.data = data; 216 } 217 } 218 219 220 225 public String getUsername() { 226 return username; 227 } 228 229 234 public void setUsername(String username) { 235 this.username = username; 236 } 237 238 243 public String getPassword() { 244 return password; 245 } 246 247 252 public void setPassword(String password) { 253 this.password = password; 254 } 255 256 261 public NextNonce getNextNonce() { 262 return (meta == null) ? null : meta.getNextNonce(); 263 } 264 265 271 public void setNextNonce(NextNonce nextNonce) { 272 if (meta == null) { 273 meta = new Meta(); 274 } 275 meta.setNextNonce(nextNonce); 276 }; 277 278 283 public Meta getMeta() { 284 return meta; 285 } 286 287 293 public void setMeta(Meta meta) { 294 this.meta = meta; 295 }; 296 297 302 public String getDeviceId() { 303 return this.deviceId; 304 } 305 306 311 public void setDeviceId(String deviceId) { 312 this.deviceId = deviceId; 313 } 314 315 321 public String getSyncMLVerProto() { 322 return this.syncMLVerProto; 323 } 324 325 332 public void setSyncMLVerProto(String syncMLVerProto) { 333 this.syncMLVerProto = syncMLVerProto; 334 } 335 336 341 public String getPrincipalId() { 342 return this.principalId; 343 } 344 345 350 public void setPrincipalId(String principalId) { 351 this.principalId = principalId; 352 } 353 } | Popular Tags |