Java tutorial
/******************************************************************************* * 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 * ProviderDataAccess. Description: * * @author Juan Diego Navarre Gonzalez - (${authorMail}) * @version 1.0 * @since 31/08/2014 07:23:59 PM */ @Component(value = "providerRepository") public class ProviderDataAccess implements UserDetailsService, HardCodedConstants { /** * dao. * * @since 31/08/2014, 07:23:59 PM */ @Autowired @Qualifier("providerLoginAccess") private NamedParameterJdbcTemplate dao; /** * name mapper. * * @since 31/08/2014, 07:23:59 PM */ @Autowired @Qualifier("providernameMapper") private RowMapper<User> nameMapper; /** * mail mapper. * * @since 31/08/2014, 07:23:59 PM */ @Autowired @Qualifier("providermailMapper") private RowMapper<User> mailMapper; /** * mapping. * * @since 31/08/2014, 07:23:59 PM */ @Autowired @Qualifier("providerMappedParameter") private MapSqlParameterSource mapping; /** * initializer. * * @since 31/08/2014, 07:23:59 PM */ private static Properties initializer = new Properties(); static { try { initializer.load(AllowedExceptionMessage.class.getResourceAsStream(providerProperties)); } 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 * @throws BadConfigException * the bad config exception * @since 31/08/2014, 07:23:59 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."); } } /** * Define key. * * @param username * the username * @return the string * @since 31/08/2014, 07:23:59 PM */ 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.PROVIDER_NAME_QUERY.getConfigKey(); } else { return AccessProperties.PROVIDER_MAIL_QUERY.getConfigKey(); } } /** * Define mapper. * * @param username * the username * @return the row mapper * @since 31/08/2014, 07:23:59 PM */ 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; } } }