re base code
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / setup / MobProxy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.execute.setup;
22
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;
32
33 import java.net.*;
34 import java.util.HashMap;
35 import java.util.List;
36
37 public class MobProxy {
38         
39         private static HashMap<Long, BrowserMobProxyServer> mobProxyServerMap = new HashMap<Long, BrowserMobProxyServer>();
40         public static InetSocketAddress localProxyAddress = getProxy(); 
41         
42         public static InetSocketAddress getProxy(){             
43                 setLogger();
44             
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);
59                     return null;
60                 } else {
61                     proxySearch.addStrategy(Strategy.OS_DEFAULT);
62                 }
63                 ProxySelector proxySelector = proxySearch.getProxySelector(); 
64
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); 
70                 String host = null;
71                 String port = null;
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); 
81                    } 
82                  } 
83                 }
84                 InetSocketAddress address = new InetSocketAddress(host, Integer.parseInt(port));
85                 return address;
86         }
87     
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);
94                 }
95         }
96         
97         public static synchronized void setProxyServer() {
98                 BrowserMobProxyServer server = new BrowserMobProxyServer();
99                 server.setTrustAllServers(true);
100                 if (localProxyAddress != null){
101                         server.setChainedProxy(localProxyAddress);
102                         server.start();
103                 } else {
104                         server.start();
105                         // filter firefox requests to mozilla when system proxy is absent
106                         server.blacklistRequests(".*mozilla.*", 200);
107                 }
108                 addProxyServerToPull(Thread.currentThread().getId(), server);
109         }
110         
111         public static synchronized BrowserMobProxyServer getPoxyServer() {
112         return mobProxyServerMap.get(Thread.currentThread().getId());
113     }
114         
115         public static void addProxyServerToPull(Long threadId, BrowserMobProxyServer server){
116                 mobProxyServerMap.put(threadId, server);
117         }
118         
119         public static synchronized void removePoxyServer() {
120                 if (getPoxyServer() != null){
121                         getPoxyServer().stop();
122                         mobProxyServerMap.remove(Thread.currentThread().getId());
123                 }
124     }
125         
126         public static void removeAllProxyServers(){
127                 for(Long threadNumber :mobProxyServerMap.keySet()){
128                         mobProxyServerMap.get(threadNumber).stop();
129                 }
130         }
131
132 }