Search in sources :

Example 6 with ServiceException

use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.

the class RetryableRestTemplate method exchangeGet.

private <T> ResponseEntity<T> exchangeGet(Env env, String path, ParameterizedTypeReference<T> reference, Object... uriVariables) {
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    String uri = uriTemplateHandler.expand(path, uriVariables).getPath();
    Transaction ct = Tracer.newTransaction("AdminAPI", uri);
    ct.addData("Env", env);
    List<ServiceDTO> services = getAdminServices(env, ct);
    HttpEntity<Void> entity = new HttpEntity<>(assembleExtraHeaders(env));
    for (ServiceDTO serviceDTO : services) {
        try {
            ResponseEntity<T> result = restTemplate.exchange(parseHost(serviceDTO) + path, HttpMethod.GET, entity, reference, uriVariables);
            ct.setStatus(Transaction.SUCCESS);
            ct.complete();
            return result;
        } catch (Throwable t) {
            logger.error("Http request failed, uri: {}, method: {}", uri, HttpMethod.GET, t);
            Tracer.logError(t);
            if (canRetry(t, HttpMethod.GET)) {
                Tracer.logEvent(TracerEventType.API_RETRY, uri);
            } else {
                // biz exception rethrow
                ct.setStatus(t);
                ct.complete();
                throw t;
            }
        }
    }
    // all admin server down
    ServiceException e = new ServiceException(String.format("Admin servers are unresponsive. meta server address: %s, admin servers: %s", portalMetaDomainService.getDomain(env), services));
    ct.setStatus(e);
    ct.complete();
    throw e;
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) HttpEntity(org.springframework.http.HttpEntity) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO)

Example 7 with ServiceException

use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.

the class ConfigsExportController method exportItems.

/**
 * export one config as file.
 * keep compatibility.
 * file name examples:
 * <pre>
 *   application.properties
 *   application.yml
 *   application.json
 * </pre>
 */
@PreAuthorize(value = "!@permissionValidator.shouldHideConfigToCurrentUser(#appId, #env, #namespaceName)")
@GetMapping("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items/export")
public void exportItems(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, HttpServletResponse res) {
    List<String> fileNameSplit = Splitter.on(".").splitToList(namespaceName);
    String fileName = namespaceName;
    // properties file or public namespace has not suffix (.properties)
    if (fileNameSplit.size() <= 1 || !ConfigFileFormat.isValidFormat(fileNameSplit.get(fileNameSplit.size() - 1))) {
        fileName = Joiner.on(".").join(namespaceName, ConfigFileFormat.Properties.getValue());
    }
    NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(appId, Env.valueOf(env), clusterName, namespaceName);
    // generate a file.
    res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName);
    // file content
    final String configFileContent = NamespaceBOUtils.convert2configFileContent(namespaceBO);
    try {
        // write content to net
        res.getOutputStream().write(configFileContent.getBytes());
    } catch (Exception e) {
        throw new ServiceException("export items failed:{}", e);
    }
}
Also used : ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) NamespaceBO(com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO) IOException(java.io.IOException) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 8 with ServiceException

use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.

the class RetryableRestTemplate method getAdminServices.

private List<ServiceDTO> getAdminServices(Env env, Transaction ct) {
    List<ServiceDTO> services = adminServiceAddressLocator.getServiceList(env);
    if (CollectionUtils.isEmpty(services)) {
        ServiceException e = new ServiceException(String.format("No available admin server." + " Maybe because of meta server down or all admin server down. " + "Meta server address: %s", portalMetaDomainService.getDomain(env)));
        ct.setStatus(e);
        ct.complete();
        throw e;
    }
    return services;
}
Also used : ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO)

Example 9 with ServiceException

use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.

the class NamespaceService method save.

@Transactional
public Namespace save(Namespace entity) {
    if (!isNamespaceUnique(entity.getAppId(), entity.getClusterName(), entity.getNamespaceName())) {
        throw new ServiceException("namespace not unique");
    }
    // protection
    entity.setId(0);
    Namespace namespace = namespaceRepository.save(entity);
    auditService.audit(Namespace.class.getSimpleName(), namespace.getId(), Audit.OP.INSERT, namespace.getDataChangeCreatedBy());
    return namespace;
}
Also used : ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with ServiceException

use of com.ctrip.framework.apollo.common.exception.ServiceException in project apollo by ctripcorp.

the class ControllerExceptionTest method createFailed.

@Test(expected = ServiceException.class)
public void createFailed() {
    AppDTO dto = generateSampleDTOData();
    when(appService.findOne(any(String.class))).thenReturn(null);
    when(adminService.createNewApp(any(App.class))).thenThrow(new ServiceException("create app failed"));
    appController.create(dto);
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) AppDTO(com.ctrip.framework.apollo.common.dto.AppDTO) Test(org.junit.Test)

Aggregations

ServiceException (com.ctrip.framework.apollo.common.exception.ServiceException)11 Transactional (org.springframework.transaction.annotation.Transactional)4 App (com.ctrip.framework.apollo.common.entity.App)3 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)3 IOException (java.io.IOException)3 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)2 Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)2 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)1 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)1 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)1 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)1 NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)1 InputStream (java.io.InputStream)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 Test (org.junit.Test)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1