Hi Jean,

The JAX-RS has full support for web forms, it should be fairly easy to do 
with WebClient for example:
 
        import javax.ws.rs.core.Form;
        import javax.ws.rs.core.Response;
 
        final WebClient client = WebClient
            .create(address)
            .accept("application/json")
            .type("application/x-www-form-urlencoded")
            .path("/");
 
        final Form form = new Form();
        form.param("token", accessToken);
 
        try (Response response = client.post(form)) {
            ...
        } 
        
I hope it answers your question, thank you.
 
Best Regards,
    Andriy Redko 

> Hi Andriy,

> The 'ugly' solution I have is to pass the (url endocded) FORM data as a
> string to my interface method, which is then written to the body content.
> E.g.:

>         //01.Creating a static threadsafe client proxy
>         private static OidcProviderApi apiProxy;
>         protected static synchronized OidcProviderApi getApiProxy() {
>                 if (apiProxy == null) {
>                         String oidcBaseAddress =
> Configuration.getInstance().getItem("oidc.introspection.uri");
>                         apiProxy = getThreadsafeProxy(oidcBaseAddress);
>                 }
>                 return apiProxy;
>         }
>         private static OidcProviderApi getThreadsafeProxy(String 
> oidcBaseAddress) {
>                 JacksonJsonProvider provider = new JacksonJsonProvider(new
> CustomObjectMapper());
>                 List<JacksonJsonProvider> providers = new
> ArrayList<JacksonJsonProvider>();
>                 providers.add(provider);

>                 //Create a threadsafe proxy for the API resource
>                 final JAXRSClientFactoryBean factory = new 
> JAXRSClientFactoryBean();
>                 factory.setAddress(oidcBaseAddress);
>                 factory.setServiceClass(OidcProviderApi.class);
>                 factory.setProviders(providers);
>                 factory.getOutInterceptors().add(new LoggingOutInterceptor());
>                 factory.getInInterceptors().add(new LoggingInInterceptor());
>                 factory.setThreadSafe(true);
>                 OidcProviderApi api = factory.create(OidcProviderApi.class);
>                 ClientConfiguration config = WebClient.getConfig(api);
>                 addTLSClientParameters(config.getHttpConduit());
>                 return api;
>         }

>         //02.Sending the POST request with 
> 'application/x-www-form-urlencoded' body
> content containing FORM data
>         //It takes the access token as input and constructs an (url encoded) 
> string
> representing the FORM data having the
>         // parameters: token, token_type_hint, client_assertion_type and
> client_assertion
>         public Response validateAccessToken(String token) throws
> MvgBusinessFatalException       {
>                 StringBuilder bldr = new 
> StringBuilder("token=").append(token);
>                 bldr.append("&token_type_hint=access_token");
>                 
> bldr.append("&client_assertion_type=").append(URLEncoder.encode("urn:ietf:params:oauth:client-assertion-type:jwt-bearer","UTF-8"));
>                 String clientAssertion = <A signed JWT token>;
>                 bldr.append("&client_assertion=").append(clientAssertion);
>                 String urlEncodedFormData = bldr.toString();

>                 OidcProviderApi apiClient = getApiProxy();
>                 ClientConfiguration config = WebClient.getConfig(apiClient);
>                 
> config.getHttpConduit().getClient().setContentType("application/x-www-form-urlencoded");
>                 return apiClient.validateAccessToken(urlEncodedFormData);
>         }

> This works, but doesn't really look nice.

> J.P.

> -----Original Message-----
> From: Jean Pierre URKENS <jean-pierre.urk...@devoteam.com>
> Sent: dinsdag 19 september 2023 9:48
> To: 'Andriy Redko' <drr...@gmail.com>
> Subject: CXF JAX-RS client: setting content-type for a specific method in
> outgoing request

> Hi Andriy,

> I've a server that expects an HTTP POST request having a body content of
> mediatype  'application/x-www-form-urlencoded' and produces a response of
> mediatype 'application/json'.

> I am using Apache CXF-v3.5.6 but I am not sure on how to create a JAXRS
> client that is able to send a POST with an
> 'application/x-www-form-urlencoded' body content.

> Are there any examples within CXF to realize this.

> Thanks for any feedback.

> Regards,

> J.P. Urkens

 

Reply via email to