Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Homematic] Removed Apache Commons (openhab#10035)
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Herbst <[email protected]>
MHerbst authored and thinkingstone committed Nov 7, 2021
1 parent cc16bc9 commit 7cf813f
Showing 36 changed files with 319 additions and 226 deletions.
Original file line number Diff line number Diff line change
@@ -12,8 +12,6 @@
*/
package org.openhab.binding.homematic.internal.common;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.openhab.binding.homematic.internal.model.HmChannel;
import org.openhab.binding.homematic.internal.model.HmGatewayInfo;
import org.openhab.binding.homematic.internal.model.HmInterface;
@@ -405,14 +403,12 @@ public boolean isNoCCUType() {

@Override
public String toString() {
ToStringBuilder tsb = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
tsb.append("gatewayAddress", gatewayAddress).append("callbackHost", callbackHost)
.append("bindAddress", bindAddress).append("xmlCallbackPort", xmlCallbackPort)
.append("binCallbackPort", binCallbackPort).append("gatewayType", gatewayType)
.append("rfPort", getRfPort()).append("wiredPort", getWiredPort()).append("hmIpPort", getHmIpPort())
.append("cuxdPort", getCuxdPort()).append("groupPort", getGroupPort()).append("timeout", timeout)
.append("discoveryTimeToLive", discoveryTimeToLive).append("installModeDuration", installModeDuration)
.append("socketMaxAlive", socketMaxAlive);
return tsb.toString();
return String.format(
"%s[gatewayAddress=%s,callbackHost=%s,bindAddress=%s,xmlCallbackPort=%d,binCallbackPort=%d,"
+ "gatewayType=%s,rfPort=%d,wiredPort=%d,hmIpPort=%d,cuxdPort=%d,groupPort=%d,timeout=%d,"
+ "discoveryTimeToLive=%d,installModeDuration=%d,socketMaxAlive=%d]",
getClass().getSimpleName(), gatewayAddress, callbackHost, bindAddress, xmlCallbackPort, binCallbackPort,
gatewayType, getRfPort(), getWiredPort(), getHmIpPort(), getCuxdPort(), getGroupPort(), timeout,
discoveryTimeToLive, installModeDuration, socketMaxAlive);
}
}
Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.homematic.internal.common.HomematicConfig;
import org.openhab.binding.homematic.internal.communicator.client.BinRpcClient;
@@ -800,7 +799,7 @@ public HomematicGatewayAdapter getGatewayAdapter() {
* Creates a virtual device for handling variables, scripts and other special gateway functions.
*/
private HmDevice createGatewayDevice() {
String type = String.format("%s-%s", HmDevice.TYPE_GATEWAY_EXTRAS, StringUtils.upperCase(id));
String type = String.format("%s-%s", HmDevice.TYPE_GATEWAY_EXTRAS, id.toUpperCase());
HmDevice device = new HmDevice(HmDevice.ADDRESS_GATEWAY_EXTRAS, getDefaultInterface(), type,
config.getGatewayInfo().getId(), null, null);
device.setName(HmDevice.TYPE_GATEWAY_EXTRAS);
Original file line number Diff line number Diff line change
@@ -18,10 +18,9 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.util.StringContentProvider;
@@ -123,7 +122,7 @@ protected void setChannelDatapointValues(HmChannel channel, HmParamsetType param
HmDevice device = channel.getDevice();
String channelName = String.format("%s.%s:%s.", device.getHmInterface().getName(), device.getAddress(),
channel.getNumber());
String datapointNames = StringUtils.join(dpNames.toArray(), "\\t");
String datapointNames = String.join("\\t", dpNames);
TclScriptDataList resultList = sendScriptByName("getAllChannelValues", TclScriptDataList.class,
new String[] { "channel_name", "datapoint_names" },
new String[] { channelName, datapointNames });
@@ -151,7 +150,7 @@ protected void addChannelDatapoints(HmChannel channel, HmParamsetType paramsetTy

@Override
protected void setVariable(HmDatapoint dp, Object value) throws IOException {
String strValue = StringUtils.replace(ObjectUtils.toString(value), "\"", "\\\"");
String strValue = Objects.toString(value, "").replace("\"", "\\\"");
if (dp.isStringType()) {
strValue = "\"" + strValue + "\"";
}
@@ -184,8 +183,10 @@ private <T> T sendScriptByName(String scriptName, Class<T> clazz) throws IOExcep
private <T> T sendScriptByName(String scriptName, Class<T> clazz, String[] variableNames, String[] values)
throws IOException {
String script = tclregaScripts.get(scriptName);
for (int i = 0; i < variableNames.length; i++) {
script = StringUtils.replace(script, "{" + variableNames[i] + "}", values[i]);
if (script != null) {
for (int i = 0; i < variableNames.length; i++) {
script = script.replace("{" + variableNames[i] + "}", values[i]);
}
}
return sendScript(script, clazz);
}
@@ -196,8 +197,8 @@ private <T> T sendScriptByName(String scriptName, Class<T> clazz, String[] varia
@SuppressWarnings("unchecked")
private synchronized <T> T sendScript(String script, Class<T> clazz) throws IOException {
try {
script = StringUtils.trim(script);
if (StringUtils.isEmpty(script)) {
script = script == null ? null : script.trim();
if (script == null || script.isEmpty()) {
throw new RuntimeException("Homematic TclRegaScript is empty!");
}
if (logger.isTraceEnabled()) {
@@ -210,7 +211,10 @@ private synchronized <T> T sendScript(String script, Class<T> clazz) throws IOEx
.header(HttpHeader.CONTENT_TYPE, "text/plain;charset=" + config.getEncoding()).send();

String result = new String(response.getContent(), config.getEncoding());
result = StringUtils.substringBeforeLast(result, "<xml><exec>");
int lastPos = result.lastIndexOf("<xml><exec>");
if (lastPos != -1) {
result = result.substring(0, lastPos);
}
if (logger.isTraceEnabled()) {
logger.trace("Result TclRegaScript: {}", result);
}
@@ -231,7 +235,8 @@ private Map<String, String> loadTclRegaScripts() throws IOException {
Map<String, String> result = new HashMap<>();
if (scriptList.getScripts() != null) {
for (TclScript script : scriptList.getScripts()) {
result.put(script.name, StringUtils.trimToNull(script.data));
String value = script.data.trim();
result.put(script.name, value.isEmpty() ? null : value);
}
}
return result;
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.openhab.binding.homematic.internal.HomematicBindingConstants;
import org.openhab.binding.homematic.internal.common.HomematicConfig;
import org.openhab.binding.homematic.internal.communicator.message.RpcRequest;
@@ -34,6 +33,7 @@
import org.openhab.binding.homematic.internal.communicator.parser.ListBidcosInterfacesParser;
import org.openhab.binding.homematic.internal.communicator.parser.ListDevicesParser;
import org.openhab.binding.homematic.internal.communicator.parser.RssiInfoParser;
import org.openhab.binding.homematic.internal.misc.MiscUtils;
import org.openhab.binding.homematic.internal.model.HmChannel;
import org.openhab.binding.homematic.internal.model.HmDatapoint;
import org.openhab.binding.homematic.internal.model.HmDevice;
@@ -233,7 +233,7 @@ public HmGatewayInfo getGatewayInfo(String id) throws IOException {

try {
ddParser = getDeviceDescription(HmInterface.RF);
isHomegear = StringUtils.equalsIgnoreCase(ddParser.getType(), "Homegear");
isHomegear = "Homegear".equalsIgnoreCase(ddParser.getType());
} catch (IOException ex) {
// can't load gateway infos via RF interface
ddParser = new GetDeviceDescriptionParser();
@@ -247,21 +247,23 @@ public HmGatewayInfo getGatewayInfo(String id) throws IOException {

HmGatewayInfo gatewayInfo = new HmGatewayInfo();
gatewayInfo.setAddress(biParser.getGatewayAddress());
String gwType = biParser.getType();
if (isHomegear) {
gatewayInfo.setId(HmGatewayInfo.ID_HOMEGEAR);
gatewayInfo.setType(ddParser.getType());
gatewayInfo.setFirmware(ddParser.getFirmware());
} else if ((StringUtils.startsWithIgnoreCase(biParser.getType(), "CCU")
|| StringUtils.startsWithIgnoreCase(biParser.getType(), "HMIP_CCU")
|| StringUtils.startsWithIgnoreCase(ddParser.getType(), "HM-RCV-50") || config.isCCUType())
} else if ((MiscUtils.strStartsWithIgnoreCase(gwType, "CCU")
|| MiscUtils.strStartsWithIgnoreCase(gwType, "HMIP_CCU")
|| MiscUtils.strStartsWithIgnoreCase(ddParser.getType(), "HM-RCV-50") || config.isCCUType())
&& !config.isNoCCUType()) {
gatewayInfo.setId(HmGatewayInfo.ID_CCU);
String type = StringUtils.isBlank(biParser.getType()) ? "CCU" : biParser.getType();
String type = gwType.isBlank() ? "CCU" : gwType;
gatewayInfo.setType(type);
gatewayInfo.setFirmware(ddParser.getFirmware() != null ? ddParser.getFirmware() : biParser.getFirmware());
gatewayInfo
.setFirmware(!ddParser.getFirmware().isEmpty() ? ddParser.getFirmware() : biParser.getFirmware());
} else {
gatewayInfo.setId(HmGatewayInfo.ID_DEFAULT);
gatewayInfo.setType(biParser.getType());
gatewayInfo.setType(gwType);
gatewayInfo.setFirmware(biParser.getFirmware());
}

Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@@ -84,7 +83,7 @@ public BinRpcMessage(InputStream is, boolean methodHeader, String encoding) thro
if (length != 4) {
throw new EOFException("Only " + length + " bytes received reading message length");
}
int datasize = (new BigInteger(ArrayUtils.subarray(sig, 4, 8))).intValue();
int datasize = (new BigInteger(Arrays.copyOfRange(sig, 4, 8))).intValue();
byte payload[] = new byte[datasize];
int offset = 0;
int currentLength;
@@ -96,7 +95,10 @@ public BinRpcMessage(InputStream is, boolean methodHeader, String encoding) thro
throw new EOFException("Only " + offset + " bytes received while reading message payload, expected "
+ datasize + " bytes");
}
byte[] message = ArrayUtils.addAll(sig, payload);
byte[] message = new byte[sig.length + payload.length];
System.arraycopy(sig, 0, message, 0, sig.length);
System.arraycopy(payload, 0, message, sig.length, payload.length);

decodeMessage(message, methodHeader);
}

Original file line number Diff line number Diff line change
@@ -23,8 +23,6 @@
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringEscapeUtils;

/**
* A XML-RPC request for sending data to the Homematic server.
*
@@ -124,7 +122,7 @@ private void generateValue(Object value) {
} else {
Class<?> clazz = value.getClass();
if (clazz == String.class || clazz == Character.class) {
sb.append(StringEscapeUtils.escapeXml(value.toString()));
sb.append(escapeXml(value.toString()));
} else if (clazz == Long.class || clazz == Integer.class || clazz == Short.class || clazz == Byte.class) {
tag("int", value.toString());
} else if (clazz == Double.class) {
@@ -176,4 +174,30 @@ private void generateValue(Object value) {
}
}
}

private StringBuilder escapeXml(String inValue) {
StringBuilder outValue = new StringBuilder(inValue.length());
for (int i = 0; i < inValue.length(); i++) {
switch (inValue.charAt(i)) {
case '<':
outValue.append("&lt;");
break;
case '>':
outValue.append("&gt;");
break;
case '&':
outValue.append("&amp;");
break;
case '\'':
outValue.append("&apost;");
break;
case '"':
outValue.append("&quot;");
break;
default:
outValue.append(inValue.charAt(i));
}
}
return outValue;
}
}
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.openhab.binding.homematic.internal.model.HmChannel;
import org.openhab.binding.homematic.internal.model.HmDatapoint;
import org.openhab.binding.homematic.internal.model.HmInterface;
@@ -52,7 +51,7 @@ public Void parse(TclScriptDataList resultList) throws IOException {
}

private String[] toOptionList(String options) {
String[] result = StringUtils.splitByWholeSeparatorPreserveAllTokens(options, ";");
String[] result = options == null ? null : options.split(";");
return result == null || result.length == 0 ? null : result;
}
}
Original file line number Diff line number Diff line change
@@ -14,7 +14,6 @@

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.openhab.binding.homematic.internal.model.HmChannel;
import org.openhab.binding.homematic.internal.model.HmDatapoint;
import org.openhab.binding.homematic.internal.model.HmParamsetType;
@@ -57,8 +56,7 @@ public Void parse(TclScriptDataList resultList) throws IOException {
}
dp.setReadOnly(entry.readOnly);
dp.setUnit(entry.unit);

String[] result = StringUtils.splitByWholeSeparatorPreserveAllTokens(entry.options, ";");
String[] result = entry.options == null ? null : entry.options.split(";");
dp.setOptions(result == null || result.length == 0 ? null : result);

if (dp.getOptions() != null) {
Original file line number Diff line number Diff line change
@@ -13,11 +13,9 @@
package org.openhab.binding.homematic.internal.communicator.parser;

import java.io.IOException;
import java.util.Objects;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.openhab.binding.homematic.internal.misc.MiscUtils;
import org.openhab.binding.homematic.internal.model.HmDatapoint;
import org.openhab.binding.homematic.internal.model.HmParamsetType;
@@ -38,7 +36,8 @@ public abstract class CommonRpcParser<M, R> implements RpcParser<M, R> {
* Converts the object to a string.
*/
protected String toString(Object object) {
return StringUtils.trimToNull(ObjectUtils.toString(object));
String value = Objects.toString(object, "").trim();
return value.isEmpty() ? null : value;
}

/**
@@ -49,7 +48,7 @@ protected Integer toInteger(Object object) {
return (Integer) object;
}
try {
return Double.valueOf(ObjectUtils.toString(object)).intValue();
return Double.valueOf(object.toString()).intValue();
} catch (NumberFormatException ex) {
logger.debug("Failed converting {} to a Double", object, ex);
return null;
@@ -64,7 +63,7 @@ protected Double toDouble(Object object) {
return (Double) object;
}
try {
return Double.valueOf(ObjectUtils.toString(object));
return Double.valueOf(object.toString());
} catch (NumberFormatException ex) {
logger.debug("Failed converting {} to a Double", object, ex);
return null;
@@ -79,7 +78,12 @@ protected Number toNumber(Object object) {
return (Number) object;
}
try {
return NumberUtils.createNumber(ObjectUtils.toString(object));
String value = object.toString();
if (value.contains(".")) {
return Float.parseFloat(value);
} else {
return Integer.parseInt(value);
}
} catch (NumberFormatException ex) {
logger.debug("Failed converting {} to a Number", object, ex);
return null;
@@ -93,7 +97,7 @@ protected Boolean toBoolean(Object object) {
if (object == null || object instanceof Boolean) {
return (Boolean) object;
}
return BooleanUtils.toBoolean(ObjectUtils.toString(object));
return "true".equals(object.toString().toLowerCase());
}

/**
@@ -114,9 +118,10 @@ protected String[] toOptionList(Object optionList) {
/**
* Returns the address of a device, replacing group address identifier and illegal characters.
*/
@NonNull
protected String getSanitizedAddress(Object object) {
String address = StringUtils.trimToNull(StringUtils.replaceOnce(toString(object), "*", "T-"));
return MiscUtils.validateCharacters(address, "Address", "_");
String address = Objects.toString(object, "").trim().replaceFirst("\\*", "T-");
return MiscUtils.validateCharacters(address.isEmpty() ? null : address, "Address", "_");
}

/**
@@ -167,8 +172,11 @@ protected HmDatapoint assembleDatapoint(String name, String unit, String type, S
HmDatapoint dp = new HmDatapoint();
dp.setName(name);
dp.setDescription(name);
dp.setUnit(StringUtils.replace(StringUtils.trimToNull(unit), "\ufffd", "°"));
if (dp.getUnit() == null && StringUtils.startsWith(dp.getName(), "RSSI_")) {
if (unit != null) {
unit = unit.trim().replace("\ufffd", "°");
}
dp.setUnit(unit == null || unit.isEmpty() ? null : unit);
if (dp.getUnit() == null && dp.getName() != null && dp.getName().startsWith("RSSI_")) {
dp.setUnit("dBm");
}

@@ -210,7 +218,7 @@ protected HmDatapoint assembleDatapoint(String name, String unit, String type, S
* Converts a string value to the type.
*/
protected Object convertToType(String value) {
if (StringUtils.isBlank(value)) {
if (value == null || value.isBlank()) {
return null;
}
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("on")) {
Loading

0 comments on commit 7cf813f

Please sign in to comment.