2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.ci.tests.execute.setup;
23 import ch.qos.logback.classic.Level;
24 import ch.qos.logback.classic.Logger;
25 import ch.qos.logback.classic.LoggerContext;
26 import com.github.markusbernhardt.proxy.ProxySearch;
27 import com.github.markusbernhardt.proxy.ProxySearch.Strategy;
28 import com.github.markusbernhardt.proxy.util.PlatformUtil;
29 import com.github.markusbernhardt.proxy.util.PlatformUtil.Platform;
30 import net.lightbody.bmp.BrowserMobProxyServer;
31 import org.slf4j.LoggerFactory;
34 import java.util.HashMap;
35 import java.util.List;
37 public class MobProxy {
39 private static HashMap<Long, BrowserMobProxyServer> mobProxyServerMap = new HashMap<Long, BrowserMobProxyServer>();
40 public static InetSocketAddress localProxyAddress = getProxy();
42 public static InetSocketAddress getProxy(){
45 ProxySearch proxySearch = new ProxySearch();
46 // proxySearch.addStrategy(Strategy.JAVA);
47 // proxySearch.addStrategy(Strategy.BROWSER);
48 // proxySearch.addStrategy(Strategy.OS_DEFAULT);
49 // proxySearch.addStrategy(Strategy.ENV_VAR);
50 if (PlatformUtil.getCurrentPlattform() == Platform.WIN) {
51 proxySearch.addStrategy(Strategy.IE);
52 proxySearch.addStrategy(Strategy.FIREFOX);
53 proxySearch.addStrategy(Strategy.JAVA);
54 } else if (PlatformUtil.getCurrentPlattform() == Platform.LINUX) {
55 proxySearch.addStrategy(Strategy.GNOME);
56 proxySearch.addStrategy(Strategy.KDE);
57 proxySearch.addStrategy(Strategy.FIREFOX);
58 proxySearch.addStrategy(Strategy.ENV_VAR);
61 proxySearch.addStrategy(Strategy.OS_DEFAULT);
63 ProxySelector proxySelector = proxySearch.getProxySelector();
65 ProxySelector.setDefault(proxySelector);
66 URI home = URI.create("http://www.google.com");
67 System.out.println("ProxySelector: " + proxySelector);
68 System.out.println("URI: " + home);
69 List<Proxy> proxyList = proxySelector.select(home);
72 if (proxyList != null && !proxyList.isEmpty()) {
73 for (Proxy proxy : proxyList) {
74 System.out.println(proxy);
75 SocketAddress address = proxy.address();
76 if (address instanceof InetSocketAddress) {
77 host = ((InetSocketAddress) address).getHostName();
78 port = Integer.toString(((InetSocketAddress) address).getPort());
79 System.setProperty("http.proxyHost", host);
80 System.setProperty("http.proxyPort", port);
84 InetSocketAddress address = new InetSocketAddress(host, Integer.parseInt(port));
88 // set logger for all classes connected to MobProxy
89 public static void setLogger() {
90 LoggerContext lc = (LoggerContext) LoggerFactory. getILoggerFactory();
91 // lc.getLogger("ROOT").setLevel(Level.DEBUG);
92 for(Logger logger:lc.getLoggerList()){
93 logger.setLevel(Level.INFO);
97 public static synchronized void setProxyServer() {
98 BrowserMobProxyServer server = new BrowserMobProxyServer();
99 server.setTrustAllServers(true);
100 if (localProxyAddress != null){
101 server.setChainedProxy(localProxyAddress);
105 // filter firefox requests to mozilla when system proxy is absent
106 server.blacklistRequests(".*mozilla.*", 200);
108 addProxyServerToPull(Thread.currentThread().getId(), server);
111 public static synchronized BrowserMobProxyServer getPoxyServer() {
112 return mobProxyServerMap.get(Thread.currentThread().getId());
115 public static void addProxyServerToPull(Long threadId, BrowserMobProxyServer server){
116 mobProxyServerMap.put(threadId, server);
119 public static synchronized void removePoxyServer() {
120 if (getPoxyServer() != null){
121 getPoxyServer().stop();
122 mobProxyServerMap.remove(Thread.currentThread().getId());
126 public static void removeAllProxyServers(){
127 for(Long threadNumber :mobProxyServerMap.keySet()){
128 mobProxyServerMap.get(threadNumber).stop();