net.navasoft.madcoin.backend.services.security.UserDataAccess.java Source code

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.services.security.UserDataAccess.java

Source

/*******************************************************************************
 * Copyright 2014 Juan Diego Navarre Gonzalez
 * 
 * Licensed 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 net.navasoft.madcoin.backend.services.security;

import java.io.IOException;
import java.sql.Types;
import java.util.Properties;

import net.navasoft.madcoin.backend.HardCodedConstants;
import net.navasoft.madcoin.backend.services.controller.exception.AllowedExceptionMessage;
import net.navasoft.madcoin.backend.services.exception.BadConfigException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * net.navasoft.madcoin.backend.services.security Class class UserDataAccess.
 * Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (${authorMail})
 * @version 1.0
 * @since 17/08/2014 06:20:43 PM
 */
@Component(value = "userRepository")
public class UserDataAccess implements UserDetailsService, HardCodedConstants {

    /**
     * data source.
     * 
     * @since 17/08/2014, 06:20:43 PM
     */
    @Autowired
    @Qualifier("endUserLoginAccess")
    private NamedParameterJdbcTemplate dao;

    @Autowired
    @Qualifier("endUsernameMapper")
    private RowMapper<User> nameMapper;

    @Autowired
    @Qualifier("endUsermailMapper")
    private RowMapper<User> mailMapper;

    @Autowired
    @Qualifier("endUserMappedParameter")
    private MapSqlParameterSource mapping;

    private static Properties initializer = new Properties();

    static {
        try {
            initializer.load(AllowedExceptionMessage.class.getResourceAsStream(userProperties));
        } catch (IOException e) {
            throw new BadConfigException("Query is not defined.");
        }
    }

    /**
     * Load user by username.
     * 
     * @param username
     *            the username
     * @return the user details
     * @throws UsernameNotFoundException
     *             the username not found exception
     * @throws DataAccessException
     *             the data access exception
     * @since 17/08/2014, 06:20:43 PM
     */
    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException, DataAccessException, BadConfigException {
        String key = defineKey(username);
        if (initializer.containsKey(key)) {
            try {
                mapping.addValue("username", username, Types.VARCHAR);
                User user = dao.queryForObject(initializer.getProperty(key), mapping, defineMapper(username));
                return user;
            } catch (DataAccessException dao) {
                if (dao instanceof EmptyResultDataAccessException) {
                    throw new UsernameNotFoundException(dao.getMessage());
                } else {
                    dao.printStackTrace();
                    throw dao;
                }
            }
        } else {
            throw new BadConfigException("Query is not defined.");
        }
    }

    private String defineKey(String username) {
        if (!username.contains("@") && (!StringUtils.containsIgnoreCase(username, ".org")
                || !StringUtils.containsIgnoreCase(username, ".net")
                || !StringUtils.containsIgnoreCase(username, ".gov")
                || !StringUtils.endsWithIgnoreCase(username, ".com"))) {
            return AccessProperties.USERNAME_QUERY.getConfigKey();
        } else {
            return AccessProperties.USERMAIL_QUERY.getConfigKey();
        }
    }

    private RowMapper<User> defineMapper(String username) {
        if (!username.contains("@") && (!StringUtils.containsIgnoreCase(username, ".org")
                || !StringUtils.containsIgnoreCase(username, ".net")
                || !StringUtils.containsIgnoreCase(username, ".gov")
                || !StringUtils.endsWithIgnoreCase(username, ".com"))) {
            return nameMapper;
        } else {
            return mailMapper;
        }
    }
}