use of com.robo4j.socket.http.message.HttpDecoratedResponse in project robo4j by Robo4J.
the class OutboundHttpSocketChannelHandler method getDecoratedResponse.
private HttpDecoratedResponse getDecoratedResponse(ByteChannel byteChannel, HttpPathMethodDTO pathMethod) {
lock.lock();
try {
final HttpDecoratedResponse result = channelResponseBuffer.getHttpDecoratedResponseByChannel(byteChannel);
result.addCallbacks(pathMethod.getCallbacks());
return result;
} catch (IOException e) {
throw new SocketException("message body write problem", e);
} finally {
lock.unlock();
}
}
use of com.robo4j.socket.http.message.HttpDecoratedResponse 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.HttpDecoratedResponse in project robo4j by Robo4J.
the class ChannelResponseBuffer method getHttpDecoratedResponseByChannel.
public HttpDecoratedResponse getHttpDecoratedResponseByChannel(ByteChannel channel) throws IOException {
final StringBuilder sbBasic = new StringBuilder();
int readBytes = channel.read(responseBuffer);
if (readBytes != BUFFER_MARK_END) {
responseBuffer.flip();
ChannelBufferUtils.addToStringBuilder(sbBasic, responseBuffer, readBytes);
final HttpDecoratedResponse result = extractDecoratedResponseByStringMessage(sbBasic.toString());
ChannelBufferUtils.readChannelBuffer(result, channel, responseBuffer, readBytes);
responseBuffer.clear();
return result;
} else {
return new HttpDecoratedResponse(new HashMap<>(), new HttpResponseDenominator(StatusCode.BAD_REQUEST, HttpVersion.HTTP_1_1));
}
}
use of com.robo4j.socket.http.message.HttpDecoratedResponse in project robo4j by Robo4J.
the class ChannelResponseBuffer method extractDecoratedResponseByStringMessage.
// TODO: 3/5/18 (miro) investigate spring responseBody
private HttpDecoratedResponse extractDecoratedResponseByStringMessage(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 String version = tokens[0];
final StatusCode statusCode = StatusCode.getByCode(Integer.valueOf(tokens[1]));
final Map<String, String> headerParams = ChannelBufferUtils.getHeaderParametersByArray(paramArray);
HttpResponseDenominator denominator = new HttpResponseDenominator(statusCode, HttpVersion.getByValue(version));
HttpDecoratedResponse result = new HttpDecoratedResponse(headerParams, denominator);
if (headerAndBody.length > 1) {
if (headerParams.containsKey(HttpHeaderFieldNames.CONTENT_LENGTH)) {
result.setLength(ChannelBufferUtils.calculateMessageSize(headerAndBody[POSITION_HEADER].length(), headerParams));
} else {
result.setLength(headerAndBody[POSITION_BODY].length());
}
Matcher matcher = ChannelBufferUtils.RESPONSE_SPRING_PATTERN.matcher(headerAndBody[POSITION_BODY]);
if (matcher.find()) {
// result.addMessage(headerAndBody[POSITION_BODY]);
result.addMessage(matcher.group(ChannelBufferUtils.RESPONSE_JSON_GROUP));
}
}
return result;
}
Aggregations