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();
}
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;
}
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);
}
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));
}
}
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;
}
Aggregations