gnodet-bot commented on code in PR #26558:
URL: https://github.com/apache/camel/pull/26558#discussion_r4039548916
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java:
##########
@@ -380,14 +440,209 @@ public static JsonObject readFile(Path dir, String file)
{
return result;
}
- /** A plain file name inside the directory; anything else (a path, a
parent reference) is refused. */
- static Path resolveFile(Path dir, String file) {
+ /**
+ * Where each route of a running integration comes from, mapped onto the
files of the project directory. The status
+ * document's {@code routes[].source} names what the runtime loaded: a jar
entry for an exported project
+ * ({@code
nested:.../target/app.jar/!BOOT-INF/classes/!/camel/foo.camel.yaml:4}), a
{@code classpath:} or
+ * {@code file:} resource, or a Java class; the answer is the path a model
can read and edit
+ * ({@code src/main/resources/camel/foo.camel.yaml}) with the line, or the
location as given with {@code missing}
+ * when no such file exists under the directory.
+ *
+ * @param routes the status document's routes (maps with {@code routeId}
and {@code source}), may be null
+ */
+ public static JsonArray routeSources(Collection<?> routes, Path dir) {
+ JsonArray out = new JsonArray();
+ if (routes == null) {
+ return out;
+ }
+ for (Object o : routes) {
+ if (!(o instanceof Map<?, ?> r) || !(r.get("source") instanceof
String source) || source.isBlank()) {
+ continue;
+ }
+ SourceLocation loc = sourceLocation(source, dir);
+ JsonObject e = new JsonObject();
+ if (r.get("routeId") != null) {
+ e.put("routeId", String.valueOf(r.get("routeId")));
+ }
+ e.put("file", loc.file());
+ if (loc.line() > 0) {
+ e.put("line", loc.line());
+ }
+ if (!loc.exists()) {
+ e.put("missing", true);
+ }
+ out.add(e);
+ }
+ return out;
+ }
+
+ record SourceLocation(String file, int line, boolean exists) {
+ }
+
+ /** Maps one route source location onto a file under the directory; see
{@link #routeSources}. */
+ static SourceLocation sourceLocation(String source, Path dir) {
+ String s = source.trim();
+ int line = 0;
+ int colon = s.lastIndexOf(':');
+ if (colon > 0 && colon < s.length() - 1 && s.substring(colon +
1).chars().allMatch(Character::isDigit)) {
+ line = Integer.parseInt(s.substring(colon + 1));
Review Comment:
💡 **Low:** `allMatch(Character::isDigit)` accepts arbitrarily long digit
strings; `Integer.parseInt` throws `NumberFormatException` on values above
`Integer.MAX_VALUE` (e.g. a source string ending in `:2147483648`). The
exception is uncaught in both the core executor lambda and
`McpFacade.getFiles`, so it would propagate as an unhandled `RuntimeException`
through the tool call. In practice Camel's runtime never emits a 10+-digit line
number, but the code is defensive on every other path (path traversal,
`InvalidPathException`) — be consistent here too:
```suggestion
if (colon > 0 && colon < s.length() - 1 && s.substring(colon +
1).length() <= 9
&& s.substring(colon +
1).chars().allMatch(Character::isDigit)) {
line = Integer.parseInt(s.substring(colon + 1));
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]