Package se.despotify.exceptions

Examples of se.despotify.exceptions.DespotifyException


  public Boolean send(ConnectionManager connectionManager) throws DespotifyException {

    // todo send multiple requests if more than 200 tracks!

    if (tracks.length == 0) {
      throw new DespotifyException("No tracks supplied to be loaded!");
    }

    if (tracks.length > 240) {
      throw new DespotifyException("Can only load up to 240 track at the time.");
    }

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

    /* Send browse request. */

    /* Create channel and buffer. */
    Channel channel = new Channel("Browse-Channel", Channel.Type.TYPE_BROWSE, callback);
    ByteBuffer buffer = ByteBuffer.allocate(2 + 1 + tracks.length * 16); //+ ((type == BrowseType.artist || type == BrowseType.album)?4:0));

//    if(ids.size() > 1 && type != BrowseType.track){
//      throw new IllegalArgumentException("Only BrowserType.track accepts multiple ids.");
//    }

    /* Append channel id and type. */
    buffer.putShort((short) channel.getId());
    buffer.put((byte) BrowseType.track.getValue());

    /* Append (16 byte) ids. */
    for (Track track : tracks) {
      buffer.put(Arrays.copyOfRange(track.getByteUUID(), 0, 16));
    }

//    /* Append zero. */
//    if(type == BrowseType.artist || type == BrowseType.album){
//      buffer.putInt(0);
//    }

    buffer.flip();

    /* Register channel. */
    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 {

//      timeUnmarshall.start();

      Reader reader = new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(compressedData)), UTF8);
      XMLStreamReader xmlr = ResponseUnmarshaller.createReader(reader);
      ResponseUnmarshaller unmarshaller = new ResponseUnmarshaller(store, xmlr);
      unmarshaller.skip();
      List<Track> tracks = unmarshaller.unmarshallLoadTracks();
      xmlr.close();
      for (Track track : tracks) {
        store.persist(track);
      }
     
//      timeUnmarshall.stop();

    } catch (IOException e) {
      throw new DespotifyException(e);
    } catch (XMLStreamException e) {
      throw new DespotifyException(e);
    }

//    System.out.println(timeRequest);
//    System.out.println(timeUnmarshall);

View Full Code Here


      try {
        XMLStreamReader xmlr = ResponseUnmarshaller.createReader(new InputStreamReader(new GZIPInputStream(new ByteArrayInputStream(data)), Charset.forName("UTF-8")));
        ResponseUnmarshaller responseUnmarshaller = new ResponseUnmarshaller(store, xmlr);
        responseUnmarshaller.skip();
        if (!"artist".equals(xmlr.getLocalName())) {
          throw new DespotifyException("Expected document root to be of type <artist>");
        }

        artist = responseUnmarshaller.unmarshallArtist(new Date());
        if (!this.artist.equals(artist)) {
          throw new DespotifyException("Artist in response has different UUID than the requested artist!");
        }
        xmlr.close();
      } catch (XMLStreamException e) {
        throw new DespotifyException(e);
      } catch (IOException e) {
        throw new DespotifyException(e);
      }

//      timeUnmarshall.stop();

    }
View Full Code Here

    try {
      XMLStreamReader xmlr = ResponseUnmarshaller.createReader(new InputStreamReader(new ByteArrayInputStream(data), Charset.forName("UTF-8")));
      ResponseUnmarshaller responseUnmarshaller = new ResponseUnmarshaller(store, xmlr);
      responseUnmarshaller.skip();
      if (!"album".equals(xmlr.getLocalName())) {
        throw new DespotifyException("Expected document root to be of type <album>");
      }
      album = responseUnmarshaller.unmarshallAlbum(new Date());
      xmlr.close();
    } catch (XMLStreamException e) {
      throw new DespotifyException(e);
    }

    if (!this.album.equals(album)) {
      throw new DespotifyException("Album in response has different UUID than the requested album!");
    }

    return (Album) store.persist(album);
  }
View Full Code Here

        final String trackHexUUID;
        if (trackData.length() != 34) {
          if (SpotifyURI.isHex(trackData)) {
            // not sure why playlist UUID is send sometimes. notice it is lacking UUID prefix byte
            if (!trackData.equals(playlist.getId())) {
              throw new DespotifyException("32 byte hex UUID does not equal the playlist UUID!");
            }
            continue;
          } else {
            throw new RuntimeException(trackData + " is not a valid 32 byte hex UUID!");
          }
        } else if (trackData.length() == 34) {
          trackHexUUID = trackData.substring(0, 32);
          if (!"01".equals(trackData.substring(32, 34))) {
            throw new DespotifyException("Expected hex UUID type suffix 01, got " + trackData.substring(32, 34));
          }
        } else {
          throw new RuntimeException("track UUID was not 16+1 or 16 byte!");
        }
