public MySqlJdbcUrl setPort(int port) { builder.setPort(port); return this; }
protected String adjustURIForPort(String uri) throws URISyntaxException { URI metadataURI = new URI(uri); if (metadataURI.getPort() < 0) { switch (metadataURI.getScheme()) { case "https": return new URIBuilder(uri).setPort(443).build().toString(); case "http": return new URIBuilder(uri).setPort(80).build().toString(); default: return uri; } } return uri; }
public CompletableFuture<ClusterStateSimpleEntity> fetchClusterMode(String app, String ip, int port) { if (StringUtil.isBlank(ip) || port <= 0) { return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter")); } try { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(FETCH_CLUSTER_MODE_PATH); return executeCommand(FETCH_CLUSTER_MODE_PATH, uriBuilder.build()) .thenApply(r -> JSON.parseObject(r, ClusterStateSimpleEntity.class)); } catch (Exception ex) { logger.warn("Error when fetching cluster mode", ex); return AsyncUtils.newFailedFuture(ex); } }
URIBuilder builder = new URIBuilder().setScheme(scheme).setHost(host).setPort(port);
public CompletableFuture<ClusterClientInfoVO> fetchClusterClientInfoAndConfig(String app, String ip, int port) { if (StringUtil.isBlank(ip) || port <= 0) { return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter")); } try { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(FETCH_CLUSTER_CLIENT_CONFIG_PATH); return executeCommand(FETCH_CLUSTER_CLIENT_CONFIG_PATH, uriBuilder.build()) .thenApply(r -> JSON.parseObject(r, ClusterClientInfoVO.class)); } catch (Exception ex) { logger.warn("Error when fetching cluster client config", ex); return AsyncUtils.newFailedFuture(ex); } }
public CompletableFuture<ClusterServerStateVO> fetchClusterServerBasicInfo(String app, String ip, int port) { if (StringUtil.isBlank(ip) || port <= 0) { return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter")); } try { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(FETCH_CLUSTER_SERVER_BASIC_INFO_PATH); return executeCommand(FETCH_CLUSTER_SERVER_BASIC_INFO_PATH, uriBuilder.build()) .thenApply(r -> JSON.parseObject(r, ClusterServerStateVO.class)); } catch (Exception ex) { logger.warn("Error when fetching cluster sever all config and basic info", ex); return AsyncUtils.newFailedFuture(ex); } } }
public CompletableFuture<Void> setParamFlowRuleOfMachine(String app, String ip, int port, List<ParamFlowRuleEntity> rules) { if (rules == null) { return CompletableFuture.completedFuture(null); } if (StringUtil.isBlank(ip) || port <= 0) { return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter")); } try { String data = JSON.toJSONString( rules.stream().map(ParamFlowRuleEntity::getRule).collect(Collectors.toList()) ); data = URLEncoder.encode(data, DEFAULT_CHARSET.name()); URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(SET_PARAM_RULE_PATH) .setParameter("data", data); return executeCommand(SET_PARAM_RULE_PATH, uriBuilder.build()) .thenCompose(e -> { if (CommandConstants.MSG_SUCCESS.equals(e)) { return CompletableFuture.completedFuture(null); } else { logger.warn("Push parameter flow rules to client failed: " + e); return AsyncUtils.newFailedFuture(new RuntimeException(e)); } }); } catch (Exception ex) { logger.warn("Error when setting parameter flow rule", ex); return AsyncUtils.newFailedFuture(ex); } }
public CompletableFuture<Void> modifyClusterServerFlowConfig(String app, String ip, int port, ServerFlowConfig config) { if (StringUtil.isBlank(ip) || port <= 0) { return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter")); } try { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(MODIFY_CLUSTER_SERVER_FLOW_CONFIG_PATH) .setParameter("data", JSON.toJSONString(config)); return executeCommand(MODIFY_CLUSTER_SERVER_FLOW_CONFIG_PATH, uriBuilder.build()) .thenCompose(e -> { if (CommandConstants.MSG_SUCCESS.equals(e)) { return CompletableFuture.completedFuture(null); } else { logger.warn("Error when modifying cluster server flow config: " + e); return AsyncUtils.newFailedFuture(new RuntimeException(e)); } }); } catch (Exception ex) { logger.warn("Error when modifying cluster server flow config", ex); return AsyncUtils.newFailedFuture(ex); } }
public CompletableFuture<Void> modifyClusterServerNamespaceSet(String app, String ip, int port, Set<String> set) { if (StringUtil.isBlank(ip) || port <= 0) { return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter")); } try { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(MODIFY_CLUSTER_SERVER_NAMESPACE_SET_PATH) .setParameter("data", JSON.toJSONString(set)); return executeCommand(MODIFY_CLUSTER_SERVER_NAMESPACE_SET_PATH, uriBuilder.build()) .thenCompose(e -> { if (CommandConstants.MSG_SUCCESS.equals(e)) { return CompletableFuture.completedFuture(null); } else { logger.warn("Error when modifying cluster server NamespaceSet: " + e); return AsyncUtils.newFailedFuture(new RuntimeException(e)); } }); } catch (Exception ex) { logger.warn("Error when modifying cluster server NamespaceSet", ex); return AsyncUtils.newFailedFuture(ex); } }
public CompletableFuture<Void> modifyClusterMode(String app, String ip, int port, int mode) { if (StringUtil.isBlank(ip) || port <= 0) { return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter")); } try { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(MODIFY_CLUSTER_MODE_PATH) .setParameter("mode", String.valueOf(mode)); return executeCommand(MODIFY_CLUSTER_MODE_PATH, uriBuilder.build()) .thenCompose(e -> { if (CommandConstants.MSG_SUCCESS.equals(e)) { return CompletableFuture.completedFuture(null); } else { logger.warn("Error when modifying cluster mode: " + e); return AsyncUtils.newFailedFuture(new RuntimeException(e)); } }); } catch (Exception ex) { logger.warn("Error when modifying cluster mode", ex); return AsyncUtils.newFailedFuture(ex); } }
public String sslServerUrl(String sslPort) { String serverUrl = serverUrl(); try { // backward compatibility, since the agent.jar requires an ssl url, but the old bootstrapper does not have one. URIBuilder url = new URIBuilder(serverUrl); if (url.getScheme().equals("http")) { url.setPort(Integer.valueOf(sslPort)); url.setScheme("https"); } return url.toString(); } catch (URISyntaxException e) { throw bomb(e); } } }
public CompletableFuture<Void> modifyClusterClientConfig(String app, String ip, int port, ClusterClientConfig config) { if (StringUtil.isBlank(ip) || port <= 0) { return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter")); } try { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(MODIFY_CLUSTER_CLIENT_CONFIG_PATH) .setParameter("data", JSON.toJSONString(config)); return executeCommand(MODIFY_CLUSTER_MODE_PATH, uriBuilder.build()) .thenCompose(e -> { if (CommandConstants.MSG_SUCCESS.equals(e)) { return CompletableFuture.completedFuture(null); } else { logger.warn("Error when modifying cluster client config: " + e); return AsyncUtils.newFailedFuture(new RuntimeException(e)); } }); } catch (Exception ex) { logger.warn("Error when modifying cluster client config", ex); return AsyncUtils.newFailedFuture(ex); } }
AssertUtil.isTrue(port > 0, "Bad machine port"); URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(GET_RULES_PATH) .setParameter("type", AUTHORITY_TYPE);
/** * Fetch all parameter flow rules from provided machine. * * @param app application name * @param ip machine client IP * @param port machine client port * @return all retrieved parameter flow rules * @since 0.2.1 */ public CompletableFuture<List<ParamFlowRuleEntity>> fetchParamFlowRulesOfMachine(String app, String ip, int port) { try { AssertUtil.notEmpty(app, "Bad app name"); AssertUtil.notEmpty(ip, "Bad machine IP"); AssertUtil.isTrue(port > 0, "Bad machine port"); URIBuilder uriBuilder = new URIBuilder(); String commandName = GET_PARAM_RULE_PATH; uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(commandName); return executeCommand(commandName, uriBuilder.build()) .thenApply(RuleUtils::parseParamFlowRule) .thenApply(rules -> rules.stream() .map(e -> ParamFlowRuleEntity.fromAuthorityRule(app, ip, port, e)) .collect(Collectors.toList()) ); } catch (Exception e) { logger.error("Error when fetching parameter flow rules", e); return AsyncUtils.newFailedFuture(e); } }
public String getConnectionURI(boolean includeDatabase) throws URISyntaxException { this.setSSLOptions(); URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("jdbc:mysql"); uriBuilder.setHost(host); uriBuilder.setPort(port); if (database != null && includeDatabase) { uriBuilder.setPath("/" + database); } for (Map.Entry<String, String> jdbcOption : jdbcOptions.entrySet()) { uriBuilder.addParameter(jdbcOption.getKey(), jdbcOption.getValue()); } // added by d8888 2018/09/10, force JDBC to use UTF-8 to support using non-english db, table & column names uriBuilder.addParameter("characterEncoding", "UTF-8"); uriBuilder.addParameter("tinyInt1isBit", "false"); return uriBuilder.build().toString(); }
public CompletableFuture<Void> modifyClusterServerTransportConfig(String app, String ip, int port, ServerTransportConfig config) { if (StringUtil.isBlank(ip) || port <= 0) { return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter")); } try { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(MODIFY_CLUSTER_SERVER_TRANSPORT_CONFIG_PATH) .setParameter("port", config.getPort().toString()) .setParameter("idleSeconds", config.getIdleSeconds().toString()); return executeCommand(MODIFY_CLUSTER_SERVER_TRANSPORT_CONFIG_PATH, uriBuilder.build()) .thenCompose(e -> { if (CommandConstants.MSG_SUCCESS.equals(e)) { return CompletableFuture.completedFuture(null); } else { logger.warn("Error when modifying cluster server transport config: " + e); return AsyncUtils.newFailedFuture(new RuntimeException(e)); } }); } catch (Exception ex) { logger.warn("Error when modifying cluster server transport config", ex); return AsyncUtils.newFailedFuture(ex); } }
/** * helper function to build a valid URI. * * @param host host name. * @param port host port. * @param path extra path after host. * @param isHttp indicates if whether Http or HTTPS should be used. * @param params extra query parameters. * @return the URI built from the inputs. */ public static URI buildUri(final String host, final int port, final String path, final boolean isHttp, final Pair<String, String>... params) throws IOException { final URIBuilder builder = new URIBuilder(); builder.setScheme(isHttp ? "http" : "https").setHost(host).setPort(port); if (null != path && path.length() > 0) { builder.setPath(path); } if (params != null) { for (final Pair<String, String> pair : params) { builder.setParameter(pair.getFirst(), pair.getSecond()); } } try { return builder.build(); } catch (final URISyntaxException e) { throw new IOException(e); } }
@Override public boolean sendHeartbeat() throws Exception { if (StringUtil.isEmpty(consoleHost)) { return false; } URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(consoleHost).setPort(consolePort) .setPath("/registry/machine") .setParameter("app", AppNameUtil.getAppName()) .setParameter("v", Constants.SENTINEL_VERSION) .setParameter("version", String.valueOf(System.currentTimeMillis())) .setParameter("hostname", HostNameUtil.getHostName()) .setParameter("ip", TransportConfig.getHeartbeatClientIp()) .setParameter("port", TransportConfig.getPort()) .setParameter("pid", String.valueOf(PidUtil.getPid())); HttpGet request = new HttpGet(uriBuilder.build()); request.setConfig(requestConfig); // Send heartbeat request. CloseableHttpResponse response = client.execute(request); response.close(); return true; }
private String httpsUri() throws Exception { HostnamePort hostPort = addressForConnector( "https" ); assertNotNull( hostPort ); return new URIBuilder() .setScheme( "https" ) .setHost( hostPort.getHost() ) .setPort( hostPort.getPort() ) .build() .toString(); }
private static void setLogLevel(final TargetInfo targetInfo, String logName, String level) throws IOException { URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http"); uriBuilder.setHost(targetInfo.getHost()); uriBuilder.setPort(targetInfo.getPort()); uriBuilder.setPath(Constants.REST_API_PREFIX + "/" + targetInfo.getRole() + "/" + LOG_LEVEL); uriBuilder.addParameter(LOG_NAME_OPTION_NAME, logName); if (level != null) { uriBuilder.addParameter(LEVEL_OPTION_NAME, level); } HttpUtils.post(uriBuilder.toString(), 5000, new HttpUtils.IProcessInputStream() { @Override public void process(InputStream inputStream) throws IOException { ObjectMapper mapper = new ObjectMapper(); LogInfo logInfo = mapper.readValue(inputStream, LogInfo.class); System.out.println(targetInfo.toString() + logInfo.toString()); } }); }