com.nkapps.billing.controllers.AuthController.java Source code

Java tutorial

Introduction

Here is the source code for com.nkapps.billing.controllers.AuthController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nkapps.billing.controllers;

import java.util.HashMap;
import java.util.LinkedHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.nkapps.billing.services.AuthService;
import com.nkapps.billing.services.AuthenticationService;

/**
 *
 * @author nuraddin
 */
@Controller
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private AuthenticationService authenticationService;

    @Autowired
    private MessageSource messageSource;

    private Logger logger = LoggerFactory.getLogger(AuthController.class);

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(ModelMap map, HttpServletRequest request) {
        authService.signOut(request);
        return "redirect:/auth/login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginGet(ModelMap map, HttpServletRequest request) {
        authService.signOut(request);
        return "auth/login";
    }

    @RequestMapping(value = "/nonce", method = RequestMethod.POST)
    public @ResponseBody HashMap<String, Object> nonce(HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> resMap = new LinkedHashMap<>();

        String serialNumber = request.getParameter("serialNumber");

        if (serialNumber == null || serialNumber.trim().isEmpty()) {
            resMap.put("success", false);
            resMap.put("reason",
                    messageSource.getMessage("auth.serial_number_invalid", null, LocaleContextHolder.getLocale()));

        } else {
            String nonce = authenticationService.getNonce(serialNumber, request.getRemoteAddr());
            resMap.put("success", true);
            resMap.put("nonce", nonce);
        }
        return resMap;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String loginPost(ModelMap map, HttpServletRequest request) {
        try {
            authService.signIn(request);
        } catch (Exception e) {
            logger.error(e.getMessage());
            map.put("errorMessage", e.getMessage().replace("\n", ";").replace("\r", ";"));
            return "/auth/login";
        }

        return "redirect:/bank-statement/index";
    }
}