Search in sources :

Example 1 with RestClientException

use of org.springframework.web.client.RestClientException in project spring-boot-admin by codecentric.

the class ApplicationRegistratorTest method register_multiple_all_failures.

@Test
public void register_multiple_all_failures() {
    adminProps.setRegisterOnce(false);
    when(restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class), eq(Map.class))).thenThrow(new RestClientException("Error")).thenThrow(new RestClientException("Error"));
    assertFalse(registrator.register());
}
Also used : RestClientException(org.springframework.web.client.RestClientException) Test(org.junit.Test)

Example 2 with RestClientException

use of org.springframework.web.client.RestClientException in project spring-security-oauth by spring-projects.

the class ServerRunning method apply.

public Statement apply(final Statement base, FrameworkMethod method, Object target) {
    // Check at the beginning, so this can be used as a static field
    if (assumeOnline) {
        Assume.assumeTrue(serverOnline.get(port));
    } else {
        Assume.assumeTrue(serverOffline.get(port));
    }
    RestTemplate client = new RestTemplate();
    boolean followRedirects = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    boolean online = false;
    try {
        client.getForEntity(new UriTemplate(getUrl("/sparklr2/login.jsp")).toString(), String.class);
        online = true;
        logger.info("Basic connectivity test passed");
    } catch (RestClientException e) {
        logger.warn(String.format("Not executing tests because basic connectivity test failed for hostName=%s, port=%d", hostName, port), e);
        if (assumeOnline) {
            Assume.assumeNoException(e);
        }
    } finally {
        HttpURLConnection.setFollowRedirects(followRedirects);
        if (online) {
            serverOffline.put(port, false);
            if (!assumeOnline) {
                Assume.assumeTrue(serverOffline.get(port));
            }
        } else {
            serverOnline.put(port, false);
        }
    }
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            try {
                postForStatus("/sparklr2/oauth/uncache_approvals", new LinkedMultiValueMap<String, String>());
                base.evaluate();
            } finally {
                postForStatus("/sparklr2/oauth/cache_approvals", new LinkedMultiValueMap<String, String>());
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) RestTemplate(org.springframework.web.client.RestTemplate) RestClientException(org.springframework.web.client.RestClientException) UriTemplate(org.springframework.web.util.UriTemplate)

Example 3 with RestClientException

use of org.springframework.web.client.RestClientException in project spring-security-oauth by spring-projects.

the class ServerRunning method apply.

public Statement apply(final Statement base, FrameworkMethod method, Object target) {
    // Check at the beginning, so this can be used as a static field
    if (assumeOnline) {
        Assume.assumeTrue(serverOnline.get(port));
    } else {
        Assume.assumeTrue(serverOffline.get(port));
    }
    RestTemplate client = new RestTemplate();
    boolean followRedirects = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    boolean online = false;
    try {
        client.getForEntity(new UriTemplate(getUrl("/sparklr2/login.jsp")).toString(), String.class);
        online = true;
        logger.info("Basic connectivity test passed");
    } catch (RestClientException e) {
        logger.warn(String.format("Not executing tests because basic connectivity test failed for hostName=%s, port=%d", hostName, port), e);
        if (assumeOnline) {
            Assume.assumeNoException(e);
        }
    } finally {
        HttpURLConnection.setFollowRedirects(followRedirects);
        if (online) {
            serverOffline.put(port, false);
            if (!assumeOnline) {
                Assume.assumeTrue(serverOffline.get(port));
            }
        } else {
            serverOnline.put(port, false);
        }
    }
    final RestOperations savedClient = getRestTemplate();
    postForStatus(savedClient, "/sparklr2/oauth/uncache_approvals", new LinkedMultiValueMap<String, String>());
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            try {
                base.evaluate();
            } finally {
                postForStatus(savedClient, "/sparklr2/oauth/cache_approvals", new LinkedMultiValueMap<String, String>());
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) RestTemplate(org.springframework.web.client.RestTemplate) RestClientException(org.springframework.web.client.RestClientException) UriTemplate(org.springframework.web.util.UriTemplate) RestOperations(org.springframework.web.client.RestOperations)

Example 4 with RestClientException

use of org.springframework.web.client.RestClientException in project portal by ixinportal.

the class WeixinUtil method getAccTokenFromWeixin.

/**
 * 获得token
 */
public void getAccTokenFromWeixin() {
    synchronized (atLock) {
        String tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}";
        try {
            Long nowDate = System.currentTimeMillis();
            if (ACCESS_TOKEN != null && new Date(nowDate + inDateTime).before(ACCESS_TOKEN.getInDate()))
                return;
            ByteArrayResource retRes = restTemplate.getForObject(tokenUrl, ByteArrayResource.class, appid, secret);
            Map<String, Object> tokenMap = jsonTool.readValue(new String(retRes.getByteArray()), Map.class);
            if (tokenMap == null || tokenMap.isEmpty()) {
                log.error("微信获取token失败,返回为空");
                return;
            }
            Integer ei = (Integer) tokenMap.get("expires_in");
            Date inDate = new Date(nowDate + ei * 1000);
            AccToken ACCESS_TOKEN_TMP = new AccToken((String) tokenMap.get("access_token"), inDate);
            ACCESS_TOKEN = ACCESS_TOKEN_TMP;
        } catch (RestClientException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RestClientException(org.springframework.web.client.RestClientException) ByteArrayResource(org.springframework.core.io.ByteArrayResource) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Date(java.util.Date)

Example 5 with RestClientException

use of org.springframework.web.client.RestClientException in project portal by ixinportal.

the class AuthService method getToken.

// @Autowired
// private static RealNameAuthenticationSerivceImpl authenticationSerivceImpl = new RealNameAuthenticationSerivceImpl();
/**
 * 获得token
 * 访问其他接口的时候 在头信息增加 Authorization :bear+token bear和token之间加个空格
 */
private static String getToken() {
    // client_secret:43b484748d3a936330bc50da70d6ce69e1dfef90
    try {
        // RealNameAuthentication realNameAuthentication =  authenticationSerivceImpl.getRealNameAuthenticationExample(new RealNameAuthenticationExample());
        long nowDate = System.currentTimeMillis();
        if (ACCESS_TOKEN != null && new Date(nowDate + inDateTime).before(inDate)) {
            return ACCESS_TOKEN;
        }
        RealNameAuthentication realNameAuthentication = CacheCustomer.getAUTH_CONFIG_MAP().get(2);
        if (realNameAuthentication == null) {
            List<RealNameAuthentication> list = SpringContextHolder.getBean(SqlSession.class).selectList("com.itrus.portal.db.RealNameAuthenticationMapper.selectByExample", new RealNameAuthenticationExample());
            if (list == null || list.isEmpty()) {
                return null;
            }
            for (RealNameAuthentication nameAuthentication : list) {
                if (nameAuthentication.getType() == 2)
                    realNameAuthentication = nameAuthentication;
            }
            if (realNameAuthentication == null) {
                return null;
            }
        }
        Map params = new HashMap();
        params.put("grant_type", "client_credentials");
        params.put("client_id", realNameAuthentication.getIdCode());
        params.put("client_secret", realNameAuthentication.getKeyCode());
        String rep = HttpClientUtil.postForm(realNameAuthentication.getAccessTokenaddress() + TOKEN, null, params);
        // System.out.println("AuthService getToken()_rep : " + rep);
        JSONObject data = JSON.parseObject(rep);
        ACCESS_TOKEN = data.getString("access_token");
        inDate = new Date(nowDate + data.getLongValue("expires_in"));
        return ACCESS_TOKEN;
    } catch (RestClientException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SqlSession(org.apache.ibatis.session.SqlSession) HashMap(java.util.HashMap) IOException(java.io.IOException) RealNameAuthentication(com.itrus.portal.db.RealNameAuthentication) JsonParseException(com.fasterxml.jackson.core.JsonParseException) Date(java.util.Date) RestClientException(org.springframework.web.client.RestClientException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) RealNameAuthenticationExample(com.itrus.portal.db.RealNameAuthenticationExample) JSONObject(com.alibaba.fastjson.JSONObject) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RestClientException(org.springframework.web.client.RestClientException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

RestClientException (org.springframework.web.client.RestClientException)43 HttpHeaders (org.springframework.http.HttpHeaders)12 HttpEntity (org.springframework.http.HttpEntity)11 IOException (java.io.IOException)9 RestTemplate (org.springframework.web.client.RestTemplate)9 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)8 Test (org.junit.Test)5 JsonParseException (com.fasterxml.jackson.core.JsonParseException)4 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)4 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)4 URI (java.net.URI)3 List (java.util.List)3 Test (org.junit.jupiter.api.Test)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 RealNameAuthentication (com.itrus.portal.db.RealNameAuthentication)2 RealNameAuthenticationExample (com.itrus.portal.db.RealNameAuthenticationExample)2 GitHubDTO (com.nixmash.blog.jpa.dto.GitHubDTO)2