move connectors
[andGMXsms.git] / connectors / sipgate / src / de / ub0r / android / websms / connector / sipgate / ConnectorSipgate.java
blobe17b301313d23bfeb42da416a650bf25b9ca8c8a
1 /*
2 * Copyright (C) 2010 Mirko Weber, Felix Bechstein
3 *
4 * This file is part of WebSMS.
5 *
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free Software
8 * Foundation; either version 3 of the License, or (at your option) any later
9 * version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; If not, see <http://www.gnu.org/licenses/>.
19 package de.ub0r.android.andGMXsms;
21 import java.io.Serializable;
22 import java.util.Hashtable;
23 import java.util.Map;
24 import java.util.Vector;
26 import org.xmlrpc.android.XMLRPCClient;
27 import org.xmlrpc.android.XMLRPCException;
28 import org.xmlrpc.android.XMLRPCFault;
30 import android.content.Context;
31 import android.util.Log;
32 import de.ub0r.android.websms.connector.common.WebSMSException;
34 /**
35 * AsyncTask to manage XMLRPC-Calls to sipgate.de remote-API.
37 * @author Mirko Weber
39 public class ConnectorSipgate extends Connector {
40 /** Tag for output. */
41 private static final String TAG = "WebSMS.Sipg";
43 /** Sipgate.de API URL. */
44 private static final String SIPGATE_URL = "https://"
45 + "samurai.sipgate.net/RPC2";
46 /** Sipfate.de TEAM-API URL. */
47 private static final String SIPGATE_TEAM_URL = "https://"
48 + "api.sipgate.net/RPC2";
50 /**
51 * Create a Sipgate Connector.
53 * @param u
54 * username
55 * @param p
56 * password
58 protected ConnectorSipgate(final String u, final String p) {
59 super(null); // FIXME:
62 /**
63 * {@inheritDoc}
65 @Override
66 protected final boolean sendMessage() throws WebSMSException {
67 Log.d(TAG, "sendMessage()");
68 Object back;
69 try {
70 XMLRPCClient client = this.init();
71 Vector<String> remoteUris = new Vector<String>();
72 for (int i = 0; i < this.to.length; i++) {
73 if (this.to[i] != null && this.to[i].length() > 1) {
74 remoteUris.add("sip:" + this.to[i].replaceAll("\\+", "")
75 + "@sipgate.net");
76 Log.d(TAG, "Telefonnummer:" + remoteUris.get(i));
79 Hashtable<String, Serializable> params = new Hashtable<String, Serializable>();
80 // FIXME: if (WebSMS.prefsSender.length() > 6) {
81 // String localUri = "sip:"
82 // + WebSMS.prefsSender.replaceAll("\\+", "")
83 // + "@sipgate.net";
84 // params.put("LocalUri", localUri);
85 // }
86 params.put("RemoteUri", remoteUris);
87 params.put("TOS", "text");
88 params.put("Content", this.text);
89 back = client.call("samurai.SessionInitiateMulti", params);
90 Log.d(TAG, back.toString());
91 } catch (XMLRPCFault e) {
92 Log.e(TAG, null, e);
93 if (e.getFaultCode() == HTTP_SERVICE_UNAUTHORIZED) {
94 throw new WebSMSException(this.context, R.string.log_error_pw);
96 throw new WebSMSException(e.toString());
97 } catch (XMLRPCException e) {
98 Log.e(TAG, null, e);
99 throw new WebSMSException(e.toString());
101 return true;
105 * {@inheritDoc}
107 @SuppressWarnings("unchecked")
108 @Override
109 protected final boolean updateMessages() throws WebSMSException {
110 Log.d(TAG, "updateMessage()");
111 Map<String, Object> back = null;
112 try {
113 XMLRPCClient client = this.init();
114 back = (Map<String, Object>) client.call("samurai.BalanceGet");
115 Log.d(TAG, back.toString());
116 if (back.get("StatusCode").equals(new Integer(HTTP_SERVICE_OK))) {
117 // FIXME WebSMS.SMS_BALANCE[SIPGATE] =
118 // String.format("%.2f \u20AC",
119 // ((Double) ((Map<String, Object>) back
120 // .get("CurrentBalance")).get("TotalIncludingVat")));
122 this.pushMessage(WebSMS.MESSAGE_FREECOUNT, null);
123 } catch (XMLRPCFault e) {
124 Log.e(TAG, null, e);
125 if (e.getFaultCode() == HTTP_SERVICE_UNAUTHORIZED) {
126 throw new WebSMSException(this.context, R.string.log_error_pw);
128 throw new WebSMSException(e.toString());
129 } catch (XMLRPCException e) {
130 Log.e(TAG, null, e);
131 throw new WebSMSException(e.toString());
134 return true;
138 * Sets up and instance of XMLRPCClient.
140 * @return the initialized XMLRPCClient
141 * @throws XMLRPCException
142 * XMLRPCException
144 private XMLRPCClient init() throws XMLRPCException {
145 Log.d(TAG, "updateMessage()");
146 Context c = this.context;
147 final String version = c.getString(R.string.app_version);
148 final String vendor = c.getString(R.string.author1);
149 XMLRPCClient client = null; // FIXME
150 // FIXME: if (WebSMS.prefsEnableSipgateTeam) {
151 // client = new XMLRPCClient(SIPGATE_TEAM_URL);
152 // } else {
153 // client = new XMLRPCClient(SIPGATE_URL);
154 // }
156 // FIXME: client.setBasicAuthentication(this.user, this.password);
157 Object back;
158 try {
159 Hashtable<String, String> ident = new Hashtable<String, String>();
160 ident.put("ClientName", TAG);
161 ident.put("ClientVersion", version);
162 ident.put("ClientVendor", vendor);
163 back = client.call("samurai.ClientIdentify", ident);
164 Log.d(TAG, back.toString());
165 return client;
166 } catch (XMLRPCException e) {
167 Log.e(TAG, "XMLRPCExceptin in init()", e);
168 throw e;