Search in sources :

Example 1 with HttpDenominator

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

the class ByteBufferTests method byteBufferFromRequestTest.

@Test
void byteBufferFromRequestTest() {
    String bodyMessage = "this is test message";
    String host = "localhost";
    String clientPath = "/test";
    HttpDenominator denominator = new HttpRequestDenominator(HttpMethod.POST, clientPath, HttpVersion.HTTP_1_1);
    String postMessage = HttpMessageBuilder.Build().setDenominator(denominator).addHeaderElement(HttpHeaderFieldNames.CONTENT_LENGTH, String.valueOf(bodyMessage.length())).addHeaderElement(HttpHeaderFieldNames.HOST, RoboHttpUtils.createHost(host, ProtocolType.HTTP.getPort())).build(bodyMessage);
    HttpDecoratedRequest decoratedRequest = ChannelBufferUtils.extractDecoratedRequestByStringMessage(postMessage);
    assertNotNull(postMessage);
    assertEquals(postMessage.length(), decoratedRequest.getLength());
    assertEquals(clientPath, decoratedRequest.getPathMethod().getPath());
    assertEquals(bodyMessage, decoratedRequest.getMessage());
}
Also used : HttpDecoratedRequest(com.robo4j.socket.http.message.HttpDecoratedRequest) HttpDenominator(com.robo4j.socket.http.message.HttpDenominator) HttpRequestDenominator(com.robo4j.socket.http.message.HttpRequestDenominator) Test(org.junit.jupiter.api.Test)

Example 2 with HttpDenominator

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

the class HttpHeaderTests method extractHeaderParameter.

@Test
void extractHeaderParameter() {
    String message = "message";
    HttpDenominator denominator = new HttpRequestDenominator(HttpMethod.POST, HttpVersion.HTTP_1_1);
    String postRequest = HttpMessageBuilder.Build().setDenominator(denominator).addHeaderElement(HttpHeaderFieldNames.HOST, RoboHttpUtils.createHost("127.0.0.1", ProtocolType.HTTP.getPort())).addHeaderElement(HttpHeaderFieldNames.CONTENT_LENGTH, String.valueOf(message.length())).build(message);
    System.out.println("HEADER: " + postRequest);
}
Also used : HttpDenominator(com.robo4j.socket.http.message.HttpDenominator) HttpRequestDenominator(com.robo4j.socket.http.message.HttpRequestDenominator) Test(org.junit.jupiter.api.Test)

Example 3 with HttpDenominator

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

HttpDenominator (com.robo4j.socket.http.message.HttpDenominator)3 HttpRequestDenominator (com.robo4j.socket.http.message.HttpRequestDenominator)2 Test (org.junit.jupiter.api.Test)2 SocketException (com.robo4j.socket.http.SocketException)1 HttpDecoratedRequest (com.robo4j.socket.http.message.HttpDecoratedRequest)1 HttpResponseDenominator (com.robo4j.socket.http.message.HttpResponseDenominator)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