📄 clickatellhttpgateway.java
字号:
getService().getLogger().logDebug("Authenticate().", null, getGatewayId());
url = new URL((this.secure ? this.HTTPS : this.HTTP) + this.URL_AUTH);
request.add(new HttpHeader("api_id", this.apiId, false));
request.add(new HttpHeader("user", this.username, false));
request.add(new HttpHeader("password", this.password, false));
synchronized (this.SYNC_Commander)
{
response = HttpPost(url, request);
}
if (response.get(0).indexOf("ERR:") == 0)
{
this.sessionId = null;
return false;
}
this.sessionId = response.get(0).substring(4);
return true;
}
boolean ping() throws IOException, MalformedURLException
{
URL url;
List<HttpHeader> request = new ArrayList<HttpHeader>();
List<String> response;
getService().getLogger().logDebug("Ping()", null, getGatewayId());
url = new URL((this.secure ? this.HTTPS : this.HTTP) + this.URL_PING);
request.add(new HttpHeader("session_id", this.sessionId, false));
synchronized (this.SYNC_Commander)
{
response = HttpPost(url, request);
}
if (response.get(0).indexOf("ERR:") == 0) return false;
return true;
}
@SuppressWarnings("unused")
@Override
public boolean sendMessage(OutboundMessage msg) throws TimeoutException, GatewayException, IOException, InterruptedException
{
URL url;
List<HttpHeader> request = new ArrayList<HttpHeader>();
List<String> response;
int requestFeatures = 0;
boolean ok = false;
if (this.sessionId == null)
{
getService().getLogger().logError("No session defined.", null, getGatewayId());
msg.setFailureCause(FailureCauses.GATEWAY_FAILURE);
return false;
}
getService().getLogger().logDebug("sendMessage()", null, getGatewayId());
try
{
if (msg.getType() == MessageTypes.OUTBOUND) url = new URL((this.secure ? this.HTTPS : this.HTTP) + this.URL_SENDMSG);
else if (msg.getType() == MessageTypes.WAPSI) url = new URL((this.secure ? this.HTTPS : this.HTTP) + this.URL_SENDWAPSI);
else
{
msg.setFailureCause(FailureCauses.BAD_FORMAT);
getService().getLogger().logError("Incorrect message format.", null, getGatewayId());
return false;
}
request.add(new HttpHeader("session_id", this.sessionId, false));
request.add(new HttpHeader("to", msg.getRecipient().substring(1), false));
request.add(new HttpHeader("concat", "3", false));
if (msg.getPriority() < 0) request.add(new HttpHeader("queue", "3", false));
else if (msg.getPriority() == 0) request.add(new HttpHeader("queue", "2", false));
else if (msg.getPriority() >= 0) request.add(new HttpHeader("queue", "1", false));
if (msg.getFrom() != null && msg.getFrom().length() != 0) request.add(new HttpHeader("from", msg.getFrom(), false));
else if (getFrom() != null && getFrom().length() != 0) request.add(new HttpHeader("from", getFrom(), false));
if (msg.getFlashSms()) request.add(new HttpHeader("msg_type", "SMS_FLASH", false));
if (msg.getType() == MessageTypes.OUTBOUND)
{
if ((msg.getSrcPort() != -1) || (msg.getDstPort() != -1)) request.add(new HttpHeader("udh", msg.getPduUserDataHeader(), false));
if (msg.getEncoding() == MessageEncodings.ENC7BIT) request.add(new HttpHeader("text", msg.getText(), false));
else if (msg.getEncoding() == MessageEncodings.ENCUCS2)
{
request.add(new HttpHeader("unicode", "1", false));
request.add(new HttpHeader("text", msg.getText(), true));
}
}
else if (msg.getType() == MessageTypes.WAPSI)
{
request.add(new HttpHeader("si_id", String.valueOf(msg.getId()), false));
if (((OutboundWapSIMessage) msg).getCreateDate() != null) request.add(new HttpHeader("si_created", formatDateUTC(((OutboundWapSIMessage) msg).getCreateDate()), false));
if (((OutboundWapSIMessage) msg).getExpireDate() != null) request.add(new HttpHeader("si_expires", formatDateUTC(((OutboundWapSIMessage) msg).getExpireDate()), false));
request.add(new HttpHeader("si_action", formatSignal(((OutboundWapSIMessage) msg).getSignal()), false));
request.add(new HttpHeader("si_url", ((OutboundWapSIMessage) msg).getUrl().toString(), false));
request.add(new HttpHeader("si_text", ((OutboundWapSIMessage) msg).getIndicationText(), false));
}
if (msg.getStatusReport()) request.add(new HttpHeader("deliv_ack", "1", false));
if ((getFrom() != null && getFrom().length() != 0) || (msg.getFrom() != null && msg.getFrom().length() != 0)) requestFeatures += 16 + 32;
if (msg.getFlashSms()) requestFeatures += 512;
if (msg.getStatusReport()) requestFeatures += 8192;
request.add(new HttpHeader("req_feat", "" + requestFeatures, false));
synchronized (this.SYNC_Commander)
{
response = HttpPost(url, request);
}
if (response.get(0).indexOf("ID:") == 0)
{
msg.setRefNo(response.get(0).substring(4));
msg.setDispatchDate(new Date());
msg.setGatewayId(getGatewayId());
msg.setMessageStatus(MessageStatuses.SENT);
incOutboundMessageCount();
ok = true;
}
else if (response.get(0).indexOf("ERR:") == 0)
{
switch (Integer.parseInt(response.get(0).substring(5, 8)))
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
msg.setFailureCause(FailureCauses.GATEWAY_AUTH);
break;
case 101:
case 102:
case 105:
case 106:
case 107:
case 112:
case 116:
case 120:
msg.setFailureCause(FailureCauses.BAD_FORMAT);
break;
case 114:
msg.setFailureCause(FailureCauses.NO_ROUTE);
break;
case 301:
case 302:
msg.setFailureCause(FailureCauses.NO_CREDIT);
break;
default:
msg.setFailureCause(FailureCauses.UNKNOWN);
break;
}
msg.setRefNo(null);
msg.setDispatchDate(null);
msg.setMessageStatus(MessageStatuses.FAILED);
ok = false;
}
}
catch (MalformedURLException e)
{
getService().getLogger().logError("Malformed URL.", e, getGatewayId());
}
catch (IOException e)
{
getService().getLogger().logError("I/O error.", e, getGatewayId());
}
return ok;
}
String formatDateUTC(Date d)
{
String strDate = "", tmp = "";
Calendar cal = Calendar.getInstance();
cal.setTime(d);
strDate = String.valueOf(cal.get(Calendar.YEAR));
tmp = String.valueOf(cal.get(Calendar.MONTH) + 1);
if (tmp.length() != 2) tmp = "0" + tmp;
strDate += "-" + tmp;
tmp = String.valueOf(cal.get(Calendar.DATE));
if (tmp.length() != 2) tmp = "0" + tmp;
strDate += "-" + tmp;
tmp = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
if (tmp.length() != 2) tmp = "0" + tmp;
strDate += "T" + tmp;
tmp = String.valueOf(cal.get(Calendar.MINUTE));
if (tmp.length() != 2) tmp = "0" + tmp;
strDate += ":" + tmp;
tmp = String.valueOf(cal.get(Calendar.SECOND));
if (tmp.length() != 2) tmp = "0" + tmp;
strDate += ":" + tmp + "Z";
return strDate;
}
String formatSignal(WapSISignals signal)
{
if (signal == WapSISignals.NONE) return "signal-none";
else if (signal == WapSISignals.LOW) return "signal-low";
else if (signal == WapSISignals.MEDIUM) return "signal-medium";
else if (signal == WapSISignals.HIGH) return "signal-high";
else if (signal == WapSISignals.DELETE) return "signal-delete";
else return "signal-none";
}
class KeepAlive extends Thread
{
public KeepAlive()
{
setPriority(MIN_PRIORITY);
start();
}
@Override
public void run()
{
getService().getLogger().logDebug("KeepAlive thread started.", null, getGatewayId());
while (true)
{
try
{
sleep(10 * 60 * 1000);
if (ClickatellHTTPGateway.this.sessionId == null) break;
getService().getLogger().logDebug("** KeepAlive START **", null, getGatewayId());
synchronized (ClickatellHTTPGateway.this.SYNC_Commander)
{
ping();
}
getService().getLogger().logDebug("** KeepAlive END **", null, getGatewayId());
}
catch (InterruptedException e)
{
if (ClickatellHTTPGateway.this.sessionId == null) break;
}
catch (Exception e)
{
// Swallow this...
}
}
getService().getLogger().logDebug("KeepAlive thread ended.", null, getGatewayId());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -