Search in sources :

Example 6 with HttpRequestDenominator

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

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

the class HttpClientCodecUnit method onMessage.

@Override
public void onMessage(ClientMessageWrapper message) {
    final String encodedMessage = clientContext.getPropertySafe(CodecRegistry.class, PROPERTY_CODEC_REGISTRY).containsEncoder(message.getClazz()) ? processMessage(message.getClazz(), message.getMessage()) : processMessage(String.class, message.toString());
    ClientPathConfig pathConfig = clientContext.getPathConfig(new PathHttpMethod(message.getPath(), HttpMethod.POST));
    final HttpRequestDenominator denominator = new HttpRequestDenominator(pathConfig.getMethod(), pathConfig.getPath(), HttpVersion.HTTP_1_1);
    final HttpDecoratedRequest request = new HttpDecoratedRequest(denominator);
    request.addMessage(encodedMessage);
    request.addCallbacks(pathConfig.getCallbacks());
    getContext().getReference(target).sendMessage(request);
}
Also used : HttpDecoratedRequest(com.robo4j.socket.http.message.HttpDecoratedRequest) HttpRequestDenominator(com.robo4j.socket.http.message.HttpRequestDenominator)

Example 8 with HttpRequestDenominator

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

the class ChannelBufferUtils method extractDecoratedRequestByStringMessage.

/**
 * @param message
 *            message
 * @return http decorate request
 */
public static HttpDecoratedRequest extractDecoratedRequestByStringMessage(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 HttpMethod method = HttpMethod.getByName(tokens[HttpMessageUtils.METHOD_KEY_POSITION]);
    final String path = tokens[HttpMessageUtils.URI_VALUE_POSITION];
    final String version = tokens[HttpMessageUtils.VERSION_POSITION];
    final Map<String, String> headerParams = getHeaderParametersByArray(paramArray);
    final HttpRequestDenominator denominator;
    if (path.contains(HttpPathUtils.DELIMITER_PATH_ATTRIBUTES)) {
        denominator = new HttpRequestDenominator(method, path.split(HttpPathUtils.REGEX_ATTRIBUTE)[0], HttpVersion.getByValue(version), HttpPathUtils.extractAttributesByPath(path));
    } else {
        denominator = new HttpRequestDenominator(method, path, HttpVersion.getByValue(version));
    }
    HttpDecoratedRequest result = new HttpDecoratedRequest(headerParams, denominator);
    if (headerParams.containsKey(HttpHeaderFieldNames.CONTENT_LENGTH)) {
        result.setLength(calculateMessageSize(headerAndBody[POSITION_HEADER].length(), headerParams));
        result.addMessage(headerAndBody[POSITION_BODY]);
    }
    return result;
}
Also used : HttpDecoratedRequest(com.robo4j.socket.http.message.HttpDecoratedRequest) HttpRequestDenominator(com.robo4j.socket.http.message.HttpRequestDenominator) HttpMethod(com.robo4j.socket.http.HttpMethod)

Example 9 with HttpRequestDenominator

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

the class ChannelRequestBuffer method getHttpDecoratedRequestByChannel.

public HttpDecoratedRequest getHttpDecoratedRequestByChannel(ByteChannel channel) throws IOException {
    final StringBuilder sbBasic = new StringBuilder();
    int readBytes = channel.read(requestBuffer);
    if (readBytes != ChannelBufferUtils.BUFFER_MARK_END) {
        requestBuffer.flip();
        ChannelBufferUtils.addToStringBuilder(sbBasic, requestBuffer, readBytes);
        final HttpDecoratedRequest result = ChannelBufferUtils.extractDecoratedRequestByStringMessage(sbBasic.toString());
        ChannelBufferUtils.readChannelBuffer(result, channel, requestBuffer, readBytes);
        requestBuffer.clear();
        return result;
    } else {
        return new HttpDecoratedRequest(new HashMap<>(), new HttpRequestDenominator(HttpMethod.GET, HttpVersion.HTTP_1_1));
    }
}
Also used : HttpDecoratedRequest(com.robo4j.socket.http.message.HttpDecoratedRequest) HttpRequestDenominator(com.robo4j.socket.http.message.HttpRequestDenominator)

Aggregations

HttpRequestDenominator (com.robo4j.socket.http.message.HttpRequestDenominator)9 HttpDecoratedRequest (com.robo4j.socket.http.message.HttpDecoratedRequest)8 RoboContext (com.robo4j.RoboContext)3 Test (org.junit.jupiter.api.Test)3 HttpDenominator (com.robo4j.socket.http.message.HttpDenominator)2 AttributeDescriptor (com.robo4j.AttributeDescriptor)1 RoboBuilder (com.robo4j.RoboBuilder)1 RoboReference (com.robo4j.RoboReference)1 SimpleLoggingUtil (com.robo4j.logging.SimpleLoggingUtil)1 HttpMethod (com.robo4j.socket.http.HttpMethod)1 ClassGetSetDTO (com.robo4j.socket.http.dto.ClassGetSetDTO)1 PathAttributeDTO (com.robo4j.socket.http.dto.PathAttributeDTO)1 PathAttributeListDTO (com.robo4j.socket.http.dto.PathAttributeListDTO)1 StatusCode (com.robo4j.socket.http.enums.StatusCode)1 ServerContext (com.robo4j.socket.http.units.ServerContext)1 ServerPathConfig (com.robo4j.socket.http.units.ServerPathConfig)1 HttpPathUtils (com.robo4j.socket.http.util.HttpPathUtils)1 JsonUtil (com.robo4j.socket.http.util.JsonUtil)1 ReflectUtils (com.robo4j.socket.http.util.ReflectUtils)1 UTF8_SOLIDUS (com.robo4j.util.Utf8Constant.UTF8_SOLIDUS)1