Skip to content

Commit

Permalink
remove apache httpclient
Browse files Browse the repository at this point in the history
  • Loading branch information
saberduck committed Nov 12, 2024
1 parent 014ae68 commit fe28fe6
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 81 deletions.
11 changes: 1 addition & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,7 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<version>5.3</version>
</dependency>


<!-- Test dependencies -->
<dependency>
Expand Down
8 changes: 0 additions & 8 deletions sonar-plugin/bridge/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
</dependency>

<dependency>
<groupId>com.google.protobuf</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,32 +153,8 @@ public BridgeServerImpl(
this.heartbeatService = Executors.newSingleThreadScheduledExecutor();
this.embeddedNode = embeddedNode;
this.http = http;
silenceHttpClientLogs();
}

private static void silenceHttpClientLogs() {
setLoggerLevelToInfo("org.apache.hc");
}

/**
* This method sets the log level of the logger with the given name to INFO.
* It assumes that SLF4J is used as the logging facade and Logback as the logging implementation.
* Since we don't want to directly depend on logback, we use reflection to set the log level.
* @param loggerName
*/
private static void setLoggerLevelToInfo(String loggerName) {
try {
ClassLoader cl = BridgeServerImpl.class.getClassLoader();
Class<?> level = cl.loadClass("ch.qos.logback.classic.Level");
Logger logger = LoggerFactory.getLogger(loggerName);
Method setLevel = logger.getClass().getMethod("setLevel", level);
setLevel.invoke(logger, level.getField("INFO").get(null));
} catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
LOG.info("Failed to set logger level to INFO for " + loggerName, e);
}
}


void heartbeat() {
LOG.trace("Pinging the bridge server");
isAlive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.util.List;
import java.util.Optional;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.util.Timeout;
import org.sonar.api.SonarProduct;
import org.sonar.plugins.javascript.bridge.Http;
import org.sonar.plugins.javascript.api.estree.ESTree;
import org.sonar.plugins.javascript.bridge.AnalysisMode;
import org.sonar.plugins.javascript.bridge.AnalysisWarningsWrapper;
Expand All @@ -45,6 +35,7 @@
import org.sonar.plugins.javascript.bridge.ESTreeFactory;
import org.sonar.plugins.javascript.bridge.EmbeddedNode;
import org.sonar.plugins.javascript.bridge.Environment;
import org.sonar.plugins.javascript.bridge.Http;
import org.sonar.plugins.javascript.bridge.NodeDeprecationWarning;
import org.sonar.plugins.javascript.bridge.RulesBundles;
import org.sonar.plugins.javascript.bridge.protobuf.Node;
Expand All @@ -58,6 +49,10 @@ public class StandaloneParser implements AutoCloseable {
private final BridgeServerImpl bridge;

public StandaloneParser() {
this(Http.getJdkHttpClient());
}

public StandaloneParser(Http http) {
ProcessWrapperImpl processWrapper = new ProcessWrapperImpl();
EmptyConfiguration emptyConfiguration = new EmptyConfiguration();
bridge = new BridgeServerImpl(
Expand All @@ -68,7 +63,7 @@ public StandaloneParser() {
new NodeDeprecationWarning(new AnalysisWarningsWrapper()),
new StandaloneTemporaryFolder(),
new EmbeddedNode(processWrapper, new Environment(emptyConfiguration)),
new ApacheHttp());
http);
try {
bridge.startServerLazily(new BridgeServerConfig(emptyConfiguration, new File(".").getAbsolutePath(), SonarProduct.SONARLINT));
bridge.initLinter(List.of(), List.of(), List.of(), AnalysisMode.DEFAULT, null, List.of());
Expand Down Expand Up @@ -124,33 +119,5 @@ public String[] getStringArray(String key) {
}
}

public static class ApacheHttp implements Http {

@Override
public Response post(String json, URI uri, long timeoutSeconds) throws IOException {
try (var httpclient = HttpClients.createDefault()) {

var config = RequestConfig.custom()
.setResponseTimeout(Timeout.ofSeconds(timeoutSeconds))
.build();

HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
httpPost.setConfig(config);

return httpclient.execute(httpPost, response -> {
var contentTypeHeader = response.getHeader("Content-Type");
var contentType = contentTypeHeader != null ? contentTypeHeader.toString() : null;
return new Response(contentType, EntityUtils.toByteArray(response.getEntity()));
});
}
}

public String get(URI uri) throws IOException{
try (var client = HttpClients.custom().build()) {
var get = new HttpGet(uri);
return client.execute(get, response -> EntityUtils.toString(response.getEntity()));
}
}
}
}

0 comments on commit fe28fe6

Please sign in to comment.