DEV Community

Discussion on: Building an Event Listener SPI (Plugin) for KeyCloak

Collapse
 
rishabhwadhwa15 profile image
rishabhwadhwa15

private String toString(AdminEvent adminEvent) {
RealmModel realm = session.realms().getRealm(adminEvent.getAuthDetails().getRealmId());
UserModel user = session.users().getUserById(adminEvent.getAuthDetails().getUserId(), realm);
System.out.println(user.getUsername());
StringBuilder sb = new StringBuilder();
JSONObject obj = new JSONObject();
JSONParser parser = new JSONParser();
AuditEvent auditEvent = new AuditEvent();
AuditServiceImpl service = new AuditServiceImpl();

    System.out.println("load context");
    System.out.println("load context23");
   ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");// this is not working

    obj.put("event_type", "keycloak_admin");
    obj.put("action", adminEvent.getOperationType());
    obj.put("realm_id", adminEvent.getAuthDetails().getRealmId());
    obj.put("client_id", adminEvent.getAuthDetails().getClientId());
    obj.put("user_id", adminEvent.getAuthDetails().getUserId());
    obj.put("user_first_name", user.getFirstName());
    obj.put("user_last_name", user.getLastName());
    obj.put("ip_address", adminEvent.getAuthDetails().getIpAddress());
    obj.put("resource_path", adminEvent.getResourcePath());
    obj.put("log_time", Long.toString(Instant.now().getEpochSecond()));
    if (adminEvent.getError() != null) {
        obj.put("error", adminEvent.getError());
    } else {
        obj.put("error", "N/A");
    }
    if (adminEvent.getRepresentation() != null) {
        try {
            String representation = adminEvent.getRepresentation().replace('.','-');
            obj.put("new_row", (JSONObject) parser.parse(representation));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    obj.put("model", "users");
    obj.put("request_source", "user_backoffice");
    System.out.println("2");
    System.out.println(obj);
    auditEvent.setId("101");
    auditEvent.setLogs((obj.toString()));
    System.out.println("Events stored");
    AuditService auditService = (AuditService) context.getBean("auditService");
    auditService.persistAudit(auditEvent);



    return "";
}
Enter fullscreen mode Exit fullscreen mode

I am trying to store these events inside a configured database but its not able to load applicationContext.xml placed inside resources folder. Resulting in filenotfoundexception. Could you please help where i am going wrong.