Java tutorial
/* * Copyright 2014 the original author or authors. * * 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 com.baidu.cc.web.rpc; import com.baidu.bjf.remoting.mcpack.annotation.McpackRpcService; import com.baidu.cc.configuration.bo.ConfigGroup; import com.baidu.cc.configuration.bo.ConfigItem; import com.baidu.cc.configuration.bo.Environment; import com.baidu.cc.configuration.bo.Project; import com.baidu.cc.configuration.bo.User; import com.baidu.cc.configuration.bo.Version; import com.baidu.cc.configuration.service.AccessSettingService; import com.baidu.cc.configuration.service.ConfigGroupService; import com.baidu.cc.configuration.service.ConfigItemService; import com.baidu.cc.configuration.service.EnvironmentService; import com.baidu.cc.configuration.service.ProjectService; import com.baidu.cc.configuration.service.UserService; import com.baidu.cc.configuration.service.VersionService; import com.baidu.cc.interfaces.ConfigServerService; import com.baidu.cc.interfaces.ExtConfigServerService; import com.baidu.rigel.platform.util.Security; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; import org.springframework.stereotype.Service; /** * A portal {@link ConfigServerService} implementor. * * * @author xiemalin * @since 1.0.0.0 */ @Service @McpackRpcService(serviceInterface = ExtConfigServerService.class) public class ConfigServerServiceImpl implements ExtConfigServerService { /** Logger for this class. */ private static final Logger LOGGER = Logger.getLogger(ConfigServerServiceImpl.class); /** passowd encrypte action class. */ StandardPBEStringEncryptor encryptor; /** * {@link UserService} instance. */ @Resource(name = "userService") private UserService userService; /** * {@link EnvironmentService} instance. */ @Resource(name = "environmentService") private EnvironmentService environmentService; /** * {@link ProjectService} instance. */ @Resource(name = "projectService") private ProjectService projectService; /** * {@link ConfigItemService} instance. */ @Resource(name = "configItemService") private ConfigItemService configItemService; /** * {@link VersionService} instance. */ @Resource(name = "versionService") private VersionService versionService; /** * {@link AccessSettingService} instance. */ @Resource(name = "accessSettingService") private AccessSettingService accessSettingService; /** * {@link ConfigGroupService} instance. */ @Resource(name = "configGroupService") private ConfigGroupService configGroupService; /** * default constructor. create {@link StandardPBEStringEncryptor} instance. */ public ConfigServerServiceImpl() { encryptor = new StandardPBEStringEncryptor(); ; encryptor.setPassword(ExtConfigServerService.class.getSimpleName()); } /** * To authenticate by user and password. if authentication failed throw<br> * {@link IllegalAccessError} exception. * * @param user * user name * @param password * password * @return {@link User} object * @throws IllegalAccessError * if authentication failed. */ protected User authenticate(String user, String password) throws IllegalAccessError { User u = userService.getByName(user); if (u == null) { throw new IllegalAccessError("User '" + user + " not exist."); } String passwordToCheck = u.getApiPassword(); if (passwordToCheck == null) { if (password == null) { return u; } else { throw new IllegalAccessError("User '" + user + "' authenticate failed."); } } String plain = encryptor.decrypt(password); String md5 = Security.MD5Encode(plain); boolean result = u.getApiPassword().equals(md5); if (!result) { throw new IllegalAccessError("User '" + user + "' authenticate failed."); } return u; } /** * To authorize by user and env id. * * @param u * {@link User} object * @param envId * environment id */ protected void authorizeEnv(User u, long envId) { boolean authorization = accessSettingService.checkAuth(u.getId(), AccessSettingService.Auth_TYPE_ENV, envId); if (!authorization) { String msg = "No access allowed for user '" + u.getName() + "' to envid=" + envId; if (LOGGER.isInfoEnabled()) { LOGGER.info(msg); } throw new IllegalAccessError(msg); } } /** * To authorize by user and project id. * * @param u * {@link User} object * @param projectId * project id */ protected void authorizeProject(User u, long projectId) { boolean authorization = accessSettingService.checkAuth(u.getId(), AccessSettingService.Auth_TYPE_PROJECT, projectId); if (!authorization) { String msg = "No access allowed for user '" + u.getName() + "' to projectId=" + projectId; if (LOGGER.isInfoEnabled()) { LOGGER.info(msg); } throw new IllegalAccessError(msg); } } /** * ???? ???? IllegalAccessError. * * @param user * ?? * @param password * ? * @param version * ? * @return Map<String,String> String.tag */ @Override public Map<String, String> getConfigItems(String user, String password, Long version) { User u = authenticate(user, password); Version v = versionService.findById(version); if (v == null) { throw new IllegalAccessError("No version id '" + version + "' found."); } authorizeProject(u, v.getProjectId()); Map<String, String> items = getConfigItems(version); // add tag items.put(TAG_KEY, v.getCheckSum()); return items; } /** * ????. * * @param version * ? * @return String.tag */ private Map<String, String> getConfigItems(Long version) { List<ConfigItem> items = configItemService.findByVersionId(version, true); Map<String, String> ret = new HashMap<String, String>(); if (CollectionUtils.isEmpty(items)) { return ret; } for (ConfigItem configItem : items) { ret.put(configItem.getName(), configItem.getVal()); } return ret; } /** * ??id?? ???? IllegalAccessError. * * @param user * ?? * @param password * ? * @param envId * ?id * @return Map<String,String> String tag */ @Override public Map<String, String> getLastestConfigItems(String user, String password, Long envId) { User u = authenticate(user, password); // should check authorization here by project authorizeEnv(u, envId); // ?envId?version id(version id) Version version = getLastestConfigVersion(envId); if (version == null) { throw new IllegalAccessError("No version under environemt id: " + envId); } Map<String, String> items = getConfigItems(version.getId()); // add tag items.put(TAG_KEY, version.getCheckSum()); return items; } /** * ?envId?version id(version id). * * @param envId * ?id * @return ?null?? */ private Version getLastestConfigVersion(Long envId) { // ?envId?version id(version id) Version version = versionService.findLastestByEnvId(envId); return version; } /** * ??id?? ???? IllegalAccessError. * * @param user * ?? * @param password * ? * @param envId * ?id * @return ? */ @Override public Long getLastestConfigVersion(String user, String password, Long envId) { User u = authenticate(user, password); // should check authorization here by project authorizeEnv(u, envId); Version version = getLastestConfigVersion(envId); if (version == null) { throw new IllegalAccessError("No version under environemt id: " + envId); } return version.getId(); } /** * ???key?? ???? IllegalAccessError. * * @param user * ?? * @param password * ? * @param version * ? * @param key * key * @return ? */ @Override public String getConfigItemValue(String user, String password, Long version, String key) { User u = authenticate(user, password); Version v = versionService.findById(version); if (v == null) { throw new IllegalAccessError("No version id '" + version + "' found. "); } authorizeProject(u, v.getProjectId()); ConfigItem item = configItemService.findByVersionIdAndName(version, key, true); if (item == null) { return null; } return item.getVal(); } /** * tag?? ???? IllegalAccessError. * * @param user * ?? * @param password * ? * @param version * ? * @param tag * tag value * @return falsetag? */ @Override public boolean checkVersionTag(String user, String password, Long version, String tag) { User u = authenticate(user, password); if (StringUtils.isBlank(tag)) { if (LOGGER.isInfoEnabled()) { LOGGER.info("To check tag value is blank will always return false."); } return false; } Version v = versionService.findById(version); if (v == null) { throw new IllegalAccessError("No version id: '" + version + "' found."); } authorizeProject(u, v.getProjectId()); return tag.equals(v.getCheckSum()); } /** * import configuration items to the configuration center server. * * @param user * user name * @param password * password * @param version * the target version id to import * @param configItems * imported configuration items */ @Override public void importConfigItems(String user, String password, Long version, Map<String, String> configItems) { if (MapUtils.isEmpty(configItems)) { if (LOGGER.isInfoEnabled()) { LOGGER.info("import a empty map will be ingored"); } return; } authenticate(user, password); // check version exist Version v = versionService.findById(version); if (v == null) { throw new IllegalAccessError("import config failed. No version id '" + version + "' found."); } List<ConfigGroup> groups = configGroupService.findByVersionId(version); if (CollectionUtils.isNotEmpty(groups)) { throw new IllegalAccessError("import config failed. version id '" + version + "' is not empty."); } ConfigGroup group = ConfigGroup.newGroup(); group.setVersionId(v.getId()); configGroupService.saveEntity(group); // get configuration group id List<ConfigItem> items = group.newItems(configItems); for (ConfigItem configItem : items) { configItemService.saveEntity(configItem); } } /** * ????????. * * @param user * ?? * @param password * ? * @param projectName * ?? * @param envName * ??? * @return ? */ @Override public Long getLastestConfigVersion(String user, String password, String projectName, String envName) { User u = authenticate(user, password); // check project name Project project = projectService.findByName(projectName); if (project == null) { throw new IllegalAccessError("No project name found'" + projectName + "'"); } authorizeProject(u, project.getId()); Environment environment; environment = environmentService.findByNameAndProjectId(envName, project.getId()); if (environment == null) { throw new IllegalAccessError( "No environment name found'" + envName + "' under project name '" + projectName + "'"); } long envId = environment.getId(); Version version = getLastestConfigVersion(envId); if (version == null) { throw new IllegalAccessError("No version under environemt id:" + envId); } return version.getId(); } /** * ????id, ?null. * * @param user * ?? * @param password * ? * @param versionName * ?? * @return id */ @Override public Long getVersionId(String user, String password, String versionName) { authenticate(user, password); Version verison = versionService.findVersionAndConfigItems(versionName); if (verison == null) { return null; } return verison.getId(); } }