Skip to content

Commit

Permalink
[tesla] Use CXF JAX-RS client builder, if available (openhab#7804)
Browse files Browse the repository at this point in the history
Signed-off-by: Kai Kreuzer <[email protected]>
  • Loading branch information
kaikreuzer authored and andrewfg committed Aug 31, 2020
1 parent 2f77946 commit 7ed58ea
Showing 1 changed file with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder;
import org.eclipse.smarthome.core.thing.ThingUID;
Expand All @@ -38,8 +40,10 @@
import org.openhab.binding.tesla.internal.protocol.TokenRequest;
import org.openhab.binding.tesla.internal.protocol.TokenRequestPassword;
import org.openhab.binding.tesla.internal.protocol.TokenResponse;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -51,30 +55,25 @@
* @author Nicolai Grødum - Initial contribution
* @author Kai Kreuzer - refactored to use Tesla account thing
*/
@NonNullByDefault
@Component(service = ConsoleCommandExtension.class, immediate = true)
public class TeslaCommandExtension extends AbstractConsoleCommandExtension {

private static final String CMD_LOGIN = "login";

private final Logger logger = LoggerFactory.getLogger(TeslaCommandExtension.class);

private final Client teslaClient = ClientBuilder.newClient();
private final WebTarget teslaTarget = teslaClient.target(URI_OWNERS);
private final WebTarget tokenTarget = teslaTarget.path(URI_ACCESS_TOKEN);
@Reference(cardinality = ReferenceCardinality.OPTIONAL)
private @Nullable ClientBuilder injectedClientBuilder;

private TeslaAccountDiscoveryService teslaAccountDiscoveryService;
private @Nullable WebTarget tokenTarget;

public TeslaCommandExtension() {
super("tesla", "Interact with the Tesla integration.");
}

@Reference
protected void setTeslaAccountDiscoveryService(TeslaAccountDiscoveryService ds) {
this.teslaAccountDiscoveryService = ds;
}
private final TeslaAccountDiscoveryService teslaAccountDiscoveryService;

protected void unsetTeslaAccountDiscoveryService(TeslaAccountDiscoveryService ds) {
this.teslaAccountDiscoveryService = null;
@Activate
public TeslaCommandExtension(@Reference TeslaAccountDiscoveryService teslaAccountDiscoveryService) {
super("tesla", "Interact with the Tesla integration.");
this.teslaAccountDiscoveryService = teslaAccountDiscoveryService;
}

@Override
Expand Down Expand Up @@ -127,7 +126,8 @@ private void login(Console console, String username, String password) {
TokenRequest token = new TokenRequestPassword(username, password);
String payLoad = gson.toJson(token);

Response response = tokenTarget.request().post(Entity.entity(payLoad, MediaType.APPLICATION_JSON_TYPE));
Response response = getTokenTarget().request()
.post(Entity.entity(payLoad, MediaType.APPLICATION_JSON_TYPE));

if (response != null) {
if (response.getStatus() == 200 && response.hasEntity()) {
Expand All @@ -152,4 +152,28 @@ private void login(Console console, String username, String password) {
logger.error("Could not get refresh token.", e);
}
}

private synchronized WebTarget getTokenTarget() {
WebTarget target = this.tokenTarget;
if (target != null) {
return target;
} else {
Client client;
try {
client = ClientBuilder.newBuilder().build();
} catch (Exception e) {
// we seem to have no Jersey, so let's hope for an injected builder by CXF
if (this.injectedClientBuilder != null) {
client = injectedClientBuilder.build();
} else {
throw new IllegalStateException("No JAX RS Client Builder available.");
}
}
WebTarget teslaTarget = client.target(URI_OWNERS);
target = teslaTarget.path(URI_ACCESS_TOKEN);
this.tokenTarget = target;
return target;
}
}

}

0 comments on commit 7ed58ea

Please sign in to comment.