Search in sources :

Example 21 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClient method registerMicroservice.

@Override
public RegisteredMicroserviceResponse registerMicroservice(Microservice microservice) {
    try {
        CreateMicroserviceRequest request = new CreateMicroserviceRequest();
        request.setService(microservice);
        HttpResponse response = httpClient.postHttpRequest("/registry/microservices", null, HttpUtils.serialize(request));
        if (response.getStatusCode() == HttpStatus.SC_OK) {
            return HttpUtils.deserialize(response.getContent(), RegisteredMicroserviceResponse.class);
        }
        sendUnAuthorizedEvent(response);
        throw new OperationException("register service fails, statusCode = " + response.getStatusCode() + "; message = " + response.getMessage() + "; content = " + response.getContent());
    } catch (IOException e) {
        throw new OperationException("register service fails", e);
    }
}
Also used : CreateMicroserviceRequest(org.apache.servicecomb.service.center.client.model.CreateMicroserviceRequest) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) OperationException(org.apache.servicecomb.service.center.client.exception.OperationException)

Example 22 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClient method updateServiceSchemaContext.

@Override
public boolean updateServiceSchemaContext(String serviceId, SchemaInfo schemaInfo) {
    try {
        CreateSchemaRequest request = new CreateSchemaRequest();
        request.setSchema(schemaInfo.getSchema());
        request.setSummary(schemaInfo.getSummary());
        HttpResponse response = httpClient.putHttpRequest("/registry/microservices/" + serviceId + "/schemas/" + schemaInfo.getSchemaId(), null, HttpUtils.serialize(request));
        if (response.getStatusCode() == HttpStatus.SC_OK) {
            return true;
        }
        sendUnAuthorizedEvent(response);
        throw new OperationException("update service schema fails, statusCode = " + response.getStatusCode() + "; message = " + response.getMessage() + "; content = " + response.getContent());
    } catch (IOException e) {
        throw new OperationException("update service schema fails", e);
    }
}
Also used : CreateSchemaRequest(org.apache.servicecomb.service.center.client.model.CreateSchemaRequest) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) OperationException(org.apache.servicecomb.service.center.client.exception.OperationException)

Example 23 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse in project java-chassis by ServiceComb.

the class ServiceCenterClient method queryToken.

@Override
public RbacTokenResponse queryToken(RbacTokenRequest request) {
    try {
        HttpResponse response = httpClient.postHttpRequestAbsoluteUrl("/v4/token", null, HttpUtils.serialize(request));
        if (response.getStatusCode() == HttpStatus.SC_OK) {
            RbacTokenResponse result = HttpUtils.deserialize(response.getContent(), RbacTokenResponse.class);
            result.setStatusCode(HttpStatus.SC_OK);
            return result;
        }
        if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            RbacTokenResponse result = new RbacTokenResponse();
            result.setStatusCode(response.getStatusCode());
            return result;
        }
        if (response.getStatusCode() == HttpStatus.SC_UNAUTHORIZED || response.getStatusCode() == HttpStatus.SC_FORBIDDEN) {
            RbacTokenResponse result = new RbacTokenResponse();
            result.setStatusCode(response.getStatusCode());
            ErrorMessage errorMessage = HttpUtils.deserialize(response.getContent(), ErrorMessage.class);
            result.setErrorCode(errorMessage.getErrorCode());
            return result;
        }
        throw new OperationException("query token failed, statusCode = " + response.getStatusCode() + "; message = " + response.getMessage() + "; content = " + response.getContent());
    } catch (IOException e) {
        throw new OperationException("query token failed", e);
    }
}
Also used : HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) ErrorMessage(org.apache.servicecomb.service.center.client.model.ErrorMessage) RbacTokenResponse(org.apache.servicecomb.service.center.client.model.RbacTokenResponse) OperationException(org.apache.servicecomb.service.center.client.exception.OperationException)

Example 24 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse 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;
        }
    }
}
Also used : HttpRequest(org.apache.servicecomb.http.client.common.HttpRequest) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException)

Example 25 with HttpResponse

use of org.apache.servicecomb.http.client.common.HttpResponse 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);
    }
}
Also used : HttpRequest(org.apache.servicecomb.http.client.common.HttpRequest) QueryConfigurationsResponse(org.apache.servicecomb.config.center.client.model.QueryConfigurationsResponse) HashMap(java.util.HashMap) HttpResponse(org.apache.servicecomb.http.client.common.HttpResponse) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) OperationException(org.apache.servicecomb.config.center.client.exception.OperationException)

Aggregations

HttpResponse (org.apache.servicecomb.http.client.common.HttpResponse)37 IOException (java.io.IOException)21 OperationException (org.apache.servicecomb.service.center.client.exception.OperationException)19 Test (org.junit.Test)15 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Microservice (org.apache.servicecomb.service.center.client.model.Microservice)4 HttpRequest (org.apache.servicecomb.http.client.common.HttpRequest)3 MicroserviceInstancesResponse (org.apache.servicecomb.service.center.client.model.MicroserviceInstancesResponse)3 RegisteredMicroserviceResponse (org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceResponse)3 HashMap (java.util.HashMap)2 MicroserviceInstance (org.apache.servicecomb.service.center.client.model.MicroserviceInstance)2 RegisteredMicroserviceInstanceResponse (org.apache.servicecomb.service.center.client.model.RegisteredMicroserviceInstanceResponse)2 SchemaInfo (org.apache.servicecomb.service.center.client.model.SchemaInfo)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 EventBus (com.google.common.eventbus.EventBus)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 OperationException (org.apache.servicecomb.config.center.client.exception.OperationException)1