|
| 1 | +/* |
| 2 | + * Copyright [2019] [恒宇少年 - 于起宇] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.minbox.framework.api.boot.plugin.logging.admin.discovery.support; |
| 19 | + |
| 20 | +import org.minbox.framework.api.boot.common.exception.ApiBootException; |
| 21 | +import org.springframework.util.Assert; |
| 22 | +import org.springframework.util.ObjectUtils; |
| 23 | + |
| 24 | +/** |
| 25 | + * ApiBoot Logging Appoint Admin Discovery |
| 26 | + * Support multiple admins |
| 27 | + * |
| 28 | + * @author:恒宇少年 - 于起宇 |
| 29 | + * <p> |
| 30 | + * DateTime:2019-07-19 15:44 |
| 31 | + * Blog:http://blog.yuqiyu.com |
| 32 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 33 | + * Gitee:https://gitee.com/hengboy |
| 34 | + * GitHub:https://github.com/hengboy |
| 35 | + */ |
| 36 | +public class LoggingAppointAdminDiscovery extends LoggingAbstractAdminDiscovery { |
| 37 | + /** |
| 38 | + * basic user split |
| 39 | + */ |
| 40 | + private static final String BASIC_SPLIT = "@"; |
| 41 | + /** |
| 42 | + * http prefix |
| 43 | + */ |
| 44 | + private static final String HTTP_PREFIX = "http://"; |
| 45 | + /** |
| 46 | + * ApiBoot Logging Admin Server Address |
| 47 | + */ |
| 48 | + private String[] adminServerAddress; |
| 49 | + /** |
| 50 | + * ApiBoot Logging Admin Server Address |
| 51 | + * To Current Thread |
| 52 | + */ |
| 53 | + private final ThreadLocal<String> CURRENT_SERVER_ADDRESS = new ThreadLocal(); |
| 54 | + |
| 55 | + public LoggingAppointAdminDiscovery(String[] adminServerAddress) { |
| 56 | + this.adminServerAddress = adminServerAddress; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * load balance lookup admin server address |
| 61 | + * |
| 62 | + * @return admin server address |
| 63 | + * @throws ApiBootException ApiBoot Exception |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public String lookup() throws ApiBootException { |
| 67 | + Assert.notNull(adminServerAddress, "ApiBoot Logging Admin Server Address Is Null."); |
| 68 | + String serverAddress = adminServerAddress[0]; |
| 69 | + CURRENT_SERVER_ADDRESS.set(serverAddress); |
| 70 | + // have basic auth |
| 71 | + if (serverAddress.indexOf(BASIC_SPLIT) > 0) { |
| 72 | + serverAddress = serverAddress.substring(serverAddress.indexOf(BASIC_SPLIT) + 1); |
| 73 | + } |
| 74 | + // append http prefix |
| 75 | + return HTTP_PREFIX + serverAddress; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * get basic auth |
| 80 | + * |
| 81 | + * @return basic auth base64 |
| 82 | + * @throws ApiBootException ApiBoot Exception |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public String getBasicAuth() throws ApiBootException { |
| 86 | + String serverAddress = CURRENT_SERVER_ADDRESS.get(); |
| 87 | + if (serverAddress.indexOf(BASIC_SPLIT) > 0) { |
| 88 | + String basicInfo = serverAddress.substring(0, serverAddress.indexOf(BASIC_SPLIT)); |
| 89 | + CURRENT_SERVER_ADDRESS.remove(); |
| 90 | + if (!ObjectUtils.isEmpty(basicInfo)) { |
| 91 | + return getBasicBase64(basicInfo); |
| 92 | + } |
| 93 | + } |
| 94 | + return null; |
| 95 | + } |
| 96 | +} |
0 commit comments