HUE 接口调用
hue接口调用首先实现登陆功能
注意hue登陆接口调用第一次调用/hue/accounts/login/请求方式为get
返回值中的csrfToken和sessionId作为第二次post请求/hue/accounts/login/的参数
返回值作为其它接口的认证参数具体代码如下
package com.para.monitor.cluster.hue.commen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import javax.annotation.Resource;
import java.net.URI;
import java.util.List;
import java.util.Map;
import static java.lang.String.join;
/**
* hue 配置
*/
@Configuration
public class HueUiConf implements InitializingBean {
private static final Logger logger = LoggerFactory.getLogger(HueUiConf.class);
@Value("${hue.host}")
private String hueHost;
@Value("${hue.username}")
private String hueUserName;
@Value("${hue.password}")
private String huePassword;
@Resource
private RestTemplate restTemplate;
private String csrfToken;
private String sessionId;
private HttpHeaders responseHeaders;
@Override
public void afterPropertiesSet() {
//curl -i -X GET https://demo.gethue.com/hue/accounts/login/?fromModal=true -o /dev/null -D -
String url = String.join("", hueHost, "hue/accounts/login/");
MultiValueMap multiValueMap = new LinkedMultiValueMap();
multiValueMap.add("fromModal", "true");
URI uri = UriComponentsBuilder.fromHttpUrl(url).queryParams(multiValueMap).build().toUri();
HttpHeaders headers = new HttpHeaders();
try {
ResponseEntity<String> exchange = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(headers), String.class);
String csrfToken = "", sessionId = "";
if (!exchange.getStatusCode().isError()) {
for (Map.Entry<String, List<String>> entry : exchange.getHeaders().entrySet()) {
if (HttpHeaders.SET_COOKIE.equals(entry.getKey())) {
List<String> value = entry.getValue();
if (0 < value.size()) {
csrfToken = value.get(0).split(";")[0];
sessionId = value.get(1).split(";")[0];
}
}
}
// 登录获取cookie
url = join("", hueHost, "hue/accounts/login/");
MultiValueMap<String, String> query = new LinkedMultiValueMap(2);
query.add("fromModal", "true");
MultiValueMap<String, String> body = new LinkedMultiValueMap(2);
body.add("username", hueUserName);
body.add("password", huePassword);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
httpHeaders.add(HttpHeaders.COOKIE, join(";", csrfToken, sessionId));
httpHeaders.set("X-CSRFToken", csrfToken.split("=")[1]);
URI loginUri = UriComponentsBuilder.fromHttpUrl(url).queryParams(query).build().toUri();
ResponseEntity<String> loginResult = restTemplate.exchange(loginUri, HttpMethod.POST, new HttpEntity<MultiValueMap>(body, httpHeaders), String.class);
if (!loginResult.getStatusCode().isError()) {
for (Map.Entry<String, List<String>> loginEntry : loginResult.getHeaders().entrySet()) {
if (HttpHeaders.SET_COOKIE.equals(loginEntry.getKey())) {
List<String> value = loginEntry.getValue();
if (0 < value.size()) {
this.csrfToken = value.get(0).split(";")[0];
this.sessionId = value.get(1).split(";")[0];
}
}
}
} else {
logger.error("hue 服务器错误,请检查相关配置!code:{}", loginResult.getStatusCode().value());
}
} else {
logger.error("hue 服务器错误,请检查相关配置!code:{}", exchange.getStatusCode().value());
}
} catch (Exception e) {
logger.error("Hue 服务器登录失败!", e.fillInStackTrace());
}
}
public String getCsrfToken() {
return csrfToken;
}
public void setCsrfToken(String csrfToken) {
this.csrfToken = csrfToken;
}
public String getSessionId() {
return sessionId;
}
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
public HttpHeaders getResponseHeaders() {
return responseHeaders;
}
public void setResponseHeaders(HttpHeaders responseHeaders) {
this.responseHeaders = responseHeaders;
}
}
接口调用使用示例
executeEngine:hive或者impala
public Object excuteSql( String executeEngine, String notebook, String snippet) throws UnsupportedEncodingException {
String url = join("", hueHost, "notebook/api/execute/", executeEngine);
String body = join("&", "notebook=" + URLEncoder.encode(notebook, Charset.forName("UTF-8").name()), "snippet=" + URLEncoder.encode(snippet, Charset.forName("UTF-8").name()));
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
httpHeaders.add(HttpHeaders.COOKIE, join(";", hueUiConf.getCsrfToken(), hueUiConf.getSessionId()));
if (Strings.isNotBlank(hueUiConf.getCsrfToken()) && 1 < hueUiConf.getCsrfToken().split("=").length)
httpHeaders.set("X-CSRFToken", hueUiConf.getCsrfToken().split("=")[1]);
httpHeaders.set("X-requested-with", "XMLHttpRequest");
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("notebook", URLEncoder.encode(notebook, Charset.forName("UTF-8").name()).toLowerCase());
params.add("snippet", URLEncoder.encode(snippet, Charset.forName("UTF-8").name()).toLowerCase());
return restTemplate.postForEntity(url, new HttpEntity<>(body, httpHeaders), String.class).getBody();
}