<!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="https://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;">
    Client Side Authentication in HTTP Gateway 
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/rajnish.kumar.76">Rajnish Kumar</a> in <i>JBoss ESB Development</i> - <a href="https://community.jboss.org/docs/DOC-19730">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p><strong style="font-size: 10pt;"><p>Dear All,</p><p>I had to do this for my project. If any one of you have this requirement then you can use it.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Environment Details :-</p><p>Consumer on HTTPs with client auth enabled</p><p>web server (apache or can be anything)</p><p>app server (Jboss ESB +++++) [I was using SOA-P 5.2]</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Issue :-</p><p>httpgateway (basically a listener) can't get client cert to authenticate the details of the customer.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Descritption, short coming and Solution :-</p><p>Basically problem lies in the layer where HTTP request with all it's header is passed to the action chain in the service where typicaly your gatewaty is httpgateway.</p><p><span style="color: #008080; font-size: 10pt;">&#160; </span></p><p><span style="color: #008080; font-size: 10pt;"><p>&lt;<span style="color: #3f7f7f; font-size: 10pt;">http-gateway</span><span style="font-size: 10pt;"> </span><span style="color: #7f007f; font-size: 10pt;">name</span><span style="font-size: 10pt;">=</span><em style="color: #2a00ff; font-size: 10pt;"><em style="color: #2a00ff; font-size: 10pt;">"HTTP-Gateway".....</em></em></p></span><p><span style="color: #000000;">Internally it uses few classes. Of particular interest to us are two classes which is used and needs to be customized to meet the need.</span></p></p><p align="left"><span style="font-size: 10pt;">org.jboss.soa.esb.listeners.gateway.http.HttpMessageComposer</span></p><p align="left"><span style="font-size: 10pt;"></span></p><p>org.jboss.soa.esb.listeners.gateway.http.HttpRequestWrapper</p><p><span style="font-size: 10pt;"></span></p><span style="font-size: 10pt;"></span></strong></p><p align="left"><span style="font-size: 10pt;"><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> &#160;</p>Changes that you will need to are following :-</span></p><p align="left"><span style="font-size: 10pt;">[1] Instruct ESB service to use your custom message composer class</span></p><p align="left"><span style="font-size: 10pt;">[2] Create a custom message composer class</span></p><p align="left"><span style="font-size: 10pt;">[3] Create a custom HttpRequestWraper class which can handle attributes, http headers and locales (what ever you need is, not necessary to handle everything)</span></p><p align="left" style="min-height: 8pt; height: 8pt; padding: 0px;"> &#160;</p><p align="left"><span style="font-size: 10pt;">Steps in details</span></p><p align="left"><span style="color: #008000; font-size: 12pt;">[1] Instruct ESB service to use your custom message composer class</span></p><p align="left" style="min-height: 8pt; height: 8pt; padding: 0px;"> &#160;</p><p align="left"> <span style="font-size: 10pt;">Modify your jboss-esb.xml file and add below statement as part of &lt;http-gateway&gt; tag</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p> <span style="color: #993366;">&lt;property name="composer-class" value="esb.gateway.CustomHttpMessageComposer"/&gt;</span></p><p>This tell the service to use the customized HttpMessageComposer</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="color: #008000; font-size: 12pt;">[2] Create a custom message composer class</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;"> &#160;</p><p><span style="color: #993366;">package esb.gateway;</span><br/><span style="color: #993366;">import java.util.Enumeration;</span><br/><span style="color: #993366;">import java.util.List;</span></p><p><span style="color: #993366;">import javax.servlet.http.HttpServletRequest;</span></p><p><span style="color: #993366;">import org.apache.log4j.Logger;</span><br/><span style="color: #993366;">import org.jboss.soa.esb.http.HttpHeader;</span><br/><span style="color: #993366;">import org.jboss.soa.esb.http.HttpRequest;</span><br/><span style="color: #993366;">import org.jboss.soa.esb.listeners.gateway.http.HttpMessageComposer;</span><br/><span style="color: #993366;">import org.jboss.soa.esb.listeners.gateway.http.HttpRequestWrapper;</span></p><p><span style="color: #993366;">public class CustomHttpMessageComposer&lt;T extends HttpRequestWrapper&gt; extends HttpMessageComposer&lt;T&gt;{</span></p><p><span style="color: #993366;"> private static final Logger logger = Logger.getLogger(CustomHttpMessageComposer.class);</span><br/><span style="color: #993366;"> </span><br/><span style="color: #993366;">&#160;&#160;&#160; @Override</span></p><p><span style="color: #993366;">/*</span></p><p><span style="color: #993366;">* this is the only method you need to override.</span></p><p><span style="color: #993366;">* make it return your custom httprequest class which can handle attributes and headers and locale</span></p><p><span style="color: #993366;">* retain all the original functionality by calling the super class method</span></p><p><span style="color: #993366;">*/</span><br/><span style="color: #993366;">&#160;&#160;&#160; public CustomHttpRequest getRequestInfo(HttpServletRequest request) {</span><br/><span style="color: #993366;">&#160;&#160;&#160;&#160; logger.info("co.za.fnbwealth.esb.gateway.CustomHttpMessageComposer getRequestInfo() called");</span><br/><span style="color: #993366;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; HttpRequest requestInfo = super.getRequestInfo(request);</span><br/><span style="color: #993366;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; CustomHttpRequest customRequestInfo = new CustomHttpRequest(requestInfo);</span><br/><span style="color: #993366;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><br/><span style="color: #993366;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; //Copy the attributes. This will have cert info</span><br/><span style="color: #993366;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; Enumeration attrNames = request.getAttributeNames();</span><br/><span style="color: #993366;">&#160; while(attrNames.hasMoreElements()){</span><br/><span style="color: #993366;">&#160;&#160; String attrName = (String)attrNames.nextElement();</span><br/><span style="color: #993366;">&#160;&#160; Object val = request.getAttribute(attrName);</span><br/><span style="color: #993366;">&#160;&#160; customRequestInfo.setAttribute(attrName, val);</span><br/><span style="color: #993366;">&#160; }</span><br/><span style="color: #993366;">&#160; </span><br/><span style="color: #993366;">&#160; //Copy Headers</span><br/><span style="color: #993366;">&#160; Enumeration hdrNames = request.getHeaderNames();</span><br/><span style="color: #993366;">&#160; List&lt;HttpHeader&gt; hdrs = customRequestInfo.getHeaders();</span><br/><span style="color: #993366;">&#160; while(hdrNames.hasMoreElements()){</span><br/><span style="color: #993366;">&#160;&#160; String hdrName = (String)hdrNames.nextElement();</span><br/><span style="color: #993366;">&#160;&#160; String hdrVal = request.getHeader(hdrName);</span><br/><span style="color: #993366;">&#160;&#160; HttpHeader hdr = new HttpHeader(hdrName, hdrVal);</span><br/><span style="color: #993366;">&#160;&#160; hdrs.add(hdr);</span><br/><span style="color: #993366;">&#160; }</span><br/><span style="color: #993366;">&#160; </span><br/><span style="color: #993366;">&#160; //Copy Locale</span><br/><span style="color: #993366;">&#160; customRequestInfo.setLocales(request.getLocales());</span></p><p><span style="color: #993366;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; return customRequestInfo;</span><br/><span style="color: #993366;">&#160;&#160;&#160; }</span></p><p><span style="color: #993366;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="color: #008000; font-size: 12pt;">[3] Create a custom HttpRequestWraper class</span></p><p><span style="color: #800080;">package esb.gateway;</span></p><p><span style="color: #800080;">import java.io.Serializable;</span><br/><span style="color: #800080;">import java.security.cert.X509Certificate;</span><br/><span style="color: #800080;">import java.util.Enumeration;</span><br/><span style="color: #800080;">import java.util.HashMap;</span><br/><span style="color: #800080;">import java.util.Map;</span></p><p><span style="color: #800080;">import org.apache.log4j.Logger;</span><br/><span style="color: #800080;">import org.jboss.soa.esb.http.HttpRequest;</span></p><p><span style="color: #800080;">public class CustomHttpRequest extends HttpRequest implements Serializable{</span><br/><span style="color: #800080;"> private static final Logger logger = Logger.getLogger(CustomHttpRequest.class);</span><br/><span style="color: #800080;"> </span><br/><span style="color: #800080;"> private X509Certificate clntCerts[];</span><br/><span style="color: #800080;"> private Map&lt;String, Object&gt; attrs = new HashMap&lt;String, Object&gt;();</span><br/><span style="color: #800080;"> //Locales implementation hasn't been tested including getter and setter for locales</span><br/><span style="color: #800080;"> private Enumeration locales;</span><br/><span style="color: #800080;"> /</span><span style="color: #800080;">*</span></p><p><span style="color: #800080;">* In the costructor get all the values by making call to the super class</span></p><p><span style="color: #800080;">* Add getter and setter method for nea features you want</span></p><p><span style="color: #800080;">* Here methods have been added to handle attribute(cleint cert comes in this), http header and locale</span></p><p><span style="color: #800080;">*/</span><br/><span style="color: #800080;"> public CustomHttpRequest(HttpRequest parent){</span><br/><span style="color: #800080;">&#160; logger.info("co.za.fnbwealth.esb.gateway.CustomHttpRequest constructor called");</span><br/><span style="color: #800080;">&#160; super.setRemoteAddr(parent.getRemoteAddr());</span><br/><span style="color: #800080;">&#160; super.setAuthType(parent.getAuthType());</span><br/><span style="color: #800080;">&#160; super.setCharacterEncoding(parent.getCharacterEncoding());</span><br/><span style="color: #800080;">&#160; super.setContentType(parent.getContentType()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setContextPath(parent.getContextPath()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setLocalAddr(parent.getLocalAddr()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setLocalName(parent.getLocalName()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setMethod(parent.getMethod()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setPathInfo(parent.getPathInfo()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setProtocol(parent.getProtocol());</span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setQueryString(parent.getQueryString());</span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setRemoteAddr(parent.getRemoteAddr()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setRemoteHost(parent.getRemoteHost()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setRemoteUser(parent.getRemoteUser()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setContentLength(parent.getContentLength()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setRequestSessionId(parent.getRequestSessionId()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setRequestURI(parent.getRequestURI()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setScheme(parent.getScheme()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setServerName(parent.getServerName()); </span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setRequestPath(parent.getRequestPath());</span><br/><span style="color: #800080;">&#160;&#160;&#160;&#160; super.setPathInfo(parent.getPathInfo());</span><br/><span style="color: #800080;"> }</span><br/><span style="color: #800080;"> public Object getAttribute(String attrName){</span><br/><span style="color: #800080;">&#160; logger.info("co.za.fnbwealth.esb.gateway.CustomHttpRequest getAttribute("+attrName+") called");</span><br/><span style="color: #800080;">&#160; return attrs.get(attrName);</span><br/><span style="color: #800080;"> }</span><br/><span style="color: #800080;"> </span><br/><span style="color: #800080;"> public void setAttribute(String attrName, Object value){</span><br/><span style="color: #800080;">&#160; logger.info("co.za.fnbwealth.esb.gateway.CustomHttpRequest setAttribute("+attrName+", "+value+") called");</span><br/><span style="color: #800080;">&#160; attrs.put(attrName, value);</span><br/><span style="color: #800080;"> }</span><br/><span style="color: #800080;"> </span><br/><span style="color: #800080;"> public void removeAttributeNames(String attrName){</span><br/><span style="color: #800080;">&#160; logger.info("co.za.fnbwealth.esb.gateway.CustomHttpRequest removeAttributeNames("+attrName+") called");</span><br/><span style="color: #800080;">&#160; attrs.remove(attrName);</span><br/><span style="color: #800080;"> }</span><br/><span style="color: #800080;"> </span><br/><span style="color: #800080;"> public Map&lt;String, Object&gt; getAttributes(){</span><br/><span style="color: #800080;">&#160; logger.info("co.za.fnbwealth.esb.gateway.CustomHttpRequest getAttributes() called");</span><br/><span style="color: #800080;">&#160; return attrs;</span><br/><span style="color: #800080;"> }</span><br/><span style="color: #800080;"> </span><br/><span style="color: #800080;"> public Enumeration getLocales(){</span><br/><span style="color: #800080;">&#160; return locales;</span><br/><span style="color: #800080;"> }</span><br/><span style="color: #800080;"> </span><br/><span style="color: #800080;"> public void setLocales(Enumeration locales){</span><br/><span style="color: #800080;">&#160; this.locales = locales;</span><br/><span style="color: #800080;"> }</span><br/><span style="color: #800080;">}</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="color: #000000;">This is all that's required for this.</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="color: #000000;">Finally note that you need to get and use customHttpRequest you have created in your custom action where you want to inspect the client cert(or. anything you customized).</span></p><p><span style="font-size: 10pt;">&#160; </span></p><p><span style="font-size: 10pt;"><p>CustomHttpRequest req = (CustomHttpRequest)message.getProperties().getProperty(</p></span></p><p><span style="color: #2a00ff; font-size: 10pt;">"org.jboss.soa.esb.http.HttpRequest#request"</span></p><p><span style="font-size: 10pt;">);</span></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Good Luck !!!</p><p>Cheers,</p><p>-Rajnish</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="https://community.jboss.org/docs/DOC-19730">going to Community</a></p>

        <p style="margin: 0;">Create a new document in JBoss ESB Development at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2032">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>