From 6d6a8949a598ff9aac9370655e2e0bd759391368 Mon Sep 17 00:00:00 2001 From: Marcos Dallagnelo Date: Thu, 16 Aug 2018 14:23:35 -0300 Subject: Have improved code, without create new thread and throw a MessageException --- src/org/traccar/sms/SmsGatewayClient.java | 47 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'src/org/traccar/sms') 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))); } } -- cgit v1.2.3