Search in sources :

Example 1 with StatusCode

use of com.robo4j.socket.http.enums.StatusCode in project robo4j by Robo4J.

the class ChannelResponseBuffer method extractDecoratedResponseByStringMessage.

// TODO: 3/5/18 (miro) investigate spring responseBody
private HttpDecoratedResponse extractDecoratedResponseByStringMessage(String message) {
    final String[] headerAndBody = message.split(HTTP_HEADER_BODY_DELIMITER);
    final String[] header = headerAndBody[POSITION_HEADER].split("[" + HTTP_NEW_LINE + "]+");
    final String firstLine = RoboHttpUtils.correctLine(header[0]);
    final String[] tokens = firstLine.split(HttpConstant.HTTP_EMPTY_SEP);
    final String[] paramArray = Arrays.copyOfRange(header, 1, header.length);
    final String version = tokens[0];
    final StatusCode statusCode = StatusCode.getByCode(Integer.valueOf(tokens[1]));
    final Map<String, String> headerParams = ChannelBufferUtils.getHeaderParametersByArray(paramArray);
    HttpResponseDenominator denominator = new HttpResponseDenominator(statusCode, HttpVersion.getByValue(version));
    HttpDecoratedResponse result = new HttpDecoratedResponse(headerParams, denominator);
    if (headerAndBody.length > 1) {
        if (headerParams.containsKey(HttpHeaderFieldNames.CONTENT_LENGTH)) {
            result.setLength(ChannelBufferUtils.calculateMessageSize(headerAndBody[POSITION_HEADER].length(), headerParams));
        } else {
            result.setLength(headerAndBody[POSITION_BODY].length());
        }
        Matcher matcher = ChannelBufferUtils.RESPONSE_SPRING_PATTERN.matcher(headerAndBody[POSITION_BODY]);
        if (matcher.find()) {
            // result.addMessage(headerAndBody[POSITION_BODY]);
            result.addMessage(matcher.group(ChannelBufferUtils.RESPONSE_JSON_GROUP));
        }
    }
    return result;
}
Also used : HttpDecoratedResponse(com.robo4j.socket.http.message.HttpDecoratedResponse) HttpResponseDenominator(com.robo4j.socket.http.message.HttpResponseDenominator) Matcher(java.util.regex.Matcher) StatusCode(com.robo4j.socket.http.enums.StatusCode)

Aggregations

StatusCode (com.robo4j.socket.http.enums.StatusCode)1 HttpDecoratedResponse (com.robo4j.socket.http.message.HttpDecoratedResponse)1 HttpResponseDenominator (com.robo4j.socket.http.message.HttpResponseDenominator)1 Matcher (java.util.regex.Matcher)1