Skip to content

Commit

Permalink
Solves issue #13196 (#13281)
Browse files Browse the repository at this point in the history
Signed-off-by: clinique <gael@lhopital.org>
  • Loading branch information
clinique authored Aug 18, 2022
1 parent 47b004f commit 4fb5289
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@

import static org.openhab.binding.linky.internal.LinkyBindingConstants.THING_TYPE_LINKY;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.linky.internal.handler.LinkyHandler;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.io.net.http.TrustAllTrustManager;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
Expand Down Expand Up @@ -70,8 +76,17 @@ protected void activate(ComponentContext componentContext) {
super.activate(componentContext);
httpClient.setFollowRedirects(false);
httpClient.setRequestBufferSize(REQUEST_BUFFER_SIZE);

try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[] { TrustAllTrustManager.getInstance() }, null);
httpClient.getSslContextFactory().setSslContext(sslContext);
httpClient.start();
} catch (NoSuchAlgorithmException e) {
logger.warn("An exception occurred while requesting the SSL encryption algorithm : '{}'", e.getMessage(),
e);
} catch (KeyManagementException e) {
logger.warn("An exception occurred while initialising the SSL context : '{}'", e.getMessage(), e);
} catch (Exception e) {
logger.warn("Unable to start Jetty HttpClient {}", e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package org.openhab.binding.linky.internal.api;

import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URI;
import java.time.LocalDate;
Expand Down Expand Up @@ -71,7 +70,6 @@ public class EnedisHttpApi {
private final Logger logger = LoggerFactory.getLogger(EnedisHttpApi.class);
private final Gson gson;
private final HttpClient httpClient;
private final CookieStore cookieStore;
private final LinkyConfiguration config;

private boolean connected = false;
Expand All @@ -80,7 +78,6 @@ public EnedisHttpApi(LinkyConfiguration config, Gson gson, HttpClient httpClient
this.gson = gson;
this.httpClient = httpClient;
this.config = config;
this.cookieStore = httpClient.getCookieStore();
}

public void initialize() throws LinkyException {
Expand Down Expand Up @@ -158,7 +155,7 @@ public void initialize() throws LinkyException {
result = httpClient.POST(el.attr("action")).content(getFormContent("SAMLResponse", samlInput.attr("value")))
.send();
if (result.getStatus() != 302) {
throw new LinkyException("Connection failed step 5");
throw new LinkyException("Connection failed step 6");
}
connected = true;
} catch (InterruptedException | TimeoutException | ExecutionException | JsonSyntaxException e) {
Expand All @@ -178,7 +175,7 @@ private void disconnect() throws LinkyException {
String location = getLocation(httpClient.GET(URL_APPS_LINCS + "/logout"));
location = getLocation(httpClient.GET(location));
getLocation(httpClient.GET(location));
cookieStore.removeAll();
httpClient.getCookieStore().removeAll();
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new LinkyException(e, "Error while disconnecting from Enedis webservice");
}
Expand All @@ -197,7 +194,7 @@ private void addCookie(String key, String value) {
HttpCookie cookie = new HttpCookie(key, value);
cookie.setDomain(ENEDIS_DOMAIN);
cookie.setPath("/");
cookieStore.add(COOKIE_URI, cookie);
httpClient.getCookieStore().add(COOKIE_URI, cookie);
}

private FormContentProvider getFormContent(String fieldName, String fieldValue) {
Expand Down

0 comments on commit 4fb5289

Please sign in to comment.