ProxyUtil.java

1
package com.jsql.util;
2
3
import com.jsql.model.InjectionModel;
4
import com.jsql.util.GitUtil.ShowOnConsole;
5
import org.apache.commons.lang3.StringUtils;
6
import org.apache.logging.log4j.LogManager;
7
import org.apache.logging.log4j.Logger;
8
9
import java.net.Socket;
10
import java.util.Optional;
11
import java.util.prefs.Preferences;
12
13
/**
14
 * Utility class managing proxy settings.
15
 * The proxy configuration is saved as preferences and applied to the JVM.
16
 */
17
public class ProxyUtil {
18
    
19
    /**
20
     * Log4j logger sent to view.
21
     */
22
    private static final Logger LOGGER = LogManager.getRootLogger();
23
    
24
    private final String httpProxyDefaultAddress;
25
    private final String httpProxyDefaultPort;
26
    private final String httpsProxyDefaultAddress;
27
    private final String httpsProxyDefaultPort;
28
    
29
    /**
30
     * Proxy IP address or name.
31
     */
32
    private String proxyAddressHttp;
33
    private String proxyAddressHttps;
34
35
    /**
36
     * Proxy port number.
37
     */
38
    private String proxyPortHttp;
39
    private String proxyPortHttps;
40
    
41
    /**
42
     * True if connection is proxified.
43
     */
44
    private boolean isUsingProxyHttp = false;
45
    private boolean isUsingProxyHttps = false;
46
    
47
    private static final String PROPERTIES_HTTP_PROXY_HOST = "http.proxyHost";
48
    private static final String PROPERTIES_HTTP_PROXY_PORT = "http.proxyPort";
49
    private static final String PROPERTIES_HTTPS_PROXY_HOST = "https.proxyHost";
50
    private static final String PROPERTIES_HTTPS_PROXY_PORT = "https.proxyPort";
51
    
52
    public ProxyUtil(InjectionModel injectionModel) {
53
        
54
        this.httpProxyDefaultAddress = injectionModel.getMediatorUtils().getPropertiesUtil().getProperties().getProperty("http.proxy.default.ip");
55
        this.httpProxyDefaultPort = injectionModel.getMediatorUtils().getPropertiesUtil().getProperties().getProperty("http.proxy.default.port");
56
        this.httpsProxyDefaultAddress = injectionModel.getMediatorUtils().getPropertiesUtil().getProperties().getProperty("https.proxy.default.ip");
57
        this.httpsProxyDefaultPort = injectionModel.getMediatorUtils().getPropertiesUtil().getProperties().getProperty("https.proxy.default.port");
58
    }
59
    
60
    /**
61
     * Save proxy configuration into the JVM preferences.
62
     * @param isUsingProxyHttp whether the connection is using a proxy
63
     * @param proxyAddressHttp IP address or name of the proxy
64
     * @param proxyPortHttp port number of proxy
65
     */
66
    public void setPreferences(
67
        boolean isUsingProxyHttp, String proxyAddressHttp, String proxyPortHttp,
68
        boolean isUsingProxyHttps, String proxyAddressHttps, String proxyPortHttps
69
    ) {
70
        
71
        // Set the application proxy settings
72 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setUsingProxyHttp → NO_COVERAGE
        this.setUsingProxyHttp(isUsingProxyHttp);
73 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setProxyAddressHttp → NO_COVERAGE
        this.setProxyAddressHttp(proxyAddressHttp);
74 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setProxyPortHttp → NO_COVERAGE
        this.setProxyPortHttp(proxyPortHttp);
75
        
76 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setUsingProxyHttps → NO_COVERAGE
        this.setUsingProxyHttps(isUsingProxyHttps);
77 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setProxyAddressHttps → NO_COVERAGE
        this.setProxyAddressHttps(proxyAddressHttps);
78 1 1. setPreferences : removed call to com/jsql/util/ProxyUtil::setProxyPortHttps → NO_COVERAGE
        this.setProxyPortHttps(proxyPortHttps);
79
80
        // Save the settings in the JVM
81
        Preferences prefs = Preferences.userRoot().node(InjectionModel.class.getName());
82 1 1. setPreferences : removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE
        prefs.putBoolean("isUsingProxy", this.isUsingProxyHttp());
83 1 1. setPreferences : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        prefs.put("proxyAddress", this.getProxyAddressHttp());
84 1 1. setPreferences : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        prefs.put("proxyPort", this.getProxyPortHttp());
85
        
86 1 1. setPreferences : removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE
        prefs.putBoolean("isUsingProxyHttps", this.isUsingProxyHttps());
87 1 1. setPreferences : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        prefs.put("proxyAddressHttps", this.getProxyAddressHttps());
88 1 1. setPreferences : removed call to java/util/prefs/Preferences::put → NO_COVERAGE
        prefs.put("proxyPortHttps", this.getProxyPortHttps());
89
90
        // Change the JVM configuration
91 1 1. setPreferences : negated conditional → NO_COVERAGE
        if (this.isUsingProxyHttp()) {
92
            
93
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, this.getProxyAddressHttp());
94
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, this.getProxyPortHttp());
95
            
96
        } else {
97
            
98
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, "");
99
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, "");
100
        }
