Merged 5.1.N (5.1.4) to 5.2.N (5.2.1)

135900 amorarasu: REPO-2164 / MNT-17555: Transfer jobs should work with a proxy
      - Made the httpclient communicating with the fstr server, aware of the proxy settings.
      - Moved the proxy config logic to a helper class.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@135967 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ancuta Morarasu
2017-03-17 17:03:08 +00:00
parent 467db88fa5
commit 40a906012c
3 changed files with 1424 additions and 1309 deletions

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* Copyright (C) 2005 - 2017 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
@@ -31,7 +31,6 @@ import java.io.InputStream;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -48,12 +47,10 @@ import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.ProxyHost;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.simple.JSONObject;
@@ -61,14 +58,8 @@ import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.extensions.webscripts.Status;
import static org.alfresco.service.cmr.favourites.FavouritesService.SortFields.username;
/**
* HttpClient powered implementation of {@link RemoteConnectorService}, which
* performs requests to remote HTTP servers.
*
* Note - this class assumes direct connectivity is available to the destination
* system, and does not support proxies.
* HttpClient powered implementation of {@link RemoteConnectorService}, which performs requests to remote HTTP servers.
*
* @author Nick Burch
* @since 4.0.2
@@ -94,16 +85,14 @@ public class RemoteConnectorServiceImpl implements RemoteConnectorService
static
{
// Create an HTTP Proxy Host if appropriate system property set
httpProxyHost = createProxyHost("http.proxyHost", "http.proxyPort", 80);
httpProxyCredentials = createProxyCredentials("http.proxyUser", "http.proxyPassword");
httpProxyHost = HttpClientHelper.createProxyHost("http.proxyHost", "http.proxyPort", 80);
httpProxyCredentials = HttpClientHelper.createProxyCredentials("http.proxyUser", "http.proxyPassword");
httpAuthScope = createProxyAuthScope(httpProxyHost);
// Create an HTTPS Proxy Host if appropriate system property set
httpsProxyHost = createProxyHost("https.proxyHost", "https.proxyPort", 443);
httpsProxyCredentials = createProxyCredentials("https.proxyUser", "https.proxyPassword");
httpsProxyHost = HttpClientHelper.createProxyHost("https.proxyHost", "https.proxyPort", 443);
httpsProxyCredentials = HttpClientHelper.createProxyCredentials("https.proxyUser", "https.proxyPassword");
httpsAuthScope = createProxyAuthScope(httpsProxyHost);
}
public RemoteConnectorServiceImpl()
@@ -157,30 +146,38 @@ public class RemoteConnectorServiceImpl implements RemoteConnectorService
URL url = new URL(request.getURL());
// Use the appropriate Proxy Host if required
if (httpProxyHost != null && url.getProtocol().equals("http") && requiresProxy(url.getHost()))
if (httpProxyHost != null && url.getProtocol().equals("http") && HttpClientHelper.requiresProxy(url.getHost()))
{
httpClient.getHostConfiguration().setProxyHost(httpProxyHost);
if (logger.isDebugEnabled())
{
logger.debug(" - using HTTP proxy host for: " + url);
}
if (httpProxyCredentials != null)
{
httpClient.getState().setProxyCredentials(httpAuthScope, httpProxyCredentials);
if (logger.isDebugEnabled())
{
logger.debug(" - using HTTP proxy credentials for proxy: " + httpProxyHost.getHostName());
}
}
else if (httpsProxyHost != null && url.getProtocol().equals("https") && requiresProxy(url.getHost()))
}
else if (httpsProxyHost != null && url.getProtocol().equals("https") && HttpClientHelper.requiresProxy(url.getHost()))
{
httpClient.getHostConfiguration().setProxyHost(httpsProxyHost);
if (logger.isDebugEnabled())
{
logger.debug(" - using HTTPS proxy host for: " + url);
}
if (httpsProxyCredentials != null)
{
httpClient.getState().setProxyCredentials(httpsAuthScope, httpsProxyCredentials);
if (logger.isDebugEnabled())
{
logger.debug(" - using HTTPS proxy credentials for proxy: " + httpsProxyHost.getHostName());
}
}
}
else
{
//host should not be proxied remove any configured proxies
@@ -189,7 +186,8 @@ public class RemoteConnectorServiceImpl implements RemoteConnectorService
}
// Log what we're doing
if (logger.isDebugEnabled()) {
if (logger.isDebugEnabled())
{
logger.debug("Performing " + request.getMethod() + " request to " + request.getURL());
for (Header hdr : request.getRequestHeaders())
{
@@ -452,56 +450,6 @@ public class RemoteConnectorServiceImpl implements RemoteConnectorService
}
}
/**
* Create proxy host for the given system host and port properties.
* If the properties are not set, no proxy will be created.
*
* @param hostProperty String
* @param portProperty String
* @param defaultPort int
*
* @return ProxyHost if appropriate properties have been set, null otherwise
*/
private static ProxyHost createProxyHost(final String hostProperty, final String portProperty, final int defaultPort)
{
final String proxyHost = System.getProperty(hostProperty);
ProxyHost proxy = null;
if (proxyHost != null && proxyHost.length() != 0)
{
final String strProxyPort = System.getProperty(portProperty);
if (strProxyPort == null || strProxyPort.length() == 0)
{
proxy = new ProxyHost(proxyHost, defaultPort);
}
else
{
proxy = new ProxyHost(proxyHost, Integer.parseInt(strProxyPort));
}
if (logger.isDebugEnabled())
logger.debug("ProxyHost: " + proxy.toString());
}
return proxy;
}
/**
* Create the proxy credentials for the given proxy user and password properties.
* If the properties are not set, not credentials will be created.
* @param proxyUserProperty String
* @param proxyPasswordProperty String
* @return Credentials if appropriate properties have been set, null otherwise
*/
private static Credentials createProxyCredentials(final String proxyUserProperty, final String proxyPasswordProperty)
{
final String proxyUser = System.getProperty(proxyUserProperty);
final String proxyPassword = System.getProperty(proxyPasswordProperty);
Credentials creds = null;
if (StringUtils.isNotBlank(proxyUser))
{
creds = new UsernamePasswordCredentials(proxyUser, proxyPassword);
}
return creds;
}
/**
* Create suitable AuthScope for ProxyHost.
* If the ProxyHost is null, no AuthsScope will be created.
@@ -518,30 +466,4 @@ public class RemoteConnectorServiceImpl implements RemoteConnectorService
return authScope;
}
/**
* Return true unless the given target host is specified in the <code>http.nonProxyHosts</code> system property.
* See http://download.oracle.com/javase/1.4.2/docs/guide/net/properties.html
* @param targetHost Non-null host name to test
* @return true if not specified in list, false if it is specifed and therefore should be excluded from proxy
*/
private boolean requiresProxy(final String targetHost)
{
boolean requiresProxy = true;
final String nonProxyHosts = System.getProperty("http.nonProxyHosts");
if (nonProxyHosts != null)
{
StringTokenizer tokenizer = new StringTokenizer(nonProxyHosts, "|");
while (tokenizer.hasMoreTokens())
{
String pattern = tokenizer.nextToken();
pattern = pattern.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*");
if (targetHost.matches(pattern))
{
requiresProxy = false;
break;
}
}
}
return requiresProxy;
}
}

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* Copyright (C) 2005 - 2017 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
@@ -50,15 +50,18 @@ import org.alfresco.service.cmr.transfer.TransferException;
import org.alfresco.service.cmr.transfer.TransferProgress;
import org.alfresco.service.cmr.transfer.TransferTarget;
import org.alfresco.service.cmr.transfer.TransferVersion;
import org.alfresco.util.HttpClientHelper;
import org.alfresco.util.PropertyCheck;
import org.alfresco.util.json.ExceptionJsonSerializer;
import org.alfresco.util.json.JsonSerializer;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.ProxyHost;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
@@ -71,7 +74,6 @@ import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.apache.commons.httpclient.protocol.SSLProtocolSocketFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
/**
@@ -106,6 +108,13 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
private NodeService nodeService;
private boolean isAuthenticationPreemptive = false;
private ProxyHost httpProxyHost;
private ProxyHost httpsProxyHost;
private Credentials httpProxyCredentials;
private Credentials httpsProxyCredentials;
private AuthScope httpAuthScope;
private AuthScope httpsAuthScope;
public HttpClientTransmitterImpl()
{
protocolMap = new TreeMap<String,Protocol>();
@@ -116,6 +125,16 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
httpClient.setHttpConnectionManager(new MultiThreadedHttpConnectionManager());
httpMethodFactory = new StandardHttpMethodFactoryImpl();
jsonErrorSerializer = new ExceptionJsonSerializer();
// Create an HTTP Proxy Host if appropriate system properties are set
httpProxyHost = HttpClientHelper.createProxyHost("http.proxyHost", "http.proxyPort", DEFAULT_HTTP_PORT);
httpProxyCredentials = HttpClientHelper.createProxyCredentials("http.proxyUser", "http.proxyPassword");
httpAuthScope = createProxyAuthScope(httpProxyHost);
// Create an HTTPS Proxy Host if appropriate system properties are set
httpsProxyHost = HttpClientHelper.createProxyHost("https.proxyHost", "https.proxyPort", DEFAULT_HTTPS_PORT);
httpsProxyCredentials = HttpClientHelper.createProxyCredentials("https.proxyUser", "https.proxyPassword");
httpsAuthScope = createProxyAuthScope(httpsProxyHost);
}
public void init()
@@ -226,12 +245,52 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
* @param target TransferTarget
* @return HttpState
*/
protected HttpState getHttpState(TransferTarget target)
private HttpState getHttpState(TransferTarget target)
{
HttpState httpState = new HttpState();
httpState.setCredentials(new AuthScope(target.getEndpointHost(), target.getEndpointPort(),
AuthScope.ANY_REALM),
new UsernamePasswordCredentials(target.getUsername(), new String(target.getPassword())));
String requiredProtocol = target.getEndpointProtocol();
if (requiredProtocol == null)
{
throw new TransferException(MSG_UNSUPPORTED_PROTOCOL, new Object[] {requiredProtocol});
}
Protocol protocol = protocolMap.get(requiredProtocol.toLowerCase().trim());
if (protocol == null)
{
log.error("Unsupported protocol: " + requiredProtocol);
throw new TransferException(MSG_UNSUPPORTED_PROTOCOL, new Object[] {requiredProtocol});
}
// Use the appropriate Proxy credentials if required
if (httpProxyHost != null && HTTP_SCHEME_NAME.equals(protocol.getScheme()) && HttpClientHelper.requiresProxy(target.getEndpointHost()))
{
if (httpProxyCredentials != null)
{
httpState.setProxyCredentials(httpAuthScope, httpProxyCredentials);
if (log.isDebugEnabled())
{
log.debug("Using HTTP proxy credentials for proxy: " + httpProxyHost.getHostName());
}
}
}
else if (httpsProxyHost != null && HTTPS_SCHEME_NAME.equals(protocol.getScheme()) && HttpClientHelper.requiresProxy(target.getEndpointHost()))
{
if (httpsProxyCredentials != null)
{
httpState.setProxyCredentials(httpsAuthScope, httpsProxyCredentials);
if (log.isDebugEnabled())
{
log.debug("Using HTTPS proxy credentials for proxy: " + httpsProxyHost.getHostName());
}
}
}
return httpState;
}
@@ -244,20 +303,56 @@ public class HttpClientTransmitterImpl implements TransferTransmitter
String requiredProtocol = target.getEndpointProtocol();
if (requiredProtocol == null)
{
throw new TransferException(MSG_UNSUPPORTED_PROTOCOL, new Object[] {target.getEndpointProtocol()});
throw new TransferException(MSG_UNSUPPORTED_PROTOCOL, new Object[] {requiredProtocol});
}
Protocol protocol = protocolMap.get(requiredProtocol.toLowerCase().trim());
if (protocol == null) {
if (protocol == null)
{
log.error("Unsupported protocol: " + target.getEndpointProtocol());
throw new TransferException(MSG_UNSUPPORTED_PROTOCOL, new Object[] {target.getEndpointProtocol()});
throw new TransferException(MSG_UNSUPPORTED_PROTOCOL, new Object[] {requiredProtocol});
}
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost(target.getEndpointHost(), target.getEndpointPort(), protocol);
// Use the appropriate Proxy Host if required
if (httpProxyHost != null && HTTP_SCHEME_NAME.equals(protocol.getScheme()) && HttpClientHelper.requiresProxy(target.getEndpointHost()))
{
hostConfig.setProxyHost(httpProxyHost);
if (log.isDebugEnabled())
{
log.debug("Using HTTP proxy host for: " + target.getEndpointHost());
}
}
else if (httpsProxyHost != null && HTTPS_SCHEME_NAME.equals(protocol.getScheme()) && HttpClientHelper.requiresProxy(target.getEndpointHost()))
{
hostConfig.setProxyHost(httpsProxyHost);
if (log.isDebugEnabled())
{
log.debug("Using HTTPS proxy host for: " + target.getEndpointHost());
}
}
return hostConfig;
}
/**
* Create suitable AuthScope for ProxyHost. If the ProxyHost is null, no AuthsScope will be created.
* @param proxyHost ProxyHost
* @return AuthScope for provided ProxyHost, null otherwise.
*/
private AuthScope createProxyAuthScope(final ProxyHost proxyHost)
{
AuthScope authScope = null;
if (proxyHost != null)
{
authScope = new AuthScope(proxyHost.getHostName(), proxyHost.getPort());
}
return authScope;
}
public Transfer begin(TransferTarget target, String fromRepositoryId, TransferVersion fromVersion) throws TransferException
{
PostMethod beginRequest = getPostMethod();

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* Copyright (C) 2005 - 2017 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
@@ -25,8 +25,14 @@
*/
package org.alfresco.util;
import java.util.StringTokenizer;
import org.alfresco.httpclient.HttpClientFactory;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.ProxyHost;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.connector.RemoteClient;
@@ -40,6 +46,19 @@ import org.springframework.extensions.webscripts.connector.RemoteClient;
*
* TODO Merge me back to Spring Surf, which is where this code has been
* pulled out from (was in {@link RemoteClient} but not available externally)
*
* This class also provides support for creating proxy configurations,
* taking into account System properties like:
* </p>
* <ul>
* <li>http.proxyHost</li>
* <li>http.proxyPort</li>
* <li>http.nonProxyHosts</li>
* <li>http.proxyUser</li>
* <li>http.proxyPassword</li>
* </ul>
* <p>
*
*/
public class HttpClientHelper
{
@@ -64,4 +83,83 @@ public class HttpClientHelper
{
return httpClient.get();
}
/**
* Create proxy host for the given system host and port properties.
* If the properties are not set, no proxy will be created.
*
* @param hostProperty the name of the system property for the proxy server (<code>http.proxyHost</code> or <code>https.proxyHost</code>)
* @param portProperty the name of the system property for the proxy server port (http.proxyPort)
* @param defaultPort
*
* @return ProxyHost if appropriate properties have been set, null otherwise
*/
public static ProxyHost createProxyHost(final String hostProperty, final String portProperty, final int defaultPort)
{
final String proxyHost = System.getProperty(hostProperty);
ProxyHost proxy = null;
if (proxyHost != null && proxyHost.length() != 0)
{
final String strProxyPort = System.getProperty(portProperty);
if (strProxyPort == null || strProxyPort.length() == 0)
{
proxy = new ProxyHost(proxyHost, defaultPort);
}
else
{
proxy = new ProxyHost(proxyHost, Integer.parseInt(strProxyPort));
}
if (logger.isDebugEnabled())
{
logger.debug("ProxyHost: " + proxy.toString());
}
}
return proxy;
}
/**
* Create the proxy credentials for the given proxy user and password properties.
* If the properties are not set, not credentials will be created.
* @param proxyUserProperty the name of the system property for the proxy user
* @param proxyPasswordProperty the name of the system property for the proxy password
* @return Credentials if appropriate properties have been set, null otherwise
*/
public static Credentials createProxyCredentials(final String proxyUserProperty, final String proxyPasswordProperty)
{
final String proxyUser = System.getProperty(proxyUserProperty);
final String proxyPassword = System.getProperty(proxyPasswordProperty);
Credentials credentials = null;
if (StringUtils.isNotBlank(proxyUser))
{
credentials = new UsernamePasswordCredentials(proxyUser, proxyPassword);
}
return credentials;
}
/**
* Return true unless the given target host is specified in the <code>http.nonProxyHosts</code> system property.
* See http://docs.oracle.com/javase/8/docs/api/java/net/doc-files/net-properties.html
* @param targetHost Non-null host name to verify
* @return true if not specified in the list, false if it is specified and therefore should be excluded from proxy
*/
public static boolean requiresProxy(final String targetHost)
{
boolean requiresProxy = true;
final String nonProxyHosts = System.getProperty("http.nonProxyHosts");
if (nonProxyHosts != null)
{
StringTokenizer tokenizer = new StringTokenizer(nonProxyHosts, "|");
while (tokenizer.hasMoreTokens())
{
String pattern = tokenizer.nextToken();
pattern = pattern.replaceAll("\\.", "\\\\.").replaceAll("\\*", ".*");
if (targetHost.matches(pattern))
{
requiresProxy = false;
break;
}
}
}
return requiresProxy;
}
}