public MySqlJdbcUrl setParameter(String param, String value) { builder.setParameter(param, value); return this; }
private void addParametersToURI(Map<String, String> parameters, URIBuilder uriBuilder) { if (parameters != null) { for (Entry<String, String> param : parameters.entrySet()) { uriBuilder.setParameter(param.getKey(), param.getValue()); } } }
/** * Set the value of a session attribute on the server */ public Response set(String key, String value, boolean storeRespCookie) throws IOException, URISyntaxException { resetURI(); reqURIBuild.setParameter("cmd", QueryCommand.SET.name()); reqURIBuild.setParameter("param", key); reqURIBuild.setParameter("value", value); return doRequest(new HttpGet(reqURIBuild.build()), storeRespCookie); }
/** * Invalidate this clients session on the server */ public Response invalidate(boolean storeRespCookie) throws IOException, URISyntaxException { resetURI(); reqURIBuild.setParameter("cmd", QueryCommand.INVALIDATE.name()); reqURIBuild.setParameter("param", "null"); return doRequest(new HttpGet(reqURIBuild.build()), storeRespCookie); }
/** * Adds the fragment to the end of the path and optionally adds an id param depending upon * the value of appendIdToRelativeURI. */ protected String buildRelativeUrlWithParam(String currentUrl, String fragment, String idParam, String idValue) { try { URIBuilder builder = new URIBuilder(currentUrl); builder.setPath(builder.getPath() + "/" + fragment); if (appendIdToRelativeURI) { builder.setParameter(idParam, String.valueOf(idValue)); } return builder.build().toString(); } catch (URISyntaxException e) { return currentUrl; } }
/** * Instantiate and execute a function in the server. Note that this function must be present in * the sesssion testing war. */ public Response executionFunction( Class<? extends Function<HttpServletRequest, String>> functionClass) throws IOException, URISyntaxException { resetURI(); reqURIBuild.setParameter("cmd", QueryCommand.FUNCTION.name()); reqURIBuild.setParameter("function", functionClass.getName()); return doRequest(new HttpGet(reqURIBuild.build()), true); }
@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; }
/** * Get the value of a session attribute on the server */ public Response get(String key, boolean storeRespCookie) throws IOException, URISyntaxException { resetURI(); reqURIBuild.setParameter("cmd", QueryCommand.GET.name()); reqURIBuild.setParameter("param", key); return doRequest(new HttpGet(reqURIBuild.build()), storeRespCookie); }
/** * Remove the session attribute on the server * * @param key - the session attribute to remove * @param storeRespCookie - whether or not to store the session cookie of this request */ public Response remove(String key, boolean storeRespCookie) throws URISyntaxException, IOException { resetURI(); reqURIBuild.setParameter("cmd", QueryCommand.REMOVE.name()); reqURIBuild.setParameter("param", key); return doRequest(new HttpGet(reqURIBuild.build()), storeRespCookie); }
/** * Set the maximum inactive interval for this client's session on the server. * * If this time interval elapses without activity on the session, the session will expire. * * @param time - Time in seconds until the session should expire */ public Response setMaxInactive(int time, boolean storeRespCookie) throws IOException, URISyntaxException { resetURI(); reqURIBuild.setParameter("cmd", QueryCommand.SET_MAX_INACTIVE.name()); reqURIBuild.setParameter("value", Integer.toString(time)); return doRequest(new HttpGet(reqURIBuild.build()), storeRespCookie); }
@Override public AuthFlowConfiguration generateConfiguration(String callbackBaseUrl, String id) { String encodedJobId = BaseEncoding.base64Url().encode(id.getBytes(Charsets.UTF_8)); URIBuilder builder = new URIBuilder() .setPath(config.getAuthUrl()) .setParameter("response_type", "code") .setParameter("client_id", clientId) .setParameter("redirect_uri", callbackBaseUrl) .setParameter("scope", String.join(" ", scopes)) .setParameter("state", encodedJobId); if (config.getAdditionalAuthUrlParameters() != null) { for (Entry<String, String> entry : config.getAdditionalAuthUrlParameters().entrySet()) { builder.setParameter(entry.getKey(), entry.getValue()); } } try { String url = builder.build().toString(); return new AuthFlowConfiguration(url, OAUTH_2, getTokenUrl()); } catch (URISyntaxException e) { throw new IllegalStateException("Could not produce url.", e); } }
private static String buildUrl(String path, List<NameValuePair> qparams) throws RestApiClientException { URIBuilder builder = new URIBuilder(); builder.setPath(path); ListIterator<NameValuePair> i = qparams.listIterator(); while (i.hasNext()) { NameValuePair keyValue = i.next(); builder.setParameter(keyValue.getName(), keyValue.getValue()); } URI uri; try { uri = builder.build(); } catch (Exception e) { throw new RestApiClientException("Failed to build url; error - " + e.getMessage(), e); } return new HttpGet(uri).getURI().toString(); }
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> 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); } }
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 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); } }
uriBuilder.setScheme("http").setHost(ip).setPort(port) .setPath(GET_RULES_PATH) .setParameter("type", AUTHORITY_TYPE); try { String body = httpGetContent(uriBuilder.build().toString());
/** * 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); } }