101
        
102 1 1. setPreferences : negated conditional → NO_COVERAGE
        if (this.isUsingProxyHttps()) {
103
            
104
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, this.getProxyAddressHttps());
105
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, this.getProxyPortHttps());
106
            
107
        } else {
108
            
109
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, "");
110
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, "");
111
        }
112
    }
113
    
114
    /**
115
     * Initialize proxy information from JVM already saved preferences.
116
     */
117
    public void initializeProxy() {
118
        
119
        // Use Preferences API to persist proxy configuration
120
        var preferences = Preferences.userRoot().node(InjectionModel.class.getName());
121
122
        // Default proxy disabled
123 1 1. initializeProxy : removed call to com/jsql/util/ProxyUtil::setUsingProxyHttp → NO_COVERAGE
        this.setUsingProxyHttp(preferences.getBoolean("isUsingProxy", false));
124 1 1. initializeProxy : removed call to com/jsql/util/ProxyUtil::setUsingProxyHttps → NO_COVERAGE
        this.setUsingProxyHttps(preferences.getBoolean("isUsingProxyHttps", false));
125
126
        // Default proxy config
127 1 1. initializeProxy : removed call to com/jsql/util/ProxyUtil::setProxyAddressHttp → NO_COVERAGE
        this.setProxyAddressHttp(preferences.get("proxyAddress", this.httpProxyDefaultAddress));
128 1 1. initializeProxy : removed call to com/jsql/util/ProxyUtil::setProxyPortHttp → NO_COVERAGE
        this.setProxyPortHttp(preferences.get("proxyPort", this.httpProxyDefaultPort));
129
        
130 1 1. initializeProxy : removed call to com/jsql/util/ProxyUtil::setProxyAddressHttps → NO_COVERAGE
        this.setProxyAddressHttps(preferences.get("proxyAddressHttps", this.httpsProxyDefaultAddress));
131 1 1. initializeProxy : removed call to com/jsql/util/ProxyUtil::setProxyPortHttps → NO_COVERAGE
        this.setProxyPortHttps(preferences.get("proxyPortHttps", this.httpsProxyDefaultPort));
132
        
133
        // Change the JVM configuration
134 1 1. initializeProxy : negated conditional → NO_COVERAGE
        if (this.isUsingProxyHttp()) {
135
            
136
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_HOST, this.getProxyAddressHttp());
137
            System.setProperty(ProxyUtil.PROPERTIES_HTTP_PROXY_PORT, this.getProxyPortHttp());
138
        }
139
        
140 1 1. initializeProxy : negated conditional → NO_COVERAGE
        if (this.isUsingProxyHttps()) {
141
            
142
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_HOST, this.getProxyAddressHttps());
143
            System.setProperty(ProxyUtil.PROPERTIES_HTTPS_PROXY_PORT, this.getProxyPortHttps());
144
        }
145
    }
146
    
