Fix StringIndexOutOfBoundsException (#15319)

Fixes #15318

Reverts #14402

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2023-07-28 10:00:48 +02:00 committed by Kai Kreuzer
parent 7f6502d769
commit e60446d305

View File

@ -43,6 +43,7 @@ import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.lang3.StringUtils;
import org.openhab.binding.digitalstrom.internal.lib.config.Config;
import org.openhab.binding.digitalstrom.internal.lib.manager.ConnectionManager;
import org.openhab.binding.digitalstrom.internal.lib.serverconnection.HttpTransport;
@ -333,8 +334,7 @@ public class HttpTransportImpl implements HttpTransport {
}
private boolean checkNeededSessionToken(String request) {
String requestFirstPart = request.substring(0, request.indexOf("?"));
String functionName = requestFirstPart.substring(requestFirstPart.lastIndexOf("/") + 1);
String functionName = StringUtils.substringAfterLast(StringUtils.substringBefore(request, "?"), "/");
return !DsAPIImpl.METHODS_MUST_NOT_BE_LOGGED_IN.contains(functionName);
}
@ -347,10 +347,8 @@ public class HttpTransportImpl implements HttpTransport {
correctedRequest = correctedRequest + "?" + ParameterKeys.TOKEN + "=" + sessionToken;
}
} else {
String strippedRequest = correctedRequest
.substring(correctedRequest.indexOf(ParameterKeys.TOKEN + "=") + ParameterKeys.TOKEN.length() + 1);
strippedRequest = strippedRequest.substring(0, strippedRequest.lastIndexOf("&"));
correctedRequest = correctedRequest.replaceFirst(strippedRequest, sessionToken);
correctedRequest = StringUtils.replaceOnce(correctedRequest, StringUtils.substringBefore(
StringUtils.substringAfter(correctedRequest, ParameterKeys.TOKEN + "="), "&"), sessionToken);
}
return correctedRequest;
}