1 19 20 package com.maverick.http; 21 22 import java.io.IOException ; 23 import java.util.Hashtable ; 24 25 import com.maverick.crypto.encoders.Base64; 26 27 31 public class BasicAuthentication extends HttpAuthenticator { 32 33 String realm; 34 35 public BasicAuthentication(String uri, String host, int port, boolean secure) { 36 super("Basic", uri, host, port, secure); } 38 39 public String getRealm() { 40 return realm; 41 } 42 43 public boolean isStateless() { 44 return true; 45 } 46 47 53 public void authenticate(HttpRequest request, HttpMethod method) { 54 String str = credentials.getUsername() + ":" + credentials.getPassword(); request.setHeaderField(authorizationHeader, "Basic " + new String (Base64.encode(str.getBytes()))); } 57 58 public int processResponse(HttpResponse response) { 59 return (hasCompleted = response.getStatus() >= 200 && response.getStatus() < 400) ? AUTHENTICATION_COMPLETED 60 : AUTHENTICATION_FAILED; 61 } 62 63 public void setChallenge(String challenge) { 64 try { 65 Hashtable params = ParameterParser.extractParams(challenge); 66 this.realm = (String ) params.get("realm"); } catch (IOException ex) { 68 this.realm = ""; } 70 } 71 72 public String getInformation() { 73 return getRealm(); 74 } 75 } 76 | Popular Tags |