Search in sources :

Example 1 with HttpDecoratedResponse

use of com.robo4j.socket.http.message.HttpDecoratedResponse in project robo4j by Robo4J.

the class OutboundHttpSocketChannelHandler method getDecoratedResponse.

private HttpDecoratedResponse getDecoratedResponse(ByteChannel byteChannel, HttpPathMethodDTO pathMethod) {
    lock.lock();
    try {
        final HttpDecoratedResponse result = channelResponseBuffer.getHttpDecoratedResponseByChannel(byteChannel);
        result.addCallbacks(pathMethod.getCallbacks());
        return result;
    } catch (IOException e) {
        throw new SocketException("message body write problem", e);
    } finally {
        lock.unlock();
    }
}
Also used : HttpDecoratedResponse(com.robo4j.socket.http.message.HttpDecoratedResponse) SocketException(com.robo4j.socket.http.SocketException) IOException(java.io.IOException)

Example 2 with HttpDecoratedResponse

use of com.robo4j.socket.http.message.HttpDecoratedResponse in project robo4j by Robo4J.

the class HttpClientUnit method onMessage.

@Override
public void onMessage(HttpDecoratedRequest message) {
    final HttpDecoratedRequest request = adjustRequest(message);
    if (message.getDenominator() == null) {
        SimpleLoggingUtil.info(getClass(), String.format("recofigured host: %s, port: %d", message.getHost(), message.getPort()));
        return;
    }
    final InetSocketAddress address = new InetSocketAddress(request.getHost(), request.getPort());
    try (SocketChannel channel = SocketChannel.open(address)) {
        if (bufferCapacity != null) {
            channel.socket().setSendBufferSize(bufferCapacity);
        }
        final HttpDecoratedResponse decoratedResponse = processRequestByChannel(channel, request);
        if (decoratedResponse != null && PROCESS_RESPONSES_STATUSES.contains(decoratedResponse.getCode())) {
            if (!decoratedResponse.getCallbacks().isEmpty()) {
                sendMessageToCallbacks(decoratedResponse.getCallbacks(), decoratedResponse.getMessage());
            }
        } else {
            SimpleLoggingUtil.error(getClass(), String.format("no callback or wrong response: %s", decoratedResponse));
        }
    } catch (Exception e) {
        SimpleLoggingUtil.error(getClass(), String.format("not available: %s, no worry I continue sending. Error: %s", address, e));
    }
}
Also used : HttpDecoratedRequest(com.robo4j.socket.http.message.HttpDecoratedRequest) SocketChannel(java.nio.channels.SocketChannel) HttpDecoratedResponse(com.robo4j.socket.http.message.HttpDecoratedResponse) InetSocketAddress(java.net.InetSocketAddress) ConfigurationException(com.robo4j.ConfigurationException)

Example 3 with HttpDecoratedResponse

use of com.robo4j.socket.http.message.HttpDecoratedResponse in project robo4j by Robo4J.

the class ChannelResponseBuffer method getHttpDecoratedResponseByChannel.

public HttpDecoratedResponse getHttpDecoratedResponseByChannel(ByteChannel channel) throws IOException {
    final StringBuilder sbBasic = new StringBuilder();
    int readBytes = channel.read(responseBuffer);
    if (readBytes != BUFFER_MARK_END) {
        responseBuffer.flip();
        ChannelBufferUtils.addToStringBuilder(sbBasic, responseBuffer, readBytes);
        final HttpDecoratedResponse result = extractDecoratedResponseByStringMessage(sbBasic.toString());
        ChannelBufferUtils.readChannelBuffer(result, channel, responseBuffer, readBytes);
        responseBuffer.clear();
        return result;
    } else {
        return new HttpDecoratedResponse(new HashMap<>(), new HttpResponseDenominator(StatusCode.BAD_REQUEST, HttpVersion.HTTP_1_1));
    }
}
Also used : HttpDecoratedResponse(com.robo4j.socket.http.message.HttpDecoratedResponse) HttpResponseDenominator(com.robo4j.socket.http.message.HttpResponseDenominator)

Example 4 with HttpDecoratedResponse

use of com.robo4j.socket.http.message.HttpDecoratedResponse 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

HttpDecoratedResponse (com.robo4j.socket.http.message.HttpDecoratedResponse)4 HttpResponseDenominator (com.robo4j.socket.http.message.HttpResponseDenominator)2 ConfigurationException (com.robo4j.ConfigurationException)1 SocketException (com.robo4j.socket.http.SocketException)1 StatusCode (com.robo4j.socket.http.enums.StatusCode)1 HttpDecoratedRequest (com.robo4j.socket.http.message.HttpDecoratedRequest)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketChannel (java.nio.channels.SocketChannel)1 Matcher (java.util.regex.Matcher)1