connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/installpackages/InstallPackages.java
changeset 0 fb279309251b
child 421 7bf2b8a16445
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/installpackages/InstallPackages.java	Fri Apr 03 23:33:03 2009 +0100
@@ -0,0 +1,235 @@
+/*
+* Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
+* All rights reserved.
+* This component and the accompanying materials are made available
+* under the terms of the License "Eclipse Public License v1.0"
+* which accompanies this distribution, and is available
+* at the URL "http://www.eclipse.org/legal/epl-v10.html".
+*
+* Initial Contributors:
+* Nokia Corporation - initial contribution.
+*
+* Contributors:
+*
+* Description: 
+*
+*/
+
+
+package com.nokia.carbide.installpackages;
+
+
+import com.nokia.carbide.installpackages.gen.InstallPackages.*;
+import com.nokia.carbide.installpackages.gen.InstallPackages.util.InstallPackagesResourceFactoryImpl;
+import com.nokia.carbide.remoteconnections.Messages;
+import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
+import com.nokia.carbide.remoteconnections.interfaces.IRemoteAgentInstallerProvider;
+import com.nokia.cpp.internal.api.utils.core.Check;
+import com.nokia.cpp.internal.api.utils.core.FileUtils;
+
+import org.apache.commons.httpclient.*;
+import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.eclipse.core.net.proxy.IProxyData;
+import org.eclipse.core.net.proxy.IProxyService;
+import org.eclipse.core.runtime.*;
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.jface.operation.IRunnableContext;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.osgi.service.datalocation.Location;
+
+import java.io.*;
+import java.lang.reflect.InvocationTargetException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.*;
+
+/**
+ *
+ */
+public class InstallPackages {
+	
+	public interface IServerData {
+		
+		/**
+		 * The file name of the master xml file
+		 * @return String
+		 */
+		String getMasterFileName();
+		
+		/**
+		 * Return the IRemoteAgentInstallerProvider for this server data
+		 * @return IRemoteAgentInstallerProvider
+		 */
+		IRemoteAgentInstallerProvider getRemoteAgentInstallerProvider();
+	}
+
+	private final IServerData serverData;
+	private List<PackageType> packageList;
+	private IPath serverPath;
+	
+	public InstallPackages(IServerData serverData, IRunnableContext runnableContext) {
+		Check.checkArg(serverData);
+		this.serverData = serverData;
+		if (runnableContext == null)
+			getPackageList();
+		else {
+			try {
+				runnableContext.run(true, false, new IRunnableWithProgress() {
+					public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+						monitor.beginTask(Messages.getString("InstallPackages.GettingPackagesJobLabel"), IProgressMonitor.UNKNOWN); //$NON-NLS-1$
+						getPackageList();
+					}
+				});
+			} catch (Exception e) {
+				RemoteConnectionsActivator.logError(e);
+			}
+		}
+	}
+	
+	private void getPackageList() {
+		try {
+			PackagesType packages = getAvailablePackagesFromServer();
+			if (packages != null) {
+				EList<PackageType> elist = packages.getPackage();
+				packageList = new ArrayList<PackageType>(elist);
+				Collections.sort(packageList, new Comparator<PackageType>() {
+					public int compare(PackageType o1, PackageType o2) {
+						return o1.getDisplayName().compareToIgnoreCase(o2.getDisplayName());
+					}
+				});
+			}
+		} catch (Exception e) {
+			RemoteConnectionsActivator.logError(e);
+			if (e.getCause() instanceof ConnectTimeoutException)
+				RemoteConnectionsActivator.logMessage(Messages.getString("InstallPackages.TimeoutMissingProxyMessage"), IStatus.INFO); //$NON-NLS-1$
+		}
+	}
+	
+	public List<PackageType> getAvailablePackageList() {
+		return packageList;
+	}
+	
+	private static PackagesType loadPackages(URL url) throws Exception  {
+		if (url == null)
+			return null;
+
+		URI xmlURI = URI.createURI(url.toString());
+
+		InstallPackagesResourceFactoryImpl factory = new InstallPackagesResourceFactoryImpl();
+		Resource r = factory.createResource(xmlURI);
+
+		r.load(null);
+		EList contents = r.getContents();
+	
+		DocumentRoot root = (DocumentRoot) contents.get(0);
+		PackagesType packages = root.getPackages();
+
+		return packages;
+	}
+
+	private PackagesType getAvailablePackagesFromServer() throws Exception {
+        GetMethod getMethod = new GetMethod(getRelativePath(serverData.getMasterFileName()));
+		HttpClient client = new HttpClient();
+		setProxyData(client, getMethod);
+        client.getHttpConnectionManager().getParams().setConnectionTimeout(8000);
+        int serverStatus = 0;
+        byte[] responseBody;
+		try {
+			serverStatus = client.executeMethod(getMethod);
+			responseBody = getMethod.getResponseBody();
+		} catch (Exception e) {
+			// could be HttpException or IOException
+			throw new Exception(e);
+		} finally {
+			getMethod.releaseConnection();
+		}
+        
+        // HTTP status codes: 2xx = Success
+    	if (serverStatus >= 200 && serverStatus < 300) {
+    		File tempDir = FileUtils.getTemporaryDirectory();
+    		IPath path = new Path(tempDir.getAbsolutePath());
+    		IPath masterFilePath = path.append(serverData.getMasterFileName());
+    		File masterFile = masterFilePath.toFile();
+    		if (masterFile.exists())
+    			masterFile.delete();
+            FileOutputStream fos = new FileOutputStream(masterFile);
+            BufferedOutputStream out = new BufferedOutputStream(fos);
+            ByteArrayInputStream in = new ByteArrayInputStream(responseBody);
+            boolean foundOpenBrace = false;
+            int c;
+            while ((c = in.read()) != -1) {
+            	if (c == '<')
+            		foundOpenBrace = true;
+            	if (foundOpenBrace)
+            		out.write(c);
+            }
+            out.close();
+            in.close();
+    		URL url = masterFile.toURL();
+    		return loadPackages(url);
+    	}
+    	
+       return null;
+	}
+
+	public static void setProxyData(HttpClient client, GetMethod getMethod) {
+		IProxyService proxyService = RemoteConnectionsActivator.getDefault().getProxyService();
+		boolean proxiesEnabled = proxyService.isProxiesEnabled();
+		if (!proxiesEnabled)
+			return;
+		IProxyData proxyData = proxyService.getProxyData(IProxyData.HTTP_PROXY_TYPE);
+		String host = proxyData.getHost();
+		int port = proxyData.getPort();
+		client.getHostConfiguration().setProxy(host, port);
+		if (proxyData.isRequiresAuthentication()) {
+			String userId = proxyData.getUserId();
+			String password = proxyData.getPassword();
+			UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userId, password);
+			AuthScope authScope = new AuthScope(host, port);
+			client.getState().setCredentials(authScope, credentials);
+			getMethod.setDoAuthentication(true);
+		}
+	}
+	
+	public String getInstallUrlString(String installFilePath) {
+		URL url;
+		try {
+			url = new URL(installFilePath);
+		} catch (MalformedURLException e) {
+			return getRelativePath(installFilePath);
+		}
+		return url.toString();
+	}
+
+	private String getRelativePath(String installFilePath) {
+		IPath path = getServerPath();
+		return path.append(installFilePath).toString();
+	}
+
+	private IPath getServerPath() {
+		if (serverPath != null)
+			return serverPath;
+		// see if there's an alternate server, otherwise use IServerData
+		Location installLocation = Platform.getInstallLocation();
+		URL url = installLocation.getURL();
+		IPath path = new Path(url.getPath());
+		path = path.append("configuration/server.properties"); //$NON-NLS-1$
+		try {
+			File file = path.toFile();
+			InputStream is = new FileInputStream(file);
+			Properties properties = new Properties();
+			properties.load(is);
+			is.close();
+			String key = serverData.getRemoteAgentInstallerProvider().getService().getIdentifier();
+			String pathStr = (String) properties.get(key);
+			if (pathStr != null)
+				return serverPath = new Path(pathStr);
+		} catch (IOException e) {
+			RemoteConnectionsActivator.logError(e);
+		}
+		return new Path(""); //$NON-NLS-1$
+	}
+}