diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-11-02 16:05:31 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-11-02 16:05:31 +1300 |
commit | fb5e953f90f0bf48593feb88138a5ee1706b6989 (patch) | |
tree | c90ca8228c5436fe6f462d8cc5730df4d0388e16 /src/org/traccar | |
parent | f6fa77c3e2e63dbfa14884ee8fe448c467c9f592 (diff) | |
download | trackermap-server-fb5e953f90f0bf48593feb88138a5ee1706b6989.tar.gz trackermap-server-fb5e953f90f0bf48593feb88138a5ee1706b6989.tar.bz2 trackermap-server-fb5e953f90f0bf48593feb88138a5ee1706b6989.zip |
Implement universal HTTP SMS manager
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/Context.java | 3 | ||||
-rw-r--r-- | src/org/traccar/notificators/NotificatorSms.java | 1 | ||||
-rw-r--r-- | src/org/traccar/sms/HttpSmsClient.java | 90 | ||||
-rw-r--r-- | src/org/traccar/sms/SmsGatewayClient.java | 71 | ||||
-rw-r--r-- | src/org/traccar/sms/SmsManager.java | 8 | ||||
-rw-r--r-- | src/org/traccar/sms/smpp/ClientSmppSessionHandler.java (renamed from src/org/traccar/smpp/ClientSmppSessionHandler.java) | 3 | ||||
-rw-r--r-- | src/org/traccar/sms/smpp/EnquireLinkTask.java (renamed from src/org/traccar/smpp/EnquireLinkTask.java) | 2 | ||||
-rw-r--r-- | src/org/traccar/sms/smpp/ReconnectionTask.java (renamed from src/org/traccar/smpp/ReconnectionTask.java) | 3 | ||||
-rw-r--r-- | src/org/traccar/sms/smpp/SmppClient.java (renamed from src/org/traccar/smpp/SmppClient.java) | 3 |
9 files changed, 105 insertions, 79 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 5b22120d7..3c3dac874 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -85,6 +85,7 @@ import org.traccar.notification.JsonTypeEventForwarder; import org.traccar.notification.NotificatorManager; import org.traccar.reports.model.TripsConfig; import org.traccar.sms.SmsManager; +import org.traccar.sms.smpp.SmppClient; import org.traccar.web.WebServer; import javax.ws.rs.client.Client; @@ -422,7 +423,7 @@ public final class Context { tripsConfig = initTripsConfig(); if (config.getBoolean("sms.enable")) { - final String smsManagerClass = config.getString("sms.manager.class", "org.traccar.smpp.SmppClient"); + final String smsManagerClass = config.getString("sms.manager.class", SmppClient.class.getCanonicalName()); try { smsManager = (SmsManager) Class.forName(smsManagerClass).newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { diff --git a/src/org/traccar/notificators/NotificatorSms.java b/src/org/traccar/notificators/NotificatorSms.java index cc19f9a58..d4052c626 100644 --- a/src/org/traccar/notificators/NotificatorSms.java +++ b/src/org/traccar/notificators/NotificatorSms.java @@ -56,4 +56,5 @@ public final class NotificatorSms extends Notificator { NotificationFormatter.formatShortMessage(userId, event, position), false); } } + } diff --git a/src/org/traccar/sms/HttpSmsClient.java b/src/org/traccar/sms/HttpSmsClient.java new file mode 100644 index 000000000..e14099772 --- /dev/null +++ b/src/org/traccar/sms/HttpSmsClient.java @@ -0,0 +1,90 @@ +/* + * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) + * + * 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.traccar.sms; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.traccar.Context; +import org.traccar.api.SecurityRequestFilter; +import org.traccar.helper.DataConverter; +import org.traccar.notification.MessageException; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +public class HttpSmsClient implements SmsManager { + + private String url; + private String authorization; + private String template; + private boolean encode; + private MediaType mediaType; + + public HttpSmsClient() { + url = Context.getConfig().getString("sms.http.url"); + authorization = Context.getConfig().getString("sms.http.authorization"); + if (authorization == null) { + String user = Context.getConfig().getString("sms.http.user"); + String password = Context.getConfig().getString("sms.http.password"); + authorization = "Basic " + DataConverter.printBase64((user + ":" + password).getBytes()); + } + template = Context.getConfig().getString("sms.http.template").trim(); + if (template.charAt(0) == '{' || template.charAt(0) == '[') { + encode = false; + mediaType = MediaType.APPLICATION_JSON_TYPE; + } else { + encode = true; + mediaType = MediaType.APPLICATION_FORM_URLENCODED_TYPE; + } + } + + private String prepareValue(String value) throws UnsupportedEncodingException { + return encode ? URLEncoder.encode(value, StandardCharsets.UTF_8.name()) : value; + } + + private String preparePayload(String destAddress, String message) { + try { + return template + .replace("{phone}", prepareValue(destAddress)) + .replace("{message}", prepareValue(message)); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + private Invocation.Builder getRequestBuilder() { + return Context.getClient().target(url).request() + .header(SecurityRequestFilter.AUTHORIZATION_HEADER, authorization); + } + + @Override + public void sendMessageSync(String destAddress, String message, boolean command) throws MessageException { + Response response = getRequestBuilder().post(Entity.entity(preparePayload(destAddress, message), mediaType)); + if (response.getStatus() / 100 != 2) { + throw new MessageException(response.readEntity(String.class)); + } + } + + @Override + public void sendMessageAsync(final String destAddress, final String message, final boolean command) { + getRequestBuilder().async().post(Entity.json(preparePayload(destAddress, message))); + } + +} diff --git a/src/org/traccar/sms/SmsGatewayClient.java b/src/org/traccar/sms/SmsGatewayClient.java deleted file mode 100644 index ea2df6881..000000000 --- a/src/org/traccar/sms/SmsGatewayClient.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2018 Anton Tananaev (anton@traccar.org) - * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) - * - * 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.traccar.sms; - -import javax.json.Json; -import javax.json.JsonArray; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.core.Response; -import org.traccar.Context; -import org.traccar.notification.MessageException; - -public class SmsGatewayClient implements SmsManager { - - private static final String URL = "https://smsgateway.me/api/v4/message/send"; - private static final String KEY_PHONE_NUMBER = "phone_number"; - private static final String KEY_MESSAGE = "message"; - private static final String KEY_DEVICE_ID = "device_id"; - private static final String KEY_AUTHORIZATION = "Authorization"; - - private final String token; - private final int deviceId; - - public SmsGatewayClient() { - token = Context.getConfig().getString("sms.smsgateway.token"); - deviceId = Context.getConfig().getInteger("sms.smsgateway.deviceId"); - } - - private JsonArray preparePayload(String destAddress, String message) { - return Json.createArrayBuilder() - .add(Json.createObjectBuilder() - .add(KEY_PHONE_NUMBER, destAddress) - .add(KEY_MESSAGE, message) - .add(KEY_DEVICE_ID, deviceId)) - .build(); - } - - private Invocation.Builder getRequestBuilder() { - return Context.getClient().target(URL).request() - .header(KEY_AUTHORIZATION, token); - } - - @Override - public void sendMessageSync(String destAddress, String message, boolean command) - throws InterruptedException, MessageException { - Response response = getRequestBuilder().post(Entity.json(preparePayload(destAddress, message))); - if (!response.getStatusInfo().equals(Response.Status.OK)) { - String output = response.readEntity(String.class); - throw new MessageException(output); - } - } - - @Override - public void sendMessageAsync(final String destAddress, final String message, final boolean command) { - getRequestBuilder().async().post(Entity.json(preparePayload(destAddress, message))); - } -} diff --git a/src/org/traccar/sms/SmsManager.java b/src/org/traccar/sms/SmsManager.java index 4bc4bd009..1e3d40f6e 100644 --- a/src/org/traccar/sms/SmsManager.java +++ b/src/org/traccar/sms/SmsManager.java @@ -20,8 +20,10 @@ import org.traccar.notification.MessageException; public interface SmsManager { - void sendMessageSync(String destAddress, String message, boolean command) - throws InterruptedException, MessageException; - void sendMessageAsync(final String destAddress, final String message, final boolean command); + void sendMessageSync( + String destAddress, String message, boolean command) throws InterruptedException, MessageException; + + void sendMessageAsync( + final String destAddress, final String message, final boolean command); } diff --git a/src/org/traccar/smpp/ClientSmppSessionHandler.java b/src/org/traccar/sms/smpp/ClientSmppSessionHandler.java index 4c5522c14..f48721ff9 100644 --- a/src/org/traccar/smpp/ClientSmppSessionHandler.java +++ b/src/org/traccar/sms/smpp/ClientSmppSessionHandler.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar.smpp; +package org.traccar.sms.smpp; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -79,4 +79,5 @@ public class ClientSmppSessionHandler extends DefaultSmppSessionHandler { LOGGER.warn("SMPP session channel unexpectedly closed"); smppClient.scheduleReconnect(); } + } diff --git a/src/org/traccar/smpp/EnquireLinkTask.java b/src/org/traccar/sms/smpp/EnquireLinkTask.java index 3072f975a..7086709d7 100644 --- a/src/org/traccar/smpp/EnquireLinkTask.java +++ b/src/org/traccar/sms/smpp/EnquireLinkTask.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar.smpp; +package org.traccar.sms.smpp; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/org/traccar/smpp/ReconnectionTask.java b/src/org/traccar/sms/smpp/ReconnectionTask.java index c9d9173ae..c009de8e7 100644 --- a/src/org/traccar/smpp/ReconnectionTask.java +++ b/src/org/traccar/sms/smpp/ReconnectionTask.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar.smpp; +package org.traccar.sms.smpp; public class ReconnectionTask implements Runnable { @@ -28,4 +28,5 @@ public class ReconnectionTask implements Runnable { public void run() { smppClient.reconnect(); } + } diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/sms/smpp/SmppClient.java index d4f201871..874253d36 100644 --- a/src/org/traccar/smpp/SmppClient.java +++ b/src/org/traccar/sms/smpp/SmppClient.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar.smpp; +package org.traccar.sms.smpp; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -268,4 +268,5 @@ public class SmppClient implements SmsManager { } }); } + } |