Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pjlink] Null characters fix #7462

Merged
merged 2 commits into from
Apr 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.text.MessageFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
Expand Down Expand Up @@ -206,6 +207,11 @@ public void addPrefixToNextCommand(String cmd) throws IOException, Authenticatio
this.prefixForNextCommand = cmd;
}

public static String preprocessResponse(String response) {
// some devices send leading zero bytes, see https://github.com/openhab/openhab-addons/issues/6725
return response.replaceAll("^\0*|\0*$", "");
}

public synchronized String execute(String command) throws IOException, AuthenticationException, ResponseException {
String fullCommand = this.prefixForNextCommand + command;
this.prefixForNextCommand = "";
Expand All @@ -227,18 +233,20 @@ public synchronized String execute(String command) throws IOException, Authentic
}

String response = null;
while ((response = getReader().readLine()) != null && response.isEmpty()) {
while ((response = getReader().readLine()) != null && preprocessResponse(response).isEmpty()) {
logger.debug("Got empty string response for request '{}' from {}, waiting for another line", response,
fullCommand.replaceAll("\r", "\\\\r"));
}
if (response == null) {
throw new ResponseException("Response to request '" + fullCommand.replaceAll("\r", "\\\\r") + "' was null");
throw new ResponseException(MessageFormat.format("Response to request ''{0}'' was null",
fullCommand.replaceAll("\r", "\\\\r")));
}

if (logger.isDebugEnabled()) {
logger.debug("Got response '{}' ({}) for request '{}' from {}", response,
Arrays.toString(response.getBytes()), fullCommand.replaceAll("\r", "\\\\r"), ipAddress);
}
return response;
return preprocessResponse(response);
}

public void checkAvailability() throws IOException, AuthenticationException, ResponseException {
Expand Down