19
19
*/
20
20
package org .sonarsource .scanner .lib ;
21
21
22
+ import java .net .InetSocketAddress ;
22
23
import java .nio .file .Path ;
23
24
import java .nio .file .Paths ;
25
+ import java .time .temporal .ChronoUnit ;
24
26
import java .util .HashMap ;
25
27
import java .util .Locale ;
26
28
import java .util .Map ;
27
29
import java .util .Objects ;
30
+ import javax .annotation .Nonnull ;
28
31
import javax .annotation .Nullable ;
29
32
import org .apache .commons .io .FileUtils ;
33
+ import org .apache .commons .lang3 .StringUtils ;
30
34
import org .slf4j .Logger ;
31
35
import org .slf4j .LoggerFactory ;
32
36
import org .sonarsource .scanner .lib .internal .ArchResolver ;
36
40
import org .sonarsource .scanner .lib .internal .Paths2 ;
37
41
import org .sonarsource .scanner .lib .internal .ScannerEngineLauncherFactory ;
38
42
import org .sonarsource .scanner .lib .internal .cache .FileCache ;
39
- import org .sonarsource .scanner .lib .internal .http .ServerConnection ;
43
+ import org .sonarsource .scanner .lib .internal .http .HttpConfig ;
44
+ import org .sonarsource .scanner .lib .internal .http .ScannerHttpClient ;
40
45
import org .sonarsource .scanner .lib .internal .util .VersionUtils ;
41
46
42
47
import static org .sonarsource .scanner .lib .ScannerProperties .SCANNER_ARCH ;
43
48
import static org .sonarsource .scanner .lib .ScannerProperties .SCANNER_OS ;
44
- import static org .sonarsource .scanner .lib .internal .http .ServerConnection .removeTrailingSlash ;
45
49
46
50
/**
47
51
* Entry point to run a Sonar analysis programmatically.
@@ -57,14 +61,14 @@ public class ScannerEngineBootstrapper {
57
61
private final IsolatedLauncherFactory launcherFactory ;
58
62
private final ScannerEngineLauncherFactory scannerEngineLauncherFactory ;
59
63
private final Map <String , String > bootstrapProperties = new HashMap <>();
60
- private final ServerConnection serverConnection ;
64
+ private final ScannerHttpClient scannerHttpClient ;
61
65
private final System2 system ;
62
66
63
67
ScannerEngineBootstrapper (String app , String version , System2 system ,
64
- ServerConnection serverConnection , IsolatedLauncherFactory launcherFactory ,
68
+ ScannerHttpClient scannerHttpClient , IsolatedLauncherFactory launcherFactory ,
65
69
ScannerEngineLauncherFactory scannerEngineLauncherFactory ) {
66
70
this .system = system ;
67
- this .serverConnection = serverConnection ;
71
+ this .scannerHttpClient = scannerHttpClient ;
68
72
this .launcherFactory = launcherFactory ;
69
73
this .scannerEngineLauncherFactory = scannerEngineLauncherFactory ;
70
74
this .setBootstrapProperty (InternalProperties .SCANNER_APP , app )
@@ -73,7 +77,7 @@ public class ScannerEngineBootstrapper {
73
77
74
78
public static ScannerEngineBootstrapper create (String app , String version ) {
75
79
System2 system = new System2 ();
76
- return new ScannerEngineBootstrapper (app , version , system , new ServerConnection (),
80
+ return new ScannerEngineBootstrapper (app , version , system , new ScannerHttpClient (),
77
81
new IsolatedLauncherFactory (), new ScannerEngineLauncherFactory (system ));
78
82
}
79
83
@@ -106,20 +110,62 @@ public ScannerEngineFacade bootstrap() {
106
110
var isSimulation = properties .containsKey (InternalProperties .SCANNER_DUMP_TO_FILE );
107
111
var sonarUserHome = resolveSonarUserHome (properties );
108
112
var fileCache = FileCache .create (sonarUserHome );
109
- serverConnection .init (properties , sonarUserHome );
113
+ var httpConfig = new HttpConfig (bootstrapProperties , sonarUserHome );
114
+ scannerHttpClient .init (httpConfig );
110
115
String serverVersion = null ;
111
116
if (!isSonarCloud ) {
112
- serverVersion = getServerVersion (serverConnection , isSimulation , properties );
117
+ serverVersion = getServerVersion (scannerHttpClient , isSimulation , properties );
113
118
}
114
119
115
120
if (isSimulation ) {
116
121
return new SimulationScannerEngineFacade (properties , isSonarCloud , serverVersion );
117
122
} else if (isSonarCloud || VersionUtils .isAtLeastIgnoringQualifier (serverVersion , SQ_VERSION_NEW_BOOTSTRAPPING )) {
118
- var launcher = scannerEngineLauncherFactory .createLauncher (serverConnection , fileCache , properties );
123
+ var launcher = scannerEngineLauncherFactory .createLauncher (scannerHttpClient , fileCache , properties );
119
124
return new NewScannerEngineFacade (properties , launcher , isSonarCloud , serverVersion );
120
125
} else {
121
- var launcher = launcherFactory .createLauncher (serverConnection , fileCache );
122
- return new InProcessScannerEngineFacade (properties , launcher , false , serverVersion );
126
+ var launcher = launcherFactory .createLauncher (scannerHttpClient , fileCache );
127
+ var adaptedProperties = adaptDeprecatedProperties (properties , httpConfig );
128
+ return new InProcessScannerEngineFacade (adaptedProperties , launcher , false , serverVersion );
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Older SonarQube versions used to rely on some different properties, or even {@link System} properties.
134
+ * For backward compatibility, we adapt the new properties to the old format.
135
+ */
136
+ @ Nonnull
137
+ Map <String , String > adaptDeprecatedProperties (Map <String , String > properties , HttpConfig httpConfig ) {
138
+ var adaptedProperties = new HashMap <>(properties );
139
+ if (!adaptedProperties .containsKey (HttpConfig .READ_TIMEOUT_SEC_PROPERTY )) {
140
+ adaptedProperties .put (HttpConfig .READ_TIMEOUT_SEC_PROPERTY , "" + httpConfig .getSocketTimeout ().get (ChronoUnit .SECONDS ));
141
+ }
142
+ var proxy = httpConfig .getProxy ();
143
+ if (proxy != null ) {
144
+ setSystemPropertyIfNotAlreadySet ("http.proxyHost" , ((InetSocketAddress ) proxy .address ()).getHostString ());
145
+ setSystemPropertyIfNotAlreadySet ("https.proxyHost" , ((InetSocketAddress ) proxy .address ()).getHostString ());
146
+ setSystemPropertyIfNotAlreadySet ("http.proxyPort" , "" + ((InetSocketAddress ) proxy .address ()).getPort ());
147
+ setSystemPropertyIfNotAlreadySet ("https.proxyPort" , "" + ((InetSocketAddress ) proxy .address ()).getPort ());
148
+ }
149
+ setSystemPropertyIfNotAlreadySet ("http.proxyUser" , httpConfig .getProxyUser ());
150
+ setSystemPropertyIfNotAlreadySet ("http.proxyPassword" , httpConfig .getProxyPassword ());
151
+
152
+ var keyStore = httpConfig .getSslConfig ().getKeyStore ();
153
+ if (keyStore != null ) {
154
+ setSystemPropertyIfNotAlreadySet ("javax.net.ssl.keyStore" , keyStore .getPath ().toString ());
155
+ setSystemPropertyIfNotAlreadySet ("javax.net.ssl.keyStorePassword" , keyStore .getKeyStorePassword ());
156
+ }
157
+ var trustStore = httpConfig .getSslConfig ().getTrustStore ();
158
+ if (trustStore != null ) {
159
+ setSystemPropertyIfNotAlreadySet ("javax.net.ssl.trustStore" , trustStore .getPath ().toString ());
160
+ setSystemPropertyIfNotAlreadySet ("javax.net.ssl.trustStorePassword" , trustStore .getKeyStorePassword ());
161
+ }
162
+
163
+ return Map .copyOf (adaptedProperties );
164
+ }
165
+
166
+ private void setSystemPropertyIfNotAlreadySet (String key , String value ) {
167
+ if (system .getProperty (key ) == null && StringUtils .isNotBlank (value )) {
168
+ System .setProperty (key , value );
123
169
}
124
170
}
125
171
@@ -134,16 +180,16 @@ private static Path resolveSonarUserHome(Map<String, String> properties) {
134
180
return Paths .get (sonarUserHome );
135
181
}
136
182
137
- private static String getServerVersion (ServerConnection serverConnection , boolean isSimulation , Map <String , String > properties ) {
183
+ private static String getServerVersion (ScannerHttpClient scannerHttpClient , boolean isSimulation , Map <String , String > properties ) {
138
184
if (isSimulation ) {
139
185
return properties .getOrDefault (InternalProperties .SCANNER_VERSION_SIMULATION , "5.6" );
140
186
}
141
187
142
188
try {
143
- return serverConnection .callRestApi ("/analysis/version" );
189
+ return scannerHttpClient .callRestApi ("/analysis/version" );
144
190
} catch (Exception e ) {
145
191
try {
146
- return serverConnection .callWebApi ("/api/server/version" );
192
+ return scannerHttpClient .callWebApi ("/api/server/version" );
147
193
} catch (Exception e2 ) {
148
194
var ex = new IllegalStateException ("Failed to get server version" , e2 );
149
195
ex .addSuppressed (e );
@@ -154,8 +200,8 @@ private static String getServerVersion(ServerConnection serverConnection, boolea
154
200
155
201
private void initBootstrapDefaultValues () {
156
202
setBootstrapPropertyIfNotAlreadySet (ScannerProperties .HOST_URL , getSonarCloudUrl ());
157
- setBootstrapPropertyIfNotAlreadySet (ScannerProperties .API_BASE_URL , isSonarCloud ( bootstrapProperties ) ?
158
- SONARCLOUD_REST_API : (removeTrailingSlash (bootstrapProperties .get (ScannerProperties .HOST_URL )) + "/api/v2" ));
203
+ setBootstrapPropertyIfNotAlreadySet (ScannerProperties .API_BASE_URL ,
204
+ isSonarCloud ( bootstrapProperties ) ? SONARCLOUD_REST_API : (StringUtils . removeEnd (bootstrapProperties .get (ScannerProperties .HOST_URL ), "/" ) + "/api/v2" ));
159
205
if (!bootstrapProperties .containsKey (SCANNER_OS )) {
160
206
setBootstrapProperty (SCANNER_OS , new OsResolver (system , new Paths2 ()).getOs ().name ().toLowerCase (Locale .ENGLISH ));
161
207
}
0 commit comments