Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced jersey dependency by native java HttpClient #292

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 0 additions & 33 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>

<jersey.version>4.0.0-M1</jersey.version>
<switcher-client.version>2.2.2</switcher-client.version>
<jsonwebtoken.version>0.12.6</jsonwebtoken.version>
<joda-time.version>2.13.0</joda-time.version>
Expand Down Expand Up @@ -210,38 +209,6 @@
<scope>runtime</scope>
</dependency>

<!-- Jersey -->
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-entity-filtering</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<exclusions>
<exclusion>
<artifactId>jersey-common</artifactId>
<groupId>org.glassfish.jersey.core</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>

<!-- Utils -->
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.HttpHeaders;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.switcherapi.ac.util.FileUtil;
import com.github.switcherapi.client.SnapshotCallback;
import com.github.switcherapi.client.SwitcherContextBase;
import com.github.switcherapi.client.SwitcherKey;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -16,7 +15,6 @@
@ConfigurationProperties(prefix = "switcher")
public class SwitcherFeatures extends SwitcherContextBase implements SnapshotCallback {

@SwitcherKey
public static final String SWITCHER_AC_ADM = "SWITCHER_AC_ADM";

private String relayCode;
Expand All @@ -25,6 +23,7 @@ public class SwitcherFeatures extends SwitcherContextBase implements SnapshotCal
@Override
protected void configureClient() {
super.truststore.setPath(FileUtil.getFilePathFromResource(truststore.getPath()));
super.registerSwitcherKeys(SWITCHER_AC_ADM);
super.configureClient();

scheduleSnapshotAutoUpdate(snapshot.getUpdateInterval(), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.github.switcherapi.ac.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirements;
import jakarta.ws.rs.core.HttpHeaders;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.annotations.SerializedName;

@JsonIgnoreProperties(ignoreUnknown = true)
public record GitHubDetail(
String id,
String name,
String login,
@JsonProperty("avatar_url")
@SerializedName("avatar_url")
String avatarUrl
) {
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.github.switcherapi.ac.service.facades;

import com.github.switcherapi.ac.model.GitHubDetail;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import com.github.switcherapi.client.exception.SwitcherRemoteException;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;

Expand All @@ -23,8 +26,8 @@ public class GitHubFacade {
public static final String INVALID_ACCOUNT = "Invalid GitHub account";

private static final String ACCESS_TOKEN = "access_token";
private static final String HEADER_ACCEPT = "accept";

private final Gson gson = new Gson();

private final String clientId;

Expand All @@ -34,7 +37,7 @@ public class GitHubFacade {

private final String gitUrlDetail;

private final Client client;
private final HttpClient httpClient;

public GitHubFacade(
@Value("${service.github.clientid}") String clientId,
Expand All @@ -45,48 +48,60 @@ public GitHubFacade(
this.oauthSecret = oauthSecret;
this.gitUrlAccess = gitUrlAccess;
this.gitUrlDetail = gitUrlDetail;
this.client = ClientBuilder.newClient();
this.httpClient = HttpClient.newHttpClient();
}

public String getToken(String code) {
var codeSanitized = sanitize(code, List.of(trim(), alphaNumeric()));

final WebTarget myResource = client.target(
String.format(gitUrlAccess, clientId, oauthSecret, codeSanitized));
try {
final URI uri = new URI(String.format(gitUrlAccess, clientId, oauthSecret, codeSanitized));

try (var response = myResource
.request(MediaType.APPLICATION_JSON)
.header(HEADER_ACCEPT, MediaType.APPLICATION_JSON)
.post(null)) {
final HttpResponse<String> response = httpClient.send(HttpRequest.newBuilder()
.uri(uri)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString())
.POST(HttpRequest.BodyPublishers.noBody())
.build(), HttpResponse.BodyHandlers.ofString());

if (response.getStatus() == 200) {
final var responseEntity = response.readEntity(Map.class);
if (responseEntity.containsKey(ACCESS_TOKEN)) {
return responseEntity.get(ACCESS_TOKEN).toString();
}
if (response.statusCode() != 200 || !response.body().contains(ACCESS_TOKEN)) {
log.error("Failed to get token from GitHub");
return StringUtils.EMPTY;
}

log.error("Failed to get token from GitHub");
return StringUtils.EMPTY;
final var responseEntity = gson.fromJson(response.body(), Map.class);
return responseEntity.get(ACCESS_TOKEN).toString();
} catch (Exception e) {
return exceptionHandler(e, gitUrlAccess);
}
}

public GitHubDetail getGitHubDetail(String token) {
final WebTarget myResource = client.target(gitUrlDetail);

try (var response = myResource
.request(MediaType.APPLICATION_JSON)
.header(HEADER_ACCEPT, MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, String.format("token %s", token))
.get()) {

if (response.getStatus() == 200) {
return response.readEntity(GitHubDetail.class);
try {
final URI uri = new URI(gitUrlDetail);

final HttpResponse<String> response = httpClient.send(HttpRequest.newBuilder()
.uri(uri)
.headers(
HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON.toString(),
HttpHeaders.AUTHORIZATION, String.format("token %s", token))
.GET().build(), HttpResponse.BodyHandlers.ofString());

if (response.statusCode() != 200) {
log.error("Failed to get GitHub detail");
return null;
}

log.error("Failed to get GitHub detail");
return null;
return gson.fromJson(response.body(), GitHubDetail.class);
} catch (Exception e) {
return exceptionHandler(e, gitUrlDetail);
}
}

private <T> T exceptionHandler(Exception e, String url) {
if (e instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
throw new SwitcherRemoteException(url, e);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import com.github.switcherapi.ac.service.AdminService;
import com.github.switcherapi.ac.service.JwtTokenService;
import com.github.switcherapi.ac.service.PlanService;
import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import com.github.switcherapi.ac.service.AdminService;
import com.github.switcherapi.ac.service.JwtTokenService;
import com.github.switcherapi.client.test.SwitcherTest;
import jakarta.ws.rs.core.HttpHeaders;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.github.switcherapi.ac.model.dto.GitHubAuthDTO;
import com.github.switcherapi.ac.service.facades.GitHubFacade;
import com.github.switcherapi.client.test.SwitcherTest;
import jakarta.ws.rs.core.MediaType;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterAll;
Expand All @@ -20,6 +19,7 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

import java.io.IOException;
Expand Down Expand Up @@ -157,7 +157,7 @@ void shouldNotLoginWithGitHub_invalidUrl() throws Exception {

private void givenGitHubToken() {
mockBackend.enqueue(new MockResponse()
.setBody("{ \"access_token\": \"123\" }")
.setBody("{\"access_token\":\"123\",\"token_type\":\"bearer\",\"scope\":\"\"}")
.addHeader("Content-Type", MediaType.APPLICATION_JSON));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.github.switcherapi.ac.model.domain.Plan;
import com.github.switcherapi.ac.service.PlanService;
import com.google.gson.Gson;
import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
Expand All @@ -12,6 +11,7 @@
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import com.github.switcherapi.ac.model.domain.Admin;
import com.github.switcherapi.ac.service.AdminService;
import com.github.switcherapi.ac.service.JwtTokenService;
import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import com.github.switcherapi.ac.service.AdminService;
import com.github.switcherapi.ac.service.JwtTokenService;
import com.github.switcherapi.ac.service.PlanService;
import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.server.ResponseStatusException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.github.switcherapi.ac.service.ValidatorBasicService;
import com.github.switcherapi.ac.service.validator.ValidatorBuilderService;
import com.google.gson.Gson;
import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
Expand All @@ -15,6 +14,7 @@
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import com.github.switcherapi.ac.model.dto.ResponseRelayDTO;
import com.github.switcherapi.ac.service.AccountService;
import com.github.switcherapi.ac.service.PlanService;
import jakarta.ws.rs.core.HttpHeaders;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.github.switcherapi.ac.model.dto.ResponseRelayDTO;
import com.github.switcherapi.ac.service.AccountService;
import com.google.gson.Gson;
import jakarta.ws.rs.core.HttpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

Expand Down
Loading
Loading