Search in sources :

Example 6 with HttpDecoratedRequest

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

the class RoboHttpUnitGetTestApp method twoKnownAttributesTest.

/**
 * Run the system with known attributes
 *
 * @throws Exception
 *             exception
 */
public void twoKnownAttributesTest() throws Exception {
    RoboContext systemGetProvider = twoAttributesSystem();
    RoboContext systemGetAccessor = attributeRequestSystem();
    systemGetProvider.start();
    System.out.println("systemGetProvider: State after start:");
    System.out.println(SystemUtil.printStateReport(systemGetProvider));
    systemGetAccessor.start();
    System.out.println("systemGetAccessor: State after start:");
    System.out.println(SystemUtil.printStateReport(systemGetAccessor));
    RoboReference<HttpDecoratedRequest> httpClient = systemGetAccessor.getReference(UNIT_ID_HTTP_CLIENT);
    HttpRequestDenominator denominator = new HttpRequestDenominator(HttpMethod.GET, "/units/controller?attributes=number", HttpVersion.HTTP_1_1);
    HttpDecoratedRequest request = new HttpDecoratedRequest(denominator);
    request.addCallback(StringConsumer.NAME);
    httpClient.sendMessage(request);
    System.out.println("Press <Enter>...");
    System.in.read();
    systemGetProvider.shutdown();
}
Also used : HttpDecoratedRequest(com.robo4j.socket.http.message.HttpDecoratedRequest) RoboContext(com.robo4j.RoboContext) HttpRequestDenominator(com.robo4j.socket.http.message.HttpRequestDenominator)

Example 7 with HttpDecoratedRequest

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

the class SocketMessageDecoratedProducerUnit method getHttpRequest.

private HttpDecoratedRequest getHttpRequest(ClientPathConfig pathConfig) {
    HttpRequestDenominator denominator = new HttpRequestDenominator(pathConfig.getMethod(), pathConfig.getPath(), HttpVersion.HTTP_1_1);
    HttpDecoratedRequest result = new HttpDecoratedRequest(denominator);
    switch(pathConfig.getMethod()) {
        case GET:
            result.addCallbacks(pathConfig.getCallbacks());
            break;
        case POST:
            result.addMessage(message);
            break;
        default:
            throw new IllegalStateException("not allowed state: " + pathConfig);
    }
    return result;
}
Also used : HttpDecoratedRequest(com.robo4j.socket.http.message.HttpDecoratedRequest) HttpRequestDenominator(com.robo4j.socket.http.message.HttpRequestDenominator)

Example 8 with HttpDecoratedRequest

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

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

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

Aggregations

HttpDecoratedRequest (com.robo4j.socket.http.message.HttpDecoratedRequest)11 HttpRequestDenominator (com.robo4j.socket.http.message.HttpRequestDenominator)7 Test (org.junit.jupiter.api.Test)3 RoboContext (com.robo4j.RoboContext)2 SocketChannel (java.nio.channels.SocketChannel)2 ConfigurationException (com.robo4j.ConfigurationException)1 RoboBuilder (com.robo4j.RoboBuilder)1 HttpMethod (com.robo4j.socket.http.HttpMethod)1 SocketException (com.robo4j.socket.http.SocketException)1 HttpDecoratedResponse (com.robo4j.socket.http.message.HttpDecoratedResponse)1 HttpDenominator (com.robo4j.socket.http.message.HttpDenominator)1 HttpResponseProcess (com.robo4j.socket.http.request.HttpResponseProcess)1 RoboRequestCallable (com.robo4j.socket.http.request.RoboRequestCallable)1 RoboRequestFactory (com.robo4j.socket.http.request.RoboRequestFactory)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 Disabled (org.junit.jupiter.api.Disabled)1