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());
}
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>());
}
}
};
}
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>());
}
}
};
}
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();
}
}
}
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;
}
Aggregations