use of org.apache.servicecomb.foundation.auth.SignRequest in project incubator-servicecomb-java-chassis by apache.
the class RestUtils method createSignRequest.
public static SignRequest createSignRequest(String method, IpPort ipPort, RequestParam requestParam, String url, Map<String, String> headers) {
SignRequest signReq = new SignRequest();
StringBuilder endpoint = new StringBuilder("https://" + ipPort.getHostOrIp());
endpoint.append(":" + ipPort.getPort());
endpoint.append(url);
try {
signReq.setEndpoint(new URI(endpoint.toString()));
} catch (URISyntaxException e) {
LOGGER.error("set uri failed, uri is {}, message: {}", endpoint.toString(), e.getMessage());
}
signReq.setContent((requestParam.getBody() != null && requestParam.getBody().length > 0) ? new ByteArrayInputStream(requestParam.getBody()) : null);
signReq.setHeaders(headers);
signReq.setHttpMethod(method);
signReq.setQueryParams(requestParam.getQueryParamsMap());
return signReq;
}
use of org.apache.servicecomb.foundation.auth.SignRequest in project incubator-servicecomb-java-chassis by apache.
the class RestUtils method httpDo.
public static void httpDo(long timeout, RequestContext requestContext, Handler<RestResponse> responseHandler) {
HttpClientWithContext vertxHttpClient = HttpClientPool.INSTANCE.getClient();
vertxHttpClient.runOnContext(httpClient -> {
IpPort ipPort = requestContext.getIpPort();
HttpMethod httpMethod = requestContext.getMethod();
RequestParam requestParam = requestContext.getParams();
if (ipPort == null) {
LOGGER.error("request address is null");
responseHandler.handle(new RestResponse(requestContext, null));
return;
}
// query params
StringBuilder url = new StringBuilder(requestContext.getUri());
String queryParams = requestParam.getQueryParams();
if (!queryParams.isEmpty()) {
url.append(url.lastIndexOf("?") > 0 ? "&" : "?").append(queryParams);
}
HttpClientRequest httpClientRequest = httpClient.request(httpMethod, ipPort.getPort(), ipPort.getHostOrIp(), url.toString(), response -> {
responseHandler.handle(new RestResponse(requestContext, response));
});
httpClientRequest.setTimeout(timeout).exceptionHandler(e -> {
LOGGER.error("{} {} fail, endpoint is {}:{}, message: {}", httpMethod, url.toString(), ipPort.getHostOrIp(), ipPort.getPort(), e.getMessage());
responseHandler.handle(new RestResponse(requestContext, null));
});
// headers
Map<String, String> headers = defaultHeaders();
httpClientRequest.headers().addAll(headers);
if (requestParam.getHeaders() != null && requestParam.getHeaders().size() > 0) {
headers.putAll(requestParam.getHeaders());
for (Map.Entry<String, String> header : requestParam.getHeaders().entrySet()) {
httpClientRequest.putHeader(header.getKey(), header.getValue());
}
}
// cookies header
if (requestParam.getCookies() != null && requestParam.getCookies().size() > 0) {
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, String> cookie : requestParam.getCookies().entrySet()) {
stringBuilder.append(cookie.getKey()).append("=").append(cookie.getValue()).append("; ");
}
httpClientRequest.putHeader("Cookie", stringBuilder.toString());
headers.put("Cookie", stringBuilder.toString());
}
// SignAuth
SignRequest signReq = createSignRequest(requestContext.getMethod().toString(), requestContext.getIpPort(), requestContext.getParams(), url.toString(), headers);
httpClientRequest.headers().addAll(getSignAuthHeaders(signReq));
// body
if (httpMethod != HttpMethod.GET && requestParam.getBody() != null && requestParam.getBody().length > 0) {
httpClientRequest.end(Buffer.buffer(requestParam.getBody()));
} else {
httpClientRequest.end();
}
});
}
use of org.apache.servicecomb.foundation.auth.SignRequest in project incubator-servicecomb-java-chassis by apache.
the class ConfigCenterClient method createSignRequest.
public static SignRequest createSignRequest(String method, String endpoint, Map<String, String> headers, InputStream content) {
SignRequest signReq = new SignRequest();
try {
signReq.setEndpoint(new URI(endpoint));
} catch (URISyntaxException e) {
LOGGER.warn("set uri failed, uri is {}, message: {}", endpoint, e.getMessage());
}
Map<String, String[]> queryParams = new HashMap<>();
if (endpoint.contains("?")) {
String parameters = endpoint.substring(endpoint.indexOf("?") + 1);
if (null != parameters && !"".equals(parameters)) {
String[] parameterarray = parameters.split("&");
for (String p : parameterarray) {
String key = p.split("=")[0];
String value = p.split("=")[1];
if (!queryParams.containsKey(key)) {
queryParams.put(key, new String[] { value });
} else {
List<String> vals = new ArrayList<>(Arrays.asList(queryParams.get(key)));
vals.add(value);
queryParams.put(key, vals.toArray(new String[vals.size()]));
}
}
}
}
signReq.setQueryParams(queryParams);
signReq.setHeaders(headers);
signReq.setHttpMethod(method);
signReq.setContent(content);
return signReq;
}
use of org.apache.servicecomb.foundation.auth.SignRequest in project incubator-servicecomb-java-chassis by apache.
the class ConfigCenterClient method refreshMembers.
private void refreshMembers(MemberDiscovery memberDiscovery) {
if (CONFIG_CENTER_CONFIG.getAutoDiscoveryEnabled()) {
String configCenter = memberDiscovery.getConfigServer();
IpPort ipPort = NetUtils.parseIpPortFromURI(configCenter);
clientMgr.findThreadBindClientPool().runOnContext(client -> {
HttpClientRequest request = client.get(ipPort.getPort(), ipPort.getHostOrIp(), URIConst.MEMBERS, rsp -> {
if (rsp.statusCode() == HttpResponseStatus.OK.code()) {
rsp.bodyHandler(buf -> {
memberDiscovery.refreshMembers(buf.toJsonObject());
});
}
});
SignRequest signReq = createSignRequest(request.method().toString(), configCenter + URIConst.MEMBERS, new HashMap<>(), null);
if (ConfigCenterConfig.INSTANCE.getToken() != null) {
request.headers().add("X-Auth-Token", ConfigCenterConfig.INSTANCE.getToken());
}
authHeaderProviders.forEach(provider -> request.headers().addAll(provider.getSignAuthHeaders(signReq)));
request.end();
});
}
}
Aggregations