Search in sources :

Example 1 with OperationException

use of org.apache.servicecomb.config.kie.client.exception.OperationException 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);
    }
}
Also used : HttpRequest(org.apache.servicecomb.http.client.common.HttpRequest) ConfigurationsResponse(org.apache.servicecomb.config.kie.client.model.ConfigurationsResponse) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) KVResponse(org.apache.servicecomb.config.kie.client.model.KVResponse) OperationException(org.apache.servicecomb.config.kie.client.exception.OperationException) OperationException(org.apache.servicecomb.config.kie.client.exception.OperationException)

Example 2 with OperationException

use of org.apache.servicecomb.config.kie.client.exception.OperationException in project java-chassis by ServiceComb.

the class KieClient method processValueType.

private Map<String, Object> processValueType(KVDoc kvDoc) {
    ValueType valueType;
    try {
        valueType = ValueType.valueOf(kvDoc.getValueType());
    } catch (IllegalArgumentException e) {
        throw new OperationException("value type not support");
    }
    Properties properties = new Properties();
    Map<String, Object> kvMap = new HashMap<>();
    try {
        switch(valueType) {
            case yml:
            case yaml:
                YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
                yamlFactory.setResources(new ByteArrayResource(kvDoc.getValue().getBytes()));
                return toMap(yamlFactory.getObject());
            case properties:
                properties.load(new StringReader(kvDoc.getValue()));
                return toMap(properties);
            case text:
            case string:
            default:
                kvMap.put(kvDoc.getKey(), kvDoc.getValue());
                return kvMap;
        }
    } catch (Exception e) {
        LOGGER.error("read config failed");
    }
    return Collections.emptyMap();
}
Also used : ValueType(org.apache.servicecomb.config.kie.client.model.ValueType) HashMap(java.util.HashMap) StringReader(java.io.StringReader) YamlPropertiesFactoryBean(org.springframework.beans.factory.config.YamlPropertiesFactoryBean) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Properties(java.util.Properties) OperationException(org.apache.servicecomb.config.kie.client.exception.OperationException) OperationException(org.apache.servicecomb.config.kie.client.exception.OperationException)

Aggregations

OperationException (org.apache.servicecomb.config.kie.client.exception.OperationException)2 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 ConfigurationsResponse (org.apache.servicecomb.config.kie.client.model.ConfigurationsResponse)1 KVResponse (org.apache.servicecomb.config.kie.client.model.KVResponse)1 ValueType (org.apache.servicecomb.config.kie.client.model.ValueType)1 HttpRequest (org.apache.servicecomb.http.client.common.HttpRequest)1 HttpResponse (org.apache.servicecomb.http.client.common.HttpResponse)1 YamlPropertiesFactoryBean (org.springframework.beans.factory.config.YamlPropertiesFactoryBean)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1