📄 clickatellhttpgateway.java
字号:
// SMSLib for Java v3
// A Java API library for sending and receiving SMS via a GSM modem
// or other supported gateways.
// Web Site: http://www.smslib.org
//
// Copyright (C) 2002-2008, Thanasis Delenikas, Athens/GREECE.
// SMSLib is distributed under the terms of the Apache License version 2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.smslib.http;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.smslib.AGateway;
import org.smslib.GatewayException;
import org.smslib.OutboundMessage;
import org.smslib.OutboundWapSIMessage;
import org.smslib.TimeoutException;
import org.smslib.Message.MessageEncodings;
import org.smslib.Message.MessageTypes;
import org.smslib.OutboundMessage.FailureCauses;
import org.smslib.OutboundMessage.MessageStatuses;
import org.smslib.OutboundWapSIMessage.WapSISignals;
import org.smslib.StatusReportMessage.DeliveryStatuses;
/**
* Gateway for Clickatell bulk operator (http://www.clickatell.com) Outbound
* only - implements HTTP & HTTPS interface.
*/
public class ClickatellHTTPGateway extends HTTPGateway
{
String apiId, username, password;
String sessionId;
KeepAlive keepAlive;
boolean secure;
Object SYNC_Commander;
String HTTP = "http://";
String HTTPS = "https://";
String URL_BALANCE = "api.clickatell.com/http/getbalance";
String URL_COVERAGE = "api.clickatell.com/utils/routeCoverage.php";
String URL_QUERYMSG = "api.clickatell.com/http/querymsg";
String URL_AUTH = "api.clickatell.com/http/auth";
String URL_PING = "api.clickatell.com/http/ping";
String URL_SENDMSG = "api.clickatell.com/http/sendmsg";
String URL_SENDWAPSI = "api.clickatell.com/mms/si_push";
public ClickatellHTTPGateway(String id, String myApiId, String myUsername, String myPassword)
{
super(id);
this.apiId = myApiId;
this.username = myUsername;
this.password = myPassword;
this.sessionId = null;
this.secure = false;
this.SYNC_Commander = new Object();
setAttributes(AGateway.GatewayAttributes.SEND | AGateway.GatewayAttributes.WAPSI | AGateway.GatewayAttributes.CUSTOMFROM | AGateway.GatewayAttributes.BIGMESSAGES | AGateway.GatewayAttributes.FLASHSMS);
}
/**
* Sets whether the gateway works in unsecured (HTTP) or secured (HTTPS)
* mode. False denotes unsecured.
*
* @param mySecure
* True for HTTPS, false for plain HTTP.
*/
public void setSecure(boolean mySecure)
{
this.secure = mySecure;
}
/**
* Return the operation mode (HTTP or HTTPS).
*
* @return True for HTTPS, false for HTTP.
* @see #setSecure(boolean)
*/
public boolean getSecure()
{
return this.secure;
}
@Override
public void startGateway() throws TimeoutException, GatewayException, IOException, InterruptedException
{
getService().getLogger().logInfo("Starting gateway.", null, getGatewayId());
connect();
super.startGateway();
}
@Override
public void stopGateway() throws TimeoutException, GatewayException, IOException, InterruptedException
{
getService().getLogger().logInfo("Stopping gateway.", null, getGatewayId());
super.stopGateway();
this.sessionId = null;
if (this.keepAlive != null)
{
this.keepAlive.interrupt();
this.keepAlive.join();
this.keepAlive = null;
}
}
@SuppressWarnings("unused")
@Override
public float queryBalance() throws TimeoutException, GatewayException, IOException, InterruptedException
{
URL url;
List<HttpHeader> request = new ArrayList<HttpHeader>();
List<String> response;
if (this.sessionId == null) throw new GatewayException("Internal Clickatell Gateway error.");
url = new URL((this.secure ? this.HTTPS : this.HTTP) + this.URL_BALANCE);
request.add(new HttpHeader("session_id", this.sessionId, false));
synchronized (this.SYNC_Commander)
{
response = HttpPost(url, request);
}
if (response.get(0).indexOf("Credit:") == 0) return Float.parseFloat(response.get(0).substring(response.get(0).indexOf(':') + 1));
return -1;
}
@SuppressWarnings("unused")
@Override
public boolean queryCoverage(OutboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
{
URL url;
List<HttpHeader> request = new ArrayList<HttpHeader>();
List<String> response;
if (this.sessionId == null) throw new GatewayException("Internal Clickatell Gateway error.");
url = new URL((this.secure ? this.HTTPS : this.HTTP) + this.URL_COVERAGE);
request.add(new HttpHeader("session_id", this.sessionId, false));
request.add(new HttpHeader("msisdn", msg.getRecipient().substring(1), false));
synchronized (this.SYNC_Commander)
{
response = HttpPost(url, request);
}
if (response.get(0).indexOf("OK") == 0) return true;
return false;
}
@SuppressWarnings("unused")
@Override
public DeliveryStatuses queryMessage(String refNo) throws TimeoutException, GatewayException, IOException, InterruptedException
{
URL url;
List<HttpHeader> request = new ArrayList<HttpHeader>();
List<String> response;
int pos;
if (this.sessionId == null) throw new GatewayException("Internal Clickatell Gateway error.");
url = new URL((this.secure ? this.HTTPS : this.HTTP) + this.URL_QUERYMSG);
request.add(new HttpHeader("session_id", this.sessionId, false));
request.add(new HttpHeader("apimsgid", refNo, false));
synchronized (this.SYNC_Commander)
{
response = HttpPost(url, request);
}
pos = response.get(0).indexOf("Status:");
setDeliveryErrorCode(Integer.parseInt(response.get(0).substring(pos + 7).trim()));
switch (getDeliveryErrorCode())
{
case 1:
return DeliveryStatuses.UNKNOWN;
case 2:
case 3:
case 8:
case 11:
return DeliveryStatuses.KEEPTRYING;
case 4:
return DeliveryStatuses.DELIVERED;
case 5:
case 6:
case 7:
return DeliveryStatuses.ABORTED;
case 9:
case 10:
return DeliveryStatuses.ABORTED;
case 12:
return DeliveryStatuses.ABORTED;
default:
return DeliveryStatuses.UNKNOWN;
}
}
void connect() throws GatewayException, IOException
{
try
{
if (!authenticate()) throw new GatewayException("Cannot authenticate to Clickatell.");
this.keepAlive = new KeepAlive();
}
catch (MalformedURLException e)
{
throw new GatewayException("Internal Clickatell Gateway error.");
}
}
boolean authenticate() throws IOException, MalformedURLException
{
URL url;
List<HttpHeader> request = new ArrayList<HttpHeader>();
List<String> response;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -