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

[squeezebox] Fix local cover art download failure when auth enabled #4654

Merged
merged 3 commits into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions addons/binding/org.openhab.binding.squeezebox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,6 @@ Therefore, it is recommended that the LMS be on a more current version than 7.7.

- There have been reports that the LMS does not play some WAV files reliably.
If you're using a TTS service that produces WAV files, and the notifications are not playing, try using an MP3-formatted TTS notification.

- The LMS treats player MAC addresses as case-sensitive.
Therefore, the case of MAC addresses in the Squeeze Player thing configuration must match the case displayed on the *Information* tab in the LMS Settings.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import static org.openhab.binding.squeezebox.internal.SqueezeBoxBindingConstants.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -68,8 +70,7 @@
* @author Mark Hilbush - Convert sound notification volume from channel to config parameter
*/
public class SqueezeBoxPlayerHandler extends BaseThingHandler implements SqueezeBoxPlayerEventListener {

private Logger logger = LoggerFactory.getLogger(SqueezeBoxPlayerHandler.class);
private final Logger logger = LoggerFactory.getLogger(SqueezeBoxPlayerHandler.class);

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
.singleton(SQUEEZEBOXPLAYER_THING_TYPE);
Expand Down Expand Up @@ -417,17 +418,18 @@ private RawType downloadImage(String mac, String url) {
// Only get the image if this is my PlayerHandler instance
if (isMe(mac)) {
if (StringUtils.isNotEmpty(url)) {
String sanitizedUrl = sanitizeUrl(url);
RawType image = IMAGE_CACHE.putIfAbsentAndGet(url, () -> {
logger.debug("Trying to download the content of URL {}", url);
logger.debug("Trying to download the content of URL {}", sanitizedUrl);
try {
return HttpUtil.downloadImage(url);
} catch (IllegalArgumentException e) {
logger.debug("IllegalArgumentException when downloading image from {}", url, e);
logger.debug("IllegalArgumentException when downloading image from {}", sanitizedUrl, e);
return null;
}
});
if (image == null) {
logger.debug("Failed to download the content of URL {}", url);
logger.debug("Failed to download the content of URL {}", sanitizedUrl);
return null;
} else {
return image;
Expand All @@ -437,6 +439,26 @@ private RawType downloadImage(String mac, String url) {
return null;
}

/*
* Replaces the password in the URL, if present
*/
private String sanitizeUrl(String url) {
String sanitizedUrl = url;
try {
URI uri = new URI(url);
String userInfo = uri.getUserInfo();
if (userInfo != null) {
String[] userInfoParts = userInfo.split(":");
if (userInfoParts.length == 2) {
sanitizedUrl = url.replace(userInfoParts[1], "**********");
}
}
} catch (URISyntaxException e) {
// Just return what was passed in
}
return sanitizedUrl;
}

/**
* Wrap the given RawType and return it as {@link State} or return {@link UnDefType#UNDEF} if the RawType is null.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@
* @author Patrik Gfeller - Moved class to its own file.
*/
class SqueezeBoxPlayerState {
boolean savedMute;
boolean savedPower;
boolean savedStop;
boolean savedControl;

int savedVolume;
int savedShuffle;
int savedRepeat;
int savedPlaylistIndex;
int savedNumberPlaylistTracks;
int savedPlayingTime;
private final Logger logger = LoggerFactory.getLogger(SqueezeBoxPlayerState.class);

private boolean savedMute;
private boolean savedPower;
private boolean savedStop;
private boolean savedControl;

private int savedVolume;
private int savedShuffle;
private int savedRepeat;
private int savedPlaylistIndex;
private int savedNumberPlaylistTracks;
private int savedPlayingTime;

private SqueezeBoxPlayerHandler playerHandler;
private Logger logger = LoggerFactory.getLogger(SqueezeBoxPlayerState.class);

public SqueezeBoxPlayerState(SqueezeBoxPlayerHandler playerHandler) {
this.playerHandler = playerHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
* @author Mark Hilbush - Get favorites from LMS; update channel and send to players
*/
public class SqueezeBoxServerHandler extends BaseBridgeHandler {
private Logger logger = LoggerFactory.getLogger(SqueezeBoxServerHandler.class);
private final Logger logger = LoggerFactory.getLogger(SqueezeBoxServerHandler.class);

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
.singleton(SQUEEZEBOXSERVER_THING_TYPE);
Expand Down Expand Up @@ -324,16 +324,27 @@ private synchronized void sendCommand(String command) {
return;
}

logger.debug("Sending command: {}", command);
logger.debug("Sending command: {}", sanitizeCommand(command));
try {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
writer.write(command + NEW_LINE);
writer.flush();
} catch (IOException e) {
logger.error("Error while sending command to Squeeze Server ({}) ", command, e);
logger.error("Error while sending command to Squeeze Server ({}) ", sanitizeCommand(command), e);
}
}

/*
* Remove password from login command to prevent it from being logged
*/
String sanitizeCommand(String command) {
String sanitizedCommand = command;
if (command.startsWith("login")) {
sanitizedCommand = command.replace(password, "**********");
}
return sanitizedCommand;
}

/**
* Connects to a SqueezeBox Server
*/
Expand Down Expand Up @@ -789,7 +800,12 @@ public void updateListener(SqueezeBoxPlayerEventListener listener) {
}

private String constructCoverArtUrl(String mac, boolean coverart, String coverid, String artwork_url) {
String hostAndPort = "http://" + host + ":" + webport;
String hostAndPort;
if (StringUtils.isNotEmpty(userId)) {
hostAndPort = "http://" + userId + ":" + password + "@" + host + ":" + webport;
} else {
hostAndPort = "http://" + host + ":" + webport;
}

// Default to using the convenience artwork URL (should be rare)
String url = hostAndPort + "/music/current/cover.jpg?player=" + encode(mac);
Expand All @@ -812,7 +828,6 @@ private String constructCoverArtUrl(String mac, boolean coverart, String coverid
url = hostAndPort + "/" + decode(artwork_url);
}
}
logger.trace("{}: URL for cover art is {}", mac, url);
return url;
}

Expand Down