Search in sources :

Example 1 with Request

use of info.xiancloud.core.util.http.Request in project xian by happyyangyuan.

the class HttpUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    ObjectInputStream ois = null;
    try {
        String reqBase64 = msg.get("req", String.class);
        ois = new ObjectInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(reqBase64)));
        Request request = (Request) ois.readObject();
        Response response;
        try {
            response = request.executeLocal();
        } catch (ConnectException e) {
            return UnitResponse.error(ISocketGroup.CODE_CONNECT_TIMEOUT, null, "Connect timeout: " + request.getUrl());
        } catch (SocketTimeoutException e) {
            return UnitResponse.error(ISocketGroup.CODE_SOCKET_TIMEOUT, null, "Read timeout: " + request.getUrl());
        } catch (Throwable e) {
            return UnitResponse.exception(e);
        }
        JSONObject retJson = new JSONObject();
        retJson.put("status", response.getStatus());
        retJson.put("headers", response.getHeaders());
        retJson.put("entity", response.string());
        return UnitResponse.success(retJson);
    } catch (Throwable e) {
        return UnitResponse.exception(e);
    } finally {
    }
}
Also used : UnitResponse(info.xiancloud.core.message.UnitResponse) Response(info.xiancloud.core.util.http.Response) SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(com.alibaba.fastjson.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) Request(info.xiancloud.core.util.http.Request) UnitRequest(info.xiancloud.core.message.UnitRequest) ObjectInputStream(java.io.ObjectInputStream) ConnectException(java.net.ConnectException)

Example 2 with Request

use of info.xiancloud.core.util.http.Request in project xian by happyyangyuan.

the class QCloudBaseUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    // 填充请求参数
    TreeMap<String, String> params = fillRequestArgs(msg);
    // 生成签名
    String sign = createSignature(params);
    params.put("Signature", sign);
    LOG.info(String.format("腾讯云API调用,请求参数:%s", JSON.toJSONString(params)));
    try {
        Request request = HttpKit.get("https://" + getAPIHost() + HHTP_APIURL);
        for (Entry<String, String> param : params.entrySet()) {
            request.addParam(param.getKey(), param.getValue());
        }
        request.setSSL(null, null);
        String result = request.executeLocal().string();
        return UnitResponse.success(convert(result));
    } catch (ConnectException e) {
        LOG.error("调用腾讯云API连接超时", e);
        throw new RuntimeException("调用腾讯云API连接超时");
    } catch (SocketTimeoutException e) {
        LOG.error("调用腾讯云API响应超时", e);
        throw new RuntimeException("调用腾讯云API响应超时");
    } catch (Exception e) {
        LOG.error("调用腾讯云API出错", e);
        throw new RuntimeException("调用腾讯云API出错");
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) Request(info.xiancloud.core.util.http.Request) UnitRequest(info.xiancloud.core.message.UnitRequest) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException)

Aggregations

UnitRequest (info.xiancloud.core.message.UnitRequest)2 Request (info.xiancloud.core.util.http.Request)2 ConnectException (java.net.ConnectException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 JSONObject (com.alibaba.fastjson.JSONObject)1 UnitResponse (info.xiancloud.core.message.UnitResponse)1 Response (info.xiancloud.core.util.http.Response)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ObjectInputStream (java.io.ObjectInputStream)1