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

Java tutorial

Introduction

Here is the source code for net.navasoft.madcoin.backend.services.security.UserEmailMapper.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.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;

/**
 * net.navasoft.madcoin.backend.services.security Class class UserMapper.
 * Description:
 * 
 * @author Juan Diego Navarre Gonzalez - (${authorMail})
 * @version 1.0
 * @since 17/08/2014 06:20:43 PM
 */
@Component("endUsermailMapper")
public class UserEmailMapper implements ParameterizedRowMapper<User>, HardCodedConstants {

    /**
     * initializer.
     * 
     * @since 18/08/2014, 04:36:37 PM
     */
    private static Properties initializer = new Properties();

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

    @Autowired
    @Qualifier("authorityMapper")
    private RowMapper<SimpleGrantedAuthority> authorityMapper;

    @Autowired
    @Qualifier("endUserLoginAccess")
    private NamedParameterJdbcTemplate dao;

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

    /**
     * Map row.
     * 
     * @param rs
     *            the rs
     * @param arg1
     *            the arg1
     * @return the user
     * @throws SQLException
     *             the SQL exception
     * @since 17/08/2014, 06:20:43 PM
     */
    @Override
    public User mapRow(ResultSet rs, int arg1) throws SQLException {
        String mail = rs.getString(USER_COLUMN);
        return new User(mail, rs.getString(PASSWORD_COLUMN), rs.getBoolean(ENABLED_COLUMN), true, true, true,
                getAuthorities(mail));
    }

    /**
     * Gets the authorities.
     * 
     * @param username
     *            the username
     * @return the authorities
     * @since 17/08/2014, 06:20:43 PM
     */
    private Collection<? extends GrantedAuthority> getAuthorities(String username) {
        String key = AccessProperties.AUTHORITY_QUERY.getConfigKey();
        if (initializer.containsKey(key)) {
            mapping.addValue("username", username, Types.VARCHAR);
            return dao.query(initializer.getProperty(key), mapping, authorityMapper);
        } else {
            throw new BadConfigException("Query is not defined.");
        }
    }
}