Class WebFunctionTools
- All Implemented Interfaces:
FunctionTools
Genai provider.
This tool set exposes two main functions:
get_web_content– Fetches web page content over HTTP(S) via GET, optionally returning plain text or content selected via a CSS selector.call_rest_api– Executes a generic REST call using an arbitrary HTTP method with optional headers and request body.
Header variable placeholders
Header values may include placeholders in the form
${propertyName}. When a Configurator is provided, those
placeholders are resolved at runtime.
Authentication
HTTP Basic authentication is supported via the URL userInfo component
(e.g., https://user:password@host/path), which is converted into
an Authorization: Basic ... header. You can also specify an
explicit Authorization header.
Outbound network policy (allow/deny lists) is intentionally left to the host application.
Usage Example
WebFunctionTools tools = new WebFunctionTools();
String html = tools.getWebContent("https://example.com", null, 5000, "UTF-8", false, "", projectDir, configurator);
String apiResult = tools.callRestApi("https://api.example.com", "POST", headers, body, 5000, "UTF-8", projectDir,
configurator);
- Author:
- Viktor Tovstyi
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringDefault response character set used by the HTTP tools.private static final org.slf4j.LoggerLogger for web fetch tool execution and diagnostics.private static final SecureRandomSource of correlation identifiers used in request log messages. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) StringapplySelectorIfPresent(String selector, String response) Applies a CSS selector to the response HTML if one was provided.callRestApi(String url, String method, Map<String, String> headers, String body, int timeout, String charsetName, File projectDir, Configurator configurator) Functional AI tool that executes a REST API call to the specified URL using the given HTTP method.private StringfetchHttpContent(String requestId, Map<String, String> headers, int timeout, String charsetName, URI uri, Configurator config) Fetches HTTP content for a parsed URI.(package private) voidfillHeader(Map<String, String> headers, HttpURLConnection connection, Configurator configurator) Applies HTTP headers to the given connection, resolving any property placeholders.private HttpURLConnectiongetConnection(String requestId, String url, String charsetName, String method, int timeout, Map<String, String> headers, String body, Configurator config) (package private) HttpURLConnectiongetConnection(URI uri, Map<String, String> headers, Configurator config) Creates and configures anHttpURLConnection.getWebContent(String url, Map<String, String> headers, int timeout, String charsetName, boolean textOnly, String selector, File projectDir, Configurator configurator) Functional AI tool that fetches a web page or project-scoped file URL.(package private) StringgetWebPage(HttpURLConnection connection, int timeout, String charsetName) Performs the HTTP GET request and returns the response content.private StringparseResult(String requestId, String charsetName, HttpURLConnection connection, int responseCode, StringBuilder response) Reads the response stream and returns the full response text.private StringreadFileContent(File file, String charsetName) Reads a local file using the requested character set.private StringreadFileUriContent(File projectDir, String charsetName, URI uri) private StringrenderTextOnlyIfRequested(boolean textOnly, String response) Converts the response to plain text when requested.
-
Field Details
-
DEFAULT_CHARSET
Default response character set used by the HTTP tools.- See Also:
-
REQUEST_ID_RANDOM
Source of correlation identifiers used in request log messages. -
logger
private static final org.slf4j.Logger loggerLogger for web fetch tool execution and diagnostics.
-
-
Constructor Details
-
WebFunctionTools
public WebFunctionTools()
-
-
Method Details
-
getWebContent
public String getWebContent(String url, Map<String, String> headers, int timeout, String charsetName, boolean textOnly, String selector, File projectDir, Configurator configurator) Functional AI tool that fetches a web page or project-scoped file URL. The response may be filtered by a CSS selector and rendered as plain text.- Parameters:
url- URL to fetch; may contain Basic-authentication user infoheaders- optional request headerstimeout- connection and read timeout in milliseconds, or zero for the connection defaultcharsetName- character set used to decode the responsetextOnly- whether to strip HTML markup from the responseselector- optional CSS selector used to select response elementsprojectDir- project root used to resolve file URLsconfigurator- configuration used to substitute URL and header values- Returns:
- fetched, optionally selected and rendered content, or an error message
-
readFileUriContent
-
fetchHttpContent
private String fetchHttpContent(String requestId, Map<String, String> headers, int timeout, String charsetName, URI uri, Configurator config) throws IOExceptionFetches HTTP content for a parsed URI.- Parameters:
requestId- request correlation identifierheaders- optional request headerstimeout- timeout in millisecondscharsetName- response character seturi- target URIconfig- configuration used for header substitution- Returns:
- response content
- Throws:
IOException- if the connection or response cannot be read
-
readFileContent
Reads a local file using the requested character set.- Parameters:
file- file to readcharsetName- character set used to decode the file- Returns:
- file content or a not-found message
-
applySelectorIfPresent
Applies a CSS selector to the response HTML if one was provided.- Parameters:
selector- CSS selector (may be blank)response- full response content- Returns:
- selected HTML content (joined with newlines) or the original response
if
selectoris blank
-
renderTextOnlyIfRequested
Converts the response to plain text when requested.- Parameters:
textOnly- whether to render text onlyresponse- response content (typically HTML)- Returns:
- rendered text content if
textOnlyistrue; otherwise the original response
-
getConnection
HttpURLConnection getConnection(URI uri, Map<String, String> headers, Configurator config) throws IOExceptionCreates and configures anHttpURLConnection.If the URI contains
userInfo, it is removed from the request URI and used to set an HTTP BasicAuthorizationheader.- Parameters:
uri- URI to connect toheaders- optional headersconfig- configuration used to resolve header placeholders- Returns:
- connection
- Throws:
IOException- if opening a connection fails
-
getWebPage
Performs the HTTP GET request and returns the response content.- Parameters:
connection- open connectiontimeout- timeout in millisecondscharsetName- charset used to decode the response- Returns:
- response content including an initial status line
- Throws:
IOException- if the request cannot be executed
-
callRestApi
public String callRestApi(String url, String method, Map<String, String> headers, String body, int timeout, String charsetName, File projectDir, Configurator configurator) throws IOExceptionFunctional AI tool that executes a REST API call to the specified URL using the given HTTP method.Supports userInfo format in the URL for basic authentication, custom headers, request body, timeout, and charset. Handles HTTP methods such as GET, POST, PUT, PATCH, DELETE, etc. If the URL contains user credentials (e.g.,
https://user:password@host/path), they are used for HTTP Basic authentication. Header values may include property placeholders resolved via the providedConfigurator.The response includes an initial status line (e.g.,
HTTP 200 OK) followed by the response body.- Parameters:
url- The URL of the REST endpoint. Supports userInfo format (e.g., https://user:password@host/path) for basic authentication.method- The HTTP method to use (GET, POST, PUT, PATCH, DELETE, etc.). Default is GET.headers- Specifies HTTP header properties. If null, no additional headers are sent.body- The request body to send (for POST, PUT, PATCH, etc.).timeout- The maximum time in milliseconds to wait for the HTTP response. If not specified, a default timeout will be used.charsetName- The name of the character set to use when decoding the response content. Default: UTF-8.projectDir- The project directory context for file-based URLs.configurator- The configuration object for property resolution and header placeholder substitution.- Returns:
- The REST API response as a string, including the status line and response body, or an error message if the call fails.
- Throws:
IOException- if the URL connection cannot be opened or configured
-
parseResult
private String parseResult(String requestId, String charsetName, HttpURLConnection connection, int responseCode, StringBuilder response) throws IOException Reads the response stream and returns the full response text.- Parameters:
requestId- correlation id used for logscharsetName- response decoding charsetconnection- open connectionresponseCode- HTTP response coderesponse- builder already containing the status line- Returns:
- response text
- Throws:
IOException- if reading the response fails
-
getConnection
private HttpURLConnection getConnection(String requestId, String url, String charsetName, String method, int timeout, Map<String, String> headers, String body, Configurator config) throws IOException- Throws:
IOException
-
fillHeader
void fillHeader(Map<String, String> headers, HttpURLConnection connection, Configurator configurator) Applies HTTP headers to the given connection, resolving any property placeholders.Each header entry is set as a request property on the
HttpURLConnection. Header values may include placeholders in the form${propertyName}, which are resolved using the providedConfigurator.- Parameters:
headers- Map of header names to values. Ifnull, no headers are applied.connection- TheHttpURLConnectionto configure.configurator- TheConfiguratorused to resolve property placeholders in header values.
-