Class WebFunctionTools
- All Implemented Interfaces:
FunctionTools
ProcessProvider 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) Creates and configures a connection for a REST request, including optional request-body transmission.(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, String uri) Resolves a project-scopedfile:URI and reads the referenced file.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:
-
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) throws IOExceptionFunctional 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 info, or a file:// scheme with a relative path resolved against projectDirheaders- 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 content, optionally restricted to matching elements and rendered as plain text
- Throws:
IllegalArgumentException- ifurlis not a valid URI orcharsetNamedoes not name a supported charsetIOException- if the target file cannot be read or the HTTP request cannot be completed
-
readFileUriContent
private String readFileUriContent(File projectDir, String charsetName, String uri) throws IOException Resolves a project-scopedfile:URI and reads the referenced file.- Parameters:
projectDir- project root used for relative file pathscharsetName- character set used to decode the fileuri- textual file URI to resolve- Returns:
- content of the resolved file
- Throws:
IllegalArgumentException- ifcharsetNamedoes not identify a supported character setIOException- if the resolved file cannot be read
-
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:
IllegalArgumentException- ifcharsetNamedoes not identify a supported character setIOException- if the request cannot be configured, connected, or read
-
readFileContent
Reads a local file using the requested character set.- Parameters:
file- file to readcharsetName- character set used to decode the file- Returns:
- decoded file content
- Throws:
IllegalArgumentException- ifcharsetNamedoes not identify a supported character setIOException- if the file cannot be opened or read
-
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 line separators, or the original
response if
selectoris blank - Throws:
IllegalArgumentException- ifselectoris not valid CSS syntax
-
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 - Throws:
IllegalArgumentException- if the response cannot be parsed as HTML
-
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:
IllegalArgumentException- ifuricannot be converted to a URLIOException- 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:
IllegalArgumentException- ifcharsetNamedoes not identify a supported character setIOException- 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, including the status line and response body; when no response stream is available, a response-code summary
- Throws:
IllegalArgumentException- ifurlis not a valid URI,methodis invalid, orcharsetNamedoes not name a supported charsetIOException- if the URL connection cannot be opened, configured, or its response cannot be read
-
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:
IllegalArgumentException- ifcharsetNamedoes not identify a supported character setIOException- 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 IOExceptionCreates and configures a connection for a REST request, including optional request-body transmission.- Parameters:
requestId- request correlation identifier used for loggingurl- endpoint URLcharsetName- character set used to encode the request bodymethod- HTTP methodtimeout- connection and read timeout in milliseconds, or zero for the connection defaultheaders- optional request headersbody- optional request bodyconfig- configuration used for header substitution- Returns:
- configured HTTP connection
- Throws:
IllegalArgumentException- if the URL, HTTP method, or character set is invalidIOException- if the connection cannot be opened or the request body cannot be written
-
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.
-