View Full Code Here

    /* Save initial server packet for auth hmac generation. 1024 bytes should be enough. */
    ByteBuffer serverPacketBuffer = ByteBuffer.allocate(1024);

    /* Read server random (first 2 bytes). */
    if((ret = this.receive(this.session.serverRandom, 0, 2, "server random packet")) == -1){
      throw new DespotifyException("Failed to read server random.");
    }

    /* Check if we got a status message. */
    if(this.session.serverRandom[0] != 0x00 || ret != 2){

      int errorCode;

      /*
       * Substatuses:
       * 0x01    : Client upgrade required.
       * 0x03    : Non-existant user.
       * 0x04    : Account has been disabled.
       * 0x06    : You need to complete your account details.
       * 0x09    : Your current country doesn't match that set in your profile.
       * Default : Unknown error
       */
      StringBuilder message = new StringBuilder(250);

      switch (this.session.serverRandom[1]) {
        case 0x01:
          message.append("Client upgrade required: ");
        case 0x03:
          message.append("Non-existant user.");
        case 0x04:
          message.append("Account has been disabled.");
        case 0x06:
          message.append("You need to complete your account details.");
        case 0x09:
          message.append("Your current country doesn't match that set in your profile.");
      }
      if (message.length() == 0) {
        message.append("Unknown error.");
      }


      /* If substatus is 'Client upgrade required', read upgrade URL. */
      if(this.session.serverRandom[1] == 0x01){
        if((ret = this.receive(buffer, 0x11a, "text message length")) > 0){
          paddingLength = buffer[0x119] & 0xFF;

          if((ret = this.receive(buffer, paddingLength, "text message")) > 0){
            message.append(new String(Arrays.copyOfRange(buffer, 0, ret)));
          }
        }
      }

      message.append(" (").append(String.valueOf(ret)).append(")");

      throw new AuthenticationException(message.toString());
    }

    /* Read server random (next 14 bytes). */
    if((ret = this.receive(this.session.serverRandom, 2, 14, "14 random  server bytes")) != 14){
      throw new DespotifyException("Failed to read server random. ("+ret+")");
    }

    /* Save server random to packet buffer. */
    serverPacketBuffer.put(this.session.serverRandom);

    /* Read server public key (Diffie Hellman key exchange). */
    if((ret = this.receive(buffer, 96, "public server key")) != 96){
      throw new DespotifyException("Failed to read server public key. ("+ret+")");
    }

    /* Save DH public key to packet buffer. */
    serverPacketBuffer.put(buffer, 0, 96);

    /*
     * Convert key, which is in raw byte form to a DHPublicKey
     * using the DHParameterSpec (for P and G values) of our
     * public key. Y value is taken from raw bytes.
     */
    this.session.dhServerPublicKey = DH.bytesToPublicKey(
      this.session.dhClientKeyPair.getPublicKey().getParams(),
      Arrays.copyOfRange(buffer, 0, 96)
    );

    /* Read server blob (256 bytes). */
    if((ret = this.receive(this.session.serverBlob, 0, 256, "server blob")) != 256){
      throw new DespotifyException("Failed to read server blob. ("+ret+")");
    }

    /* Save RSA signature to packet buffer. */
    serverPacketBuffer.put(this.session.serverBlob);

    /* Read salt (10 bytes). */
    if((ret = this.receive(this.session.salt, 0, 10, "salt")) != 10){
      throw new DespotifyException("Failed to read salt. ("+ret+")");
    }

    /* Save salt to packet buffer. */
    serverPacketBuffer.put(this.session.salt);

    /* Read padding length (1 byte). */
    if((paddingLength = this.receive("padding length")) == -1){
      throw new DespotifyException("Failed to read paddling length.");
    }

    /* Save padding length to packet buffer. */
    serverPacketBuffer.put((byte)paddingLength);

    /* Check if padding length is valid. */
    if(paddingLength <= 0){
      throw new DespotifyException("Padding length is negative or zero.");
    }

    /* Read username length. */
    if((usernameLength = this.receive("")) == -1){
      throw new DespotifyException("Failed to read username length.");
    }

    /* Save username length to packet buffer. */
    serverPacketBuffer.put((byte)usernameLength);

    /* Read lengths of puzzle challenge and unknown fields */
    this.receive(buffer, 8, "length of puzzle challenge + length of 3 unknown fields");

    /* Save bytes to packet buffer. */
    serverPacketBuffer.put(buffer, 0, 8);

    /* Get lengths of puzzle challenge and unknown fields.  */
    ByteBuffer dataBuffer     = ByteBuffer.wrap(buffer, 0, 8);
    int puzzleChallengeLength = dataBuffer.getShort();
    int unknownLength1        = dataBuffer.getShort();
    int unknownLength2        = dataBuffer.getShort();
    int unknownLength3        = dataBuffer.getShort();

    /* Read padding. */
    if((ret = this.receive(buffer, paddingLength, "padding")) != paddingLength){
      throw new DespotifyException("Failed to read padding. (" + ret + ")");
    }

    /* Save padding (random bytes) to packet buffer. */
    serverPacketBuffer.put(buffer, 0, paddingLength);

    /* Read username into buffer and copy it to 'session.username'. */
    if((ret = this.receive(buffer, usernameLength, "username")) != usernameLength){
      throw new DespotifyException("Failed to read username. (" + ret + ")");
    }

    /* Save username to packet buffer. */
    serverPacketBuffer.put(buffer, 0, usernameLength);

    /* Save username to session. */
    this.session.username = Arrays.copyOfRange(buffer, 0, usernameLength);

    /* Receive puzzle challenge and unknown bytes. */
    this.receive(buffer,                                                       0, puzzleChallengeLength, "puzzle challange data");
    this.receive(buffer,                                   puzzleChallengeLength, unknownLength1, "unknown field 1");
    this.receive(buffer,                  puzzleChallengeLength + unknownLength1, unknownLength2, "unknown field 2");
    this.receive(buffer, puzzleChallengeLength + unknownLength1 + unknownLength2, unknownLength3, "unknown field 3");

    /* Save to packet buffer. */
    serverPacketBuffer.put(buffer, 0, puzzleChallengeLength + unknownLength1 + unknownLength2 + unknownLength3);
    serverPacketBuffer.flip();

    /* Write data from packet buffer to byte array. */
    this.session.initialServerPacket = new byte[serverPacketBuffer.remaining()];

    serverPacketBuffer.get(this.session.initialServerPacket);

    /* Wrap buffer in order to get values. */
    dataBuffer = ByteBuffer.wrap(buffer, 0, puzzleChallengeLength + unknownLength1 + unknownLength2 + unknownLength3);

    /* Get puzzle denominator and magic. */
    if(dataBuffer.get() == 0x01){
      this.session.puzzleDenominator = dataBuffer.get();
      this.session.puzzleMagic       = dataBuffer.getInt();
    }
    else{
      throw new DespotifyException("Unexpected puzzle challenge.");
    }
  }
