use of com.robo4j.socket.http.units.ServerPathConfig in project robo4j by Robo4J.
the class ReadDatagramSelectionKeyHandler method handle.
// TODO: 2/25/18 (miro) -> Datagram type -> not only string is possible
@Override
public SelectionKey handle() {
final DatagramChannel channel = (DatagramChannel) key.channel();
try {
ByteBuffer buffer = serverContext.getPropertySafe(ByteBuffer.class, PROPERTY_BYTE_BUFFER);
buffer.clear();
channel.receive(buffer);
buffer.flip();
String message = ChannelBufferUtils.byteBufferToString(buffer);
final String[] headerAndBody = message.split(HTTP_HEADER_BODY_DELIMITER);
final String firstLine = RoboHttpUtils.correctLine(headerAndBody[0]);
final String[] tokens = firstLine.split(HttpConstant.HTTP_EMPTY_SEP);
final String body = headerAndBody[1];
final ServerPathConfig serverPathConfig = serverContext.getPathConfig(new PathHttpMethod(tokens[1], null));
final RoboReference<Object> roboReference = serverPathConfig.getRoboUnit();
final SocketDecoder<Object, Object> decoder = codecRegistry.getDecoder(roboReference.getMessageType());
final Object decodedMessage = decoder.decode(body);
serverPathConfig.getRoboUnit().sendMessage(decodedMessage);
final DatagramResponseProcess responseProcess = new DatagramResponseProcess(tokens[1], roboReference, decodedMessage);
outBuffers.put(key, responseProcess);
return key;
} catch (IOException e) {
throw new SocketException("hanlde", e);
}
}
use of com.robo4j.socket.http.units.ServerPathConfig 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.units.ServerPathConfig in project robo4j by Robo4J.
the class WriteSelectionKeyHandler method sendMessageToTargetRoboReference.
private void sendMessageToTargetRoboReference(HttpResponseProcess process) {
final ServerPathConfig pathConfig = serverContext.getPathConfig(new PathHttpMethod(process.getPath(), process.getMethod()));
if (pathConfig.getRoboUnit() != null && pathConfig.getRoboUnit().getMessageType().equals(process.getResult().getClass())) {
RoboReference<Object> reference = pathConfig.getRoboUnit();
reference.sendMessage(process.getResult());
} else {
throw new IllegalStateException(String.format("process %s", process));
}
}
use of com.robo4j.socket.http.units.ServerPathConfig in project robo4j by Robo4J.
the class DatagramPathUtils method updateDatagramServerContextPaths.
public static void updateDatagramServerContextPaths(final RoboContext context, final ServerContext serverContext, final Collection<HttpPathMethodDTO> paths) {
final Map<PathHttpMethod, ServerPathConfig> resultPaths = paths.stream().map(e -> {
RoboReference<Object> reference = context.getReference(e.getRoboUnit());
return new ServerPathConfig(toPath(SystemPath.UNITS.getPath(), e.getRoboUnit()), reference, e.getMethod(), e.getCallbacks());
}).collect(Collectors.toMap(e -> new PathHttpMethod(e.getPath(), null), e -> e));
serverContext.addPaths(resultPaths);
}
use of com.robo4j.socket.http.units.ServerPathConfig in project robo4j by Robo4J.
the class HttpPathUtils method updateHttpServerContextPaths.
public static void updateHttpServerContextPaths(final RoboContext context, final ServerContext serverContext, final Collection<HttpPathMethodDTO> paths) {
final Map<PathHttpMethod, ServerPathConfig> resultPaths = paths.stream().map(e -> {
RoboReference<Object> reference = context.getReference(e.getRoboUnit());
return HttpPathUtils.toHttpPathConfig(e, reference);
}).collect(Collectors.toMap(e -> new PathHttpMethod(e.getPath(), e.getMethod()), e -> e));
resultPaths.put(new PathHttpMethod(Utf8Constant.UTF8_SOLIDUS, HttpMethod.GET), new ServerPathConfig(Utf8Constant.UTF8_SOLIDUS, null, HttpMethod.GET));
serverContext.addPaths(resultPaths);
}
Aggregations