<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">
<div>
<table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
<tbody>
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
<tbody>
<tr>
<td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
<h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
<!-- To have a header image/logo replace the name below with your img tag -->
<!-- Email clients will render the images when the message is read so any image -->
<!-- must be made available on a public server, so that all recipients can load the image. -->
<a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px; -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
Re: SSL calls thru a proxy with authentication?
</h3>
<span style="margin-bottom: 10px;">
created by <a href="http://community.jboss.org/people/mnccouk">Matt Carless</a> in <i>JBoss Remoting</i> - <a href="http://community.jboss.org/message/564153#564153">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">
<div class="jive-rendered-content"><p>After many painful hours of trying various things I finally have the answer.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>The following is the only way I could get proxy authentication working via aweb service using jboss 5.1.0.GA.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p><span>First upgrade your jbossws to jbossws-cxf-3.3.1.GA(latest version as of writing) from (</span><a class="jive-link-external-small" href="http://www.jboss.org/jbossws/downloads.html" target="_blank">http://www.jboss.org/jbossws/downloads.html</a><span>). It's a simple task make sure you have apache ant installed as you will need it to install this update into jboss.</span></p><p>To upgrage download and unpack the zip file</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Follow the instuctions in the doc folder, basically it's set the home folder of your jboss app in the ant.properties file and run ant deploy-jboss510</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>You should be able to run ant test to test your installation (Don't forget to start your server first).</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Now jboss should be in good shape to handle proxies for web service clients</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>You will need to create a proxySelector</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Register the proxySelector</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Add some proxy-Authentication to the header of the web service request and your done.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Here's an example proxy selector. This has been taken from the JDK5 documetation. Change the proxy address to yours.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>package com.fugrodata.bgwebservices.session;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>import java.net.*;<br/>import java.util.ArrayList;<br/>import java.util.HashMap;<br/>import java.io.IOException;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>public class MyProxySelector extends ProxySelector {<br/>    // Keep a reference on the previous default<br/>    ProxySelector defsel = null;<br/>    <br/>    /*<br/>     * Inner class representing a Proxy and a few extra data<br/>     */<br/>    class InnerProxy {<br/>        Proxy proxy;<br/>        SocketAddress addr;<br/>        // How many times did we fail to reach this proxy?<br/>        int failedCount = 0;<br/>        <br/>        InnerProxy(InetSocketAddress a) {<br/>            addr = a;<br/>            proxy = new Proxy(Proxy.Type.HTTP, a);<br/>        }<br/>        <br/>        SocketAddress address() {<br/>            return addr;<br/>        }<br/>        <br/>        Proxy toProxy() {<br/>            return proxy;<br/>        }<br/>        <br/>        int failed() {<br/>            return ++failedCount;<br/>        }<br/>    }<br/>    <br/>    /*<br/>     * A list of proxies, indexed by their address.<br/>     */<br/>    HashMap<SocketAddress, InnerProxy> proxies = new HashMap<SocketAddress, InnerProxy>();</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>    MyProxySelector(ProxySelector def) {<br/>      // Save the previous default<br/>      defsel = def;<br/>      <br/>      // Populate the HashMap (List of proxies)<br/>      InnerProxy i = new InnerProxy(new InetSocketAddress("proxy", 8080));<br/>      proxies.put(i.address(), i);<br/>      //i = new InnerProxy(new InetSocketAddress("webcache2.mydomain.com", 8080));<br/>      //proxies.put(i.address(), i);<br/>      //i = new InnerProxy(new InetSocketAddress("webcache3.mydomain.com", 8080));<br/>      //proxies.put(i.address(), i);<br/>      }<br/>      <br/>      /*<br/>       * This is the method that the handlers will call.<br/>       * Returns a List of proxy.<br/>       */<br/>      public java.util.List<Proxy> select(URI uri) {<br/>        // Let's stick to the specs. <br/>        if (uri == null) {<br/>            throw new IllegalArgumentException("URI can't be null.");<br/>        }<br/>        <br/>        <br/>        <br/>        /*<br/>         * If it's a http (or https) URL, then we use our own<br/>         * list.<br/>         */<br/>        String protocol = uri.getScheme();<br/>        if ("http".equalsIgnoreCase(protocol) ||<br/>            "https".equalsIgnoreCase(protocol)) {<br/>            ArrayList<Proxy> l = new ArrayList<Proxy>();<br/>            for (InnerProxy p : proxies.values()) {<br/>              l.add(p.toProxy());<br/>            }<br/>            return l;<br/>        }<br/>        <br/>        /*<br/>         * Not HTTP or HTTPS (could be SOCKS or FTP)<br/>         * defer to the default selector.<br/>         */<br/>        if (defsel != null) {<br/>            return defsel.select(uri);<br/>        } else {<br/>            ArrayList<Proxy> l = new ArrayList<Proxy>();<br/>            l.add(Proxy.NO_PROXY);<br/>            return l;<br/>        }<br/>    }<br/>    <br/>    /*<br/>     * Method called by the handlers when it failed to connect<br/>     * to one of the proxies returned by select().<br/>     */<br/>    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {<br/>        // Let's stick to the specs again.<br/>        if (uri == null || sa == null || ioe == null) {<br/>            throw new IllegalArgumentException("Arguments can't be null.");<br/>        }<br/>        <br/>        /*<br/>         * Let's lookup for the proxy <br/>         */<br/>        InnerProxy p = proxies.get(sa); <br/>            if (p != null) {<br/>                /*<br/>                 * It's one of ours, if it failed more than 3 times<br/>                 * let's remove it from the list.<br/>                 */<br/>                if (p.failed() >= 3)<br/>                    proxies.remove(sa);<br/>            } else {<br/>                /*<br/>                 * Not one of ours, let's delegate to the default.<br/>                 */<br/>                if (defsel != null)<br/>                  defsel.connectFailed(uri, sa, ioe);<br/>            }<br/>     }<br/>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Register the proxy above using this code</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>MyProxySelector ps = new MyProxySelector(ProxySelector.getDefault());</p><p>ProxySelector.setDefault(ps);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Your jvm will start to use the proxy selector from this point forward. Now to add the authentication for the webservice request to authenticate against the proxy. The Proxy-Authorization header is added manually to the web service request. This is done on the webservice classes, here's an example.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>           Service service = new Service();<br/>           ServiceOperations portType = service.getService();<br/>            <br/>      <br/>            <br/>         Map<String,  List<String>> map2 = (Map<String,  List<String>>) ((BindingProvider) portType).getRequestContext().get(MessageContext.HTTP_REQUEST_HEADERS);<br/>         Map<String, List<String>> map = map2;<br/>         if (map == null)<br/>             map = new HashMap<String, List<String>>();<br/>        <br/>         map.put("Proxy-Authorization", Collections.singletonList(<br/>                 "Basic " +  Base64.encodeBytes(("username:password").getBytes())));<br/>        <br/>         System.out.println("Adding to HttpRequestHeaders [map="+map+"]  ...");<br/>         ((BindingProvider) portType).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS,  map);<br/>               <br/>///        portType.webserviceMethod(...);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Change the username and password to suit your proxy in the code above.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> </p><p>Job done!</p></div>
<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
<p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/564153#564153">going to Community</a></p>
<p style="margin: 0;">Start a new discussion in JBoss Remoting at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2050">Community</a></p>
</div></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>