Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThierry BLIND2019-04-16 21:13:50 -0400
committerThierry BLIND2019-04-16 21:13:50 -0400
commit10c03cbd9ba6b03d9b777611617980fe8ec8ec9b (patch)
tree6b9d7d39b435b62f538b6acf97097d2609aa6ad8
parente992f2edd332b4a0811747e3193ba56ce2ea9b33 (diff)
downloadorg.eclipse.pdt-10c03cbd9ba6b03d9b777611617980fe8ec8ec9b.tar.gz
org.eclipse.pdt-10c03cbd9ba6b03d9b777611617980fe8ec8ec9b.tar.xz
org.eclipse.pdt-10c03cbd9ba6b03d9b777611617980fe8ec8ec9b.zip
Bug 546208 - Packagist search returns no results.HEADmaster
Change-Id: I07cb10b3df94a6642fe6b0f905ada6661728f886 Signed-off-by: Thierry BLIND <thierryblind@msn.com>
-rwxr-xr-xplugins/org.eclipse.php.composer.api/META-INF/MANIFEST.MF4
-rw-r--r--plugins/org.eclipse.php.composer.api/src/org/eclipse/php/composer/api/packages/AsyncDownloader.java108
-rw-r--r--plugins/org.eclipse.php.composer.api/src/org/eclipse/php/composer/api/packages/ProxyHelper.java112
-rwxr-xr-xplugins/org.eclipse.php.composer.core/src/org/eclipse/php/composer/core/ComposerPlugin.java6
4 files changed, 189 insertions, 41 deletions
diff --git a/plugins/org.eclipse.php.composer.api/META-INF/MANIFEST.MF b/plugins/org.eclipse.php.composer.api/META-INF/MANIFEST.MF
index 655b4b0a9..e9f1711d6 100755
--- a/plugins/org.eclipse.php.composer.api/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.php.composer.api/META-INF/MANIFEST.MF
@@ -9,7 +9,9 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.apache.commons.logging,
org.apache.httpcomponents.httpclient;bundle-version="4.3.6",
org.apache.httpcomponents.httpcore;bundle-version="4.3.3",
- com.google.gson;bundle-version="2.2.4"
+ com.google.gson;bundle-version="2.2.4",
+ org.eclipse.core.net,
+ org.eclipse.osgi
Export-Package: org.eclipse.php.composer.api,
org.eclipse.php.composer.api.collection,
org.eclipse.php.composer.api.entities,
diff --git a/plugins/org.eclipse.php.composer.api/src/org/eclipse/php/composer/api/packages/AsyncDownloader.java b/plugins/org.eclipse.php.composer.api/src/org/eclipse/php/composer/api/packages/AsyncDownloader.java
index 206c1f7a8..b6c86cf73 100644
--- a/plugins/org.eclipse.php.composer.api/src/org/eclipse/php/composer/api/packages/AsyncDownloader.java
+++ b/plugins/org.eclipse.php.composer.api/src/org/eclipse/php/composer/api/packages/AsyncDownloader.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2012, 2016, 2017 PDT Extension Group and others.
+ * Copyright (c) 2012, 2016, 2017, 2019 PDT Extension Group and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
@@ -13,42 +13,37 @@
*******************************************************************************/
package org.eclipse.php.composer.api.packages;
-import java.net.ProxySelector;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
-import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.http.HttpClientConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
-import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
-import org.apache.http.conn.ConnectionRequest;
-import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
-import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
-import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
public class AsyncDownloader extends AbstractDownloader {
- public final static int TIMEOUT = 30;
+ public final static int TIMEOUT_SECONDS = 30;
private int lastSlot = 1;
private Log log = LogFactory.getLog(AsyncDownloader.class);
private PoolingHttpClientConnectionManager connectionManager;
@@ -79,42 +74,66 @@ public class AsyncDownloader extends AbstractDownloader {
try {
URI uri = URI.create(url);
- final HttpGet httpGet = new HttpGet(uri);
- httpGet.addHeader("Accept", "*/*"); //$NON-NLS-1$ //$NON-NLS-2$
- httpGet.addHeader("User-Agent", getClass().getName()); //$NON-NLS-1$
- httpGet.addHeader("Host", uri.getHost()); //$NON-NLS-1$
- HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
- SystemDefaultRoutePlanner planner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
- HttpClientContext context = HttpClientContext.create();
- HttpRoute route = planner.determineRoute(host, httpGet, context);
-
- ConnectionRequest connRequest = connectionManager.requestConnection(route, null);
- HttpClientConnection conn = connRequest.get(TIMEOUT, TimeUnit.SECONDS);
+ // https://bugs.eclipse.org/bugs/show_bug.cgi?id=546208
+ //
+ // Previous connection code didn't take care about proxy
+ // authentication, even when SystemDefaultRoutePlanner was
+ // "luckily" able to find the right proxy to use.
+ // Also calling directly planner.determineRoute() didn't take
+ // care about the proxy authentication requests,
+ // see InternalHttpClient.doExecute() for better request
+ // execution chain.
+ // All proxy authentication informations (and the manual
+ // proxy definitions) are part of the proxy data settings stored
+ // and managed by eclipse, and can be retrieved
+ // by using IProxyService and a service tracker.
+ //
+ // final HttpGet httpGet = new HttpGet(uri);
+ // httpGet.addHeader("Accept", "*/*"); //$NON-NLS-1$
+ // //$NON-NLS-2$
+ // httpGet.addHeader("User-Agent", getClass().getName());
+ // //$NON-NLS-1$
+ // httpGet.addHeader("Host", uri.getHost()); //$NON-NLS-1$
+ // HttpHost host = new HttpHost(uri.getHost(), uri.getPort(),
+ // uri.getScheme());
+ // SystemDefaultRoutePlanner planner = new
+ // SystemDefaultRoutePlanner(ProxySelector.getDefault());
+ // HttpClientContext context = HttpClientContext.create();
+ // HttpRoute route = planner.determineRoute(host, httpGet,
+ // context);
+
+ HttpClientBuilder httpClientBuilder = ProxyHelper.createHttpClientBuilder(uri);
+
+ httpClientBuilder.setUserAgent("org.eclipse.php.composer.api"); //$NON-NLS-1$
+ httpClientBuilder.setConnectionManager(connectionManager);
+ RequestConfig.Builder requestBuilder = RequestConfig.custom();
+ requestBuilder = requestBuilder.setConnectTimeout(TIMEOUT_SECONDS * 1000)
+ .setConnectionRequestTimeout(TIMEOUT_SECONDS * 1000).setSocketTimeout(TIMEOUT_SECONDS * 1000);
+ httpClientBuilder.setDefaultRequestConfig(requestBuilder.build());
+
+ CloseableHttpClient httpClient = httpClientBuilder.build();
+ HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
+ HttpGet httpGet = new HttpGet(uri);
+
+ CloseableHttpResponse response = null;
+
try {
if (Thread.currentThread().isInterrupted()) {
closed();
return;
}
- connectionManager.connect(conn, route, 1000, context);
- if (Thread.currentThread().isInterrupted()) {
- closed();
- return;
- }
- connectionManager.routeComplete(conn, route, context);
+ response = httpClient.execute(target, httpGet);
+
if (Thread.currentThread().isInterrupted()) {
+ httpGet.abort();
closed();
return;
}
- HttpRequestExecutor exeRequest = new HttpRequestExecutor();
- context.setTargetHost(host);
- HttpResponse response = exeRequest.execute(httpGet, conn, context);
-
if (response.getStatusLine().getStatusCode() >= 300) {
throw new HttpResponseException(response.getStatusLine().getStatusCode(),
response.getStatusLine().getReasonPhrase());
-
}
HttpEntity entity = response.getEntity();
@@ -147,9 +166,13 @@ public class AsyncDownloader extends AbstractDownloader {
EntityUtils.consume(entity);
}
} finally {
- if (conn != null) {
- connectionManager.releaseConnection(conn, null, 1, TimeUnit.SECONDS);
+ if (response != null) {
+ response.close();
}
+ // Closing httpClient also shuts the
+ // PoolingHttpClientConnectionManager "connectionManager"
+ // down, don't do that.
+ // httpClient.close();
}
} catch (Exception ex) {
@@ -208,9 +231,9 @@ public class AsyncDownloader extends AbstractDownloader {
}
/**
- * Starts the async download. The returned number is the internal slot for this
- * download transfer, which can be used as parameter in abort to stop this
- * specific transfer.
+ * Starts the async download. The returned number is the internal slot for
+ * this download transfer, which can be used as parameter in abort to stop
+ * this specific transfer.
*
* @return slot
*/
@@ -277,4 +300,13 @@ public class AsyncDownloader extends AbstractDownloader {
connectionManager.shutdown();
}
}
+
+ @Override
+ protected void finalize() throws Throwable {
+ try {
+ shutdown();
+ } finally {
+ super.finalize();
+ }
+ }
}
diff --git a/plugins/org.eclipse.php.composer.api/src/org/eclipse/php/composer/api/packages/ProxyHelper.java b/plugins/org.eclipse.php.composer.api/src/org/eclipse/php/composer/api/packages/ProxyHelper.java
new file mode 100644
index 000000000..f3fbf72e5
--- /dev/null
+++ b/plugins/org.eclipse.php.composer.api/src/org/eclipse/php/composer/api/packages/ProxyHelper.java
@@ -0,0 +1,112 @@
+/*******************************************************************************
+ * Copyright (c) 2019 The Eclipse Foundation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Yatta Solutions - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.php.composer.api.packages;
+
+import java.net.URI;
+
+import org.apache.http.HttpHost;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
+import org.eclipse.core.net.proxy.IProxyData;
+import org.eclipse.core.net.proxy.IProxyService;
+import org.osgi.framework.BundleContext;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * See also classes org.eclipse.epp.internal.mpc.core.util.HttpUtil and
+ * org.eclipse.epp.internal.mpc.core.util.ProxyHelper from Marketplace Client
+ * Project (epp.mpc).
+ *
+ * @author Carsten Reckord
+ * @author Thiery Blind
+ *
+ * @see https://github.com/eclipse/epp.mpc/blob/master/org.eclipse.epp.mpc.core/src/org/eclipse/epp/internal/mpc/core/util/HttpUtil.java
+ * @see https://github.com/eclipse/epp.mpc/blob/master/org.eclipse.epp.mpc.core/src/org/eclipse/epp/internal/mpc/core/util/ProxyHelper.java
+ */
+public class ProxyHelper {
+ @SuppressWarnings("rawtypes")
+ private static ServiceTracker proxyServiceTracker;
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ public static synchronized void acquireProxyService(BundleContext context) {
+ if (proxyServiceTracker == null) {
+ proxyServiceTracker = new ServiceTracker(context, IProxyService.class.getName(), null);
+ proxyServiceTracker.open();
+ }
+ }
+
+ public static synchronized void releaseProxyService() {
+ if (proxyServiceTracker != null) {
+ proxyServiceTracker.close();
+ proxyServiceTracker = null;
+ }
+ }
+
+ public static synchronized IProxyService getProxyService() {
+ return proxyServiceTracker == null ? null : (IProxyService) proxyServiceTracker.getService();
+ }
+
+ public static IProxyData getProxyData(URI uri) {
+ final IProxyService proxyService = getProxyService();
+ if (proxyService != null) {
+ return doGetProxyData(proxyService, uri);
+ }
+ return null;
+ }
+
+ private static IProxyData doGetProxyData(final IProxyService proxyService, URI uri) {
+ if (uri.getHost() == null || uri.getScheme() == null) {
+ return null;
+ }
+ final IProxyData[] proxyData = proxyService.select(uri);
+ if (proxyData == null) {
+ return null;
+ }
+ for (IProxyData pd : proxyData) {
+ if (pd != null && pd.getHost() != null) {
+ return pd;
+ }
+ }
+ return null;
+ }
+
+ public static HttpClientBuilder createHttpClientBuilder(URI baseUri) {
+ HttpClientBuilder hcBuilder = HttpClients.custom();
+
+ if (baseUri != null) {
+ configureProxy(hcBuilder, baseUri);
+ }
+
+ return hcBuilder;
+ }
+
+ public static void configureProxy(HttpClientBuilder hcBuilder, URI uri) {
+ final IProxyData proxyData = getProxyData(uri);
+ if (proxyData != null && !IProxyData.SOCKS_PROXY_TYPE.equals(proxyData.getType())) {
+ HttpHost proxy = new HttpHost(proxyData.getHost(), proxyData.getPort());
+ DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
+ hcBuilder.setRoutePlanner(routePlanner);
+ if (proxyData.isRequiresAuthentication()) {
+ CredentialsProvider provider = new BasicCredentialsProvider();
+ provider.setCredentials(new AuthScope(proxyData.getHost(), proxyData.getPort()),
+ new UsernamePasswordCredentials(proxyData.getUserId(), proxyData.getPassword()));
+ hcBuilder.setDefaultCredentialsProvider(provider);
+ }
+ }
+ }
+}
diff --git a/plugins/org.eclipse.php.composer.core/src/org/eclipse/php/composer/core/ComposerPlugin.java b/plugins/org.eclipse.php.composer.core/src/org/eclipse/php/composer/core/ComposerPlugin.java
index e7d1e6741..d4a13e6f4 100755
--- a/plugins/org.eclipse.php.composer.core/src/org/eclipse/php/composer/core/ComposerPlugin.java
+++ b/plugins/org.eclipse.php.composer.core/src/org/eclipse/php/composer/core/ComposerPlugin.java
@@ -20,6 +20,7 @@ import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.dltk.core.IScriptProject;
+import org.eclipse.php.composer.api.packages.ProxyHelper;
import org.eclipse.php.composer.core.model.ModelAccess;
import org.eclipse.php.composer.core.resources.IComposerProject;
import org.eclipse.php.composer.internal.core.resources.ComposerProject;
@@ -50,13 +51,14 @@ public class ComposerPlugin extends Plugin {
};
workspace.addResourceChangeListener(listener);
+ ProxyHelper.acquireProxyService(bundleContext);
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
-
- super.stop(bundleContext);
+ ProxyHelper.releaseProxyService();
plugin = null;
+ super.stop(bundleContext);
}
public static ComposerPlugin getDefault() {

Back to the top