]> gerrit.simantics Code Review - simantics/district.git/blob - 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
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.maps;
13
14 import java.io.IOException;
15 import java.net.HttpURLConnection;
16 import java.net.InetSocketAddress;
17 import java.net.MalformedURLException;
18 import java.net.Proxy;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.net.URL;
22 import java.net.URLConnection;
23 import java.util.Properties;
24
25 import org.simantics.maps.pojo.AppletProxyUtil;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class WebService {
31
32     protected URI uri;
33     protected URL service;
34
35     protected HttpURLConnection connection;
36
37     public WebService(String address) throws URISyntaxException, MalformedURLException {
38         this.uri = new URI(address);
39         this.service = uri.toURL();
40     }
41     
42     /**
43      * Eclipse has different proxy configuration than POJO applications have,
44      * and Eclipse proxy code cannot be included in POJO applications.
45      * 
46      * @return
47      */
48     private IProxyUtil getProxyUtil() {
49         IProxyUtil util = null;
50         try {
51             Class<?> proxyClass = (Class<?>) Class.forName("org.simantics.maps.eclipse.EclipseProxyUtil");
52             util = (IProxyUtil)proxyClass.newInstance();
53         } catch (ClassNotFoundException e1) {
54         } catch (InstantiationException e) {
55         } catch (IllegalAccessException e) {
56         }
57         // AppletProxyUtil should exist always..
58         if(util == null) {
59             util = new AppletProxyUtil();
60         }
61         return util;
62     }
63
64     public URI getURI() {
65         return uri;
66     }
67
68     public HttpURLConnection openConnection(String file, String query) throws MalformedURLException, IOException {
69         HttpURLConnection connection = null;
70
71         String suffix = query != null ? "?"+query : "";
72         URL url = new URL(service.getProtocol(), service.getHost(), service.getPort(), service.getFile() +  file + suffix);
73
74         Properties systemProperties = System.getProperties();
75         Proxy proxy = getProxyUtil().getProxyService(url.toString());
76         if(proxy != null && ((InetSocketAddress)proxy.address()) != null) {
77             systemProperties.setProperty("http.proxyHost", ((InetSocketAddress)proxy.address()).getHostName());
78             systemProperties.setProperty("http.proxyPort", ""+((InetSocketAddress)proxy.address()).getPort());
79         }
80
81         URLConnection con = url.openConnection();
82         if(con instanceof HttpURLConnection) {
83             connection = (HttpURLConnection)con;
84             connection.setConnectTimeout(10000);
85             connection.connect();
86         }
87         return connection;
88     }
89
90 }