Search in sources :

Example 21 with Player

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());
}
Also used : Player(org.libresonic.player.domain.Player) Test(org.junit.Test)

Example 22 with Player

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);
}
Also used : Player(org.libresonic.player.domain.Player) Date(java.util.Date) Test(org.junit.Test)

Example 23 with Player

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;
}
Also used : Res(org.fourthline.cling.support.model.Res) Player(org.libresonic.player.domain.Player) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MimeType(org.seamless.util.MimeType)

Example 24 with Player

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;
}
Also used : Cookie(javax.servlet.http.Cookie) Player(org.libresonic.player.domain.Player) Date(java.util.Date)

Example 25 with 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;
}
Also used : Player(org.libresonic.player.domain.Player) User(org.libresonic.player.domain.User)

Aggregations

Player (org.libresonic.player.domain.Player)25 Test (org.junit.Test)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 MediaFile (org.libresonic.player.domain.MediaFile)4 User (org.libresonic.player.domain.User)4 ModelAndView (org.springframework.web.servlet.ModelAndView)4 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Transcoding (org.libresonic.player.domain.Transcoding)3 File (java.io.File)2 PlayQueue (org.libresonic.player.domain.PlayQueue)2 Playlist (org.libresonic.player.domain.Playlist)2 TransferStatus (org.libresonic.player.domain.TransferStatus)2 UserSettings (org.libresonic.player.domain.UserSettings)2 RedirectView (org.springframework.web.servlet.view.RedirectView)2 PrintWriter (java.io.PrintWriter)1 DateFormat (java.text.DateFormat)1 Cookie (javax.servlet.http.Cookie)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1