diff options
author | Marcos Dallagnelo <marcos.dallagnelo@gmail.com> | 2018-08-16 14:23:35 -0300 |
---|---|---|
committer | Marcos Dallagnelo <marcos.dallagnelo@gmail.com> | 2018-08-16 14:23:35 -0300 |
commit | 6d6a8949a598ff9aac9370655e2e0bd759391368 (patch) | |
tree | 00960db38cd5828c7e02ce1d867168b430898c3a /src/org/traccar/sms/SmsGatewayClient.java | |
parent | 23505018bebe2120c4ceac219f0d22a0646200ce (diff) | |
download | trackermap-server-6d6a8949a598ff9aac9370655e2e0bd759391368.tar.gz trackermap-server-6d6a8949a598ff9aac9370655e2e0bd759391368.tar.bz2 trackermap-server-6d6a8949a598ff9aac9370655e2e0bd759391368.zip |
Have improved code, without create new thread and throw a MessageException
Diffstat (limited to 'src/org/traccar/sms/SmsGatewayClient.java')
-rw-r--r-- | src/org/traccar/sms/SmsGatewayClient.java | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/src/org/traccar/sms/SmsGatewayClient.java b/src/org/traccar/sms/SmsGatewayClient.java index b1683783d..da0b59ec1 100644 --- a/src/org/traccar/sms/SmsGatewayClient.java +++ b/src/org/traccar/sms/SmsGatewayClient.java @@ -1,7 +1,18 @@ /* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. + * 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; @@ -11,13 +22,8 @@ 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.helper.Log; import org.traccar.notification.MessageException; -/** - * - * @author marcos - */ public class SmsGatewayClient implements SmsManager { private static final String URL = "https://smsgateway.me/api/v4/message/send"; @@ -34,7 +40,7 @@ public class SmsGatewayClient implements SmsManager { deviceId = Context.getConfig().getInteger("sms.smsgateway.deviceId"); } - protected JsonArray preparePayload(String destAddress, String message) { + private JsonArray preparePayload(String destAddress, String message) { return Json.createArrayBuilder() .add(Json.createObjectBuilder() .add(KEY_PHONE_NUMBER, destAddress) @@ -43,30 +49,23 @@ public class SmsGatewayClient implements SmsManager { .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 { - Invocation.Builder requestBuilder = Context.getClient().target(URL).request(); - - requestBuilder = requestBuilder.header(KEY_AUTHORIZATION, token); - - Response response = requestBuilder.post(Entity.json(preparePayload(destAddress, message))); + Response response = getRequestBuilder().post(Entity.json(preparePayload(destAddress, message))); if (!response.getStatusInfo().equals(Response.Status.OK)) { String output = response.readEntity(String.class); - Log.error(output); + throw new MessageException(new Exception(output)); } } @Override public void sendMessageAsync(final String destAddress, final String message, final boolean command) { - new Thread(new Runnable() { - public void run() { - try { - sendMessageSync(destAddress, message, command); - } catch (MessageException | InterruptedException error) { - Log.warning(error); - } - } - }).start(); + getRequestBuilder().async().post(Entity.json(preparePayload(destAddress, message))); } } |