View Full Code Here

    byte[] buffer = new byte[512];
    int payloadLength;

    /* Read status and length. */
    if(this.receive(buffer, 2, "authentification status and length") != 2){
      throw new DespotifyException("Failed to read status and length bytes.");
    }

    /* Check status. */
    if(buffer[0] != 0x00){
      throw new DespotifyException("Authentication failed! (Error " + buffer[1] + ")");
    }

    /* Check payload length. AND with 0x00FF so we don't get a negative integer. */
    if((payloadLength = buffer[1] & 0xFF) <= 0){
      throw new DespotifyException("Payload length is negative or zero.");
    }

    /* Read payload. */
    if(this.receive(buffer, payloadLength,"authentification payload") != payloadLength){
      throw new DespotifyException("Failed to read payload.");
    }
  }
View Full Code Here

    try{
      for(int n = payloadLength, r; n > 0 && (r = this.channel.read(buffer)) > 0; n -= r);
    }
    catch(IOException e){
      throw new DespotifyException("Failed to read payload: " + e.getMessage());
    }

    /* Extend it again to payload and mac length. */
    buffer.limit(payloadLength + macLength);

    try{
      for(int n = macLength, r; n > 0 && (r = this.channel.read(buffer)) > 0; n -= r);
    }
    catch(IOException e){
      throw new DespotifyException("Failed to read MAC: " + e.getMessage());
    }

    /* Decrypt payload. */
    this.session.shannonRecv.decrypt(bytes);

 
View Full Code Here

        log.info("sent "+packetDescription+", "+array.length+" bytes:\n" + Hex.log(array, log));
      }

    }
    catch (IOException e){
      throw new DespotifyException("Error writing data to socket: " + e.getMessage());
    }
  }
View Full Code Here

      }

      return b & 0xff;
    }
    catch(IOException e){
      throw new DespotifyException("Error reading data from socket: " + e.getMessage());
    }
  }
View Full Code Here

    try{
      for(int r; n < len && (r = this.channel.read(buffer)) > 0; n += r);
    }
    catch(IOException e){
      throw new DespotifyException("Error reading data from socket: " + e.getMessage());
    }

    if (packetDescription != null && log.isInfoEnabled()) {
      byte[] arr = buffer.array();
      log.info("received " + packetDescription + ", " + n + " bytes:\n" + Hex.log(arr, off, n, log));
View Full Code Here

TOP

Related Classes of se.despotify.exceptions.DespotifyException

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.