Android Open Source - SipgateInfo Sipgate Xml Rpc Impl






From Project

Back to project page SipgateInfo.

License

The source code is released under:

GNU General Public License

If you think the Android project SipgateInfo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.skweez.sipgate.api.xmlrpc;
/*ww  w.  ja v  a  2  s .  co m*/
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import net.skweez.sipgate.api.AuthenticationException;
import net.skweez.sipgate.api.Call;
import net.skweez.sipgate.api.ECallStatus;
import net.skweez.sipgate.api.ISipgateAPI;
import net.skweez.sipgate.api.Price;
import net.skweez.sipgate.api.SipgateException;
import net.skweez.sipgate.api.UserName;
import net.skweez.sipgate.api.UserUri;

import org.apache.http.HttpStatus;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;

/**
 * @author Michael Kanis
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class SipgateXmlRpcImpl implements ISipgateAPI {

  private static final URI API_URI;

  static {
    API_URI = URI.create("https://samurai.sipgate.net/RPC2");
  }

  /** {@inheritDoc} */
  public Price getBalance() {
    Map<String, Map> result = (Map<String, Map>) executeMethod("samurai.BalanceGet");
    Map currentBalance = result.get("CurrentBalance");

    return new Price((Double) currentBalance.get("TotalIncludingVat"),
        (String) currentBalance.get("Currency"));
  }

  public List<Call> getHistoryByDate() {
    List<Call> callList = new ArrayList<Call>();

    Map result = executeMethod("samurai.HistoryGetByDate");
    Object[] history = (Object[]) result.get("History");

    for (Object object : history) {
      callList.add(createCallFromMap((Map) object));
    }

    Collections.sort(callList, new Comparator<Call>() {
      public int compare(Call call1, Call call2) {
        // Sort the list in reverse order (-1), so that the newest call
        // comes first
        return (-1)
            * call1.getTimestamp().compareTo(call2.getTimestamp());
      }
    });

    return callList;
  }

  private Call createCallFromMap(Map map) {
    Call call = new Call();

    call.setRemoteURI(SipgateUriHelper.createUriFromString((String) map
        .get("RemoteUri")));
    call.setStatus(ECallStatus.fromString((String) map.get("Status")));
    call.setTimestamp((String) map.get("Timestamp"));

    return call;
  }

  public List<UserUri> getOwnUriList() {
    Map<String, Object> result = (Map<String, Object>) executeMethod("samurai.OwnUriListGet");

    Object[] userUriMap = (Object[]) result.get("OwnUriList");
    List<UserUri> list = new ArrayList<UserUri>();

    for (int i = 0; i < userUriMap.length; i++) {
      Map entry = (Map) userUriMap[i];

      list.add(new UserUri(entry.get("E164Out").toString(),
          SipgateUriHelper.createUriFromString(entry.get("SipUri")
              .toString()), new Boolean(entry.get("DefaultUri")
              .toString())));
    }
    return list;
  }

  public UserName getUserdataGreeting() {
    Map<String, String> result = (Map<String, String>) executeMethod("samurai.UserdataGreetingGet");
    return new UserName(result.get("FirstName"), result.get("LastName"));
  }

  private Map<String, ? extends Object> executeMethod(String method,
      String... params) {

    try {
      return (Map<String, Object>) getAuthenticatedClient().callEx(
          method, params);
    } catch (final XMLRPCException exception) {

      // Sorry, this uses a modified version of XMLRPC :-(
      // see http://code.google.com/p/android-xmlrpc/issues/detail?id=30
      if (exception.getHttpStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
        throw new AuthenticationException("Wrong username or password.");
      }

      throw new SipgateException(exception);
    }
  }

  private XMLRPCClient getAuthenticatedClient()
      throws AuthenticationException {

    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 AuthenticationException(
          "Please set up your username and password first.");
    }
  }
}




Java Source Code List

net.skweez.sipgate.CallUtils.java
net.skweez.sipgate.PreferencesAuthenticator.java
net.skweez.sipgate.SipgateApplication.java
net.skweez.sipgate.activity.AccountInfoAdapter.java
net.skweez.sipgate.activity.CallListAdapter.java
net.skweez.sipgate.activity.MainActivity.java
net.skweez.sipgate.activity.SetupActivity.java
net.skweez.sipgate.api.AuthenticationException.java
net.skweez.sipgate.api.Call.java
net.skweez.sipgate.api.ECallStatus.java
net.skweez.sipgate.api.ISipgateAPI.java
net.skweez.sipgate.api.Price.java
net.skweez.sipgate.api.SipgateException.java
net.skweez.sipgate.api.UserName.java
net.skweez.sipgate.api.UserUri.java
net.skweez.sipgate.api.xmlrpc.SipgateUriHelper.java
net.skweez.sipgate.api.xmlrpc.SipgateXmlRpcImpl.java
net.skweez.sipgate.db.DataSource.java
net.skweez.sipgate.db.DatabaseHelper.java
net.skweez.sipgate.model.AccountInfo.java
net.skweez.sipgate.service.QueryResultReceiver.java
net.skweez.sipgate.service.QueryService.java