Ejemplo n.º 1
0
  /**
   * saves a playlist to the database for the current user. outputs a single integer which is the
   * playlist ID if all goes well, otherwise you'll get a description of the problem.
   *
   * @throws IOException
   */
  protected void savePlaylist() throws IOException, SQLException, BadRequestException {

    final Request req = getRequest();
    final User user = getUser();
    final Locale locale = getLocale();
    final String name = req.getUrlParam(2).trim();
    final String[] args = req.getPlayParams(2);

    String result = locale.getString("www.json.error.unknown");

    // make sure data is ok first
    if (name.equals("")) result = locale.getString("www.json.error.noName");
    else if (args.length == 0) result = locale.getString("www.json.error.noArguments");
    else if (user == null) result = locale.getString("www.json.error.notLoggedIn");
    else {

      final Database db = getDatabase();
      final List<Track> vTracks = Track.getTracksFromPlayArgs(db, args);
      final Track[] tracks = new Track[vTracks.size()];

      for (int i = 0; i < vTracks.size(); i++) tracks[i] = vTracks.get(i);

      result = Integer.toString(cm.savePlaylist(name, tracks, user));
    }

    final TString tpl = new TString();
    tpl.setResult(result);
    getResponse().showJson(tpl.makeRenderer());
  }
Ejemplo n.º 2
0
  /**
   * queries audioscrobbler for artists similar to the one specified, and then check against our
   * artists to see which ones we have which are similar
   *
   * @throws BadRequestException
   * @throws SQLException
   * @throws IOException
   */
  protected void similarArtists()
      throws BadRequestException, SQLException, IOException, CacheException {

    final AudioScrobbler scrobbler = new AudioScrobbler(getDatabase(), cache);
    final RelatedArtists related = new RelatedArtists(getDatabase(), scrobbler);
    final Request req = getRequest();
    final int artistId = Integer.parseInt(req.getUrlParam(2));

    showSimilarArtists(related.getRelatedArtistsFor(artistId));
  }
Ejemplo n.º 3
0
  /**
   * this method extracts the full path in the request where the relative path after the json action
   * name is prefixed by the collection path specified in the query string.
   *
   * <p>e.g. /json/action/File/System/Path?collectionId=2
   *
   * <p>Will return /home/rod/File/System/Path because the collection with id = 2 is rooted at
   * /home/rod
   *
   * @return String
   */
  private String getPathFromRequest() throws SQLException, BadRequestException {

    final Request req = getRequest();
    final Locale locale = getLocale();
    final int collectionId = Integer.parseInt(req.getArgument("collectionId"));

    String path = "";
    ResultSet rs = null;
    PreparedStatement st = null;

    for (int i = 2; i < req.getParamCount(); i++) {
      final String pathElement = req.getUrlParam(i);
      // don't allow going up directories
      if (!pathElement.equals("..")) path += "/" + req.getUrlParam(i);
    }

    try {

      final Database db = getDatabase();
      final String sql = " select c.path " + " from collection c " + " where c.id = ? ";
      st = db.prepare(sql);
      st.setInt(1, collectionId);
      rs = st.executeQuery();

      // check the collection exists and we got it's root path
      if (rs.next()) {
        // we need to trim the trailing slash off the collection path
        final String collPath = rs.getString("path");
        path = collPath.substring(0, collPath.length() - 1) + path;
      } else throw new BadRequestException(locale.getString("www.error.invalidCollectionId"), 404);

      path = path.replaceAll("\\/\\/", "\\/");

    } finally {
      Utils.close(rs);
      Utils.close(st);
    }

    log.debug("pathFromRequest: " + path);

    return path;
  }
Ejemplo n.º 4
0
  /**
   * performs a search on the music collection for the specified string and then creates a json
   * results page
   *
   * @throws SQLException
   * @throws IOException
   */
  protected void search() throws SQLException, IOException {

    final MusicSearch musicSearch = new MusicSearch(getDatabase());

    final Request req = getRequest();
    final String query = req.getUrlParam(2);

    final TSearch tpl = new TSearch();
    tpl.setItems(musicSearch.search(query));
    getResponse().showJson(tpl.makeRenderer());
  }
Ejemplo n.º 5
0
  /**
   * handles the json command which generates json documents
   *
   * @param res the response object
   * @param args the command arguments
   * @param user current user
   * @throws SQLException
   * @throws IOException
   * @throws BadRequestException
   */
  public void handleRequest()
      throws SQLException, IOException, BadRequestException, CacheException {

    final Request req = getRequest();
    final String type = req.getUrlParam(1);

    if (type.equals("search")) search();
    else if (type.equals("savePlaylist")) savePlaylist();
    else if (type.equals("deletePlaylist")) deletePlaylist();
    else if (type.equals("folder")) folder();
    else if (type.equals("resolvePath")) resolvePath();
    else if (type.equals("tracksForPath")) tracksForPath();
    else if (type.equals("similarArtists")) similarArtists();
    else if (type.equals("tracks")) tracks();
    else if (type.equals("serverinfo")) serverinfo();
    else throw new BadRequestException("Unknown json request (" + type + ")", 400);
  }
Ejemplo n.º 6
0
  /**
   * tries to delete a users playlist. needs to check things like did they create it, etc... if all
   * goes ok then sends back the ID so that the javascript handler can do whatever...
   *
   * @throws BadRequestException
   * @throws SQLException
   * @throws IOException
   */
  protected void deletePlaylist() throws BadRequestException, SQLException, IOException {

    final Request req = getRequest();
    final User user = getUser();
    final Locale locale = getLocale();

    if (user == null)
      throw new BadRequestException(locale.getString("www.json.error.notLoggedIn"), 403);

    final Database db = getDatabase();
    final int id = Integer.parseInt(req.getUrlParam(2));
    final String sql =
        " select 1 " + " from playlists p " + " where p.id = ? " + " and p.user_id = ? ";

    ResultSet rs = null;
    PreparedStatement st = null;

    try {

      // check user owns playlist before deleting it
      st = db.prepare(sql);
      st.setInt(1, id);
      st.setInt(2, user.getId());
      rs = st.executeQuery();
      if (!rs.next()) throw new BadRequestException("You don't own that playlist", 403);

      cm.removePlaylist(id);

      // done, send success response
      final TString tpl = new TString();
      tpl.setResult(Integer.toString(id));
      getResponse().showJson(tpl.makeRenderer());

    } finally {
      Utils.close(rs);
      Utils.close(st);
    }
  }