use of org.apache.servicecomb.http.client.common.HttpRequest in project java-chassis by ServiceComb.
the class ServiceCenterRawClient method doHttpRequest.
private HttpResponse doHttpRequest(String url, boolean absoluteUrl, Map<String, String> headers, String content, String method) throws IOException {
String address = addressManager.address();
String formatUrl = addressManager.formatUrl(url, absoluteUrl, address);
if (headers == null) {
headers = new HashMap<>();
}
headers.put(HEADER_TENANT_NAME, tenantName);
HttpRequest httpRequest = new HttpRequest(formatUrl, headers, content, method);
try {
HttpResponse httpResponse = httpTransport.doRequest(httpRequest);
addressManager.recordSuccessState(address);
return httpResponse;
} catch (IOException e) {
addressManager.recordFailState(address);
String retryAddress = addressManager.address();
formatUrl = addressManager.formatUrl(url, absoluteUrl, retryAddress);
LOGGER.warn("send request to {} failed and retry to {} once. ", address, retryAddress, e);
httpRequest = new HttpRequest(formatUrl, headers, content, method);
try {
return httpTransport.doRequest(httpRequest);
} catch (IOException ioException) {
addressManager.recordFailState(retryAddress);
LOGGER.warn("retry to {} failed again. ", retryAddress, e);
throw ioException;
}
}
}
use of org.apache.servicecomb.http.client.common.HttpRequest in project java-chassis by ServiceComb.
the class ConfigCenterClient method queryConfigurations.
@Override
public QueryConfigurationsResponse queryConfigurations(QueryConfigurationsRequest request) {
String dimensionsInfo = buildDimensionsInfo(request, true);
QueryConfigurationsResponse queryConfigurationsResponse = new QueryConfigurationsResponse();
Map<String, Object> configurations = new HashMap<>();
String uri = null;
String address = addressManager.address();
try {
uri = address + "/configuration/items?dimensionsInfo=" + HttpUtils.encodeURLParam(dimensionsInfo) + "&revision=" + request.getRevision();
Map<String, String> headers = new HashMap<>();
headers.put("x-environment", request.getEnvironment());
HttpRequest httpRequest = new HttpRequest(uri, headers, null, HttpRequest.GET);
HttpResponse httpResponse = httpTransport.doRequest(httpRequest);
if (httpResponse.getStatusCode() == HttpStatus.SC_OK) {
Map<String, Map<String, Object>> allConfigMap = HttpUtils.deserialize(httpResponse.getContent(), new TypeReference<Map<String, Map<String, Object>>>() {
});
if (allConfigMap.get(REVISION) != null) {
queryConfigurationsResponse.setRevision((String) allConfigMap.get(REVISION).get("version"));
}
if (allConfigMap.get(APPLICATION_CONFIG) != null) {
configurations.putAll(allConfigMap.get(APPLICATION_CONFIG));
}
if (allConfigMap.get(buildDimensionsInfo(request, false)) != null) {
configurations.putAll(allConfigMap.get(buildDimensionsInfo(request, false)));
}
if (allConfigMap.get(buildDarkLaunchDimensionsInfo(request)) != null) {
configurations.putAll(allConfigMap.get(buildDarkLaunchDimensionsInfo(request)));
}
if (allConfigMap.get(dimensionsInfo) != null) {
configurations.putAll(allConfigMap.get(dimensionsInfo));
}
queryConfigurationsResponse.setConfigurations(configurations);
queryConfigurationsResponse.setChanged(true);
addressManager.recordSuccessState(address);
return queryConfigurationsResponse;
} else if (httpResponse.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
queryConfigurationsResponse.setChanged(false);
addressManager.recordSuccessState(address);
return queryConfigurationsResponse;
} else if (httpResponse.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
throw new OperationException("Bad request for query configurations.");
} else {
addressManager.recordFailState(address);
throw new OperationException("read response failed. status:" + httpResponse.getStatusCode() + "; message:" + httpResponse.getMessage() + "; content:" + httpResponse.getContent());
}
} catch (IOException e) {
addressManager.recordFailState(address);
LOGGER.error("query configuration from {} failed, message={}", uri, e.getMessage());
throw new OperationException("", e);
}
}
use of org.apache.servicecomb.http.client.common.HttpRequest in project java-chassis by ServiceComb.
the class KieClient method queryConfigurations.
@Override
public ConfigurationsResponse queryConfigurations(ConfigurationsRequest request) {
String address = addressManager.address();
String url = buildUrl(request, address);
try {
if (kieConfiguration.isEnableLongPolling()) {
url += "&wait=" + kieConfiguration.getPollingWaitInSeconds() + "s";
}
HttpRequest httpRequest = new HttpRequest(url, null, null, HttpRequest.GET);
HttpResponse httpResponse = httpTransport.doRequest(httpRequest);
ConfigurationsResponse configurationsResponse = new ConfigurationsResponse();
if (httpResponse.getStatusCode() == HttpStatus.SC_OK) {
revision = httpResponse.getHeader("X-Kie-Revision");
KVResponse allConfigList = HttpUtils.deserialize(httpResponse.getContent(), KVResponse.class);
Map<String, Object> configurations = getConfigByLabel(allConfigList);
configurationsResponse.setConfigurations(configurations);
configurationsResponse.setChanged(true);
configurationsResponse.setRevision(revision);
addressManager.recordSuccessState(address);
return configurationsResponse;
}
if (httpResponse.getStatusCode() == HttpStatus.SC_BAD_REQUEST) {
throw new OperationException("Bad request for query configurations.");
}
if (httpResponse.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
configurationsResponse.setChanged(false);
addressManager.recordSuccessState(address);
return configurationsResponse;
}
addressManager.recordFailState(address);
throw new OperationException("read response failed. status:" + httpResponse.getStatusCode() + "; message:" + httpResponse.getMessage() + "; content:" + httpResponse.getContent());
} catch (Exception e) {
LOGGER.error("query configuration from {} failed, message={}", url, e.getMessage());
throw new OperationException("read response failed. ", e);
}
}
Aggregations