some experimental code for proxy handling - will be removed later
authorKatja Luther <k.luther@bgbm.org>
Thu, 18 Nov 2021 13:22:19 +0000 (14:22 +0100)
committerKatja Luther <k.luther@bgbm.org>
Thu, 18 Nov 2021 13:22:19 +0000 (14:22 +0100)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/handler/update/UpdateHandler.java

index a932a96f1cc468508763f270415b5925221cad0a..be6b6c492ca4f21e2f471c79f97959c25496c731 100644 (file)
  *******************************************************************************/
 package eu.etaxonomy.taxeditor.handler.update;
 
+import java.net.InetSocketAddress;
+import java.net.MalformedURLException;
+import java.net.Proxy;
+import java.net.SocketAddress;
 import java.net.URI;
+import java.net.URL;
+import java.net.URLConnection;
 
 import org.apache.log4j.Logger;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -28,15 +35,19 @@ import org.eclipse.equinox.p2.operations.ProvisioningSession;
 import org.eclipse.equinox.p2.operations.UpdateOperation;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.swt.widgets.Shell;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
 import org.osgi.util.tracker.ServiceTracker;
-
+import org.eclipse.core.internal.net.ProxyData;
+import org.eclipse.core.internal.net.ProxyManager;
 import org.eclipse.core.net.proxy.IProxyData;
 import org.eclipse.core.net.proxy.IProxyService;
 
 import eu.etaxonomy.taxeditor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.workbench.Activator;
 
 /**
  * UpdateHandler invokes the check for updates UI
@@ -50,6 +61,7 @@ public class UpdateHandler {
     private UpdateOperation operation;
     
     private ServiceTracker proxyTracker;
+    Proxy proxy;
 
     @Execute
     public void execute(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync,
@@ -58,12 +70,17 @@ public class UpdateHandler {
         System.setProperty("socks.proxySet", "true");
         System.setProperty("socks.proxyHost", "127.0.0.1");
         System.setProperty("socks.proxyPort", "8090");
-        
-        proxyTracker = new ServiceTracker(FrameworkUtil.getBundle(
-                this.getClass()).getBundleContext(), IProxyService.class
-                .getName(), null);
-        proxyTracker.open();
-        
+//      
+       
+       SocketAddress addr = new InetSocketAddress("127.0.0.1", 8090);
+       proxy = new Proxy(Proxy.Type.SOCKS, addr);
+//     
+////           System.setProperty("socks.proxyPort", "8090");
+//        proxyTracker = new ServiceTracker(FrameworkUtil.getBundle(
+//                this.getClass()).getBundleContext(), IProxyService.class
+//                .getName(), null);
+//        proxyTracker.open();
+//        
        
         Job checkUpdateJob = new Job(Messages.UpdateHandler_CHECK_UPDATE_JOB) {
             @Override
@@ -110,7 +127,8 @@ public class UpdateHandler {
         });
     }
 
-    private IStatus checkForUpdates(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync,
+    @SuppressWarnings("restriction")
+       private IStatus checkForUpdates(final IProvisioningAgent agent, final Shell shell, final UISynchronize sync,
             IProgressMonitor monitor) {
 
         // configure update operation
@@ -121,25 +139,49 @@ public class UpdateHandler {
         
         configureUpdate(operation);
        
-        IProxyService proxyService = (IProxyService) proxyTracker.getService();
+        ProxyManager proxyService = getProxyService();
         URI uri = P2Util.getP2UpdateRepository();
+        proxyService.setSystemProxiesEnabled(false);
+        proxyService.setProxiesEnabled(true);
+        String[] nonProxiedHosts = new String[1];
+               nonProxiedHosts[0] = "130.133.70.27";
+               proxyService.setNonProxiedHosts(nonProxiedHosts);
+        ProxyData dta = (ProxyData) proxyService.getProxyData("SOCKS");
+        dta.setDynamic(true);
+        IProxyData[] proxies = new IProxyData[1];
+        proxies[0] = dta;
+        
+        proxyService.setProxyData(proxies);
+               IProxyData[] data = proxyService.getNativeProxyData();
+               
         MessagingUtils.info("proxy data: " + proxyService.getProxyData("SOCKS").toString());
         MessagingUtils.info("proxy data: " + proxyService.getProxyData("HTTP").toString());
         MessagingUtils.info("proxy data: " + proxyService.getProxyData("HTTPS").toString());
         boolean isEnabled = proxyService.isProxiesEnabled();
+        URL url;
+               try {
+                       url = uri.toURL();
+                       URLConnection conn = url.openConnection(proxy);
+                       boolean sysProxies = proxyService.hasSystemProxies();
+                        IProxyData[] proxyDataForHost = proxyService.select(uri);
+                       for (IProxyData proxyData : proxyDataForHost) {
+                           if (proxyData.getHost() != null) {
+                               MessagingUtils.info("http.proxyHost: " + proxyData.getHost());
+                               
+                               MessagingUtils.info("http.proxyPort: " + String.valueOf(proxyData
+                                       .getPort()));
+                           }
+                           
+                       }
+               } catch (Exception e) {
+                       // TODO Auto-generated catch block
+                       e.printStackTrace();
+               }
         
-        IProxyData[] proxyDataForHost = proxyService.select(uri);
-        for (IProxyData data : proxyDataForHost) {
-            if (data.getHost() != null) {
-               MessagingUtils.info("http.proxyHost: " + data.getHost());
-                
-                MessagingUtils.info("http.proxyPort: " + String.valueOf(data
-                        .getPort()));
-            }
-            
-        }
+       
         // check for updates, this causes I/O
         final IStatus status = operation.resolveModal(monitor);
+        ungetProxyService();
         MessagingUtils.info("update resolved " + System.currentTimeMillis() + "Status: " + status.toString());
         // failed to find updates (inform user and exit)
         if (status.getCode() == UpdateOperation.STATUS_NOTHING_TO_UPDATE) {
@@ -148,6 +190,20 @@ public class UpdateHandler {
         }
         return Status.OK_STATUS;
     }
+    
+    public static ProxyManager getProxyService() {
+        BundleContext bc = Activator.getDefault().getBundle().getBundleContext();
+        ServiceReference serviceReference = bc.getServiceReference(IProxyService.class.getName());
+        ProxyManager service = (ProxyManager) bc.getService(serviceReference);
+        return service;
+      }
+    
+    public static boolean ungetProxyService() {
+        BundleContext bc = Activator.getDefault().getBundle().getBundleContext();
+        ServiceReference serviceReference = bc.getServiceReference(IProxyService.class.getName());
+        return bc.ungetService(serviceReference);
+        
+      }
 
     private void configureProvisioningJob(ProvisioningJob provisioningJob, final Shell shell, final UISynchronize sync,
             final IWorkbench workbench) {