Migration of UserSettingsController + unit test.

master
Rémi Cocula 8 years ago
parent 0c265e2f3d
commit f4636a09ed
  1. 62
      libresonic-main/src/main/java/org/libresonic/player/controller/UserSettingsController.java
  2. 12
      libresonic-main/src/main/java/org/libresonic/player/validator/UserSettingsValidator.java
  3. 17
      libresonic-main/src/main/webapp/WEB-INF/libresonic-servlet.xml

@ -30,8 +30,16 @@ import org.libresonic.player.service.TranscodingService;
import org.libresonic.player.util.Util; import org.libresonic.player.util.Util;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.libresonic.player.validator.UserSettingsValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils; import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.SimpleFormController; import org.springframework.web.servlet.mvc.SimpleFormController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -45,14 +53,27 @@ import java.util.List;
* *
* @author Sindre Mehus * @author Sindre Mehus
*/ */
public class UserSettingsController extends SimpleFormController { @Controller
@RequestMapping("/userSettings")
@SessionAttributes(value = "command")
public class UserSettingsController {
@Autowired
private SecurityService securityService; private SecurityService securityService;
@Autowired
private SettingsService settingsService; private SettingsService settingsService;
@Autowired
private TranscodingService transcodingService; private TranscodingService transcodingService;
@Autowired
private UserSettingsValidator userSettingsValidator;
@Override @InitBinder
protected Object formBackingObject(HttpServletRequest request) throws Exception { protected void initBinder(WebDataBinder binder) {
binder.addValidators(userSettingsValidator);
}
@RequestMapping(method = RequestMethod.GET)
protected String formBackingObject(HttpServletRequest request,Model model) throws Exception {
UserSettingsCommand command = new UserSettingsCommand(); UserSettingsCommand command = new UserSettingsCommand();
User user = getUser(request); User user = getUser(request);
@ -77,7 +98,8 @@ public class UserSettingsController extends SimpleFormController {
command.setAllMusicFolders(settingsService.getAllMusicFolders()); command.setAllMusicFolders(settingsService.getAllMusicFolders());
command.setAllowedMusicFolderIds(Util.toIntArray(getAllowedMusicFolderIds(user))); command.setAllowedMusicFolderIds(Util.toIntArray(getAllowedMusicFolderIds(user)));
return command; model.addAttribute("command",command);
return "userSettings";
} }
private User getUser(HttpServletRequest request) throws ServletRequestBindingException { private User getUser(HttpServletRequest request) throws ServletRequestBindingException {
@ -103,18 +125,21 @@ public class UserSettingsController extends SimpleFormController {
return result; return result;
} }
@Override @RequestMapping(method = RequestMethod.POST)
protected void doSubmitAction(Object comm) throws Exception { protected String doSubmitAction(@ModelAttribute("command") @Validated UserSettingsCommand command,final BindingResult binding) throws Exception {
UserSettingsCommand command = (UserSettingsCommand) comm;
if (command.isDeleteUser()) { if (!binding.hasErrors()) {
deleteUser(command); if (command.isDeleteUser()) {
} else if (command.isNewUser()) { deleteUser(command);
createUser(command); } else if (command.isNewUser()) {
} else { createUser(command);
updateUser(command); } else {
updateUser(command);
}
} }
resetCommand(command); resetCommand(command);
return "userSettings";
} }
private void deleteUser(UserSettingsCommand command) { private void deleteUser(UserSettingsCommand command) {
@ -176,15 +201,4 @@ public class UserSettingsController extends SimpleFormController {
command.setReload(true); command.setReload(true);
} }
public void setSecurityService(SecurityService securityService) {
this.securityService = securityService;
}
public void setSettingsService(SettingsService settingsService) {
this.settingsService = settingsService;
}
public void setTranscodingService(TranscodingService transcodingService) {
this.transcodingService = transcodingService;
}
} }

@ -24,6 +24,8 @@ import org.libresonic.player.controller.UserSettingsController;
import org.libresonic.player.service.SecurityService; import org.libresonic.player.service.SecurityService;
import org.libresonic.player.service.SettingsService; import org.libresonic.player.service.SettingsService;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import org.springframework.validation.Validator; import org.springframework.validation.Validator;
@ -32,9 +34,12 @@ import org.springframework.validation.Validator;
* *
* @author Sindre Mehus * @author Sindre Mehus
*/ */
@Component
public class UserSettingsValidator implements Validator { public class UserSettingsValidator implements Validator {
@Autowired
private SecurityService securityService; private SecurityService securityService;
@Autowired
private SettingsService settingsService; private SettingsService settingsService;
/** /**
@ -82,11 +87,4 @@ public class UserSettingsValidator implements Validator {
} }
public void setSecurityService(SecurityService securityService) {
this.securityService = securityService;
}
public void setSettingsService(SettingsService settingsService) {
this.settingsService = settingsService;
}
} }

@ -13,7 +13,7 @@
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven /> <mvc:annotation-driven />
<context:component-scan base-package="org.libresonic.player.controller"/> <context:component-scan base-package="org.libresonic.player.controller, org.libresonic.player.validator"/>
<bean id="leftController" class="org.libresonic.player.controller.LeftController"> <bean id="leftController" class="org.libresonic.player.controller.LeftController">
<property name="viewName" value="left"/> <property name="viewName" value="left"/>
@ -203,16 +203,6 @@
<property name="settingsService" ref="settingsService"/> <property name="settingsService" ref="settingsService"/>
<property name="securityService" ref="securityService"/> <property name="securityService" ref="securityService"/>
</bean> </bean>
<bean id="userSettingsController" class="org.libresonic.player.controller.UserSettingsController">
<property name="sessionForm" value="true"/>
<property name="commandClass" value="org.libresonic.player.command.UserSettingsCommand"/>
<property name="validator" ref="userSettingsValidator"/>
<property name="successView" value="userSettings"/>
<property name="formView" value="userSettings"/>
<property name="securityService" ref="securityService"/>
<property name="settingsService" ref="settingsService"/>
<property name="transcodingService" ref="transcodingService"/>
</bean>
<bean id="passwordSettingsController" class="org.libresonic.player.controller.PasswordSettingsController"> <bean id="passwordSettingsController" class="org.libresonic.player.controller.PasswordSettingsController">
<property name="sessionForm" value="true"/> <property name="sessionForm" value="true"/>
<property name="commandClass" value="org.libresonic.player.command.PasswordSettingsCommand"/> <property name="commandClass" value="org.libresonic.player.command.PasswordSettingsCommand"/>
@ -364,10 +354,6 @@
<bean id="premiumSettingsValidator" class="org.libresonic.player.validator.PremiumSettingsValidator"> <bean id="premiumSettingsValidator" class="org.libresonic.player.validator.PremiumSettingsValidator">
<property name="settingsService" ref="settingsService"/> <property name="settingsService" ref="settingsService"/>
</bean> </bean>
<bean id="userSettingsValidator" class="org.libresonic.player.validator.UserSettingsValidator">
<property name="securityService" ref="securityService"/>
<property name="settingsService" ref="settingsService"/>
</bean>
<bean id="passwordSettingsValidator" class="org.libresonic.player.validator.PasswordSettingsValidator"/> <bean id="passwordSettingsValidator" class="org.libresonic.player.validator.PasswordSettingsValidator"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
@ -412,7 +398,6 @@
<prop key="/internetRadioSettings.view">internetRadioSettingsController</prop> <prop key="/internetRadioSettings.view">internetRadioSettingsController</prop>
<prop key="/personalSettings.view">personalSettingsController</prop> <prop key="/personalSettings.view">personalSettingsController</prop>
<prop key="/avatarUpload.view">avatarUploadController</prop> <prop key="/avatarUpload.view">avatarUploadController</prop>
<prop key="/userSettings.view">userSettingsController</prop>
<prop key="/passwordSettings.view">passwordSettingsController</prop> <prop key="/passwordSettings.view">passwordSettingsController</prop>
<prop key="/premiumSettings.view">premiumSettingsController</prop> <prop key="/premiumSettings.view">premiumSettingsController</prop>
<prop key="/allmusic.view">allmusicController</prop> <prop key="/allmusic.view">allmusicController</prop>

Loading…
Cancel
Save