gnodet opened a new pull request, #31106:
URL: https://github.com/apache/camel/pull/31106

   ## Summary
   
   Re-resolves the OAuth token from `oauthProfile` on every outbound request in 
`camel-docling` and `camel-huggingface`, instead of once at startup. After 
token expiry, all requests previously failed with HTTP 401; with this fix, 
camel-oauth's cached token is transparently renewed when it expires.
   
   ## Root Cause
   
   Both producers called `OAuthHelper.resolveOAuthToken()` once inside 
`doStart()`:
   
   - **DoclingProducer**: the resolved token was stored in 
`configuration.setAuthenticationToken()` and then baked into 
`BearerTokenDoclingServeClient` as a `final String authorization`. The token 
never changed after startup.
   - **HuggingFaceProducer**: the resolved token was stored in 
`config.setAuthToken()` before `loadModel()`. After that call, the token was 
not refreshed and expired requests would fail.
   
   ## Fix
   
   **camel-docling** (`BearerTokenDoclingServeClient`, `DoclingProducer`):
   - Changed `BearerTokenDoclingServeClient` to accept a `Supplier<String>` 
instead of a `String` for the bearer token. The `createRequestBuilder()` 
override calls `tokenSupplier.get()` on each request.
   - When `oauthProfile` is configured, 
`DoclingProducer.resolveBearerTokenSupplier()` returns a lambda that calls 
`OAuthHelper.resolveOAuthToken()` on each invocation (camel-oauth caches tokens 
until expiry, so the HTTP overhead is minimal).
   - When no OAuth profile is configured, the supplier returns the static 
`authenticationToken` as before.
   - Removed the startup `resolveOAuthToken()` call that was no longer needed.
   
   **camel-huggingface** (`HuggingFaceProducer`):
   - Renamed `resolveOAuthToken()` to `resolveAndRefreshOAuthToken()` and added 
a call to it at the start of `process()`, so `config.authToken` is kept current 
before every inference request.
   - The startup call before `loadModel()` is retained so the initial model 
download (which needs `HF_TOKEN`) gets a valid token.
   
   ## Tests
   
   **camel-docling** 
(`DoclingProducerAuthTest.oauthProfileTokenIsReResolvedOnEveryRequest`):
   - Sets up a stub `OAuthClientAuthenticationFactory` (registered via META-INF 
service) that returns a configurable token.
   - Sends two requests with different tokens in between; verifies the stub 
HTTP server receives each token on the respective request — not the startup 
token on both.
   
   **camel-huggingface** 
(`HuggingFaceOAuthTokenRefreshTest.oauthTokenIsReResolvedOnEveryProcess`):
   - Uses a stub factory and a no-op predictor (injected via reflection to 
bypass DJL model loading).
   - Calls `process()` twice with a token change in between; verifies 
`config.getAuthToken()` is updated on each call.
   
   Fixes CAMEL-27752
   
   ---
   _Hermes Agent (Claude Sonnet 4.6) on behalf of Guillaume Nodet_
   


-- 
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]

Reply via email to