From 0dc1164973df726770cc8c9ec9c1c2321d7ad799 Mon Sep 17 00:00:00 2001 From: Michael Kanis Date: Mon, 15 Aug 2011 13:06:19 +0200 Subject: [PATCH] Clean up authentication mechanism --- .../sipgate/NoAuthenticationException.java | 5 ++ .../sipgate/PreferencesAuthenticator.java | 18 +++-- .../sipgate/api/xmlrpc/SipgateXmlRpcImpl.java | 68 ++++++++----------- 3 files changed, 46 insertions(+), 45 deletions(-) create mode 100644 src/net/skweez/sipgate/NoAuthenticationException.java diff --git a/src/net/skweez/sipgate/NoAuthenticationException.java b/src/net/skweez/sipgate/NoAuthenticationException.java new file mode 100644 index 0000000..1dc38b6 --- /dev/null +++ b/src/net/skweez/sipgate/NoAuthenticationException.java @@ -0,0 +1,5 @@ +package net.skweez.sipgate; + +public class NoAuthenticationException extends Exception { + +} diff --git a/src/net/skweez/sipgate/PreferencesAuthenticator.java b/src/net/skweez/sipgate/PreferencesAuthenticator.java index cbf7b97..ae2b10b 100644 --- a/src/net/skweez/sipgate/PreferencesAuthenticator.java +++ b/src/net/skweez/sipgate/PreferencesAuthenticator.java @@ -6,11 +6,10 @@ import android.content.SharedPreferences; /** + * An implementation of {@link Authenticator} that gets user name and password + * from the {@link SharedPreferences}. * - * @author mks - * @author $Author: mks $ - * @version $Rev: 9 $ - * @levd.rating RED Rev: + * @author Michael Kanis */ public class PreferencesAuthenticator extends Authenticator { @@ -23,7 +22,14 @@ public PreferencesAuthenticator(SharedPreferences preferences) { /** {@inheritDoc} */ @Override protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(prefs.getString("username", ""), - prefs.getString("password", "").toCharArray()); + + String userName = prefs.getString("username", null); + String password = prefs.getString("password", null); + + if (userName == null || password == null) { + return null; + } + + return new PasswordAuthentication(userName, password.toCharArray()); } } diff --git a/src/net/skweez/sipgate/api/xmlrpc/SipgateXmlRpcImpl.java b/src/net/skweez/sipgate/api/xmlrpc/SipgateXmlRpcImpl.java index 6969633..1908b52 100644 --- a/src/net/skweez/sipgate/api/xmlrpc/SipgateXmlRpcImpl.java +++ b/src/net/skweez/sipgate/api/xmlrpc/SipgateXmlRpcImpl.java @@ -1,17 +1,3 @@ -/*-----------------------------------------------------------------------+ - | sipgate Kontostand - | | - $Id: codetemplates.xml 9 2009-03-06 10:09:52Z mks $ - | | - | Copyright (c) 2004-2011 Technische Universitaet Muenchen | - | | - | Technische Universitaet Muenchen ######### ########## | - | Institut fuer Informatik - Lehrstuhl IV ## ## ## ## ## | - | Prof. Dr. Manfred Broy ## ## ## ## ## | - | Boltzmannstr. 3 ## ## ## ## ## | - | 85748 Garching bei Muenchen ## ## ## ## ## | - | Germany ## ###### ## ## | - +-----------------------------------------------------------------------*/ package net.skweez.sipgate.api.xmlrpc; import java.net.Authenticator; @@ -23,6 +9,7 @@ import java.util.List; import java.util.Map; +import net.skweez.sipgate.NoAuthenticationException; import net.skweez.sipgate.api.Call; import net.skweez.sipgate.api.ECallStatus; import net.skweez.sipgate.api.Gender; @@ -33,14 +20,9 @@ import net.skweez.sipgate.api.UserUri; import org.xmlrpc.android.XMLRPCClient; -import org.xmlrpc.android.XMLRPCException; /** - * - * @author mks - * @author $Author: mks $ - * @version $Rev: 9 $ - * @levd.rating RED Rev: + * @author Michael Kanis */ public class SipgateXmlRpcImpl implements ISipgateAPI { @@ -50,20 +32,7 @@ public class SipgateXmlRpcImpl implements ISipgateAPI { API_URI = URI.create("https://samurai.sipgate.net/RPC2"); } - private final XMLRPCClient client; - - public SipgateXmlRpcImpl() { - PasswordAuthentication authentication = Authenticator - .requestPasswordAuthentication(null, 80, "http", null, null); - - String username = authentication.getUserName(); - String password = String.valueOf(authentication.getPassword()); - - client = new XMLRPCClient(API_URI, username, password); - } - /** {@inheritDoc} */ - @SuppressWarnings({ "rawtypes", "unchecked" }) public Price getBalance() { Map result = (Map) executeMethod("samurai.BalanceGet"); Map currentBalance = result.get("CurrentBalance"); @@ -97,8 +66,10 @@ public int compare(Call call1, Call call2) { private Call createCallFromMap(Map map) { Call call = new Call(); - call.setLocalURI(SipgateUriHelper.createUriFromString((String) map.get("LocalUri"))); - call.setRemoteURI(SipgateUriHelper.createUriFromString((String) map.get("RemoteUri"))); + call.setLocalURI(SipgateUriHelper.createUriFromString((String) map + .get("LocalUri"))); + call.setRemoteURI(SipgateUriHelper.createUriFromString((String) map + .get("RemoteUri"))); call.setStatus(ECallStatus.fromString((String) map.get("Status"))); call.setTimestamp((String) map.get("Timestamp")); @@ -115,8 +86,9 @@ public UserUri[] getUserUriList() { Map entry = (Map) userUriMap[i]; userUriList[i] = new UserUri(entry.get("E164Out").toString(), - SipgateUriHelper.createUriFromString(entry.get("SipUri").toString()), - new Boolean(entry.get("DefaultUri").toString())); + SipgateUriHelper.createUriFromString(entry.get("SipUri") + .toString()), new Boolean(entry.get("DefaultUri") + .toString())); } return userUriList; } @@ -130,10 +102,28 @@ public UserName getUserName() { private Map executeMethod(String method, String... params) { + try { - return (Map) client.callEx(method, params); - } catch (final XMLRPCException exception) { + return (Map) getAuthenticatedClient().callEx( + method, params); + } catch (final Exception exception) { throw new SipgateException(exception); } } + + private XMLRPCClient getAuthenticatedClient() + throws NoAuthenticationException { + + PasswordAuthentication authentication = Authenticator + .requestPasswordAuthentication(null, 80, "http", null, null); + + if (authentication != null) { + String username = authentication.getUserName(); + String password = String.valueOf(authentication.getPassword()); + + return new XMLRPCClient(API_URI, username, password); + } else { + throw new NoAuthenticationException(); + } + } }