java调用azkaban接口

我也不知道为什么要写这一篇,其实我也不懂的,就用一下,就记录一下吧。----

使用RestTemplate调用接口、由于azkaban接口是https请求,需要重写RestTemplate,代码如下:

public class HttpClientUtils {
    public static CloseableHttpClient acceptsUntrustedCertsHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        HttpClientBuilder b = HttpClientBuilder.create();
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();
        b.setSslcontext(sslContext) ;
        HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
        Registry socketFactoryRegistry = RegistryBuilder.create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", sslSocketFactory)
                .build();

        PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        connMgr.setMaxTotal(200);
        connMgr.setDefaultMaxPerRoute(100);
        b.setConnectionManager(connMgr);

        CloseableHttpClient client = b.build();

        return client;
    }
}
@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate httpsRestTemplate(HttpComponentsClientHttpRequestFactory httpsFactory){
        RestTemplate restTemplate = new RestTemplate(httpsFactory);
        restTemplate.setErrorHandler(new ResponseErrorHandler() {
            @Override
            public boolean hasError(ClientHttpResponse clientHttpResponse) {
                return false;
            }

            @Override
            public void handleError(ClientHttpResponse clientHttpResponse) {
                //默认处理非200的返回,会抛异常
            }
        });
        return restTemplate;
    }

    @Bean(name = "httpsFactory")
    public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory() throws Exception{
        CloseableHttpClient httpClient = HttpClientUtils.acceptsUntrustedCertsHttpClient();
        HttpComponentsClientHttpRequestFactory httpsFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        httpsFactory.setReadTimeout(2000);
        httpsFactory.setConnectTimeout(2000);
        return httpsFactory;
    }


}

这样就能绕开SSL认证了,接下来直接调用接口

1.登陆azkaban

HttpHeaders hs = getHttpHeaders() ;
LinkedMultiValueMap linkedMultiValueMap = new LinkedMultiValueMap();
linkedMultiValueMap.add("action", "login");
linkedMultiValueMap.add("username", username);
linkedMultiValueMap.add("password", password);
HttpEntity> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String postForObject = restTemplate.postForObject(API, httpEntity, String.class);

由于请求头都差不多,后面的接口也会用到,于是抽了出来,是这样的:

HttpHeaders hs = new HttpHeaders();
hs.add("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
hs.add("X-Requested-With", "XMLHttpRequest");
hs.add("Accept", "text/plain;charset=utf-8");

2.创建项目

HttpHeaders hs = getHttpHeaders() ;
LinkedMultiValueMap linkedMultiValueMap = new LinkedMultiValueMap();
linkedMultiValueMap.add("session.id", sessionId);
linkedMultiValueMap.add("action", "create");
linkedMultiValueMap.add("name", proName);
linkedMultiValueMap.add("description", proDesc);
HttpEntity> httpEntity = new HttpEntity<>(linkedMultiValueMap, hs);
String postForObject = restTemplate.postForObject(API + "/manager", httpEntity, String.class);

到这里值得思考一下,是不是登陆的session是否会过期、其实我也不知道,懒得去查、但是故意写错session他是会返回“Invalid Session. Need to re-login”,大概是要重新登陆吧,至于怎么重新拿到session就看自己什么业务需求吧,重新登陆一哈就好了。

###3.修改项目描述

HttpHeaders hs = getHttpHeaders() ;
Map map = new HashMap<>();
map.put("id", sessionId);
map.put("project", proName);
map.put("description", desc) ;
ResponseEntity exchange = restTemplate.exchange(
                API + "/manager?session.id={id}&ajax=changeDescription&project={project}&description={description}", HttpMethod.GET,
                new HttpEntity(hs), String.class, map);

对的、只能修改项目描述、项目名称应该是不让修改的,至于说应该,因为不确定,至少没发现可以修改的接口,在azkaban页面也没有修改项目名称的地方,其实官方给出的api接口修改项目描述的也没有,我在azkaban页面看到的。

4.删除项目

HttpHeaders hs = getHttpHeaders() ;
Map map = new HashMap<>();
map.put("id", sessionId);
map.put("project", proName);
ResponseEntity exchange = restTemplate.exchange(
                API + "/manager?session.id={id}&delete=true&project={project}", HttpMethod.GET,
                new HttpEntity(hs), String.class, map);

5.上传zip文件

HttpHeaders hs = getHttpHeaders() ;
FileSystemResource resource = new FileSystemResource(new File(path));
LinkedMultiValueMap linkedMultiValueMap = new LinkedMultiValueMap();
linkedMultiValueMap.add("session.id", sessionId+"1");
linkedMultiValueMap.add("ajax", "upload");
linkedMultiValueMap.add("project", proName);
linkedMultiValueMap.add("file", resource);
String postForObject = restTemplate.postForObject(API + "/manager", linkedMultiValueMap, String.class);

6…

其实还有好多接口的、调用方式就和上面的雷同、大同小异吧,暂时写这么多。

官方接口地址: https://azkaban.github.io/azkaban/docs/latest/#ajax-api

你可能感兴趣的:(java,java)