2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.easee.internal.command.account;
15 import static org.openhab.binding.easee.internal.EaseeBindingConstants.*;
17 import java.nio.charset.StandardCharsets;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.api.Request;
22 import org.eclipse.jetty.client.api.Result;
23 import org.eclipse.jetty.client.util.StringContentProvider;
24 import org.eclipse.jetty.http.HttpMethod;
25 import org.openhab.binding.easee.internal.command.AbstractCommand;
26 import org.openhab.binding.easee.internal.command.JsonResultProcessor;
27 import org.openhab.binding.easee.internal.handler.EaseeBridgeHandler;
29 import com.google.gson.JsonObject;
32 * implements the login to the webinterface
34 * @author Alexander Friese - initial contribution
37 public class Login extends AbstractCommand {
40 final String userName;
41 final String password;
43 public LoginData(String userName, String password) {
44 this.userName = userName;
45 this.password = password;
49 private final LoginData loginData;
51 public Login(EaseeBridgeHandler handler, JsonResultProcessor resultProcessor) {
52 // flags do not matter as "onComplete" is overwritten in this class.
53 super(handler, RetryOnFailure.NO, ProcessFailureResponse.NO, resultProcessor);
54 loginData = new LoginData(handler.getBridgeConfiguration().getUsername(),
55 handler.getBridgeConfiguration().getPassword());
59 protected Request prepareRequest(Request requestToPrepare) {
60 StringContentProvider cp = new StringContentProvider(gson.toJson(loginData));
61 requestToPrepare.content(cp);
62 requestToPrepare.method(HttpMethod.POST);
64 return requestToPrepare;
68 protected String getURL() {
73 public void onComplete(@Nullable Result result) {
74 String json = getContentAsString(StandardCharsets.UTF_8);
75 JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
77 if (jsonObject != null) {
78 processResult(jsonObject);
83 protected String getChannelGroup() {
84 return CHANNEL_GROUP_NONE;