me.bulat.jivr.webmin.web.root.AppRootController.java Source code

Java tutorial

Introduction

Here is the source code for me.bulat.jivr.webmin.web.root.AppRootController.java

Source

/*
 * Licensed to JIVR under one or more contributor
 *  license agreements. See the NOTICE file distributed with
 *  this work for additional information regarding copyright
 *  ownership. JIVR licenses this file to you under
 *  the Apache License, Version 2.0 (the "License"); you may
 *  not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

package me.bulat.jivr.webmin.web.root;

import me.bulat.jivr.webmin.data.model.user.Role;
import me.bulat.jivr.webmin.data.model.user.User;
import me.bulat.jivr.webmin.data.service.login.LoginService;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Alex Bogatikov
 *         Created on 25/08/16.
 */
@Controller
public class AppRootController {

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = { "/" }, method = { RequestMethod.GET })
    public ModelAndView welcomePage() {
        ModelAndView model = new ModelAndView();
        model.addObject("title", "Spring Security Tutorial");
        model.addObject("message", "Welcome Page !");
        model.setViewName("/welcome");
        return model;
    }

    @RequestMapping(value = { "/login" }, method = { RequestMethod.GET })
    public ModelAndView loginPage() {
        ModelAndView model = new ModelAndView();
        model.addObject("title", "Jivr Web Console Login page");
        model.addObject("message", "Login Page!");
        model.setViewName("form/login");
        return model;
    }

    @RequestMapping(value = { "/logout" }, method = { RequestMethod.GET })
    public ModelAndView logoutPage() {
        ModelAndView model = new ModelAndView();
        model.addObject("title", "Jivr Web Console Logout page");
        model.addObject("message", "Logout Page!");
        model.setViewName("form/logout");
        return model;
    }

    @RequestMapping(value = { "/registration" }, method = { RequestMethod.GET })
    public ModelAndView registerPage() {
        ModelAndView model = new ModelAndView();
        model.addObject("title", "Jivr Web Console Registration page");
        model.addObject("message", "Registration Page!");
        model.addObject("registerForm", new UserForm());
        model.setViewName("form/registration");
        return model;
    }

    @RequestMapping(value = { "/success" }, method = { RequestMethod.GET })
    public ModelAndView successRegistration() {
        ModelAndView model = new ModelAndView();
        model.addObject("title", "Jivr Web Console Registration page");
        model.addObject("message", "Registration Success!");
        model.setViewName("form/success");
        return model;
    }

    @RequestMapping(value = { "/exist" }, method = { RequestMethod.GET })
    public ModelAndView usernameExists() {
        ModelAndView model = new ModelAndView();
        model.addObject("title", "Jivr Web Console Registration page");
        model.addObject("message", "Registration unsuccess!");
        model.setViewName("form/exist");
        return model;
    }

    /**
     * Page with incorrect describe.
     * @return page model.
     */
    @RequestMapping(value = { "/incorrect" }, method = { RequestMethod.GET })
    public ModelAndView incorrectInput() {
        ModelAndView model = new ModelAndView();
        model.addObject("title", "Jivr Web Console Registration page");
        model.addObject("message", "Registration unsuccess!");
        model.setViewName("form/incorrect");
        return model;
    }

    /**
     * Create new user by himself.
     *
     * @param userForm user form entered by user.
     * @param binding request.
     * @return redirect page.
     */
    @RequestMapping(value = { "/registration" }, method = { RequestMethod.POST })
    public String registerNewUser(@Valid @ModelAttribute("registerForm") UserForm userForm, BindingResult binding) {

        if (binding.hasErrors()) {
            return "redirect:/incorrect";
        }

        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        User user = new User();
        user.setEmail(userForm.getEmail());
        user.setPassword(passwordEncoder.encode(userForm.getPassword()));
        user.setUserName(userForm.getUsername());
        user.setEnabled(true);
        Role role = new Role();
        role.setRole("ROLE_USER");
        List<Role> roles = new ArrayList<>();
        roles.add(role);
        user.setRoles(roles);
        loginService.saveUser(user);
        return "redirect:/success";
    }

    @ExceptionHandler({ SQLException.class, ConstraintViolationException.class })
    public String addUserToDBError() {
        return "redirect:/exist";
    }
}