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