147
    /**
148
     * Check if enabled proxies are up when application starts,
149
     * injection begins,- checking IP, sending reports.
150
     * Display logs except when sending unhandled exception.
151
     * @param showOnConsole whether the message should be presented to the user
152
     * @return true if enabled proxies are up
153
     */
154
    public boolean isNotLive(ShowOnConsole showOnConsole) {
155
        
156
        var isLive = true;
157
        
158
        if (
159 1 1. isNotLive : negated conditional → NO_COVERAGE
            this.isUsingProxyHttp()
160 1 1. isNotLive : negated conditional → NO_COVERAGE
            && StringUtils.isNotEmpty(this.getProxyAddressHttp())
161 1 1. isNotLive : negated conditional → NO_COVERAGE
            && StringUtils.isNotEmpty(this.getProxyPortHttp())
162
        ) {
163
            isLive = this.isSocketOn(showOnConsole, this.getProxyAddressHttp(), this.getProxyPortHttp(), "HTTP");
164
        }
165
166
        if (
167 1 1. isNotLive : negated conditional → NO_COVERAGE
            this.isUsingProxyHttps()
168 1 1. isNotLive : negated conditional → NO_COVERAGE
            && StringUtils.isNotEmpty(this.getProxyAddressHttps())
169 1 1. isNotLive : negated conditional → NO_COVERAGE
            && StringUtils.isNotEmpty(this.getProxyPortHttps())
170
        ) {
171
            isLive = this.isSocketOn(showOnConsole, this.getProxyAddressHttps(), this.getProxyPortHttps(), "HTTPS");
172
        }
173
        
174 2 1. isNotLive : negated conditional → NO_COVERAGE
2. isNotLive : replaced boolean return with true for com/jsql/util/ProxyUtil::isNotLive → NO_COVERAGE
        return !isLive;
175
    }
176
    
177
    private boolean isSocketOn(ShowOnConsole showOnConsole, String address, String port, String protocol) {
178
        
179
        var isSocketOn = true;
180
        
181
        try {
182
            var socket = new Socket(address, Integer.parseInt(port));
183 1 1. isSocketOn : removed call to java/net/Socket::close → NO_COVERAGE
            socket.close();
184
            
185 1 1. isSocketOn : removed call to com/jsql/util/ProxyUtil::logStatus → NO_COVERAGE
            this.logStatus(showOnConsole, address, port, protocol);
186
            
187
        } catch (Exception e) {
188
            
189
            isSocketOn = false;
190 1 1. isSocketOn : removed call to com/jsql/util/ProxyUtil::logStatus → NO_COVERAGE
            this.logStatus(showOnConsole, address, port, protocol, e);
191
        }
192
        
193 2 1. isSocketOn : replaced boolean return with false for com/jsql/util/ProxyUtil::isSocketOn → NO_COVERAGE
2. isSocketOn : replaced boolean return with true for com/jsql/util/ProxyUtil::isSocketOn → NO_COVERAGE
        return isSocketOn;
194
    }
195
    
196
    private void logStatus(ShowOnConsole showOnConsole, String address, String port, String protocol) {
197 1 1. logStatus : negated conditional → NO_COVERAGE
        if (showOnConsole == ShowOnConsole.YES) {
198
            LOGGER.log(
199
                LogLevelUtil.CONSOLE_SUCCESS,
200
                "Connection successful to {} proxy {}:{}",
201 1 1. lambda$logStatus$0 : replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$0 → NO_COVERAGE
                () -> protocol,
202 1 1. lambda$logStatus$1 : replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$1 → NO_COVERAGE
                () -> address,
203 1 1. lambda$logStatus$2 : replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$2 → NO_COVERAGE
                () ->port
204
            );
205
        }
206
    }
207
    
