Search in sources :

Example 1 with Response

use of info.xiancloud.core.util.http.Response 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)

Aggregations

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