Skip to content

Commit

Permalink
Remove kotlin and okhttp as dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Jun 10, 2022
1 parent 0b8a502 commit 5ff5411
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 45 deletions.
15 changes: 3 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,12 @@ dependencies {
implementation("org.openrewrite:rewrite-yaml:${rewriteVersion}")
implementation("org.openrewrite:rewrite-maven:${rewriteVersion}")

// for locating list of released Spring Boot versions
implementation("com.squareup.okhttp3:okhttp:4.9.+")

implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.6.21"))

// eliminates "unknown enum constant DeprecationLevel.WARNING" warnings from the build log
// see https://github.com/gradle/kotlin-dsl-samples/issues/1301 for why (okhttp is leaking parts of kotlin stdlib)
compileOnly("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

runtimeOnly("org.openrewrite.recipe:rewrite-testing-frameworks:${rewriteVersion}")
runtimeOnly("org.openrewrite:rewrite-java-11:$rewriteVersion")

testImplementation("org.jetbrains.kotlin:kotlin-reflect")
testImplementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

testImplementation(platform(kotlin("bom", "1.6.21")))
testImplementation(kotlin("reflect"))
testImplementation(kotlin("stdlib"))
testImplementation("org.junit.jupiter:junit-jupiter-api:latest.release")
testImplementation("org.junit.jupiter:junit-jupiter-params:latest.release")
testImplementation("org.junit.jupiter:junit-jupiter-engine:latest.release")
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.gradle.caching=true
org.gradle.parallel=true
kotlin.stdlib.default.dependency=false
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package org.openrewrite.java.spring.internal;

import okhttp3.*;

import org.openrewrite.ipc.http.HttpSender;
import org.openrewrite.ipc.http.HttpUrlConnectionSender;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand All @@ -30,31 +32,26 @@
public class SpringBootReleases {
private static volatile Set<String> availableReleases;

private final OkHttpClient httpClient = new OkHttpClient.Builder()
.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS))
.build();

private final String repositoryUrl = "https://repo1.maven.org/maven2";

public Stream<ModuleDownload> download(String version) {
List<String> denyList = Arrays.asList("sample", "gradle", "experimental", "legacy",
"maven", "tests", "spring-boot-versions");

Request request = new Request.Builder()
.url(repositoryUrl + "/org/springframework/boot")
HttpUrlConnectionSender httpSender = new HttpUrlConnectionSender();
HttpSender.Request request = HttpSender.Request.build(repositoryUrl + "/org/springframework/boot", httpSender)
.withMethod(HttpSender.Method.GET)
.build();

try (Response response = httpClient.newCall(request).execute()) {
try (HttpSender.Response response = httpSender.send(request)) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}

Set<String> modules = new HashSet<>();

ResponseBody responseBody = response.body();
if (responseBody != null) {
if (response.isSuccessful()) {
Matcher moduleMatcher = Pattern.compile("href=\"([^\"]+)/\"")
.matcher(responseBody.string());
.matcher(new String(response.getBodyAsBytes()));

while (moduleMatcher.find()) {
String module = moduleMatcher.group(1);
Expand All @@ -65,24 +62,24 @@ public Stream<ModuleDownload> download(String version) {

return modules.stream()
.map(module -> {
Request moduleRequest = new Request.Builder()
.url(repositoryUrl + "/org/springframework/boot/" + module + "/" + version +
"/" + module + "-" + version + ".jar")
HttpSender.Request moduleRequest = HttpSender.Request
.build(repositoryUrl + "/org/springframework/boot/" + module + "/" + version +
"/" + module + "-" + version + ".jar", httpSender)
.withMethod(HttpSender.Method.GET)
.build();

try {
Response moduleResponse = httpClient.newCall(moduleRequest).execute();
try(HttpSender.Response moduleResponse = httpSender.send(moduleRequest)) {
if (!moduleResponse.isSuccessful()) {
if (moduleResponse.code() == 404) {
if (moduleResponse.getCode() == 404) {
return null;
}
throw new IOException("Unexpected code " + moduleResponse);
}

ResponseBody moduleResponseBody = moduleResponse.body();
return moduleResponseBody == null ?
null :
new ModuleDownload(module, moduleResponseBody);
byte[] body = moduleResponse.getBodyAsBytes();
if(body.length == 0) {
return null;
}
return new ModuleDownload(module, body);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -98,21 +95,23 @@ public Stream<ModuleDownload> download(String version) {

public Set<String> allReleases() {
if (availableReleases == null) {
Request request = new Request.Builder()
.url(repositoryUrl + "/org/springframework/boot/spring-boot-starter-parent")
HttpUrlConnectionSender httpSender = new HttpUrlConnectionSender();
HttpSender.Request request = HttpSender.Request
.build(repositoryUrl + "/org/springframework/boot/spring-boot-starter-parent", httpSender)
.withMethod(HttpSender.Method.GET)
.build();

try (Response response = httpClient.newCall(request).execute()) {
try (HttpSender.Response response = httpSender.send(request)) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}

Set<String> releases = new HashSet<>();

ResponseBody responseBody = response.body();
if (responseBody != null) {
byte[] responseBody = response.getBodyAsBytes();
if (responseBody.length > 0) {
Matcher releaseMatcher = Pattern.compile("href=\"([^\"]+[.RELEASE]*)/\"")
.matcher(responseBody.string());
.matcher(new String(responseBody));

while (releaseMatcher.find()) {
if ("..".equals(releaseMatcher.group(1))) {
Expand Down Expand Up @@ -203,9 +202,9 @@ public String latestMatchingVersion(String version) {

public static class ModuleDownload {
private final String moduleName;
private final ResponseBody body;
private final byte[] body;

public ModuleDownload(String moduleName, ResponseBody body) {
public ModuleDownload(String moduleName, byte[] body) {
this.moduleName = moduleName;
this.body = body;
}
Expand All @@ -214,7 +213,7 @@ public String getModuleName() {
return moduleName;
}

public ResponseBody getBody() {
public byte[] getBody() {
return body;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/GeneratePropertiesMigratorConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object GeneratePropertiesMigratorConfiguration {
if (versionDir.mkdirs()) {
println("Downloading version $version")
springBootReleases.download(version).forEach { download ->
download.body.use { it.byteStream().copyTo(File(versionDir, "${download.moduleName}-${version}.jar").outputStream()) }
File(versionDir, "${download.moduleName}-${version}.jar").writeBytes(download.body)
}
} else {
println("Using existing download of version $version")
Expand Down

0 comments on commit 5ff5411

Please sign in to comment.