Commit 97b84754 authored by lakshmikommalapati's avatar lakshmikommalapati
Browse files

added the logs in utility package classes

Showing with 19 additions and 4 deletions
+19 -4
......@@ -53,7 +53,7 @@ public class KeycloakTokenRetriever {
}
public JsonNode getAdminToken() throws IOException {
String tokenEndpoint = ADMIN_TOKEN_ENDPOINT;
logger.info("Token endpoint: {}" ,tokenEndpoint);
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(tokenEndpoint);
......@@ -65,11 +65,13 @@ public class KeycloakTokenRetriever {
"&client_id=admin-cli" +
"&client_secret=" + ADMIN_TOKEN_SECRET;
logger.info("Request body: {}", requestBody);
StringEntity entity = new StringEntity(requestBody);
httpPost.setEntity(entity);
org.apache.http.HttpResponse response = httpClient.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
logger.info("Response body: {}", responseBody);
JsonNode jsonNode = mapper.readTree(responseBody);
return jsonNode;
}
......
......@@ -45,8 +45,11 @@ public class KeycloakUserCreator {
}
public String createUser(final JsonNode body) throws IOException {
String keycloakBaseUrl = KEYCLOAK_USER_BASE_URL;
logger.info("keycloakBaseUrl: " ,keycloakBaseUrl);
JsonNode adminToken = keycloakTokenRetriever.getAdminToken();
logger.info("adminToken: {}" ,adminToken);
String accessToken = adminToken.get("access_token").asText();
logger.info("accessToken: {}" ,accessToken);
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(keycloakBaseUrl);
......@@ -71,12 +74,13 @@ public class KeycloakUserCreator {
"\"temporary\": false" +
"}]" +
"}";
logger.info("Request body: {}", requestBody);
StringEntity entity = new StringEntity(requestBody);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
String responseBody = EntityUtils.toString(response.getEntity());
logger.info("ResponseBody {}", responseBody);
return responseBody;
}
}
......@@ -43,11 +43,15 @@ public class KeycloakUserGetter {
}
public String findUser(final String userID) throws IOException {
String userEndpoint = KEYCLOAK_USER_BASE_URL;
logger.info("userEndpoint: " ,userEndpoint);
if(userID != null ) {
userEndpoint = userEndpoint + "/" + userID;
}
logger.info("userEndpoint after adding useerId : " ,userEndpoint);
JsonNode adminToken = keycloakTokenRetriever.getAdminToken();
logger.info("adminToken: " ,adminToken);
String accessToken = adminToken.get("access_token").asText();
logger.info("accessToken: " ,accessToken);
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(userEndpoint);
......@@ -57,6 +61,7 @@ public class KeycloakUserGetter {
org.apache.http.HttpResponse response = httpClient.execute(httpGet);
String responseBody = EntityUtils.toString(response.getEntity());
logger.info("ResponseBody {}", responseBody);
return responseBody;
}
}
......@@ -7,10 +7,14 @@ import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class KeycloakUserUpdater {
private static final Logger logger = LoggerFactory.getLogger(KeycloakUserUpdater.class);
public static void main(String[] args) throws IOException {
String keycloakBaseUrl = "http://localhost:8080/auth/admin/realms/example/users";
String accessToken = "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJUM3F6M0t6TnNYamx2ZmNVX1FKY1R6eHJJUjgwNGlKWnktaE1uSU96dktrIn0.eyJqdGkiOiIyZmYyNjU3NS0yMTE4LTQzYjctYmYxNi05MzIzOTc5ZjAxOTEiLCJleHAiOjE2OTIxMDU5MDMsIm5iZiI6MCwiaWF0IjoxNjkyMTA1ODQzLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwODAvYXV0aC9yZWFsbXMvbWFzdGVyIiwic3ViIjoiM2UxOGExYjMtYTM5YS00MDhhLWIwYmYtMjJhMWJkOGY4ZDk1IiwidHlwIjoiQmVhcmVyIiwiYXpwIjoic2VjdXJpdHktYWRtaW4tY29uc29sZSIsIm5vbmNlIjoiYzU5YzM4NGYtOTY5My00ODdkLTg0OGQtNmFmYmViMGM0ODM4IiwiYXV0aF90aW1lIjoxNjkyMTA0Njk5LCJzZXNzaW9uX3N0YXRlIjoiNTY5NTlhN2QtNjBjYi00YzI5LTg0ZmQtNGNkYjdhYzA0N2U0IiwiYWNyIjoiMSIsInNjb3BlIjoib3BlbmlkIHByb2ZpbGUgZW1haWwiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsIm5hbWUiOiJtYWhlc2ggbWFuZXkgciIsInByZWZlcnJlZF91c2VybmFtZSI6ImFkbWluIiwiZ2l2ZW5fbmFtZSI6Im1haGVzaCIsImZhbWlseV9uYW1lIjoibWFuZXkgciIsImVtYWlsIjoibWFoZXNoLm1hbmV5QGdtYWlsLmNvbSJ9.LeSON6AeD__ZRI5D2Dexca51tF4vP2BrP4y1H3y_BdrGKy6Gu_QV5bTtjuqjiY8txM-C9ADd7jSXohtpPWHdcgj_bSAaFwWR3U0NliKBusXgSgQpyyCZ5aCkT7JYICZnG2_nvEC6AN1YCuc_hglwEhksWaNoU0KbnjTxnSDxb2HPhM7mYkkObMLZpqpyavYls5JpSaer-n6zNaPh7snyy-EhyXralYaqq0nXJ5uIsIjXxp9NSBchbi4KD-K0xCwRFAWInNkXJcPVKsWv2_nImuUOX_k56ePVQrRZN2KAb7pcuv2T7F4N3gTItt_UUlPdHXMGN3M10M8Ndun_4L7cVw"; // Replace with your access token
......@@ -28,13 +32,13 @@ public class KeycloakUserUpdater {
"\"username\": \"def.ghi@yopmail.com\"," +
"\"emailVerified\": true" +
"}";
logger.info("Request body: {}", requestBody);
StringEntity entity = new StringEntity(requestBody);
httpPut.setEntity(entity);
HttpResponse response = httpClient.execute(httpPut);
String responseBody = EntityUtils.toString(response.getEntity());
logger.info("ResponseBody {}", responseBody);
if (response.getStatusLine().getStatusCode() == 204) {
System.out.println("User updated successfully.");
} else {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment