Tuesday, July 5, 2016

Overcoming proxy / non proxy host issue in axis2.xml WSO2 ESB ( Resolving Host to IP )

When using the WSO2 ESB and configuring the HTTP Proxy in axis2.xml, indicating that all the requests out from ESB should or shouldn't go through a HTTP Proxy ( eg: Squid Proxy ), will encounter a problem that when we use wild card inside the axis2.xml to filter the requests and use Host name inside the call mediator or send mediator, then due to ESB not able to convert the host name to IP, the HTTP Proxy configuration will not work.

To overcome the issue, need to use a custom class mediator which can convert the Host name to IP and set it to message context and then use uri-template in WSO2 ESB to call the URL as dynamic.


Below given is the sample axis configuration, proxy service and the custom class mediator.

<transportSender name="http" class="org.apache.synapse.transport.passthru.PassThroughHttpSender">
        <parameter name="non-blocking" locked="false">true</parameter>
        <parameter name="non-blocking" locked="false">true</parameter>
        <parameter name="http.proxyHost" locked="false">172.22.217.35</parameter>
        <parameter name="http.proxyPort" locked="false">3128</parameter>
        <parameter locked="false" name="http.nonProxyHosts">172.22.217.*</parameter>
        <parameter name="HostnameVerifier">AllowAll</parameter>
    </transportSender>

    <transportSender name="https" class="org.apache.synapse.transport.passthru.PassThroughHttpSSLSender">
        <parameter name="non-blocking" locked="false">true</parameter>
        <parameter name="http.proxyHost" locked="false">172.22.217.35</parameter>
        <parameter name="http.proxyPort" locked="false">3128</parameter>
        <parameter locked="false" name="http.nonProxyHosts">172.22.217.*</parameter>
        <parameter name="HostnameVerifier">AllowAll</parameter>
        <parameter name="keystore" locked="false">
            <KeyStore>
                <Location>repository/resources/security/wso2carbon.jks</Location>
                <Type>JKS</Type>
                <Password>wso2carbon</Password>
                <KeyPassword>wso2carbon</KeyPassword>
            </KeyStore>
        </parameter>
        <parameter name="truststore" locked="false">
            <TrustStore>
                <Location>repository/resources/security/client-truststore.jks</Location>
                <Type>JKS</Type>
                <Password>wso2carbon</Password>
            </TrustStore>
        </parameter>
        <!--<parameter name="HostnameVerifier">DefaultAndLocalhost</parameter>-->
            <!--supports Strict|AllowAll|DefaultAndLocalhost or the default if none specified -->
    </transportSender>

Sample Proxy Service

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="HostToIPResolver"
       transports="https http"
       startOnLoad="true"
       trace="disable">
   <description/>
   <target>
      <inSequence>
         <log level="custom">
            <property name="STATUS:"
                      value="----------------HostToIPResolver Proxy Invoked--------------------------"/>
         </log>
         <class name="com.custom.hostip.reslver.CustomHostToIPResolver">
            <property name="endpointHostName" value="ajanthan.wso2.com"/>
         </class>
         <property name="url_1" value="http://"/>
         <property name="url_2" expression="get-property('ResolvedIP')"/>
         <property name="url_3" value=":8088/mockechoSoap11Binding"/>
         <property name="concat_url"
                   expression="fn:concat(get-property('url_1'),get-property('url_2'), get-property('url_3'))"/>
         <log level="custom">
            <property name="Concatenated_URL:" expression="get-property('concat_url')"/>
         </log>
         <call>
            <endpoint name="endpointName"
                      template="HostEndpointTemplate"
                      uri="${concat_url}"/>
         </call>
         <respond/>
      </inSequence>
   </target>
</proxy>


