use of com.robo4j.socket.http.message.HttpRequestDenominator in project robo4j by Robo4J.
the class RoboRequestCallable method call.
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public HttpResponseProcess call() throws Exception {
final HttpResponseProcessBuilder resultBuilder = HttpResponseProcessBuilder.Builder();
final ServerPathConfig pathConfig = serverContext.getPathConfig(decoratedRequest.getPathMethod());
if (isValidPath(pathConfig)) {
resultBuilder.setMethod(pathConfig.getMethod());
resultBuilder.setPath(pathConfig.getPath());
switch(pathConfig.getMethod()) {
case GET:
if (pathConfig.getPath().equals(UTF8_SOLIDUS)) {
resultBuilder.setCode(StatusCode.OK);
resultBuilder.setResult(factory.processGet(context));
} else {
resultBuilder.setTarget(pathConfig.getRoboUnit().getId());
final Object unitDescription;
// the system needs to have one more worker thread to evaluate Future get
final HttpRequestDenominator denominator = (HttpRequestDenominator) decoratedRequest.getDenominator();
final Set<String> requestAttributes = denominator.getAttributes().get(HttpPathUtils.ATTRIBUTES_PATH_VALUE);
if (requestAttributes == null) {
unitDescription = factory.processGet(pathConfig);
} else if (requestAttributes.isEmpty()) {
RoboReference<?> unit = context.getReference(pathConfig.getRoboUnit().getId());
PathAttributeListDTO pathAttributes = new PathAttributeListDTO();
unit.getKnownAttributes().forEach(a -> {
PathAttributeDTO attributeDescriptor = new PathAttributeDTO();
attributeDescriptor.setName(a.getAttributeName());
attributeDescriptor.setValue(a.getAttributeType().getCanonicalName());
pathAttributes.addAttribute(attributeDescriptor);
});
unitDescription = ReflectUtils.createJson(pathAttributes);
} else {
RoboReference<?> unit = context.getReference(pathConfig.getRoboUnit().getId());
List<PathAttributeDTO> attributes = new ArrayList<>();
for (AttributeDescriptor attr : unit.getKnownAttributes()) {
if (requestAttributes.contains(attr.getAttributeName())) {
PathAttributeDTO attribute = new PathAttributeDTO();
String valueString = String.valueOf(unit.getAttribute(attr).get());
attribute.setValue(valueString);
attribute.setName(attr.getAttributeName());
attributes.add(attribute);
}
}
if (attributes.size() == 1) {
Map<String, ClassGetSetDTO> responseAttributeDescriptorMap = ReflectUtils.getFieldsTypeMap(PathAttributeDTO.class);
unitDescription = JsonUtil.toJson(responseAttributeDescriptorMap, attributes.get(0));
} else {
unitDescription = JsonUtil.toJsonArray(attributes);
}
}
resultBuilder.setCode(StatusCode.OK);
resultBuilder.setResult(unitDescription);
}
break;
case POST:
if (pathConfig.getPath().equals(UTF8_SOLIDUS)) {
resultBuilder.setCode(StatusCode.BAD_REQUEST);
} else {
resultBuilder.setTarget(pathConfig.getRoboUnit().getId());
Object respObj = factory.processPost(pathConfig.getRoboUnit(), decoratedRequest.getMessage());
if (respObj == null) {
resultBuilder.setCode(StatusCode.BAD_REQUEST);
} else {
resultBuilder.setCode(StatusCode.ACCEPTED);
resultBuilder.setResult(respObj);
}
}
break;
default:
resultBuilder.setCode(StatusCode.BAD_REQUEST);
SimpleLoggingUtil.debug(getClass(), "not implemented method: " + decoratedRequest.getPathMethod());
}
} else {
resultBuilder.setCode(StatusCode.BAD_REQUEST);
}
return resultBuilder.build();
}
use of com.robo4j.socket.http.message.HttpRequestDenominator in project robo4j by Robo4J.
the class RoboHttpDynamicTests method pingExternalSystem.
/**
* testing ping external system
*
* @throws Exception
* exception
*/
@Disabled("intent to run manual")
@Test
void pingExternalSystem() throws Exception {
RoboBuilder pingSystemBuilder = getHttpClientRobotBuilder("127.0.0.1", 8080);
pingSystemBuilder.add(StringConsumer.class, StringConsumer.NAME);
RoboContext pingSystemContext = pingSystemBuilder.build();
pingSystemContext.start();
System.out.println("PingSystem state after start:");
System.out.println(SystemUtil.printStateReport(pingSystemContext));
RoboReference<HttpDecoratedRequest> httpClient = pingSystemContext.getReference(ID_CLIENT_UNIT);
Thread.sleep(1000);
for (int i = 0; i < 1; i++) {
HttpRequestDenominator denominator = new HttpRequestDenominator(HttpMethod.GET, "/noparams", HttpVersion.HTTP_1_1);
HttpDecoratedRequest request = new HttpDecoratedRequest(denominator);
request.addCallback(StringConsumer.NAME);
httpClient.sendMessage(request);
}
Thread.sleep(1000);
pingSystemContext.stop();
System.out.println("PingSystem state after stop:");
System.out.println(SystemUtil.printStateReport(pingSystemContext));
}
use of com.robo4j.socket.http.message.HttpRequestDenominator in project robo4j by Robo4J.
the class ByteBufferTests method byteBufferFromRequestTest.
@Test
void byteBufferFromRequestTest() {
String bodyMessage = "this is test message";
String host = "localhost";
String clientPath = "/test";
HttpDenominator denominator = new HttpRequestDenominator(HttpMethod.POST, clientPath, HttpVersion.HTTP_1_1);
String postMessage = HttpMessageBuilder.Build().setDenominator(denominator).addHeaderElement(HttpHeaderFieldNames.CONTENT_LENGTH, String.valueOf(bodyMessage.length())).addHeaderElement(HttpHeaderFieldNames.HOST, RoboHttpUtils.createHost(host, ProtocolType.HTTP.getPort())).build(bodyMessage);
HttpDecoratedRequest decoratedRequest = ChannelBufferUtils.extractDecoratedRequestByStringMessage(postMessage);
assertNotNull(postMessage);
assertEquals(postMessage.length(), decoratedRequest.getLength());
assertEquals(clientPath, decoratedRequest.getPathMethod().getPath());
assertEquals(bodyMessage, decoratedRequest.getMessage());
}
use of com.robo4j.socket.http.message.HttpRequestDenominator 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.HttpRequestDenominator 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;
}
Aggregations