-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
7 changed files
with
164 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
sonar-plugin/bridge/src/main/java/org/sonar/plugins/javascript/bridge/Http.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* SonarQube JavaScript Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.plugins.javascript.bridge; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import javax.annotation.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public interface Http { | ||
|
||
static Http getJdkHttpClient() { | ||
return new JdkHttp(); | ||
} | ||
|
||
Response post(String json, URI uri, long timeoutSeconds) throws IOException; | ||
|
||
String get(URI uri) throws IOException; | ||
|
||
record Response(@Nullable String contentType, byte[] body) {} | ||
|
||
class JdkHttp implements Http { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(JdkHttp.class); | ||
private final HttpClient client; | ||
|
||
JdkHttp() { | ||
this.client = HttpClient.newBuilder().build(); | ||
} | ||
|
||
@Override | ||
public Response post(String json, URI uri, long timeoutSeconds) throws IOException { | ||
var request = HttpRequest | ||
.newBuilder() | ||
.uri(uri) | ||
.timeout(Duration.ofSeconds(timeoutSeconds)) | ||
.header("Content-Type", "application/json") | ||
.POST(HttpRequest.BodyPublishers.ofString(json)) | ||
.build(); | ||
|
||
try { | ||
var response = client.send(request, HttpResponse.BodyHandlers.ofByteArray()); | ||
var contentType = response.headers().firstValue("Content-Type") | ||
.orElse(null); | ||
return new Response(contentType, response.body()); | ||
} catch (InterruptedException e) { | ||
throw handleInterruptedException(e, "Request " + uri + " was interrupted."); | ||
} | ||
} | ||
|
||
@Override | ||
public String get(URI uri) throws IOException{ | ||
var request = HttpRequest.newBuilder(uri).GET().build(); | ||
try { | ||
var response = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
return response.body(); | ||
} catch (InterruptedException e) { | ||
throw handleInterruptedException(e, "isAlive was interrupted"); | ||
} | ||
} | ||
|
||
private static IllegalStateException handleInterruptedException( | ||
InterruptedException e, | ||
String msg | ||
) { | ||
LOG.error(msg, e); | ||
Thread.currentThread().interrupt(); | ||
return new IllegalStateException(msg, e); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.