/******************************************************************************* * 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.lang.reflect.InvocationTargetException; 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; 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.getDeclaredConstructor().newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException 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; } }