Skip to content

Commit

Permalink
[http] Improve binding (openhab#16282)
Browse files Browse the repository at this point in the history
This adds many improvements, new features and contains bugfixes.

Signed-off-by: Jan N. Klug <github@klug.nrw>
Signed-off-by: Jørgen Austvik <jaustvik@acm.org>
  • Loading branch information
J-N-K authored and austvik committed Mar 27, 2024
1 parent e8d308e commit 8e11e4b
Show file tree
Hide file tree
Showing 40 changed files with 1,106 additions and 2,096 deletions.
159 changes: 80 additions & 79 deletions bundles/org.openhab.binding.http/README.md

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions bundles/org.openhab.binding.http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,47 @@

<name>openHAB Add-ons :: Bundles :: HTTP Binding</name>

<properties>
<jetty.version>9.4.52.v20230823</jetty.version>
</properties>

<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-proxy</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<!-- testing, we need to exclude and declare jetty bundles because the declared transitive dependency 9.2.28 is too old -->
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.27.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.type.ChannelTypeUID;

/**
* The {@link HttpBindingConstants} class defines common constants, which are
Expand All @@ -23,8 +24,12 @@
*/
@NonNullByDefault
public class HttpBindingConstants {

public static final String BINDING_ID = "http";
private static final String BINDING_ID = "http";

public static final ThingTypeUID THING_TYPE_URL = new ThingTypeUID(BINDING_ID, "url");

public static final ChannelTypeUID REQUEST_DATE_TIME_CHANNELTYPE_UID = new ChannelTypeUID(BINDING_ID,
"request-date-time");
public static final String CHANNEL_LAST_SUCCESS = "last-success";
public static final String CHANNEL_LAST_FAILURE = "last-failure";
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@
*/
package org.openhab.binding.http.internal;

import static org.openhab.binding.http.internal.HttpBindingConstants.*;
import static org.openhab.binding.http.internal.HttpBindingConstants.THING_TYPE_URL;

import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.openhab.binding.http.internal.transform.CascadedValueTransformationImpl;
import org.openhab.binding.http.internal.transform.NoOpValueTransformation;
import org.openhab.binding.http.internal.transform.ValueTransformation;
import org.openhab.binding.http.internal.transform.ValueTransformationProvider;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.thing.Thing;
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.openhab.core.transform.TransformationHelper;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
Expand All @@ -46,31 +42,35 @@
*/
@NonNullByDefault
@Component(configurationPid = "binding.http", service = ThingHandlerFactory.class)
public class HttpHandlerFactory extends BaseThingHandlerFactory
implements ValueTransformationProvider, HttpClientProvider {
public class HttpHandlerFactory extends BaseThingHandlerFactory implements HttpClientProvider {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_URL);
private final Logger logger = LoggerFactory.getLogger(HttpHandlerFactory.class);

private final HttpClient secureClient;
private final HttpClient insecureClient;

private final HttpDynamicStateDescriptionProvider httpDynamicStateDescriptionProvider;
private final TimeZoneProvider timeZoneProvider;

@Activate
public HttpHandlerFactory(@Reference HttpClientFactory httpClientFactory,
@Reference HttpDynamicStateDescriptionProvider httpDynamicStateDescriptionProvider) {
this.secureClient = httpClientFactory.createHttpClient(BINDING_ID + "-secure", new SslContextFactory.Client());
this.insecureClient = httpClientFactory.createHttpClient(BINDING_ID + "-insecure",
new SslContextFactory.Client(true));
@Reference HttpDynamicStateDescriptionProvider httpDynamicStateDescriptionProvider,
@Reference TimeZoneProvider timeZoneProvider) {
this.secureClient = new HttpClient(new SslContextFactory.Client());
this.insecureClient = new HttpClient(new SslContextFactory.Client(true));
// clear user agent, this needs to be set later in the thing configuration as additional header
this.secureClient.setUserAgentField(null);
this.insecureClient.setUserAgentField(null);
try {
this.secureClient.start();
this.insecureClient.start();
} catch (Exception e) {
// catching exception is necessary due to the signature of HttpClient.start()
logger.warn("Failed to start insecure http client: {}", e.getMessage());
throw new IllegalStateException("Could not create insecure HttpClient");
logger.warn("Failed to start http client: {}", e.getMessage());
throw new IllegalStateException("Could not create HttpClient", e);
}
this.httpDynamicStateDescriptionProvider = httpDynamicStateDescriptionProvider;
this.timeZoneProvider = timeZoneProvider;
}

@Deactivate
Expand All @@ -94,21 +94,12 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();

if (THING_TYPE_URL.equals(thingTypeUID)) {
return new HttpThingHandler(thing, this, this, httpDynamicStateDescriptionProvider);
return new HttpThingHandler(thing, this, httpDynamicStateDescriptionProvider, timeZoneProvider);
}

return null;
}

@Override
public ValueTransformation getValueTransformation(@Nullable String pattern) {
if (pattern == null || pattern.isEmpty()) {
return NoOpValueTransformation.getInstance();
}
return new CascadedValueTransformationImpl(pattern,
name -> TransformationHelper.getTransformationService(bundleContext, name));
}

@Override
public HttpClient getSecureClient() {
return secureClient;
Expand Down
Loading

0 comments on commit 8e11e4b

Please sign in to comment.