Remove Jersey ClientBuilder compatibility (#8561)
Fixes #7531 Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
@@ -15,11 +15,12 @@ package org.openhab.binding.tesla.internal;
|
||||
import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.tesla.internal.handler.TeslaAccountHandler;
|
||||
import org.openhab.binding.tesla.internal.handler.TeslaVehicleHandler;
|
||||
import org.openhab.core.thing.Bridge;
|
||||
@@ -28,9 +29,9 @@ import org.openhab.core.thing.ThingTypeUID;
|
||||
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
|
||||
import org.openhab.core.thing.binding.ThingHandler;
|
||||
import org.openhab.core.thing.binding.ThingHandlerFactory;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
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
|
||||
@@ -41,27 +42,23 @@ import org.osgi.service.component.annotations.ReferenceCardinality;
|
||||
* @author Kai Kreuzer - Introduced account handler
|
||||
*/
|
||||
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.tesla")
|
||||
@NonNullByDefault
|
||||
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";
|
||||
private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3;
|
||||
private static final int EVENT_STREAM_READ_TIMEOUT = 200;
|
||||
|
||||
public static final String READ_TIMEOUT = "http.receive.timeout";
|
||||
public static final String CONNECT_TIMEOUT = "http.connection.timeout";
|
||||
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_ACCOUNT, THING_TYPE_MODELS,
|
||||
THING_TYPE_MODEL3, THING_TYPE_MODELX, THING_TYPE_MODELY);
|
||||
|
||||
private static final int EVENT_STREAM_CONNECT_TIMEOUT = 3000;
|
||||
private static final int EVENT_STREAM_READ_TIMEOUT = 200000;
|
||||
private final ClientBuilder clientBuilder;
|
||||
|
||||
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;
|
||||
@Activate
|
||||
public TeslaHandlerFactory(@Reference ClientBuilder clientBuilder) {
|
||||
this.clientBuilder = clientBuilder //
|
||||
.connectTimeout(EVENT_STREAM_CONNECT_TIMEOUT, TimeUnit.SECONDS)
|
||||
.readTimeout(EVENT_STREAM_READ_TIMEOUT, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
|
||||
@@ -69,33 +66,13 @@ public class TeslaHandlerFactory extends BaseThingHandlerFactory {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ThingHandler createHandler(Thing thing) {
|
||||
protected @Nullable ThingHandler createHandler(Thing thing) {
|
||||
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
|
||||
|
||||
if (thingTypeUID.equals(THING_TYPE_ACCOUNT)) {
|
||||
return new TeslaAccountHandler((Bridge) thing, getClientBuilder().build());
|
||||
return new TeslaAccountHandler((Bridge) thing, clientBuilder.build());
|
||||
} else {
|
||||
return new TeslaVehicleHandler(thing, getClientBuilder());
|
||||
return new TeslaVehicleHandler(thing, clientBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user