Package se.despotify

Examples of se.despotify.ManagedConnection


    /* Register channel. */
    Channel.register(channel);

    /* Send packet. */
    ManagedConnection connection = connectionManager.getManagedConnection();
    connection.getProtocol().sendPacket(PacketType.changePlaylist, buffer, "remove track from playlist");

    /* Get response. */
    byte[] data = callback.getData("remove track from playlist ack");
    connection.close();

    xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<playlist>\n" +
        new String(data, Charset.forName("UTF-8")) +
        "\n</playlist>";
    if (log.isDebugEnabled()) {
View Full Code Here


  @Override
  public Playlist send(ConnectionManager connectionManager) throws DespotifyException {

    if (user == null) {
      ManagedConnection connection = connectionManager.getManagedConnection();
      user = connection.getSession().getUser();
      connection.close();
    }

    byte[] playlistUUID = new ReserveRandomPlaylistUUID(store, user, playlistName, collaborative).send(connectionManager);
    String hexUUID = Hex.toHex(playlistUUID);
    Playlist playlist = store.getPlaylist(hexUUID);
View Full Code Here

  @Override
  public Boolean send(ConnectionManager connectionManager) throws DespotifyException {


    if (user == null) {
      ManagedConnection connection = connectionManager.getManagedConnection();
      user = connection.getSession().getUser();
      connection.close();
    }
   

    if (playlist.getLoaded() == null) {
      new LoadPlaylist(store, playlist).send(connectionManager);
    }


    if (!playlist.isCollaborative() && !playlist.getAuthor().equals(user.getId())) {
      throw new DespotifyException("Playlist must be collaborative or owned by the current user!");
    }

    if (user.getPlaylists() == null) {
      new LoadUserPlaylists(store, user).send(connectionManager);
    }

    if (playlist.getTracks() == null) {
      playlist.setTracks(new ArrayList<Track>());
    }


    long previousChecksum = playlist.calculateChecksum();

    if (position != null && position != playlist.getTracks().size()) {
      throw new IllegalArgumentException("position not implemented!");
    }
   
    if (position == null) {
      position = playlist.size();
    }
    playlist.getTracks().add(position, track);

    playlist.setChecksum(playlist.calculateChecksum());

    String xml = String.format(
        "<change><ops><add><i>%s</i><items>%s</items></add></ops><time>%d</time><user>%s</user></change>" +
            "<version>%010d,%010d,%010d,%d</version>",
        position,
        track.getId() + "01", // hex uuid tag
        new Date().getTime() / 1000,
        user.getId(),
        playlist.getRevision() + 1,
        playlist.getTracks().size(),
        playlist.getChecksum(),
        playlist.isCollaborative() ? 1 : 0
    );

    /* Create channel callback */
    ChannelCallback callback = new ChannelCallback();

    Channel channel = new Channel("Change-Playlist-Channel", Channel.Type.TYPE_PLAYLIST, callback);
    byte[] xmlBytes = xml.getBytes();
    ByteBuffer buffer = ByteBuffer.allocate(2 + 16 + 1 + 4 + 4 + 4 + 1 + 1 + xmlBytes.length);

    /* Append channel id, playlist id and some bytes... */
    buffer.putShort((short) channel.getId());
    buffer.put(playlist.getByteUUID());
    buffer.put((byte) 0x02); // track UUID type tag

    buffer.putInt(playlist.getRevision().intValue());
    buffer.putInt(playlist.getTracks().size() - 1);
    buffer.putInt((int)previousChecksum); // -1 only seen when creating new playlist.
    buffer.put((byte) (playlist.isCollaborative() ? 0x01 : 0x00));
    buffer.put((byte) 0x03); // unknown
    buffer.put(xmlBytes);
    buffer.flip();

    /* Register channel. */
    Channel.register(channel);

    /* Send packet. */
    ManagedConnection connection = connectionManager.getManagedConnection();
    connection.getProtocol().sendPacket(PacketType.changePlaylist, buffer, "add track to playlist");

    /* Get response. */
    byte[] data = callback.getData("add track to playlist, updated playlist response");
    connection.close();
   

    xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<playlist>\n" +
        new String(data, Charset.forName("UTF-8")) +
        "\n</playlist>";
View Full Code Here

      throw new RuntimeException();
    }

    // todo probably don't destoy collaborative that is not owned by user? or?

    ManagedConnection connection = connectionManager.getManagedConnection();
    if (!sendDelete(connection.getProtocol(), position)) {
      connection.close();
      throw new DespotifyException();
    }
    if (!sendDestroy(connection.getProtocol(), position)) {
      connection.close();
      throw new DespotifyException();
    }
    connection.close();
    return true;
  }
View Full Code Here

   * @return uuid
   */
  public Boolean send(ConnectionManager connectionManager) throws DespotifyException {

    if (user == null) {
      ManagedConnection connection = connectionManager.getManagedConnection();
      user = connection.getSession().getUser();
      connection.close();
    }

    if (user.getPlaylists() == null) {
      log.warn("user playlists not loaded yet! should it be? loading..");
      new LoadUserPlaylists(store, user).send(connectionManager);
    }
   


    String xml = String.format(
        "<id-is-unique/><change><ops><create/><name>%s</name></ops><time>%d</time><user>%s</user></change>" +
            "<version>0000000001,0000000000,0000000001,%s</version>",
        playlistName,
        new Date().getTime() / 1000,
        user.getId(),
        collaborative ? 1 : 0
    );

    /* Create channel callback */
    ChannelCallback callback = new ChannelCallback();


    /* Create channel and buffer. */
    Channel channel = new Channel("Create-Playlist-Channel", Channel.Type.TYPE_PLAYLIST, callback);
    byte[] xmlBytes = xml.getBytes();
    ByteBuffer buffer = ByteBuffer.allocate(2 + 17 + 4 + 4 + 4 + 1 + 1 + xmlBytes.length);

    /* Append channel id, playlist id and some bytes... */
    buffer.putShort((short) channel.getId());
    buffer.put(requestedPlaylistUUID);
    buffer.put((byte) 0x02); // uuid playlist marker?
    buffer.putInt(0); // playlist revision?
    buffer.putInt(0); // playlist tracks size?
    buffer.putInt(-1); // playlist checksum? -1 when create uuid
    buffer.put((byte) 0x01); // normally means collaborate, not sure whats with this here
    buffer.put((byte) 0x03); // unknown
    buffer.put(xmlBytes);
    buffer.flip();

    /* Register channel. */
    Channel.register(channel);

    /* Send packet. */
    ManagedConnection connection = connectionManager.getManagedConnection();
    connection.getProtocol().sendPacket(PacketType.changePlaylist, buffer, "create playlist UUID");

    /* Get response. */
    byte[] data = callback.getData("create playlist uuid reponse");
    connection.close();

    xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<playlist>\n" +
        new String(data, Charset.forName("UTF-8")) +
        "\n</playlist>";

View Full Code Here

    /* Register channel. */
    Channel.register(channel);

    /* Send packet. */
    ManagedConnection connection = connectionManager.getManagedConnection();
    connection.getProtocol().sendPacket(PacketType.image, buffer, "load image");
    connection.close();

      /* Get data. */
    image.setBytes(callback.getData("load image response"));

    image.setLoaded(new Date());
View Full Code Here

    /* Register channel. */
    Channel.register(channel);

    /* Send packet. */
    ManagedConnection connection = connectionManager.getManagedConnection();
    connection.getProtocol().sendPacket(PacketType.search, buffer, "search");

    /* Get data and inflate it. */
    byte[] data = callback.getData("gzipped search response");
    connection.close();

//    System.out.println(new String(GZIP.inflate(data), UTF8));

    try {
      XMLStreamReader xmlr = ResponseUnmarshaller.createReader(new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(data)), Charset.forName("UTF-8")));
View Full Code Here

    buffer.putInt(-1); // unknown
    buffer.put((byte)0x00); // 00 = get playlist ids, 01 = do not get playlist ids?
    buffer.flip();

    Channel.register(channel);
    ManagedConnection connection = connectionManager.getManagedConnection();
    connection.getProtocol().sendPacket(PacketType.getPlaylist, buffer, "request list of user playlists");


    byte[] data = callback.getData("user playlists response");
    connection.close();
          
    if (data.length == 0) {
      throw new DespotifyException("received an empty response!");
    }
View Full Code Here

    Channel.register(channel);

    /* Send packet. */
//    timeRequest.start();

    ManagedConnection connection = connectionManager.getManagedConnection();
    connection.getProtocol().sendPacket(PacketType.browse, buffer, "load track");
    byte[] compressedData = callback.getData("gzipped load tracks reponse");
    connection.close();

//    timeRequest.stop();

    try {

View Full Code Here

    /* Register channel. */
    Channel.register(channel);

    /* Send packet. */
    ManagedConnection connection = connectionManager.getManagedConnection();
    connection.getProtocol().sendPacket(PacketType.changePlaylist, buffer, "rename playlist");

    /* Get response. */
    byte[] data = callback.getData("rename playlist response");
    connection.close();

    xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><playlist>" +
        new String(data, Charset.forName("UTF-8")) +
        "</playlist>";

View Full Code Here

TOP

Related Classes of se.despotify.ManagedConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.