1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
| import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.DependsOn; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONPath; import cn.hutool.core.convert.Convert; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpStatus;
@DependsOn("rmsConfig") @Component public class RabbitmqHttpApi {
private static String managementUrl; private static String adminUsername; private static String adminPassword;
@Autowired public void setMqttConfig(RmsConfig rmsConfig) { RabbitmqHttpApi.managementUrl = rmsConfig.getManagementUrl(); RabbitmqHttpApi.adminUsername = rmsConfig.getUsername(); RabbitmqHttpApi.adminPassword = rmsConfig.getPassword(); }
public enum Tags { ADMINISTRATOR("administrator"), MONITORING("monitoring"), POLICYMAKER("policymaker"), MANAGEMENT("management"), IMPERSONATOR("impersonator"), NONE(""); private final String text; private Tags(String text) { this.text = text; } }
public static boolean createCommonUser(String username, String password) throws Exception { return createUser(username, password, Tags.NONE); }
public static boolean createAdminUser(String username, String password) throws Exception { return createUser(username, password, Tags.ADMINISTRATOR); }
public static boolean createUser(String username, String password, Tags tags) throws Exception { String param = String.format("{\"password\":\"%s\",\"tags\":\"%s\"}", password, tags.text); String url = String.format("%s/api/users/%s", managementUrl, username); HttpResponse response = HttpRequest.put(url).basicAuth(adminUsername, adminPassword).body(param).execute(); return response.getStatus() == HttpStatus.HTTP_CREATED || response.getStatus() == HttpStatus.HTTP_NO_CONTENT; }
public static boolean setAllPermissions(String username) throws Exception { return setPermissions(username, "/", ".*", ".*", ".*"); }
public static boolean setPermissions(String username, String virtualHost, String configureRegexp, String writeRegexp, String readRegexp) throws Exception { String param = String.format("{\"configure\":\".*\",\"write\":\".*\",\"read\":\".*\"}", configureRegexp, writeRegexp, readRegexp); virtualHost = StrUtil.equals("/", virtualHost) ? "%2F" : virtualHost; String url = managementUrl + "/api/permissions/" + virtualHost + "/" + username; HttpResponse response = HttpRequest.put(url).basicAuth(adminUsername, adminPassword).body(param).execute(); return response.getStatus() == HttpStatus.HTTP_CREATED || response.getStatus() == HttpStatus.HTTP_NO_CONTENT; }
public static Integer[] getUsedSockets() throws Exception { String url = managementUrl + "/api/nodes"; HttpResponse response = HttpRequest.get(url).basicAuth(adminUsername, adminPassword).execute(); Object object = JSONPath.read(response.body(), "$['sockets_used']"); return ObjectUtil.isNotNull(object) ? Convert.toIntArray(object) : new Integer[] {}; }
}
|