use of com.robo4j.socket.http.message.HttpResponseDenominator 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.HttpResponseDenominator 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;
}
use of com.robo4j.socket.http.message.HttpResponseDenominator in project robo4j by Robo4J.
the class WriteSelectionKeyHandler method handle.
@Override
public SelectionKey handle() {
SocketChannel channel = (SocketChannel) key.channel();
final HttpResponseProcess responseProcess = outBuffers.get(key);
ByteBuffer buffer;
if (responseProcess.getMethod() != null) {
switch(responseProcess.getMethod()) {
case GET:
String getResponse;
if (responseProcess.getResult() != null && responseProcess.getCode().equals(StatusCode.OK)) {
// FIXME: 2/18/18 (miro) put abstraction
String responseMessage = responseProcess.getResult().toString();
HttpDenominator denominator = new HttpResponseDenominator(responseProcess.getCode(), HttpVersion.HTTP_1_1);
getResponse = HttpMessageBuilder.Build().setDenominator(denominator).addHeaderElement(HttpHeaderFieldNames.ROBO_UNIT_UID, context.getId()).addHeaderElement(HttpHeaderFieldNames.CONTENT_LENGTH, String.valueOf(responseMessage.length())).build(responseMessage);
} else {
HttpDenominator denominator = new HttpResponseDenominator(responseProcess.getCode(), HttpVersion.HTTP_1_1);
getResponse = HttpMessageBuilder.Build().setDenominator(denominator).build();
}
buffer = ChannelBufferUtils.getByteBufferByString(getResponse);
ChannelUtils.handleWriteChannelAndBuffer("get write", channel, buffer);
break;
case POST:
HttpDenominator denominator = new HttpResponseDenominator(responseProcess.getCode(), HttpVersion.HTTP_1_1);
String postResponse = HttpMessageBuilder.Build().setDenominator(denominator).build();
if (responseProcess.getResult() != null && responseProcess.getCode().equals(StatusCode.ACCEPTED)) {
buffer = ChannelBufferUtils.getByteBufferByString(postResponse);
ChannelUtils.handleWriteChannelAndBuffer("post write", channel, buffer);
sendMessageToTargetRoboReference(responseProcess);
} else {
buffer = ChannelBufferUtils.getByteBufferByString(postResponse);
ChannelUtils.handleWriteChannelAndBuffer("post write", channel, buffer);
}
default:
break;
}
} else {
HttpDenominator denominator = new HttpResponseDenominator(StatusCode.BAD_REQUEST, HttpVersion.HTTP_1_1);
String badResponse = HttpMessageBuilder.Build().setDenominator(denominator).build();
buffer = ChannelBufferUtils.getByteBufferByString(badResponse);
try {
ChannelUtils.writeBuffer(channel, buffer);
} catch (Exception e) {
throw new SocketException("post write", e);
}
buffer.clear();
}
try {
key.cancel();
key.channel().close();
} catch (IOException e) {
throw new SocketException(e.getMessage());
}
return key;
}
Aggregations