Example usage for org.springframework.web.bind ServletRequestUtils getRequiredFloatParameter

List of usage examples for org.springframework.web.bind ServletRequestUtils getRequiredFloatParameter

Introduction

In this page you can find the example usage for org.springframework.web.bind ServletRequestUtils getRequiredFloatParameter.

Prototype

public static float getRequiredFloatParameter(ServletRequest request, String name)
        throws ServletRequestBindingException 

Source Link

Document

Get a float parameter, throwing an exception if it isn't found or isn't a number.

Usage

From source file:net.sourceforge.subsonic.controller.RESTController.java

public void jukeboxControl(HttpServletRequest request, HttpServletResponse response) throws Exception {
    request = wrapRequest(request, true);

    User user = securityService.getCurrentUser(request);
    if (!user.isJukeboxRole()) {
        error(request, response, ErrorCode.NOT_AUTHORIZED,
                user.getUsername() + " is not authorized to use jukebox.");
        return;// w w  w.  j a  v  a  2s  .  c om
    }

    try {
        boolean returnPlaylist = false;
        String action = ServletRequestUtils.getRequiredStringParameter(request, "action");
        if ("start".equals(action)) {
            playlistControlService.doStart(request, response);
        } else if ("stop".equals(action)) {
            playlistControlService.doStop(request, response);
        } else if ("skip".equals(action)) {
            int index = ServletRequestUtils.getRequiredIntParameter(request, "index");
            int offset = ServletRequestUtils.getIntParameter(request, "offset", 0);
            playlistControlService.doSkip(request, response, index, offset);
        } else if ("add".equals(action)) {
            int[] ids = ServletRequestUtils.getIntParameters(request, "id");
            List<String> paths = new ArrayList<String>(ids.length);
            for (int id : ids) {
                paths.add(mediaFileService.getMediaFile(id).getPath());
            }
            playlistControlService.doAdd(request, response, paths);
        } else if ("set".equals(action)) {
            int[] ids = ServletRequestUtils.getIntParameters(request, "id");
            List<String> paths = new ArrayList<String>(ids.length);
            for (int id : ids) {
                paths.add(mediaFileService.getMediaFile(id).getPath());
            }
            playlistControlService.doSet(request, response, paths);
        } else if ("clear".equals(action)) {
            playlistControlService.doClear(request, response);
        } else if ("remove".equals(action)) {
            int index = ServletRequestUtils.getRequiredIntParameter(request, "index");
            playlistControlService.doRemove(request, response, index);
        } else if ("shuffle".equals(action)) {
            playlistControlService.doShuffle(request, response);
        } else if ("setGain".equals(action)) {
            float gain = ServletRequestUtils.getRequiredFloatParameter(request, "gain");
            jukeboxService.setGain(gain);
        } else if ("get".equals(action)) {
            returnPlaylist = true;
        } else if ("status".equals(action)) {
            // No action necessary.
        } else {
            throw new Exception("Unknown jukebox action: '" + action + "'.");
        }

        XMLBuilder builder = createXMLBuilder(request, response, true);

        Player player = playerService.getPlayer(request, response);
        Player jukeboxPlayer = jukeboxService.getPlayer();
        boolean controlsJukebox = jukeboxPlayer != null && jukeboxPlayer.getId().equals(player.getId());
        Playlist playlist = player.getPlaylist();

        List<Attribute> attrs = new ArrayList<Attribute>(Arrays.asList(
                new Attribute("currentIndex",
                        controlsJukebox && !playlist.isEmpty() ? playlist.getIndex() : -1),
                new Attribute("playing",
                        controlsJukebox && !playlist.isEmpty()
                                && playlist.getStatus() == Playlist.Status.PLAYING),
                new Attribute("gain", jukeboxService.getGain()), new Attribute("position",
                        controlsJukebox && !playlist.isEmpty() ? jukeboxService.getPosition() : 0)));

        if (returnPlaylist) {
            builder.add("jukeboxPlaylist", attrs, false);
            List<MediaFile> result;
            synchronized (playlist) {
                result = playlist.getFiles();
            }
            for (MediaFile mediaFile : result) {
                AttributeSet attributes = createAttributesForMediaFile(player, mediaFile);
                builder.add("entry", attributes, true);
            }
        } else {
            builder.add("jukeboxStatus", attrs, false);
        }

        builder.endAll();
        response.getWriter().print(builder);

    } catch (ServletRequestBindingException x) {
        error(request, response, ErrorCode.MISSING_PARAMETER, getErrorMessage(x));
    } catch (Exception x) {
        LOG.warn("Error in REST API.", x);
        error(request, response, ErrorCode.GENERIC, getErrorMessage(x));
    }
}