Custom Class Mediator
package com.custom.hostip.reslver;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class CustomHostToIPResolver extends AbstractMediator {

 private static Log log = LogFactory.getLog(CustomHostToIPResolver.class);
 private String endpointHostName;

 public boolean mediate(MessageContext context) {

  if (log.isDebugEnabled()) {
   log.debug("[CUSTOM_LOG] CustomHostToIPResolver mediate method invoked.");
  }

  InetAddress address = null;

  try {
   address = InetAddress.getByName(endpointHostName);
  } catch (UnknownHostException e) {
   if (log.isDebugEnabled()) {
    log.debug("[CUSTOM_LOG] Exception Occured while resolving the IP Address."
      + e.getMessage());
   }
  }
  log.info("[CUSTOM_LOG] Resolved IP Address: "
    + address.getHostAddress());
  context.setProperty("ResolvedIP", address.getHostAddress());
  return true;
 }

 public String getEndpointHostName() {
  return endpointHostName;
 }

 public void setEndpointHostName(String endpointHostName) {
  this.endpointHostName = endpointHostName;
 }

}


pom.xml used to build the maven project

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.custom.hostip.reslver.CustomHostToIPResolver</groupId>
  <artifactId>CustomHostToIPResolver</artifactId>
  <version>1.0.0</version>
  <packaging>bundle</packaging>
  <name>CustomHostToIPResolver</name>
  <description>CustomHostToIPResolver</description>
  <properties>
    <CApp.type>lib/synapse/mediator</CApp.type>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.apache.synapse</groupId>
      <artifactId>synapse-core</artifactId>
      <version>2.1.2-wso2v2</version>
    </dependency>
    <dependency>
      <groupId>commons-httpclient.wso2</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1.0.wso2v2</version>
    </dependency>
    <dependency>
      <groupId>commons-codec.wso2</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.4.0.wso2v1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.woden.wso2</groupId>
      <artifactId>woden</artifactId>
      <version>1.0.0.M8-wso2v1</version>
    </dependency>
    <dependency>
      <groupId>wsdl4j.wso2</groupId>
      <artifactId>wsdl4j</artifactId>
      <version>1.6.2.wso2v4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.abdera.wso2</groupId>
      <artifactId>abdera</artifactId>
      <version>1.0.0.wso2v3</version>
    </dependency>
    <dependency>
      <groupId>org.apache.geronimo.specs.wso2</groupId>
      <artifactId>geronimo-stax-api_1.0_spec</artifactId>
      <version>1.0.1.wso2v2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.ws.commons.schema.wso2</groupId>
      <artifactId>XmlSchema</artifactId>
      <version>1.4.7.wso2v2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents.wso2</groupId>
      <artifactId>httpcore</artifactId>
      <version>4.3.0.wso2v1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.neethi.wso2</groupId>
      <artifactId>neethi</artifactId>
      <version>2.0.4.wso2v4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.ws.commons.axiom.wso2</groupId>
      <artifactId>axiom</artifactId>
      <version>1.2.11.wso2v4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.axis2.wso2</groupId>
      <artifactId>axis2</artifactId>
      <version>1.6.1.wso2v10</version>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io.wso2</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.0.0.wso2v2</version>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>daily</updatePolicy>
        <checksumPolicy>ignore</checksumPolicy>
      </releases>
      <id>wso2-nexus</id>
      <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>daily</updatePolicy>
        <checksumPolicy>ignore</checksumPolicy>
      </releases>
      <id>wso2-nexus</id>
      <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>2.3.4</version>
        <extensions>true</extensions>
        <configuration>
          <instructions>
            <Bundle-SymbolicName>CustomHostToIPResolver</Bundle-SymbolicName>
            <Bundle-Name>CustomHostToIPResolver</Bundle-Name>
            <Export-Package>com.custom.hostip.reslver</Export-Package>
            <DynamicImport-Package>*</DynamicImport-Package>
          </instructions>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.9</version>
        <configuration>
          <buildcommands>
            <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
          </buildcommands>
          <projectnatures>
            <projectnature>org.wso2.developerstudio.eclipse.artifact.mediator.project.nature</projectnature>
            <projectnature>org.eclipse.jdt.core.javanature</projectnature>
          </projectnatures>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>




No comments:

Post a Comment