use of org.libresonic.player.domain.Player in project libresonic by Libresonic.
the class PlayerDaoTestCase method testDeletePlayer.
@Test
public void testDeletePlayer() {
assertEquals("Wrong number of players.", 0, playerDao.getAllPlayers().size());
playerDao.createPlayer(new Player());
assertEquals("Wrong number of players.", 1, playerDao.getAllPlayers().size());
playerDao.createPlayer(new Player());
assertEquals("Wrong number of players.", 2, playerDao.getAllPlayers().size());
playerDao.deletePlayer("1");
assertEquals("Wrong number of players.", 1, playerDao.getAllPlayers().size());
playerDao.deletePlayer("2");
assertEquals("Wrong number of players.", 0, playerDao.getAllPlayers().size());
}
use of org.libresonic.player.domain.Player in project libresonic by Libresonic.
the class PlayerDaoTestCase method testCreatePlayer.
@Test
public void testCreatePlayer() {
Player player = new Player();
player.setName("name");
player.setType("type");
player.setUsername("username");
player.setIpAddress("ipaddress");
player.setDynamicIp(false);
player.setAutoControlEnabled(false);
player.setTechnology(PlayerTechnology.EXTERNAL_WITH_PLAYLIST);
player.setClientId("android");
player.setLastSeen(new Date());
player.setTranscodeScheme(TranscodeScheme.MAX_160);
playerDao.createPlayer(player);
Player newPlayer = playerDao.getAllPlayers().get(0);
assertPlayerEquals(player, newPlayer);
Player newPlayer2 = playerDao.getPlayerById(newPlayer.getId());
assertPlayerEquals(player, newPlayer2);
}
use of org.libresonic.player.domain.Player in project libresonic by Libresonic.
the class LibresonicContentDirectory method createResourceForSong.
protected Res createResourceForSong(MediaFile song) {
Player player = playerService.getGuestPlayer(null);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(getBaseUrl() + "/ext/stream").queryParam("id", song.getId()).queryParam("player", player.getId());
if (song.isVideo()) {
builder.queryParam("format", TranscodingService.FORMAT_RAW);
}
jwtSecurityService.addJWTToken(builder);
String url = builder.toUriString();
String suffix = song.isVideo() ? FilenameUtils.getExtension(song.getPath()) : transcodingService.getSuffix(player, song, null);
String mimeTypeString = StringUtil.getMimeType(suffix);
MimeType mimeType = mimeTypeString == null ? null : MimeType.valueOf(mimeTypeString);
Res res = new Res(mimeType, null, url);
res.setDuration(formatDuration(song.getDurationSeconds()));
return res;
}
use of org.libresonic.player.domain.Player in project libresonic by Libresonic.
the class PlayerService method getPlayer.
/**
* Returns the player associated with the given HTTP request. If no such player exists, a new
* one is created.
*
* @param request The HTTP request.
* @param response The HTTP response.
* @param remoteControlEnabled Whether this method should return a remote-controlled player.
* @param isStreamRequest Whether the HTTP request is a request for streaming data.
* @return The player associated with the given HTTP request.
*/
public synchronized Player getPlayer(HttpServletRequest request, HttpServletResponse response, boolean remoteControlEnabled, boolean isStreamRequest) {
// Find by 'player' request parameter.
Player player = getPlayerById(request.getParameter("player"));
// Find in session context.
if (player == null && remoteControlEnabled) {
String playerId = (String) request.getSession().getAttribute("player");
if (playerId != null) {
player = getPlayerById(playerId);
}
}
// Find by cookie.
String username = securityService.getCurrentUsername(request);
if (player == null && remoteControlEnabled) {
player = getPlayerById(getPlayerIdFromCookie(request, username));
}
// Make sure we're not hijacking the player of another user.
if (player != null && player.getUsername() != null && username != null && !player.getUsername().equals(username)) {
player = null;
}
// Look for player with same IP address and user name.
if (player == null) {
player = getNonRestPlayerByIpAddressAndUsername(request.getRemoteAddr(), username);
}
// If no player was found, create it.
if (player == null) {
player = new Player();
createPlayer(player);
// LOG.debug("Created player " + player.getId() + " (remoteControlEnabled: " + remoteControlEnabled +
// ", isStreamRequest: " + isStreamRequest + ", username: " + username +
// ", ip: " + request.getRemoteAddr() + ").");
}
// Update player data.
boolean isUpdate = false;
if (username != null && player.getUsername() == null) {
player.setUsername(username);
isUpdate = true;
}
if (player.getIpAddress() == null || isStreamRequest || (!isPlayerConnected(player) && player.isDynamicIp() && !request.getRemoteAddr().equals(player.getIpAddress()))) {
player.setIpAddress(request.getRemoteAddr());
isUpdate = true;
}
String userAgent = request.getHeader("user-agent");
if (isStreamRequest) {
player.setType(userAgent);
player.setLastSeen(new Date());
isUpdate = true;
}
if (isUpdate) {
updatePlayer(player);
}
// Set cookie in response.
if (response != null) {
String cookieName = COOKIE_NAME + "-" + StringUtil.utf8HexEncode(username);
Cookie cookie = new Cookie(cookieName, player.getId());
cookie.setMaxAge(COOKIE_EXPIRY);
String path = request.getContextPath();
if (StringUtils.isEmpty(path)) {
path = "/";
}
cookie.setPath(path);
response.addCookie(cookie);
}
// Save player in session context.
if (remoteControlEnabled) {
request.getSession().setAttribute("player", player.getId());
}
return player;
}
use of org.libresonic.player.domain.Player in project libresonic by Libresonic.
the class PlayerService method getGuestPlayer.
/**
* Returns a player associated to the special "guest" user, creating it if necessary.
*/
public Player getGuestPlayer(HttpServletRequest request) {
// Create guest user if necessary.
User user = securityService.getUserByName(User.USERNAME_GUEST);
if (user == null) {
user = new User(User.USERNAME_GUEST, RandomStringUtils.randomAlphanumeric(30), null);
user.setStreamRole(true);
securityService.createUser(user);
}
// Look for existing player.
List<Player> players = getPlayersForUserAndClientId(User.USERNAME_GUEST, null);
if (!players.isEmpty()) {
return players.get(0);
}
// Create player if necessary.
Player player = new Player();
if (request != null) {
player.setIpAddress(request.getRemoteAddr());
}
player.setUsername(User.USERNAME_GUEST);
createPlayer(player);
return player;
}
Aggregations