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.net.HttpURLConnection;
16 import java.net.InetSocketAddress;
17 import java.net.MalformedURLException;
18 import java.net.Proxy;
20 import java.net.URISyntaxException;
22 import java.net.URLConnection;
23 import java.util.Properties;
25 import org.simantics.maps.pojo.AppletProxyUtil;
28 * @author Tuukka Lehtonen
30 public class WebService {
33 protected URL service;
35 protected HttpURLConnection connection;
37 public WebService(String address) throws URISyntaxException, MalformedURLException {
38 this.uri = new URI(address);
39 this.service = uri.toURL();
43 * Eclipse has different proxy configuration than POJO applications have,
44 * and Eclipse proxy code cannot be included in POJO applications.
48 private IProxyUtil getProxyUtil() {
49 IProxyUtil util = null;
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) {
57 // AppletProxyUtil should exist always..
59 util = new AppletProxyUtil();
68 public HttpURLConnection openConnection(String file, String query) throws MalformedURLException, IOException {
69 HttpURLConnection connection = null;
71 String suffix = query != null ? "?"+query : "";
72 URL url = new URL(service.getProtocol(), service.getHost(), service.getPort(), service.getFile() + file + suffix);
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());
81 URLConnection con = url.openConnection();
82 if(con instanceof HttpURLConnection) {
83 connection = (HttpURLConnection)con;
84 connection.setConnectTimeout(10000);