Skip to content

Commit

Permalink
[pjlink] Null characters fix (openhab#7462)
Browse files Browse the repository at this point in the history
* use MessageFormat where applicable
* remove null characters from response

see openhab#6725

Signed-off-by: Nils Schnabel <github@to.nilsschnabel.de>
Signed-off-by: Eugen Freiter <freiter@gmx.de>
  • Loading branch information
nils authored and Eugen Freiter committed Apr 27, 2020
1 parent ba995e2 commit 1bfdc78
Showing 1 changed file with 11 additions and 3 deletions.
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

0 comments on commit 1bfdc78

Please sign in to comment.