Search in sources :

Example 1 with HttpResponseDenominator

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

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

Example 3 with HttpResponseDenominator

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

the class WriteSelectionKeyHandler method handle.

@Override
public SelectionKey handle() {
    SocketChannel channel = (SocketChannel) key.channel();
    final HttpResponseProcess responseProcess = outBuffers.get(key);
    ByteBuffer buffer;
    if (responseProcess.getMethod() != null) {
        switch(responseProcess.getMethod()) {
            case GET:
                String getResponse;
                if (responseProcess.getResult() != null && responseProcess.getCode().equals(StatusCode.OK)) {
                    // FIXME: 2/18/18 (miro) put abstraction
                    String responseMessage = responseProcess.getResult().toString();
                    HttpDenominator denominator = new HttpResponseDenominator(responseProcess.getCode(), HttpVersion.HTTP_1_1);
                    getResponse = HttpMessageBuilder.Build().setDenominator(denominator).addHeaderElement(HttpHeaderFieldNames.ROBO_UNIT_UID, context.getId()).addHeaderElement(HttpHeaderFieldNames.CONTENT_LENGTH, String.valueOf(responseMessage.length())).build(responseMessage);
                } else {
                    HttpDenominator denominator = new HttpResponseDenominator(responseProcess.getCode(), HttpVersion.HTTP_1_1);
                    getResponse = HttpMessageBuilder.Build().setDenominator(denominator).build();
                }
                buffer = ChannelBufferUtils.getByteBufferByString(getResponse);
                ChannelUtils.handleWriteChannelAndBuffer("get write", channel, buffer);
                break;
            case POST:
                HttpDenominator denominator = new HttpResponseDenominator(responseProcess.getCode(), HttpVersion.HTTP_1_1);
                String postResponse = HttpMessageBuilder.Build().setDenominator(denominator).build();
                if (responseProcess.getResult() != null && responseProcess.getCode().equals(StatusCode.ACCEPTED)) {
                    buffer = ChannelBufferUtils.getByteBufferByString(postResponse);
                    ChannelUtils.handleWriteChannelAndBuffer("post write", channel, buffer);
                    sendMessageToTargetRoboReference(responseProcess);
                } else {
                    buffer = ChannelBufferUtils.getByteBufferByString(postResponse);
                    ChannelUtils.handleWriteChannelAndBuffer("post write", channel, buffer);
                }
            default:
                break;
        }
    } else {
        HttpDenominator denominator = new HttpResponseDenominator(StatusCode.BAD_REQUEST, HttpVersion.HTTP_1_1);
        String badResponse = HttpMessageBuilder.Build().setDenominator(denominator).build();
        buffer = ChannelBufferUtils.getByteBufferByString(badResponse);
        try {
            ChannelUtils.writeBuffer(channel, buffer);
        } catch (Exception e) {
            throw new SocketException("post write", e);
        }
        buffer.clear();
    }
    try {
        key.cancel();
        key.channel().close();
    } catch (IOException e) {
        throw new SocketException(e.getMessage());
    }
    return key;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) HttpDenominator(com.robo4j.socket.http.message.HttpDenominator) SocketException(com.robo4j.socket.http.SocketException) HttpResponseDenominator(com.robo4j.socket.http.message.HttpResponseDenominator) HttpResponseProcess(com.robo4j.socket.http.request.HttpResponseProcess) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SocketException(com.robo4j.socket.http.SocketException) IOException(java.io.IOException)

Aggregations

HttpResponseDenominator (com.robo4j.socket.http.message.HttpResponseDenominator)3 HttpDecoratedResponse (com.robo4j.socket.http.message.HttpDecoratedResponse)2 SocketException (com.robo4j.socket.http.SocketException)1 StatusCode (com.robo4j.socket.http.enums.StatusCode)1 HttpDenominator (com.robo4j.socket.http.message.HttpDenominator)1 HttpResponseProcess (com.robo4j.socket.http.request.HttpResponseProcess)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 SocketChannel (java.nio.channels.SocketChannel)1 Matcher (java.util.regex.Matcher)1