208
    private void logStatus(ShowOnConsole showOnConsole, String address, String port, String protocol, Exception e) {
209 1 1. logStatus : negated conditional → NO_COVERAGE
        if (showOnConsole == ShowOnConsole.YES) {
210
            
211
            String message = Optional.ofNullable(e.getMessage()).orElse(StringUtils.EMPTY);
212
            
213
            LOGGER.log(
214
                LogLevelUtil.CONSOLE_ERROR,
215 1 1. lambda$logStatus$3 : replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$3 → NO_COVERAGE
                () -> String.format(
216
                    "Connection to %s proxy %s:%s failed with error \"%s\", verify your proxy settings",
217
                    protocol,
218
                    address,
219
                    port,
220
                    message.replace(e.getClass().getName() +": ", StringUtils.EMPTY)
221
                )
222
            );
223
        }
224
    }
225
    
226
    
227
    // Getters and setters
228
    
229
    public String getProxyAddressHttp() {
230 1 1. getProxyAddressHttp : replaced return value with "" for com/jsql/util/ProxyUtil::getProxyAddressHttp → NO_COVERAGE
        return this.proxyAddressHttp;
231
    }
232
233
    public void setProxyAddressHttp(String proxyAddressHttp) {
234
        this.proxyAddressHttp = proxyAddressHttp;
235
    }
236
237
    public String getProxyPortHttp() {
238 1 1. getProxyPortHttp : replaced return value with "" for com/jsql/util/ProxyUtil::getProxyPortHttp → NO_COVERAGE
        return this.proxyPortHttp;
239
    }
240
241
    public void setProxyPortHttp(String proxyPortHttp) {
242
        this.proxyPortHttp = proxyPortHttp;
243
    }
244
245
    public boolean isUsingProxyHttp() {
246 2 1. isUsingProxyHttp : replaced boolean return with false for com/jsql/util/ProxyUtil::isUsingProxyHttp → NO_COVERAGE
2. isUsingProxyHttp : replaced boolean return with true for com/jsql/util/ProxyUtil::isUsingProxyHttp → NO_COVERAGE
        return this.isUsingProxyHttp;
247
    }
248
249
    public void setUsingProxyHttp(boolean isUsingProxyHttp) {
250
        this.isUsingProxyHttp = isUsingProxyHttp;
251
    }
252
253
    public String getProxyAddressHttps() {
254 1 1. getProxyAddressHttps : replaced return value with "" for com/jsql/util/ProxyUtil::getProxyAddressHttps → NO_COVERAGE
        return this.proxyAddressHttps;
255
    }
256
257
    public void setProxyAddressHttps(String proxyAddressHttps) {
258
        this.proxyAddressHttps = proxyAddressHttps;
259
    }
260
261
    public String getProxyPortHttps() {
262 1 1. getProxyPortHttps : replaced return value with "" for com/jsql/util/ProxyUtil::getProxyPortHttps → NO_COVERAGE
        return this.proxyPortHttps;
263
    }
264
265
    public void setProxyPortHttps(String proxyPortHttps) {
266
        this.proxyPortHttps = proxyPortHttps;
267
    }
268
269
    public boolean isUsingProxyHttps() {
270 2 1. isUsingProxyHttps : replaced boolean return with true for com/jsql/util/ProxyUtil::isUsingProxyHttps → NO_COVERAGE
2. isUsingProxyHttps : replaced boolean return with false for com/jsql/util/ProxyUtil::isUsingProxyHttps → NO_COVERAGE
        return this.isUsingProxyHttps;
271
    }
272
273
    public void setUsingProxyHttps(boolean isUsingProxyHttps) {
274
        this.isUsingProxyHttps = isUsingProxyHttps;
275
    }
276
}

Mutations

72

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setUsingProxyHttp → NO_COVERAGE

73

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyAddressHttp → NO_COVERAGE

74

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyPortHttp → NO_COVERAGE

76

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setUsingProxyHttps → NO_COVERAGE

77

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyAddressHttps → NO_COVERAGE

78

1.1
Location : setPreferences
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyPortHttps → NO_COVERAGE

82

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE

83

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

84

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

86

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::putBoolean → NO_COVERAGE

87

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

88

1.1
Location : setPreferences
Killed by : none
removed call to java/util/prefs/Preferences::put → NO_COVERAGE

91

