1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.maps;
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;
21 import java.net.URISyntaxException;
23 import java.net.URLConnection;
24 import java.util.Properties;
26 import org.simantics.maps.pojo.AppletProxyUtil;
29 * @author Tuukka Lehtonen
31 public class WebService {
34 protected URL service;
36 public WebService(String address) throws URISyntaxException, MalformedURLException {
37 this.uri = new URI(address);
38 this.service = uri.toURL();
42 * Eclipse has different proxy configuration than POJO applications have,
43 * and Eclipse proxy code cannot be included in POJO applications.
47 private IProxyUtil getProxyUtil() {
48 IProxyUtil util = null;
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) {
55 // AppletProxyUtil should exist always..
57 util = new AppletProxyUtil();
66 public HttpURLConnection openConnection(String file, String query) throws MalformedURLException, IOException {
67 HttpURLConnection connection = null;
69 String suffix = query != null ? "?"+query : "";
70 URL url = new URL(service.getProtocol(), service.getHost(), service.getPort(), service.getFile() + file + suffix);
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());
79 URLConnection con = url.openConnection();
80 if(con instanceof HttpURLConnection) {
81 connection = (HttpURLConnection)con;
82 connection.setConnectTimeout(10000);