Search in sources :

Example 1 with ProxyProfileConfig

use of org.apache.synapse.transport.http.conn.ProxyProfileConfig in project wso2-synapse by wso2.

the class ProxyConfigBuilder method build.

/**
 * Tries to read the axis2.xml transport sender's proxy configuration
 * @param transportOut axis2 transport out description
 * @return ProxyConfig
 * @throws AxisFault
 */
public ProxyConfig build(TransportOutDescription transportOut) throws AxisFault {
    name = transportOut.getName();
    Map<String, ProxyProfileConfig> proxyProfileConfigMap = getProxyProfiles(transportOut);
    // if proxy profile is configured we only read profile related configuration
    if (proxyProfileConfigMap == null) {
        String proxyHost = null;
        int proxyPort = -1;
        Parameter proxyHostParam = transportOut.getParameter(PassThroughConstants.HTTP_PROXY_HOST);
        if (proxyHostParam != null) {
            proxyHost = (String) proxyHostParam.getValue();
            Parameter proxyPortParam = transportOut.getParameter(PassThroughConstants.HTTP_PROXY_PORT);
            if (proxyPortParam != null) {
                proxyPort = Integer.parseInt((String) proxyPortParam.getValue());
            }
        }
        if (proxyHost == null) {
            proxyHost = System.getProperty(PassThroughConstants.HTTP_PROXY_HOST);
            if (proxyHost != null) {
                String s = System.getProperty(PassThroughConstants.HTTP_PROXY_PORT);
                if (s != null) {
                    proxyPort = Integer.parseInt(s);
                }
            }
        }
        if (proxyHost != null) {
            proxy = new HttpHost(proxyHost, proxyPort >= 0 ? proxyPort : 80);
            String bypassListStr = null;
            Parameter bypassListParam = transportOut.getParameter(PassThroughConstants.HTTP_NON_PROXY_HOST);
            if (bypassListParam == null) {
                bypassListStr = System.getProperty(PassThroughConstants.HTTP_NON_PROXY_HOST);
            } else {
                bypassListStr = (String) bypassListParam.getValue();
            }
            if (bypassListStr != null) {
                proxyBypass = bypassListStr.split("\\|");
            }
            Parameter proxyUsernameParam = transportOut.getParameter(PassThroughConstants.HTTP_PROXY_USERNAME);
            Parameter proxyPasswordParam = transportOut.getParameter(PassThroughConstants.HTTP_PROXY_PASSWORD);
            if (proxyUsernameParam != null) {
                proxyCredentials = new UsernamePasswordCredentials((String) proxyUsernameParam.getValue(), proxyPasswordParam != null ? (String) proxyPasswordParam.getValue() : "");
            }
        }
    }
    return new ProxyConfig(proxy, proxyCredentials, proxyBypass, proxyProfileConfigMap);
}
Also used : ProxyProfileConfig(org.apache.synapse.transport.http.conn.ProxyProfileConfig) HttpHost(org.apache.http.HttpHost) Parameter(org.apache.axis2.description.Parameter) ProxyConfig(org.apache.synapse.transport.http.conn.ProxyConfig) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 2 with ProxyProfileConfig

use of org.apache.synapse.transport.http.conn.ProxyProfileConfig in project wso2-synapse by wso2.

the class ProxyConfigBuilder method getProxyProfiles.

/**
 * Looks for a transport parameter named proxyProfiles and initializes a map of ProxyProfileConfig.
 * The syntax for defining a proxy profiles is as follows.
 * {@code
 * <parameter name="proxyProfiles">
 *      <profile>
 *          <targetHosts>example.com, *.sample.com</targetHosts>
 *          <proxyHost>localhost</proxyHost>
 *          <proxyPort>3128</proxyPort>
 *          <proxyUserName>squidUser</proxyUserName>
 *          <proxyPassword>password</proxyPassword>
 *          <bypass>xxx.sample.com</bypass>
 *      </profile>
 *      <profile>
 *          <targetHosts>localhost</targetHosts>
 *          <proxyHost>localhost</proxyHost>
 *          <proxyPort>7443</proxyPort>
 *      </profile>
 *      <profile>
 *          <targetHosts>*</targetHosts>
 *          <proxyHost>localhost</proxyHost>
 *          <proxyPort>7443</proxyPort>
 *          <bypass>test.com, direct.com</bypass>
 *      </profile>
 * </parameter>
 * }
 *
 * @param transportOut transport out description
 * @return map of <code>ProxyProfileConfig<code/> if configured in axis2.xml; otherwise null
 * @throws AxisFault if proxy profile is not properly configured
 */
private Map<String, ProxyProfileConfig> getProxyProfiles(TransportOutDescription transportOut) throws AxisFault {
    Parameter proxyProfilesParam = transportOut.getParameter("proxyProfiles");
    if (proxyProfilesParam == null) {
        return null;
    }
    if (log.isDebugEnabled()) {
        log.debug(name + " Loading proxy profiles for the HTTP/S sender");
    }
    OMElement proxyProfilesParamEle = proxyProfilesParam.getParameterElement();
    Iterator<?> profiles = proxyProfilesParamEle.getChildrenWithName(Q_PROFILE);
    Map<String, ProxyProfileConfig> proxyProfileMap = new HashMap<String, ProxyProfileConfig>();
    while (profiles.hasNext()) {
        OMElement profile = (OMElement) profiles.next();
        OMElement targetHostsEle = profile.getFirstChildWithName(Q_TARGET_HOSTS);
        if (targetHostsEle == null || targetHostsEle.getText().isEmpty()) {
            String msg = "Each proxy profile must define at least one host " + "or a wildcard matcher under the targetHosts element";
            log.error(name + " " + msg);
            throw new AxisFault(msg);
        }
        HttpHost proxy = getHttpProxy(profile, targetHostsEle.getText());
        UsernamePasswordCredentials proxyCredentials = getUsernamePasswordCredentials(profile);
        Set<String> proxyBypass = getProxyBypass(profile);
        ProxyProfileConfig proxyProfileConfig = new ProxyProfileConfig(proxy, proxyCredentials, proxyBypass);
        String[] targetHosts = targetHostsEle.getText().split(",");
        for (String endpoint : targetHosts) {
            endpoint = endpoint.trim();
            if (!proxyProfileMap.containsKey(endpoint)) {
                proxyProfileMap.put(endpoint, proxyProfileConfig);
            } else {
                log.warn(name + " Multiple proxy profiles were found for the endpoint: " + endpoint + ". Ignoring the excessive profiles.");
            }
        }
    }
    if (proxyProfileMap.size() > 0) {
        log.info(name + " Proxy profiles initialized for " + proxyProfileMap.size() + " targetHosts");
        return proxyProfileMap;
    }
    return null;
}
Also used : ProxyProfileConfig(org.apache.synapse.transport.http.conn.ProxyProfileConfig) AxisFault(org.apache.axis2.AxisFault) HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) Parameter(org.apache.axis2.description.Parameter) OMElement(org.apache.axiom.om.OMElement) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

Parameter (org.apache.axis2.description.Parameter)2 HttpHost (org.apache.http.HttpHost)2 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)2 ProxyProfileConfig (org.apache.synapse.transport.http.conn.ProxyProfileConfig)2 HashMap (java.util.HashMap)1 OMElement (org.apache.axiom.om.OMElement)1 AxisFault (org.apache.axis2.AxisFault)1 ProxyConfig (org.apache.synapse.transport.http.conn.ProxyConfig)1