]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.maps/src/org/simantics/maps/WebService.java
Share some projects for Simantics District
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / WebService.java
diff --git a/org.simantics.district.maps/src/org/simantics/maps/WebService.java b/org.simantics.district.maps/src/org/simantics/maps/WebService.java
new file mode 100644 (file)
index 0000000..f515d47
--- /dev/null
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.maps;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.InetSocketAddress;
+import java.net.MalformedURLException;
+import java.net.Proxy;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Properties;
+
+import org.simantics.maps.pojo.AppletProxyUtil;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class WebService {
+
+    protected URI uri;
+    protected URL service;
+
+    protected HttpURLConnection connection;
+
+    public WebService(String address) throws URISyntaxException, MalformedURLException {
+        this.uri = new URI(address);
+        this.service = uri.toURL();
+    }
+    
+    /**
+     * Eclipse has different proxy configuration than POJO applications have,
+     * and Eclipse proxy code cannot be included in POJO applications.
+     * 
+     * @return
+     */
+    private IProxyUtil getProxyUtil() {
+        IProxyUtil util = null;
+        try {
+            Class<?> proxyClass = (Class<?>) Class.forName("org.simantics.maps.eclipse.EclipseProxyUtil");
+            util = (IProxyUtil)proxyClass.newInstance();
+        } catch (ClassNotFoundException e1) {
+        } catch (InstantiationException e) {
+        } catch (IllegalAccessException e) {
+        }
+        // AppletProxyUtil should exist always..
+        if(util == null) {
+            util = new AppletProxyUtil();
+        }
+        return util;
+    }
+
+    public URI getURI() {
+        return uri;
+    }
+
+    public HttpURLConnection openConnection(String file, String query) throws MalformedURLException, IOException {
+        HttpURLConnection connection = null;
+
+        String suffix = query != null ? "?"+query : "";
+        URL url = new URL(service.getProtocol(), service.getHost(), service.getPort(), service.getFile() +  file + suffix);
+
+        Properties systemProperties = System.getProperties();
+        Proxy proxy = getProxyUtil().getProxyService(url.toString());
+        if(proxy != null && ((InetSocketAddress)proxy.address()) != null) {
+            systemProperties.setProperty("http.proxyHost", ((InetSocketAddress)proxy.address()).getHostName());
+            systemProperties.setProperty("http.proxyPort", ""+((InetSocketAddress)proxy.address()).getPort());
+        }
+
+        URLConnection con = url.openConnection();
+        if(con instanceof HttpURLConnection) {
+            connection = (HttpURLConnection)con;
+            connection.setConnectTimeout(10000);
+            connection.connect();
+        }
+        return connection;
+    }
+
+}