1.1
Location : setPreferences
Killed by : none
negated conditional → NO_COVERAGE

102

1.1
Location : setPreferences
Killed by : none
negated conditional → NO_COVERAGE

123

1.1
Location : initializeProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setUsingProxyHttp → NO_COVERAGE

124

1.1
Location : initializeProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setUsingProxyHttps → NO_COVERAGE

127

1.1
Location : initializeProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyAddressHttp → NO_COVERAGE

128

1.1
Location : initializeProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyPortHttp → NO_COVERAGE

130

1.1
Location : initializeProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyAddressHttps → NO_COVERAGE

131

1.1
Location : initializeProxy
Killed by : none
removed call to com/jsql/util/ProxyUtil::setProxyPortHttps → NO_COVERAGE

134

1.1
Location : initializeProxy
Killed by : none
negated conditional → NO_COVERAGE

140

1.1
Location : initializeProxy
Killed by : none
negated conditional → NO_COVERAGE

159

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

160

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

161

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

167

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

168

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

169

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

174

1.1
Location : isNotLive
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : isNotLive
Killed by : none
replaced boolean return with true for com/jsql/util/ProxyUtil::isNotLive → NO_COVERAGE

183

1.1
Location : isSocketOn
Killed by : none
removed call to java/net/Socket::close → NO_COVERAGE

185

1.1
Location : isSocketOn
Killed by : none
removed call to com/jsql/util/ProxyUtil::logStatus → NO_COVERAGE

190

1.1
Location : isSocketOn
Killed by : none
removed call to com/jsql/util/ProxyUtil::logStatus → NO_COVERAGE

193

1.1
Location : isSocketOn
Killed by : none
replaced boolean return with false for com/jsql/util/ProxyUtil::isSocketOn → NO_COVERAGE

2.2
Location : isSocketOn
Killed by : none
replaced boolean return with true for com/jsql/util/ProxyUtil::isSocketOn → NO_COVERAGE

197

1.1
Location : logStatus
Killed by : none
negated conditional → NO_COVERAGE

201

1.1
Location : lambda$logStatus$0
Killed by : none
replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$0 → NO_COVERAGE

202

1.1
Location : lambda$logStatus$1
Killed by : none
replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$1 → NO_COVERAGE

203

1.1
Location : lambda$logStatus$2
Killed by : none
replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$2 → NO_COVERAGE

209

1.1
Location : logStatus
Killed by : none
negated conditional → NO_COVERAGE

215

1.1
Location : lambda$logStatus$3
Killed by : none
replaced return value with null for com/jsql/util/ProxyUtil::lambda$logStatus$3 → NO_COVERAGE

230

1.1
Location : getProxyAddressHttp
Killed by : none
replaced return value with "" for com/jsql/util/ProxyUtil::getProxyAddressHttp → NO_COVERAGE

238

1.1
Location : getProxyPortHttp
Killed by : none
replaced return value with "" for com/jsql/util/ProxyUtil::getProxyPortHttp → NO_COVERAGE

246

1.1
Location : isUsingProxyHttp
Killed by : none
replaced boolean return with false for com/jsql/util/ProxyUtil::isUsingProxyHttp → NO_COVERAGE

2.2
Location : isUsingProxyHttp
Killed by : none
replaced boolean return with true for com/jsql/util/ProxyUtil::isUsingProxyHttp → NO_COVERAGE

254

1.1
Location : getProxyAddressHttps
Killed by : none
replaced return value with "" for com/jsql/util/ProxyUtil::getProxyAddressHttps → NO_COVERAGE

262

1.1
Location : getProxyPortHttps
Killed by : none
replaced return value with "" for com/jsql/util/ProxyUtil::getProxyPortHttps → NO_COVERAGE

270

1.1
Location : isUsingProxyHttps
Killed by : none
replaced boolean return with true for com/jsql/util/ProxyUtil::isUsingProxyHttps → NO_COVERAGE

2.2
Location : isUsingProxyHttps
Killed by : none
replaced boolean return with false for com/jsql/util/ProxyUtil::isUsingProxyHttps → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.16.1