Skip to content

Commit

Permalink
[tesla] Use CXF JAX-RS client builder, if available. (openhab#7789)
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Kreuzer <kai@openhab.org>
  • Loading branch information
kaikreuzer authored and andrewfg committed Aug 31, 2020
1 parent 2959210 commit 0a8ee1e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.ws.rs.client.ClientBuilder;

import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
Expand All @@ -27,6 +29,8 @@
import org.openhab.binding.tesla.internal.handler.TeslaAccountHandler;
import org.openhab.binding.tesla.internal.handler.TeslaVehicleHandler;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;

/**
* The {@link TeslaHandlerFactory} is responsible for creating things and thing
Expand All @@ -39,10 +43,26 @@
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.tesla")
public class TeslaHandlerFactory extends BaseThingHandlerFactory {

// TODO: Those constants are Jersey specific - once we move away from Jersey,
// this can be removed and the client builder creation simplified.
public static final String READ_TIMEOUT_JERSEY = "jersey.config.client.readTimeout";
public static final String CONNECT_TIMEOUT_JERSEY = "jersey.config.client.connectTimeout";

public static final String READ_TIMEOUT = "http.receive.timeout";
public static final String CONNECT_TIMEOUT = "http.connection.timeout";

private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3000;
private static final int EVENT_STREAM_READ_TIMEOUT = 200000;

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
.of(THING_TYPE_ACCOUNT, THING_TYPE_MODELS, THING_TYPE_MODEL3, THING_TYPE_MODELX, THING_TYPE_MODELY)
.collect(Collectors.toSet());

@Reference(cardinality = ReferenceCardinality.OPTIONAL)
private ClientBuilder injectedClientBuilder;

private ClientBuilder clientBuilder;

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
Expand All @@ -53,9 +73,29 @@ protected ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();

if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
return new TeslaAccountHandler((Bridge) thing);
return new TeslaAccountHandler((Bridge) thing, getClientBuilder().build());
} else {
return new TeslaVehicleHandler(thing);
return new TeslaVehicleHandler(thing, getClientBuilder());
}
}

private synchronized ClientBuilder getClientBuilder() {
if (clientBuilder == null) {
try {
clientBuilder = ClientBuilder.newBuilder();
clientBuilder.property(CONNECT_TIMEOUT_JERSEY, EVENT_STREAM_CONNECT_TIMEOUT);
clientBuilder.property(READ_TIMEOUT_JERSEY, EVENT_STREAM_READ_TIMEOUT);
} catch (Exception e) {
// we seem to have no Jersey, so let's hope for an injected builder by CXF
if (this.injectedClientBuilder != null) {
clientBuilder = injectedClientBuilder;
clientBuilder.property(CONNECT_TIMEOUT, EVENT_STREAM_CONNECT_TIMEOUT);
clientBuilder.property(READ_TIMEOUT, EVENT_STREAM_READ_TIMEOUT);
} else {
throw new IllegalStateException("No JAX RS Client Builder available.");
}
}
}
return clientBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.client.Entity;
Expand Down Expand Up @@ -86,14 +85,13 @@ public class TeslaAccountHandler extends BaseBridgeHandler {
private final Logger logger = LoggerFactory.getLogger(TeslaAccountHandler.class);

// REST Client API variables
private final Client teslaClient = ClientBuilder.newClient();
private final WebTarget teslaTarget = teslaClient.target(URI_OWNERS);
private final WebTarget tokenTarget = teslaTarget.path(URI_ACCESS_TOKEN);
final WebTarget vehiclesTarget = teslaTarget.path(API_VERSION).path(VEHICLES);
final WebTarget vehicleTarget = vehiclesTarget.path(PATH_VEHICLE_ID);
final WebTarget dataRequestTarget = vehicleTarget.path(PATH_DATA_REQUEST);
final WebTarget commandTarget = vehicleTarget.path(PATH_COMMAND);
final WebTarget wakeUpTarget = vehicleTarget.path(PATH_WAKE_UP);
private final WebTarget teslaTarget;
private final WebTarget tokenTarget;
WebTarget vehiclesTarget; // this cannot be marked final as it is used in the runnable
final WebTarget vehicleTarget;
final WebTarget dataRequestTarget;
final WebTarget commandTarget;
final WebTarget wakeUpTarget;

// Threading and Job related variables
protected ScheduledFuture<?> connectJob;
Expand All @@ -111,8 +109,15 @@ public class TeslaAccountHandler extends BaseBridgeHandler {
private TokenResponse logonToken;
private final Set<VehicleListener> vehicleListeners = new HashSet<>();

public TeslaAccountHandler(Bridge bridge) {
public TeslaAccountHandler(Bridge bridge, Client teslaClient) {
super(bridge);
this.teslaTarget = teslaClient.target(URI_OWNERS);
this.tokenTarget = teslaTarget.path(URI_ACCESS_TOKEN);
this.vehiclesTarget = teslaTarget.path(API_VERSION).path(VEHICLES);
this.vehicleTarget = vehiclesTarget.path(PATH_VEHICLE_ID);
this.dataRequestTarget = vehicleTarget.path(PATH_DATA_REQUEST);
this.commandTarget = vehicleTarget.path(PATH_COMMAND);
this.wakeUpTarget = vehicleTarget.path(PATH_WAKE_UP);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@
*/
public class TeslaVehicleHandler extends BaseThingHandler {

// TODO: Those constants are Jersey specific - once we move away from Jersey,
// this must be changed to https://stackoverflow.com/a/49736022 (assuming we have a JAX-RS 2.1 implementation).
public static final String READ_TIMEOUT = "jersey.config.client.readTimeout";
public static final String CONNECT_TIMEOUT = "jersey.config.client.connectTimeout";

private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3000;
private static final int EVENT_STREAM_READ_TIMEOUT = 200000;
private static final int EVENT_STREAM_PAUSE = 5000;
private static final int EVENT_TIMESTAMP_AGE_LIMIT = 3000;
private static final int EVENT_TIMESTAMP_MAX_DELTA = 10000;
Expand Down Expand Up @@ -136,7 +129,8 @@ public class TeslaVehicleHandler extends BaseThingHandler {
protected TeslaAccountHandler account;

protected QueueChannelThrottler stateThrottler;
protected Client eventClient = ClientBuilder.newClient();
protected ClientBuilder clientBuilder;
protected Client eventClient;
protected TeslaChannelSelectorProxy teslaChannelSelectorProxy = new TeslaChannelSelectorProxy();
protected Thread eventThread;
protected ScheduledFuture<?> fastStateJob;
Expand All @@ -145,8 +139,9 @@ public class TeslaVehicleHandler extends BaseThingHandler {
private final Gson gson = new Gson();
private final JsonParser parser = new JsonParser();

public TeslaVehicleHandler(Thing thing) {
public TeslaVehicleHandler(Thing thing, ClientBuilder clientBuilder) {
super(thing);
this.clientBuilder = clientBuilder;
}

@SuppressWarnings("null")
Expand Down Expand Up @@ -218,7 +213,9 @@ public void dispose() {
lock.unlock();
}

eventClient.close();
if (eventClient != null) {
eventClient.close();
}
}

/**
Expand Down Expand Up @@ -991,8 +988,7 @@ protected boolean establishEventStream() {
if (!isEstablished) {
eventBufferedReader = null;

eventClient = ClientBuilder.newClient().property(CONNECT_TIMEOUT, EVENT_STREAM_CONNECT_TIMEOUT)
.property(READ_TIMEOUT, EVENT_STREAM_READ_TIMEOUT)
eventClient = clientBuilder.build()
.register(new Authenticator((String) getConfig().get(CONFIG_USERNAME), vehicle.tokens[0]));
eventTarget = eventClient.target(URI_EVENT).path(vehicle.vehicle_id + "/").queryParam("values",
StringUtils.join(EventKeys.values(), ',', 1, EventKeys.values().length));
Expand Down

0 comments on commit 0a8ee1e

Please sign in to comment.