review null annotation and null pointer warning

Signed-off-by: Laurent ARNAL <laurent@clae.net>
This commit is contained in:
Laurent ARNAL 2024-02-02 09:49:52 +01:00
parent a84023ee3c
commit 34459b3a48
5 changed files with 64 additions and 75 deletions

View File

@ -51,8 +51,6 @@ public class LinkyAuthServlet extends HttpServlet {
private static final String HTML_USER_AUTHORIZED = "<p class='block authorized'>Addon authorized for %s.</p>";
private static final String HTML_ERROR = "<p class='block error'>Call to Enedis failed with error: %s</p>";
private static final String HTML_META_REFRESH_CONTENT = "<meta http-equiv='refresh' content='10; url=%s'>";
// Keys present in the index.html
private static final String KEY_AUTHORIZE_URI = "authorize.uri";
private static final String KEY_RETRIEVE_TOKEN_URI = "retrieveToken.uri";
@ -72,16 +70,17 @@ public class LinkyAuthServlet extends HttpServlet {
}
@Override
protected void doGet(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp)
throws ServletException, IOException {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
logger.debug("Linky auth callback servlet received GET request {}.", req.getRequestURI());
final Map<String, String> replaceMap = new HashMap<>();
final String servletBaseURL = req.getRequestURL().toString();
String servletBaseURL = "";
StringBuffer requestURL = req.getRequestURL();
if (requestURL != null) {
servletBaseURL = requestURL.toString();
}
String servletBaseURLSecure = servletBaseURL;
// .replace("http://", "https://");
// .replace("8080", "8443");
handleLinkyRedirect(replaceMap, servletBaseURLSecure, req.getQueryString());
@ -91,15 +90,19 @@ public class LinkyAuthServlet extends HttpServlet {
StringBuffer optionBuffer = new StringBuffer();
String[] prmIds = accountHandler.getAllPrmId();
for (String prmId : prmIds) {
optionBuffer.append("<option value=\"" + prmId + "\">" + prmId + "</option>");
if (accountHandler != null) {
String[] prmIds = accountHandler.getAllPrmId();
for (String prmId : prmIds) {
optionBuffer.append("<option value=\"" + prmId + "\">" + prmId + "</option>");
}
}
replaceMap.put(KEY_PRMID_OPTION, optionBuffer.toString());
replaceMap.put(KEY_REDIRECT_URI, servletBaseURLSecure);
replaceMap.put(KEY_RETRIEVE_TOKEN_URI, servletBaseURLSecure + "?state=OK");
replaceMap.put(KEY_AUTHORIZE_URI, accountHandler.formatAuthorizationUrl(servletBaseURLSecure));
if (accountHandler != null) {
replaceMap.put(KEY_AUTHORIZE_URI, accountHandler.formatAuthorizationUrl(servletBaseURLSecure));
}
resp.getWriter().append(replaceKeysFromMap(indexTemplate, replaceMap));
resp.getWriter().close();
}

View File

@ -100,7 +100,6 @@ public class LinkyHandlerFactory extends BaseThingHandlerFactory implements Link
private final LocaleProvider localeProvider;
private final HttpClient httpClient;
private final OAuthFactory oAuthFactory;
private final LinkyAuthService authService;
private final boolean oAuthSupport = false;
private @Nullable OAuthClientService oAuthService;
@ -126,8 +125,6 @@ public class LinkyHandlerFactory extends BaseThingHandlerFactory implements Link
this.httpClient = httpClientFactory.createHttpClient(LinkyBindingConstants.BINDING_ID, sslContextFactory);
httpClient.setFollowRedirects(false);
httpClient.setRequestBufferSize(REQUEST_BUFFER_SIZE);
httpClient.setResponseBufferSize(RESPONSE_BUFFER_SIZE);
this.oAuthFactory = oAuthFactory;
this.authService = authService;
this.oAuthService = oAuthFactory.createOAuthClientService("Linky", LinkyBindingConstants.LINKY_API_TOKEN_URL,
@ -165,7 +162,7 @@ public class LinkyHandlerFactory extends BaseThingHandlerFactory implements Link
protected @Nullable ThingHandler createHandler(Thing thing) {
if (supportsThingType(thing.getThingTypeUID())) {
LinkyHandler handler = new LinkyHandler(thing, localeProvider, gson, httpClient, oAuthFactory);
LinkyHandler handler = new LinkyHandler(thing, localeProvider, gson, httpClient);
return handler;
}
@ -235,7 +232,9 @@ public class LinkyHandlerFactory extends BaseThingHandlerFactory implements Link
config.put("token", token);
LinkyHandler handler = (LinkyHandler) thing.getHandler();
handler.saveConfiguration(config);
if (handler != null) {
handler.saveConfiguration(config);
}
}
}

View File

@ -24,13 +24,9 @@ import java.util.concurrent.TimeoutException;
import javax.ws.rs.core.MediaType;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.util.FormContentProvider;
import org.eclipse.jetty.client.util.StringContentProvider;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.util.Fields;
@ -80,11 +76,11 @@ public class EnedisHttpApi {
private final Logger logger = LoggerFactory.getLogger(EnedisHttpApi.class);
private final Gson gson;
private final HttpClient httpClient;
private @Nullable LinkyConfiguration config;
private LinkyConfiguration config;
private boolean connected = false;
public EnedisHttpApi(@Nullable LinkyConfiguration config, Gson gson, HttpClient httpClient) {
public EnedisHttpApi(LinkyConfiguration config, Gson gson, HttpClient httpClient) {
this.gson = gson;
this.httpClient = httpClient;
this.config = config;
@ -93,10 +89,6 @@ public class EnedisHttpApi {
public void initialize() throws LinkyException {
}
private String getLocation(ContentResponse response) {
return response.getHeaders().get(HttpHeader.LOCATION);
}
private void disconnect() throws LinkyException {
if (connected) {
logger.debug("Logout process");
@ -127,8 +119,8 @@ public class EnedisHttpApi {
ContentResponse result = request.send();
if (result.getStatus() == 307) {
String loc = result.getHeaders().get("Location");
url = BASE_URL + loc.substring(1);
request = httpClient.newRequest(url);
String newUrl = BASE_URL + loc.substring(1);
request = httpClient.newRequest(newUrl);
request = request.method(HttpMethod.GET);
result = request.send();

View File

@ -24,5 +24,7 @@ import org.eclipse.jetty.jaas.spi.UserInfo;
*/
public class TempoResponse extends HashMap<String, String> {
@java.io.Serial
private static final long serialVersionUID = 362498820763181264L;
}

View File

@ -18,7 +18,6 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -35,7 +34,6 @@ import org.openhab.binding.linky.internal.api.ExpiringDayCache;
import org.openhab.binding.linky.internal.dto.IntervalReading;
import org.openhab.binding.linky.internal.dto.MeterReading;
import org.openhab.binding.linky.internal.dto.PrmInfo;
import org.openhab.core.auth.client.oauth2.OAuthFactory;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.library.types.QuantityType;
@ -70,17 +68,12 @@ public class LinkyHandler extends BaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(LinkyHandler.class);
private final HttpClient httpClient;
private final Gson gson;
private final WeekFields weekFields;
private final ExpiringDayCache<MeterReading> dailyConsumption;
private @Nullable LinkyConfiguration config;
private @Nullable ScheduledFuture<?> refreshJob;
private @Nullable EnedisHttpApi enedisApi;
private final OAuthFactory oAuthFactory;
private @NonNullByDefault({}) String prmId;
private @NonNullByDefault({}) String userId;
@ -90,13 +83,10 @@ public class LinkyHandler extends BaseThingHandler {
ALL
}
public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpClient httpClient,
OAuthFactory oAuthFactory) {
public LinkyHandler(Thing thing, LocaleProvider localeProvider, Gson gson, HttpClient httpClient) {
super(thing);
this.gson = gson;
this.httpClient = httpClient;
this.weekFields = WeekFields.of(localeProvider.getLocale());
this.oAuthFactory = oAuthFactory;
this.dailyConsumption = new ExpiringDayCache<>("dailyConsumption", REFRESH_FIRST_HOUR_OF_DAY, () -> {
LocalDate today = LocalDate.now();
@ -158,16 +148,18 @@ public class LinkyHandler extends BaseThingHandler {
logger.debug("Initializing Linky handler.");
updateStatus(ThingStatus.UNKNOWN);
config = getConfigAs(LinkyConfiguration.class);
LinkyConfiguration config = getConfigAs(LinkyConfiguration.class);
if (config.seemsValid()) {
enedisApi = new EnedisHttpApi(config, gson, httpClient);
EnedisHttpApi api = new EnedisHttpApi(config, gson, httpClient);
this.enedisApi = api;
scheduler.submit(() -> {
try {
enedisApi.initialize();
api.initialize();
updateStatus(ThingStatus.ONLINE);
PrmInfo prmInfo = enedisApi.getPrmInfo();
PrmInfo prmInfo = api.getPrmInfo();
updateProperties(Map.of(USER_ID, prmInfo.customerId, PUISSANCE,
prmInfo.contractInfo.subscribedPower, PRM_ID, prmInfo.prmId));
@ -547,52 +539,53 @@ public class LinkyHandler extends BaseThingHandler {
return null;
}
meterReading.WeekValue = new IntervalReading[208];
meterReading.MonthValue = new IntervalReading[48];
meterReading.YearValue = new IntervalReading[4];
if (meterReading != null) {
meterReading.weekValue = new IntervalReading[208];
meterReading.monthValue = new IntervalReading[48];
meterReading.yearValue = new IntervalReading[4];
for (int idx = 0; idx < 208; idx++) {
meterReading.WeekValue[idx] = new IntervalReading();
}
for (int idx = 0; idx < 48; idx++) {
meterReading.MonthValue[idx] = new IntervalReading();
}
for (int idx = 0; idx < 4; idx++) {
meterReading.YearValue[idx] = new IntervalReading();
}
for (int idx = 0; idx < 208; idx++) {
meterReading.weekValue[idx] = new IntervalReading();
}
for (int idx = 0; idx < 48; idx++) {
meterReading.monthValue[idx] = new IntervalReading();
}
for (int idx = 0; idx < 4; idx++) {
meterReading.yearValue[idx] = new IntervalReading();
}
int size = meterReading.intervalReading.length;
int baseYear = meterReading.intervalReading[0].date.getYear();
int size = meterReading.dayValue.length;
int baseYear = meterReading.dayValue[0].date.getYear();
for (int idx = 0; idx < size; idx++) {
IntervalReading ir = meterReading.intervalReading[idx];
LocalDate dt = ir.date;
double value = ir.value;
for (int idx = 0; idx < size; idx++) {
IntervalReading ir = meterReading.dayValue[idx];
LocalDateTime dt = ir.date;
double value = ir.value;
int idxYear = dt.getYear() - baseYear;
int idxYear = dt.getYear() - baseYear;
int dayOfYear = dt.getDayOfYear();
int week = (dayOfYear / 7) + 1;
int month = dt.getMonthValue();
int dayOfYear = dt.getDayOfYear();
int week = ((dayOfYear - 1) / 7) + 1;
int month = dt.getMonthValue();
int idxMonth = (idxYear * 12) + month;
int idxWeek = (idxYear * 52) + week;
int idxMonth = (idxYear * 12) + month;
int idxWeek = (idxYear * 52) + week;
meterReading.WeekValue[idxWeek].value += value;
meterReading.MonthValue[idxMonth].value += value;
meterReading.YearValue[idxYear].value += value;
meterReading.weekValue[idxWeek].value += value;
meterReading.monthValue[idxMonth].value += value;
meterReading.yearValue[idxYear].value += value;
}
}
return meterReading;
}
private void checkData(@Nullable MeterReading meterReading) throws LinkyException {
if (meterReading.intervalReading.length == 0) {
throw new LinkyException("Invalid meterReading data: no day period");
if (meterReading != null) {
if (meterReading.dayValue.length == 0) {
throw new LinkyException("Invalid meterReading data: no day period");
}
}
// if (meterReading.intervalReading.length != 1095) {
// throw new LinkyException("Imcomplete meterReading data < 1095 days");
// }
}
/*