Search in sources :

Example 1 with JsonValueSource

use of com.terran4j.commons.util.value.JsonValueSource in project commons by terran4j.

the class Request method exe.

public Response exe() throws HttpException {
    // 获取实际的 URL。
    String actualURL = getActualURL();
    HttpRequest request = new HttpRequest(actualURL);
    // 获取实际的入参。
    final Map<String, String> actualParams = getActualParams();
    request.setParam(actualParams);
    RequestMethod method = RequestMethod.GET;
    String methodName = action.getMethod();
    if (StringUtils.hasText(methodName)) {
        method = RequestMethod.valueOf(methodName);
        if (method == null) {
            String msg = String.format("http method[%s] not supported in action: %s", methodName, action.getId());
            throw new UnsupportedOperationException(msg);
        }
    }
    request.setMethod(method);
    Map<String, String> actualHeaders = getActualHeaders();
    request.setHeaders(actualHeaders);
    List<HttpClientListener> listeners = action.getHttpClient().getListeners();
    for (HttpClientListener listener : listeners) {
        listener.beforeExecute(request);
    }
    String response = request.execute();
    for (HttpClientListener listener : listeners) {
        response = listener.afterExecute(request, response);
    }
    JsonElement result;
    try {
        result = parser.parse(response);
    } catch (JsonSyntaxException e) {
        // 不是 json 串,就按普通的字符串来处理。
        result = new JsonPrimitive(response);
    }
    List<Write> writes = action.getWrites();
    if (writes != null && writes.size() > 0) {
        JsonObject resultObject = null;
        if (result.isJsonObject()) {
            resultObject = result.getAsJsonObject();
        }
        context.push(new JsonValueSource(resultObject));
        for (Write write : writes) {
            write.doWrite(session, context);
        }
        context.pop();
    }
    return new Response(result, session);
}
Also used : RequestMethod(org.springframework.web.bind.annotation.RequestMethod) JsonValueSource(com.terran4j.commons.util.value.JsonValueSource)

Aggregations

JsonValueSource (com.terran4j.commons.util.value.JsonValueSource)1 RequestMethod (org.springframework.web.bind.annotation.RequestMethod)1