From 5b8e2b94c15012f57295e7fd401506efea021e0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Cocula?= Date: Thu, 15 Dec 2016 23:06:05 +0100 Subject: [PATCH] Controllers migration. --- .../player/controller/CoverArtController.java | 117 ++++++------------ .../player/controller/MainController.java | 98 +++++---------- .../MusicFolderSettingsController.java | 7 +- .../player/controller/SettingsController.java | 34 ++--- .../resources/applicationContext-security.xml | 52 ++++---- .../src/main/resources/libresonic-servlet.xml | 23 ---- 6 files changed, 120 insertions(+), 211 deletions(-) diff --git a/libresonic-main/src/main/java/org/libresonic/player/controller/CoverArtController.java b/libresonic-main/src/main/java/org/libresonic/player/controller/CoverArtController.java index 239bc225..7e8d59ab 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/controller/CoverArtController.java +++ b/libresonic-main/src/main/java/org/libresonic/player/controller/CoverArtController.java @@ -19,62 +19,45 @@ */ package org.libresonic.player.controller; -import java.awt.Color; -import java.awt.Font; -import java.awt.GradientPaint; -import java.awt.Graphics2D; -import java.awt.RenderingHints; -import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.Semaphore; - -import javax.imageio.ImageIO; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; -import org.springframework.web.bind.ServletRequestUtils; -import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.servlet.mvc.Controller; -import org.springframework.web.servlet.mvc.LastModified; - import org.libresonic.player.Logger; import org.libresonic.player.dao.AlbumDao; import org.libresonic.player.dao.ArtistDao; -import org.libresonic.player.domain.Album; -import org.libresonic.player.domain.Artist; -import org.libresonic.player.domain.CoverArtScheme; -import org.libresonic.player.domain.MediaFile; -import org.libresonic.player.domain.Playlist; -import org.libresonic.player.domain.PodcastChannel; -import org.libresonic.player.domain.Transcoding; -import org.libresonic.player.domain.VideoTranscodingSettings; -import org.libresonic.player.service.MediaFileService; -import org.libresonic.player.service.PlaylistService; -import org.libresonic.player.service.PodcastService; -import org.libresonic.player.service.SettingsService; -import org.libresonic.player.service.TranscodingService; +import org.libresonic.player.domain.*; +import org.libresonic.player.service.*; import org.libresonic.player.service.metadata.JaudiotaggerParser; import org.libresonic.player.util.StringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.ServletRequestUtils; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.LastModified; + +import javax.annotation.PostConstruct; +import javax.imageio.ImageIO; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.*; +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.Semaphore; /** * Controller which produces cover art images. * * @author Sindre Mehus */ -public class CoverArtController implements Controller, LastModified { +@Controller +@RequestMapping("/coverArt") +public class CoverArtController implements LastModified { public static final String ALBUM_COVERART_PREFIX = "al-"; public static final String ARTIST_COVERART_PREFIX = "ar-"; @@ -83,26 +66,34 @@ public class CoverArtController implements Controller, LastModified { private static final Logger LOG = Logger.getLogger(CoverArtController.class); + @Autowired private MediaFileService mediaFileService; + @Autowired private TranscodingService transcodingService; + @Autowired private SettingsService settingsService; + @Autowired private PlaylistService playlistService; + @Autowired private PodcastService podcastService; + @Autowired private ArtistDao artistDao; + @Autowired private AlbumDao albumDao; private Semaphore semaphore; + @PostConstruct public void init() { semaphore = new Semaphore(settingsService.getCoverArtConcurrency()); } public long getLastModified(HttpServletRequest request) { CoverArtRequest coverArtRequest = createCoverArtRequest(request); - long result = coverArtRequest.lastModified(); -// LOG.info("getLastModified - " + coverArtRequest + ": " + new Date(result)); - return result; + // LOG.info("getLastModified - " + coverArtRequest + ": " + new Date(result)); + return coverArtRequest.lastModified(); } + @RequestMapping(method = RequestMethod.GET) public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { CoverArtRequest coverArtRequest = createCoverArtRequest(request); @@ -341,33 +332,6 @@ public class CoverArtController implements Controller, LastModified { return thumb; } - public void setMediaFileService(MediaFileService mediaFileService) { - this.mediaFileService = mediaFileService; - } - - public void setArtistDao(ArtistDao artistDao) { - this.artistDao = artistDao; - } - - public void setAlbumDao(AlbumDao albumDao) { - this.albumDao = albumDao; - } - - public void setTranscodingService(TranscodingService transcodingService) { - this.transcodingService = transcodingService; - } - - public void setSettingsService(SettingsService settingsService) { - this.settingsService = settingsService; - } - - public void setPlaylistService(PlaylistService playlistService) { - this.playlistService = playlistService; - } - - public void setPodcastService(PodcastService podcastService) { - this.podcastService = podcastService; - } private abstract class CoverArtRequest { @@ -551,7 +515,7 @@ public class CoverArtController implements Controller, LastModified { albums.add(album); } } - return new ArrayList(albums); + return new ArrayList<>(albums); } } @@ -559,7 +523,7 @@ public class CoverArtController implements Controller, LastModified { private final PodcastChannel channel; - public PodcastCoverArtRequest(PodcastChannel channel) { + PodcastCoverArtRequest(PodcastChannel channel) { this.channel = channel; } @@ -633,7 +597,8 @@ public class CoverArtController implements Controller, LastModified { @Override public BufferedImage createImage(int size) { - int height = size; + int height; + height = size; int width = height * 16 / 9; InputStream in = null; try { @@ -687,7 +652,7 @@ public class CoverArtController implements Controller, LastModified { private final int height; private final Color color; - public AutoCover(Graphics2D graphics, String key, String artist, String album, int width, int height) { + AutoCover(Graphics2D graphics, String key, String artist, String album, int width, int height) { this.graphics = graphics; this.artist = artist; this.album = album; diff --git a/libresonic-main/src/main/java/org/libresonic/player/controller/MainController.java b/libresonic-main/src/main/java/org/libresonic/player/controller/MainController.java index 68217c85..29511e23 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/controller/MainController.java +++ b/libresonic-main/src/main/java/org/libresonic/player/controller/MainController.java @@ -19,54 +19,47 @@ */ package org.libresonic.player.controller; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.SortedSet; -import java.util.TreeSet; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - +import org.libresonic.player.domain.*; +import org.libresonic.player.service.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.ServletRequestUtils; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.servlet.mvc.AbstractController; import org.springframework.web.servlet.view.RedirectView; -import org.libresonic.player.domain.CoverArtScheme; -import org.libresonic.player.domain.MediaFile; -import org.libresonic.player.domain.MediaFileComparator; -import org.libresonic.player.domain.MusicFolder; -import org.libresonic.player.domain.Player; -import org.libresonic.player.domain.UserSettings; -import org.libresonic.player.service.AdService; -import org.libresonic.player.service.MediaFileService; -import org.libresonic.player.service.PlayerService; -import org.libresonic.player.service.RatingService; -import org.libresonic.player.service.SecurityService; -import org.libresonic.player.service.SettingsService; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.*; +import java.util.stream.Collectors; /** * Controller for the main page. * * @author Sindre Mehus */ -public class MainController extends AbstractController { +@Controller +@RequestMapping("/main") +public class MainController { + @Autowired private SecurityService securityService; + @Autowired private PlayerService playerService; + @Autowired private SettingsService settingsService; + @Autowired private RatingService ratingService; + @Autowired private MediaFileService mediaFileService; + @Autowired private AdService adService; - @Override + @RequestMapping(method = RequestMethod.GET) protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { - Map map = new HashMap(); + Map map = new HashMap<>(); Player player = playerService.getPlayer(request, response); List mediaFiles = getMediaFiles(request); @@ -91,8 +84,8 @@ public class MainController extends AbstractController { } List children = mediaFiles.size() == 1 ? mediaFileService.getChildrenOf(dir, true, true, true) : getMultiFolderChildren(mediaFiles); - List files = new ArrayList(); - List subDirs = new ArrayList(); + List files = new ArrayList<>(); + List subDirs = new ArrayList<>(); for (MediaFile child : children) { if (child.isFile()) { files.add(child); @@ -159,9 +152,7 @@ public class MainController extends AbstractController { view = "artistMain"; } - ModelAndView result = new ModelAndView(view); - result.addObject("model", map); - return result; + return new ModelAndView(view,"model",map); } private boolean isViewAsList(HttpServletRequest request, UserSettings userSettings) { @@ -188,7 +179,7 @@ public class MainController extends AbstractController { } private List getMediaFiles(HttpServletRequest request) { - List mediaFiles = new ArrayList(); + List mediaFiles = new ArrayList<>(); for (String path : ServletRequestUtils.getStringParameters(request, "path")) { MediaFile mediaFile = mediaFileService.getMediaFile(path); if (mediaFile != null) { @@ -223,18 +214,18 @@ public class MainController extends AbstractController { } private List getMultiFolderChildren(List mediaFiles) throws IOException { - SortedSet result = new TreeSet(new MediaFileComparator(settingsService.isSortAlbumsByYear())); + SortedSet result = new TreeSet<>(new MediaFileComparator(settingsService.isSortAlbumsByYear())); for (MediaFile mediaFile : mediaFiles) { if (mediaFile.isFile()) { mediaFile = mediaFileService.getParentOf(mediaFile); } result.addAll(mediaFileService.getChildrenOf(mediaFile, true, true, true)); } - return new ArrayList(result); + return new ArrayList<>(result); } private List getAncestors(MediaFile dir) throws IOException { - LinkedList result = new LinkedList(); + LinkedList result = new LinkedList<>(); try { MediaFile parent = mediaFileService.getParentOf(dir); @@ -249,41 +240,14 @@ public class MainController extends AbstractController { } private List getSieblingAlbums(MediaFile dir) { - List result = new ArrayList(); + List result = new ArrayList<>(); MediaFile parent = mediaFileService.getParentOf(dir); if (!mediaFileService.isRoot(parent)) { List sieblings = mediaFileService.getChildrenOf(parent, false, true, true); - for (MediaFile siebling : sieblings) { - if (siebling.isAlbum() && !siebling.equals(dir)) { - result.add(siebling); - } - } + result.addAll(sieblings.stream().filter(siebling -> siebling.isAlbum() && !siebling.equals(dir)).collect(Collectors.toList())); } return result; } - public void setSecurityService(SecurityService securityService) { - this.securityService = securityService; - } - - public void setPlayerService(PlayerService playerService) { - this.playerService = playerService; - } - - public void setSettingsService(SettingsService settingsService) { - this.settingsService = settingsService; - } - - public void setRatingService(RatingService ratingService) { - this.ratingService = ratingService; - } - - public void setAdService(AdService adService) { - this.adService = adService; - } - - public void setMediaFileService(MediaFileService mediaFileService) { - this.mediaFileService = mediaFileService; - } } diff --git a/libresonic-main/src/main/java/org/libresonic/player/controller/MusicFolderSettingsController.java b/libresonic-main/src/main/java/org/libresonic/player/controller/MusicFolderSettingsController.java index 47a66562..96bebde1 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/controller/MusicFolderSettingsController.java +++ b/libresonic-main/src/main/java/org/libresonic/player/controller/MusicFolderSettingsController.java @@ -36,6 +36,7 @@ import org.springframework.web.bind.annotation.RequestParam; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * Controller for the page used to administrate the set of music folders. @@ -97,11 +98,7 @@ public class MusicFolderSettingsController { } private List wrap(List musicFolders) { - ArrayList result = new ArrayList(); - for (MusicFolder musicFolder : musicFolders) { - result.add(new MusicFolderSettingsCommand.MusicFolderInfo(musicFolder)); - } - return result; + return musicFolders.stream().map(MusicFolderSettingsCommand.MusicFolderInfo::new).collect(Collectors.toCollection(ArrayList::new)); } @RequestMapping(method = RequestMethod.POST) diff --git a/libresonic-main/src/main/java/org/libresonic/player/controller/SettingsController.java b/libresonic-main/src/main/java/org/libresonic/player/controller/SettingsController.java index ce4910bf..647d3145 100644 --- a/libresonic-main/src/main/java/org/libresonic/player/controller/SettingsController.java +++ b/libresonic-main/src/main/java/org/libresonic/player/controller/SettingsController.java @@ -19,35 +19,41 @@ */ package org.libresonic.player.controller; -import org.libresonic.player.domain.*; -import org.libresonic.player.service.*; -import org.springframework.web.servlet.*; -import org.springframework.web.servlet.view.*; -import org.springframework.web.servlet.mvc.*; - -import javax.servlet.http.*; +import org.libresonic.player.domain.User; +import org.libresonic.player.service.SecurityService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.AbstractController; +import org.springframework.web.servlet.view.RedirectView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; /** * Controller for the main settings page. * * @author Sindre Mehus */ -public class SettingsController extends AbstractController { +@Controller +@RequestMapping("/settings") +public class SettingsController { + @Autowired private SecurityService securityService; - - protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { +@RequestMapping(method = RequestMethod.GET) + protected ModelAndView handleRequestInternal(HttpServletRequest request) throws Exception { User user = securityService.getCurrentUser(request); // Redirect to music folder settings if admin. - String view = user.isAdminRole() ? "musicFolderSettings.view" : "personalSettings.view"; + String view = user.isAdminRole() ? "musicFolderSettings" : "personalSettings"; return new ModelAndView(new RedirectView(view)); } - public void setSecurityService(SecurityService securityService) { - this.securityService = securityService; - } } diff --git a/libresonic-main/src/main/resources/applicationContext-security.xml b/libresonic-main/src/main/resources/applicationContext-security.xml index d5b193ab..442c9c9c 100644 --- a/libresonic-main/src/main/resources/applicationContext-security.xml +++ b/libresonic-main/src/main/resources/applicationContext-security.xml @@ -29,35 +29,35 @@ - - - - - + + + + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - - - - - - + + + + + + + + + diff --git a/libresonic-main/src/main/resources/libresonic-servlet.xml b/libresonic-main/src/main/resources/libresonic-servlet.xml index d056d2e7..a61f71e5 100644 --- a/libresonic-main/src/main/resources/libresonic-servlet.xml +++ b/libresonic-main/src/main/resources/libresonic-servlet.xml @@ -19,14 +19,6 @@ - - - - - - - - @@ -137,9 +129,6 @@ - - - @@ -177,15 +166,6 @@ - - - - - - - - - @@ -294,7 +274,6 @@ - mainController playlistController playlistsController helpController @@ -315,7 +294,6 @@ multiController videoPlayerController starredController - settingsController dlnaSettingsController sonosSettingsController shareSettingsController @@ -324,7 +302,6 @@ avatarUploadController allmusicController editTagsController - coverArtController avatarController proxyController statusChartController