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);
}
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;
}
Aggregations