Search in sources :

Example 11 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project hive by apache.

the class TestHS2HttpServer method testConfStrippedFromWebUI.

@Test
public void testConfStrippedFromWebUI() throws Exception {
    String pwdValFound = null;
    String pwdKeyFound = null;
    CloseableHttpClient httpclient = null;
    try {
        httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://localhost:" + webUIPort + "/conf");
        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        try {
            HttpEntity entity1 = response1.getEntity();
            BufferedReader br = new BufferedReader(new InputStreamReader(entity1.getContent()));
            String line;
            while ((line = br.readLine()) != null) {
                if (line.contains(metastorePasswd)) {
                    pwdValFound = line;
                }
                if (line.contains(ConfVars.METASTOREPWD.varname)) {
                    pwdKeyFound = line;
                }
            }
            EntityUtils.consume(entity1);
        } finally {
            response1.close();
        }
    } finally {
        if (httpclient != null) {
            httpclient.close();
        }
    }
    assertNotNull(pwdKeyFound);
    assertNull(pwdValFound);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader) Test(org.junit.Test)

Example 12 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project weixin-java-tools by chanjarster.

the class MaterialDeleteRequestExecutor method execute.

public Boolean execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    Map<String, String> params = new HashMap<>();
    params.put("media_id", materialId);
    httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
    CloseableHttpResponse response = httpclient.execute(httpPost);
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    WxError error = WxError.fromJson(responseContent);
    if (error.getErrorCode() != 0) {
        throw new WxErrorException(error);
    } else {
        return true;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) StringEntity(org.apache.http.entity.StringEntity) WxError(me.chanjar.weixin.common.bean.result.WxError) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 13 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project weixin-java-tools by chanjarster.

the class MaterialNewsInfoRequestExecutor method execute.

public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    Map<String, String> params = new HashMap<>();
    params.put("media_id", materialId);
    httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
    CloseableHttpResponse response = httpclient.execute(httpPost);
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    WxError error = WxError.fromJson(responseContent);
    if (error.getErrorCode() != 0) {
        throw new WxErrorException(error);
    } else {
        return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) StringEntity(org.apache.http.entity.StringEntity) WxError(me.chanjar.weixin.common.bean.result.WxError) HashMap(java.util.HashMap) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 14 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project weixin-java-tools by chanjarster.

the class QrCodeRequestExecutor method execute.

@Override
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException {
    if (ticket != null) {
        if (uri.indexOf('?') == -1) {
            uri += '?';
        }
        uri += uri.endsWith("?") ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
    }
    HttpGet httpGet = new HttpGet(uri);
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpGet.setConfig(config);
    }
    try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
        Header[] contentTypeHeader = response.getHeaders("Content-Type");
        if (contentTypeHeader != null && contentTypeHeader.length > 0) {
            // 出错
            if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
                String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
                throw new WxErrorException(WxError.fromJson(responseContent));
            }
        }
        InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
        File localFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
        return localFile;
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) Header(org.apache.http.Header) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) File(java.io.File) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException)

Example 15 with CloseableHttpResponse

use of org.apache.http.client.methods.CloseableHttpResponse in project weixin-java-tools by chanjarster.

the class WxMpServiceImpl method sendRedpack.

@Override
public WxRedpackResult sendRedpack(Map<String, String> parameters) throws WxErrorException {
    String nonce_str = System.currentTimeMillis() + "";
    SortedMap<String, String> packageParams = new TreeMap<String, String>(parameters);
    packageParams.put("wxappid", wxMpConfigStorage.getAppId());
    packageParams.put("mch_id", wxMpConfigStorage.getPartnerId());
    packageParams.put("nonce_str", nonce_str);
    String sign = WxCryptUtil.createSign(packageParams, wxMpConfigStorage.getPartnerKey());
    packageParams.put("sign", sign);
    StringBuilder request = new StringBuilder("<xml>");
    for (Entry<String, String> para : packageParams.entrySet()) {
        request.append(String.format("<%s>%s</%s>", para.getKey(), para.getValue(), para.getKey()));
    }
    request.append("</xml>");
    HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");
    if (httpProxy != null) {
        RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
        httpPost.setConfig(config);
    }
    StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
    httpPost.setEntity(entity);
    try {
        CloseableHttpResponse response = getHttpclient().execute(httpPost);
        String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
        XStream xstream = XStreamInitializer.getInstance();
        xstream.processAnnotations(WxRedpackResult.class);
        WxRedpackResult wxMpRedpackResult = (WxRedpackResult) xstream.fromXML(responseContent);
        return wxMpRedpackResult;
    } catch (IOException e) {
        log.error(MessageFormatter.format("The exception was happened when sending redpack '{}'.", request.toString()).getMessage(), e);
        WxError error = new WxError();
        error.setErrorCode(-1);
        throw new WxErrorException(error);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) RequestConfig(org.apache.http.client.config.RequestConfig) WxError(me.chanjar.weixin.common.bean.result.WxError) XStream(com.thoughtworks.xstream.XStream) IOException(java.io.IOException) TreeMap(java.util.TreeMap) WxErrorException(me.chanjar.weixin.common.exception.WxErrorException) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) WxRedpackResult(me.chanjar.weixin.mp.bean.result.WxRedpackResult)

Aggregations

CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1314 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)502 HttpGet (org.apache.http.client.methods.HttpGet)485 Test (org.junit.Test)370 IOException (java.io.IOException)362 HttpPost (org.apache.http.client.methods.HttpPost)286 HttpEntity (org.apache.http.HttpEntity)248 StringEntity (org.apache.http.entity.StringEntity)229 JsonNode (com.fasterxml.jackson.databind.JsonNode)126 StatusLine (org.apache.http.StatusLine)121 URI (java.net.URI)118 InputStream (java.io.InputStream)112 ArrayList (java.util.ArrayList)87 Deployment (org.activiti.engine.test.Deployment)87 RequestConfig (org.apache.http.client.config.RequestConfig)87 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)87 HttpPut (org.apache.http.client.methods.HttpPut)79 Map (java.util.Map)75 Header (org.apache.http.Header)75 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)73