Search in sources :

Example 26 with DtCenterDefException

use of com.dtstack.taier.common.exception.DtCenterDefException in project Taier by DTStack.

the class PoolHttpClient method get.

/**
 * @param url
 * @param cookies
 * @param headers
 * @param dealWithRespBody true :deal with RespBody,false: Don't deal with RespBody
 * @return
 */
public static String get(String url, Map<String, Object> cookies, Map<String, Object> headers, Boolean dealWithRespBody) {
    HttpGet httpGet = new HttpGet(url);
    setConfig(httpGet);
    if (cookies != null && cookies.size() > 0) {
        httpGet.setHeader("Cookie", getCookieFormate(cookies));
    }
    if (headers != null && !headers.isEmpty()) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            httpGet.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
        }
    }
    // true :默认逻辑处理 false:直接返回返回结果 不对response进行任何处理 直接返回
    try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity, Charsets.UTF_8);
        if (BooleanUtils.isTrue(dealWithRespBody)) {
            String respBody = null;
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                respBody = result;
            } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
                throw new DtCenterDefException("登陆状态失效");
            } else {
                logger.error("request url:{} fail:{}", url, result);
            }
            return respBody;
        } else {
            return result;
        }
    } catch (Exception e) {
        logger.error("url:{}--->http request error:{}", url, e);
    }
    return null;
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) Map(java.util.Map) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) IOException(java.io.IOException)

Aggregations

DtCenterDefException (com.dtstack.taier.common.exception.DtCenterDefException)26 IOException (java.io.IOException)11 JSONObject (com.alibaba.fastjson.JSONObject)7 RdosDefineException (com.dtstack.taier.common.exception.RdosDefineException)7 SftpException (com.jcraft.jsch.SftpException)6 IHdfsFile (com.dtstack.dtcenter.loader.client.IHdfsFile)5 HdfsSourceDTO (com.dtstack.dtcenter.loader.dto.source.HdfsSourceDTO)5 UnknownHostException (java.net.UnknownHostException)5 Matcher (java.util.regex.Matcher)4 ArrayList (java.util.ArrayList)3 IClient (com.dtstack.dtcenter.loader.client.IClient)2 IKerberos (com.dtstack.dtcenter.loader.client.IKerberos)2 ColumnMetaDTO (com.dtstack.dtcenter.loader.dto.ColumnMetaDTO)2 SqlQueryDTO (com.dtstack.dtcenter.loader.dto.SqlQueryDTO)2 ISourceDTO (com.dtstack.dtcenter.loader.dto.source.ISourceDTO)2 DataSourceTypeEnum (com.dtstack.taier.common.enums.DataSourceTypeEnum)2 PubSvcDefineException (com.dtstack.taier.common.exception.PubSvcDefineException)2 DsInfo (com.dtstack.taier.dao.domain.DsInfo)2 File (java.io.File)2 HttpEntity (org.apache.http.HttpEntity)2