Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WS Client with Proxy and Autentification

I know this isn't exactly the correct way to ask a question, but I'm having a problem:

I have a wsdl stored locally, and I need to create a Web Service Client to call that Web Service. The problem is the service is behind a firewall and I have to connect to it through a proxy and after that I have to authentify to connect to the WS.

What i did is generate the WS Client with Apache CXF 2.4.6 then set a system wide proxy

System.getProperties().put("proxySet", "true");
System.getProperties().put("https.proxyHost", "10.10.10.10");
System.getProperties().put("https.proxyPort", "8080");

I know this isn't a best practice, so please suggest a better solution, also if anyone can give me a tip on how to set the authentification I'dd really appreciate it

like image 847
cosmin.danisor Avatar asked Jun 20 '13 13:06

cosmin.danisor


People also ask

What is proxy in HTTP client?

What Does HTTP Proxy Mean? An HTTP Proxy serves two intermediary roles as an HTTP Client and an HTTP Server for security, management, and caching functionality. The HTTP Proxy routes HTTP Client requests from a Web browser to the Internet, while supporting the caching of Internet data.


2 Answers

With apache CXF

HelloService hello = new HelloService();
HelloPortType helloPort = cliente.getHelloPort();
org.apache.cxf.endpoint.Client client = ClientProxy.getClient(helloPort);
HTTPConduit http = (HTTPConduit) client.getConduit();
http.getClient().setProxyServer("proxy");
http.getClient().setProxyServerPort(8080);
http.getProxyAuthorization().setUserName("user proxy");
http.getProxyAuthorization().setPassword("password proxy");
like image 178
Chuidiang Avatar answered Sep 23 '22 18:09

Chuidiang


If you´re using Spring Java configuration, to configure a JAX-WS Client with Apache CXF (3.x.x), the following code will work:

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import de.codecentric.namespace.weatherservice.WeatherService;

@Configuration
public class WeatherServiceConfiguration {

    @Bean
    public WeatherService weatherService() {
        JaxWsProxyFactoryBean jaxWsFactory = new JaxWsProxyFactoryBean();
        jaxWsFactory.setServiceClass(WeatherService.class);
        jaxWsFactory.setAddress("http://yourserviceurl.com/WeatherSoapService_1.0");
        return (WeatherService) jaxWsFactory.create();
    }

    @Bean
    public Client client() {
        Client client = ClientProxy.getClient(weatherService());
        HTTPConduit http = (HTTPConduit) client.getConduit();
        http.getClient().setProxyServer("yourproxy");
        http.getClient().setProxyServerPort(8080); // your proxy-port
        return client;
    }
}
like image 32
jonashackt Avatar answered Sep 21 '22 18:09

jonashackt