]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/WebService.java
Fixed most warnings from district codebase after JavaSE-11 switch
[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.lang.reflect.InvocationTargetException;
16 import java.net.HttpURLConnection;
17 import java.net.InetSocketAddress;
18 import java.net.MalformedURLException;
19 import java.net.Proxy;
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 import java.net.URL;
23 import java.net.URLConnection;
24 import java.util.Properties;
25
26 import org.simantics.maps.pojo.AppletProxyUtil;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class WebService {
32
33     protected URI uri;
34     protected URL service;
35
36     public WebService(String address) throws URISyntaxException, MalformedURLException {
37         this.uri = new URI(address);
38         this.service = uri.toURL();
39     }
40     
41     /**
42      * Eclipse has different proxy configuration than POJO applications have,
43      * and Eclipse proxy code cannot be included in POJO applications.
44      * 
45      * @return
46      */
47     private IProxyUtil getProxyUtil() {
48         IProxyUtil util = null;
49         try {
50             Class<?> proxyClass = (Class<?>) Class.forName("org.simantics.maps.eclipse.EclipseProxyUtil");
51             util = (IProxyUtil)proxyClass.getDeclaredConstructor().newInstance();
52         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException
53                 | InvocationTargetException | NoSuchMethodException | SecurityException e) {
54         }
55         // AppletProxyUtil should exist always..
56         if(util == null) {
57             util = new AppletProxyUtil();
58         }
59         return util;
60     }
61
62     public URI getURI() {
63         return uri;
64     }
65
66     public HttpURLConnection openConnection(String file, String query) throws MalformedURLException, IOException {
67         HttpURLConnection connection = null;
68
69         String suffix = query != null ? "?"+query : "";
70         URL url = new URL(service.getProtocol(), service.getHost(), service.getPort(), service.getFile() +  file + suffix);
71
72         Properties systemProperties = System.getProperties();
73         Proxy proxy = getProxyUtil().getProxyService(url.toString());
74         if(proxy != null && ((InetSocketAddress)proxy.address()) != null) {
75             systemProperties.setProperty("http.proxyHost", ((InetSocketAddress)proxy.address()).getHostName());
76             systemProperties.setProperty("http.proxyPort", ""+((InetSocketAddress)proxy.address()).getPort());
77         }
78
79         URLConnection con = url.openConnection();
80         if(con instanceof HttpURLConnection) {
81             connection = (HttpURLConnection)con;
82             connection.setConnectTimeout(10000);
83             connection.connect();
84         }
85         return connection;
86     }
87
88 }