|
| 1 | +package dasniko.keycloak.user.external; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 4 | +import dasniko.keycloak.user.Constants; |
| 5 | +import lombok.SneakyThrows; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 8 | +import org.keycloak.broker.provider.util.SimpleHttp; |
| 9 | +import org.keycloak.component.ComponentModel; |
| 10 | +import org.keycloak.connections.httpclient.HttpClientProvider; |
| 11 | +import org.keycloak.models.KeycloakSession; |
| 12 | + |
| 13 | +import javax.ws.rs.WebApplicationException; |
| 14 | +import javax.ws.rs.core.Response; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Niko Köbler, https://www.n-k.de, @dasniko |
| 19 | + */ |
| 20 | +@Slf4j |
| 21 | +public class PeanutsClientSimpleHttp implements PeanutsClient { |
| 22 | + |
| 23 | + private final CloseableHttpClient httpClient; |
| 24 | + private final String baseUrl; |
| 25 | + private final String basicUsername; |
| 26 | + private final String basicPassword; |
| 27 | + |
| 28 | + public PeanutsClientSimpleHttp(KeycloakSession session, ComponentModel model) { |
| 29 | + this.httpClient = session.getProvider(HttpClientProvider.class).getHttpClient(); |
| 30 | + this.baseUrl = model.get(Constants.BASE_URL); |
| 31 | + this.basicUsername = model.get(Constants.AUTH_USERNAME); |
| 32 | + this.basicPassword = model.get(Constants.AUTH_PASSWORD); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + @SneakyThrows |
| 37 | + public List<Peanut> getPeanuts(String search, int first, int max) { |
| 38 | + SimpleHttp simpleHttp = SimpleHttp.doGet(baseUrl, httpClient).authBasic(basicUsername, basicPassword) |
| 39 | + .param("first", String.valueOf(first)) |
| 40 | + .param("max", String.valueOf(max)); |
| 41 | + if (search != null) { |
| 42 | + simpleHttp.param("search", search); |
| 43 | + } |
| 44 | + return simpleHttp.asJson(new TypeReference<>() {}); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + @SneakyThrows |
| 49 | + public Integer getPeanutsCount() { |
| 50 | + String url = String.format("%s/count", baseUrl); |
| 51 | + String count = SimpleHttp.doGet(url, httpClient).authBasic(basicUsername, basicPassword).asString(); |
| 52 | + return Integer.valueOf(count); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + @SneakyThrows |
| 57 | + public Peanut getPeanutById(String id) { |
| 58 | + String url = String.format("%s/%s", baseUrl, id); |
| 59 | + SimpleHttp.Response response = SimpleHttp.doGet(url, httpClient).authBasic(basicUsername, basicPassword).asResponse(); |
| 60 | + if (response.getStatus() == 404) { |
| 61 | + throw new WebApplicationException(response.getStatus()); |
| 62 | + } |
| 63 | + return response.asJson(Peanut.class); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + @SneakyThrows |
| 68 | + public CredentialData getCredentialData(String id) { |
| 69 | + String url = String.format("%s/%s/credentials", baseUrl, id); |
| 70 | + SimpleHttp.Response response = SimpleHttp.doGet(url, httpClient).authBasic(basicUsername, basicPassword).asResponse(); |
| 71 | + if (response.getStatus() == 404) { |
| 72 | + throw new WebApplicationException(response.getStatus()); |
| 73 | + } |
| 74 | + return response.asJson(CredentialData.class); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + @SneakyThrows |
| 79 | + public Response updateCredentialData(String id, CredentialData credentialData) { |
| 80 | + String url = String.format("%s/%s/credentials", baseUrl, id); |
| 81 | + int status = SimpleHttp.doPut(url, httpClient).authBasic(basicUsername, basicPassword).json(credentialData).asStatus(); |
| 82 | + return Response.status(status).build(); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments