[[HTTP_Proxy]]
= HTTP Proxy

The HTTP Proxy related functionalities of JBoss Web Services are
provided by the Apache CXF http transport layer.

The suggested configuration mechanism when running JBoss Web Services is
explained below; for further information please refer to the
http://cxf.apache.org/docs/client-http-transport-including-ssl-support.html[Apache
CXF documentation].

[[configuration]]
== Configuration

The HTTP proxy configuration for a given JAX-WS client can be set in the
following ways:

* through the `http.proxyHost` and `http.proxyPort` system properties,
or
* leveraging the `org.apache.cxf.transport.http.HTTPConduit` options

The former is a JVM level configuration; for instance, assuming the http
proxy is currently running at http://localhost:9934, here is the setup:

[source, java]
----
System.getProperties().setProperty("http.proxyHost", "localhost");
System.getProperties().setProperty("http.proxyPort", 9934);
----

The latter is a client stub/port level configuration: the setup is
performed on the `HTTPConduit` object that's part of the Apache CXF
`Client` abstraction.

[source, java]
----
import org.apache.cxf.configuration.security.ProxyAuthorizationPolicy;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.cxf.transports.http.configuration.ProxyServerType;
...
 
Service service = Service.create(wsdlURL, new QName("http://org.jboss.ws/jaxws/cxf/httpproxy", "HelloWorldService"));
HelloWorld port = (HelloWorld) service.getPort(new QName("http://org.jboss.ws/jaxws/cxf/httpproxy", "HelloWorldImplPort"), HelloWorld.class);
 
Client client = ClientProxy.getClient(port);
HTTPConduit conduit = (HTTPConduit)client.getConduit();
ProxyAuthorizationPolicy policy = new ProxyAuthorizationPolicy();
policy.setAuthorizationType("Basic");
policy.setUserName(PROXY_USER);
policy.setPassword(PROXY_PWD);
conduit.setProxyAuthorization(policy);
 
port.echo("Foo");
----

The `ProxyAuthorizationPolicy` also allows for setting the authotization
type as well as the username / password to be used.

Speaking of authorization and authentication, please note that the JDK
already features the `java.net.Authenticator` facility, which is used
whenever opening a connection to a given URL requiring a http proxy.
Users might want to set a custom Authenticator for instance when needing
to read WSDL contracts before actually calling into the JBoss Web
Services / Apache CXF code; here is an example:

[source, java]
----
import java.net.Authenticator;
import java.net.PasswordAuthentication;
...
public class ProxyAuthenticator extends Authenticator
{
   private String user, password;
 
   public ProxyAuthenticator(String user, String password)
   {
      this.user = user;
      this.password = password;
   }
 
   protected PasswordAuthentication getPasswordAuthentication()
   {
      return new PasswordAuthentication(user, password.toCharArray());
   }
}
 
...
 
Authenticator.setDefault(new ProxyAuthenticator(PROXY_USER, PROXY_PWD));
----
