Skip to content

Commit 8788f8f

Browse files
committed
WIP
1 parent 6af0a10 commit 8788f8f

8 files changed

Lines changed: 133 additions & 12 deletions

File tree

src/java/org/jivesoftware/openfire/plugin/rest/controller/MUCRoomController.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.jivesoftware.openfire.plugin.rest.entity.*;
2626
import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType;
2727
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
28+
import org.jivesoftware.openfire.plugin.rest.utils.LoggingUtils;
2829
import org.jivesoftware.openfire.plugin.rest.utils.MUCRoomUtils;
2930
import org.jivesoftware.openfire.plugin.rest.utils.UserUtils;
3031
import org.jivesoftware.util.AlreadyExistsException;
@@ -36,7 +37,6 @@
3637
import org.xmpp.packet.Presence;
3738

3839
import javax.annotation.Nonnull;
39-
import javax.servlet.ServletException;
4040
import javax.ws.rs.core.Response;
4141
import java.lang.reflect.InvocationTargetException;
4242
import java.util.*;
@@ -166,7 +166,11 @@ protected static MUCRoom getRoom(@Nonnull final String serviceName, @Nonnull fin
166166
*/
167167
public MUCRoomEntities getChatRooms(String serviceName, String channelType, String roomSearch, boolean expand) throws ServiceException
168168
{
169-
log("Get the chat rooms");
169+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_LIST_ROOMS,
170+
"serviceName", serviceName,
171+
"channelType", channelType,
172+
"roomSearch", roomSearch,
173+
"expand", expand);
170174
final MultiUserChatService service = getService(serviceName);
171175
Set<String> roomNames = service.getAllRoomNames();
172176

@@ -207,7 +211,10 @@ public MUCRoomEntities getChatRooms(String serviceName, String channelType, Stri
207211
* the service exception
208212
*/
209213
public MUCRoomEntity getChatRoom(String roomName, String serviceName, boolean expand) throws ServiceException {
210-
log("Get the chat room: " + roomName);
214+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_GET_ROOM,
215+
"roomName", roomName,
216+
"serviceName", serviceName,
217+
"expand", expand);
211218
final MUCRoom chatRoom = getRoom(serviceName, roomName);
212219
return convertToMUCRoomEntity(chatRoom, expand);
213220
}
@@ -223,7 +230,9 @@ public MUCRoomEntity getChatRoom(String roomName, String serviceName, boolean ex
223230
* the service exception
224231
*/
225232
public void deleteChatRoom(String roomName, String serviceName) throws ServiceException {
226-
log("Delete the chat room: " + roomName);
233+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_DELETE_ROOM,
234+
"roomName", roomName,
235+
"serviceName", serviceName);
227236
final MUCRoom chatRoom = getRoom(serviceName, roomName);
228237
chatRoom.destroyRoom(null, null);
229238
}
@@ -239,7 +248,9 @@ public void deleteChatRoom(String roomName, String serviceName) throws ServiceEx
239248
* the service exception
240249
*/
241250
public void createChatRoom(String serviceName, MUCRoomEntity mucRoomEntity) throws ServiceException {
242-
log("Create a chat room: " + mucRoomEntity.getRoomName());
251+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_CREATE_ROOM,
252+
"serviceName", serviceName,
253+
"mucRoomEntity", mucRoomEntity);
243254
try {
244255
createRoom(mucRoomEntity, serviceName);
245256
} catch (NotAllowedException | ForbiddenException e) {
@@ -268,7 +279,10 @@ public void createChatRoom(String serviceName, MUCRoomEntity mucRoomEntity) thro
268279
*/
269280
public void updateChatRoom(String roomName, String serviceName, MUCRoomEntity mucRoomEntity)
270281
throws ServiceException {
271-
log("Update a chat room: " + mucRoomEntity.getRoomName());
282+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_UPDATE_ROOM,
283+
"roomName", roomName,
284+
"serviceName", serviceName,
285+
"mucRoomEntity", mucRoomEntity);
272286
try {
273287
// If the room name is different throw exception
274288
if (!roomName.equals(mucRoomEntity.getRoomName())) {
@@ -409,7 +423,9 @@ private boolean equalToAffiliations(MUCRoom room, MUCRoomEntity mucRoomEntity) {
409423
*/
410424
public ParticipantEntities getRoomParticipants(String roomName, String serviceName) throws ServiceException
411425
{
412-
log("Get room participants for room: " + roomName);
426+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_GET_PARTICIPANT_LIST,
427+
"roomName", roomName,
428+
"serviceName", serviceName);
413429
ParticipantEntities participantEntities = new ParticipantEntities();
414430
List<ParticipantEntity> participants = new ArrayList<>();
415431

@@ -439,7 +455,9 @@ public ParticipantEntities getRoomParticipants(String roomName, String serviceNa
439455
*/
440456
public OccupantEntities getRoomOccupants(String roomName, String serviceName) throws ServiceException
441457
{
442-
log("Get room occupants for room: " + roomName);
458+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_GET_OCCUPANT_LIST,
459+
"roomName", roomName,
460+
"serviceName", serviceName);
443461
OccupantEntities occupantEntities = new OccupantEntities();
444462
List<OccupantEntity> occupants = new ArrayList<>();
445463

@@ -469,7 +487,9 @@ public OccupantEntities getRoomOccupants(String roomName, String serviceName) th
469487
* @return the room chat history
470488
*/
471489
public MUCRoomMessageEntities getRoomHistory(String roomName, String serviceName) throws ServiceException {
472-
log("Get room history for room: " + roomName);
490+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_GET_ROOM_HISTORY,
491+
"roomName", roomName,
492+
"serviceName", serviceName);
473493
MUCRoomMessageEntities mucRoomMessageEntities = new MUCRoomMessageEntities();
474494
List<MUCRoomMessageEntity> listMessages = new ArrayList<>();
475495

@@ -517,6 +537,11 @@ public MUCRoomMessageEntities getRoomHistory(String roomName, String serviceName
517537
*/
518538
public void inviteUser(String serviceName, String roomName, String jid, MUCInvitationEntity mucInvitationEntity)
519539
throws ServiceException {
540+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_INVITE_USER,
541+
"roomName", roomName,
542+
"serviceName", serviceName,
543+
"jid", jid,
544+
"invitation" , mucInvitationEntity);
520545
MUCRoom room = getRoom(serviceName, roomName);
521546

522547
try {
@@ -664,6 +689,7 @@ private void setRoles(MUCRoom room, MUCRoomEntity mucRoomEntity) throws Forbidde
664689
* the service exception
665690
*/
666691
public void addAdmin(String serviceName, String roomName, String jid) throws ServiceException {
692+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_ADMIN, serviceName, roomName, jid);
667693
MUCRoom room = getRoom(serviceName, roomName);
668694
try {
669695
room.addAdmin(UserUtils.checkAndGetJID(jid), room.getRole());
@@ -687,6 +713,7 @@ public void addAdmin(String serviceName, String roomName, String jid) throws Ser
687713
* the service exception
688714
*/
689715
public void addOwner(String serviceName, String roomName, String jid) throws ServiceException {
716+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_OWNER, serviceName, roomName, jid);
690717
MUCRoom room = getRoom(serviceName, roomName);
691718
try {
692719
room.addOwner(UserUtils.checkAndGetJID(jid), room.getRole());
@@ -708,6 +735,7 @@ public void addOwner(String serviceName, String roomName, String jid) throws Ser
708735
* the service exception
709736
*/
710737
public void addMember(String serviceName, String roomName, String jid) throws ServiceException {
738+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_MEMBER, serviceName, roomName, jid);
711739
MUCRoom room = getRoom(serviceName, roomName);
712740
try {
713741
room.addMember(UserUtils.checkAndGetJID(jid), null, room.getRole());
@@ -729,6 +757,7 @@ public void addMember(String serviceName, String roomName, String jid) throws Se
729757
* the service exception
730758
*/
731759
public void addOutcast(String serviceName, String roomName, String jid) throws ServiceException {
760+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_OUTCAST, serviceName, roomName, jid);
732761
MUCRoom room = getRoom(serviceName, roomName);
733762
try {
734763
room.addOutcast(UserUtils.checkAndGetJID(jid), null, room.getRole());
@@ -753,6 +782,7 @@ public void addOutcast(String serviceName, String roomName, String jid) throws S
753782
*/
754783
public Collection<JID> getByAffiliation(@Nonnull final String serviceName, @Nonnull final String roomName, @Nonnull final MUCRole.Affiliation affiliation) throws ServiceException
755784
{
785+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_LIST_AFFILIATED_USERS_FOR_AFFILIATION, serviceName, roomName, affiliation);
756786
final MUCRoom room = getRoom(serviceName, roomName);
757787
switch (affiliation) {
758788
case admin:
@@ -789,6 +819,7 @@ public Collection<JID> getByAffiliation(@Nonnull final String serviceName, @Nonn
789819
*/
790820
public void replaceAffiliatedUsers(@Nonnull final String serviceName, @Nonnull final String roomName, @Nonnull final MUCRole.Affiliation affiliation, @Nonnull final Collection<String> jids) throws ServiceException
791821
{
822+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_REPLACE_AFFILIATED_USERS_FOR_AFFILIATION, serviceName, roomName, affiliation, jids);
792823
final Collection<JID> replacements = new HashSet<>();
793824

794825
// Input validation.
@@ -864,6 +895,7 @@ public void replaceAffiliatedUsers(@Nonnull final String serviceName, @Nonnull f
864895
*/
865896
public void addAffiliatedUsers(@Nonnull final String serviceName, @Nonnull final String roomName, @Nonnull final MUCRole.Affiliation affiliation, @Nonnull final Collection<String> jids) throws ServiceException
866897
{
898+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_ADD_AFFILIATED_USERS_FOR_AFFILIATION, serviceName, roomName, affiliation, jids);
867899
final Collection<JID> additions = new HashSet<>();
868900

869901
// Input validation.
@@ -927,6 +959,7 @@ public void addAffiliatedUsers(@Nonnull final String serviceName, @Nonnull final
927959
* the service exception
928960
*/
929961
public void deleteAffiliation(String serviceName, String roomName, String jid) throws ServiceException {
962+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MUC_REMOVE_AFFILIATED_USER_OR_GROUP_FOR_AFFILIATION, serviceName, roomName, jid);
930963
MUCRoom room = getRoom(serviceName, roomName);
931964
try {
932965
JID userJid = UserUtils.checkAndGetJID(jid);

src/java/org/jivesoftware/openfire/plugin/rest/controller/MessageController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.jivesoftware.openfire.plugin.rest.entity.MessageEntity;
2323
import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType;
2424
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
25+
import org.jivesoftware.openfire.plugin.rest.utils.LoggingUtils;
2526

2627
/**
2728
* The Class MessageController.
@@ -48,6 +49,7 @@ public static MessageController getInstance() {
4849
* the service exception
4950
*/
5051
public void sendBroadcastMessage(MessageEntity messageEntity) throws ServiceException {
52+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MESSAGE_BROADCAST, messageEntity);
5153
if (messageEntity.getBody() != null && !messageEntity.getBody().isEmpty()) {
5254
SessionManager.getInstance().sendServerMessage(null, messageEntity.getBody());
5355
} else {

src/java/org/jivesoftware/openfire/plugin/rest/controller/MsgArchiveController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.sql.SQLException;
2323

2424
import org.jivesoftware.database.DbConnectionManager;
25+
import org.jivesoftware.openfire.plugin.rest.utils.LoggingUtils;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
2728
import org.xmpp.packet.JID;
@@ -64,6 +65,7 @@ private MsgArchiveController() {
6465
* @return the total number of user unread messages.
6566
*/
6667
public int getUnReadMessagesCount(JID jid) {
68+
LoggingUtils.auditEvent(LoggingUtils.AuditEvent.MESSAGE_ARCHIVE_UNREAD_COUNT, jid);
6769
int messageCount = 0;
6870
Connection con = null;
6971
PreparedStatement pstmt = null;

src/java/org/jivesoftware/openfire/plugin/rest/entity/GroupEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package org.jivesoftware.openfire.plugin.rest.entity;
1818

1919
import com.fasterxml.jackson.annotation.JsonProperty;
20-
import com.google.common.base.MoreObjects;
2120
import io.swagger.v3.oas.annotations.media.ArraySchema;
2221
import io.swagger.v3.oas.annotations.media.Schema;
22+
import org.glassfish.jersey.internal.guava.MoreObjects;
2323
import org.jivesoftware.util.StringUtils;
2424

2525
import java.util.List;

src/java/org/jivesoftware/openfire/plugin/rest/entity/MUCInvitationEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ public void setReason(String reason) {
3636
this.reason = reason;
3737
}
3838

39+
@Override
40+
public String toString() {
41+
return "MUCInvitationEntity [reason=" + reason + "]";
42+
}
43+
3944
}

src/java/org/jivesoftware/openfire/plugin/rest/entity/MUCRoomEntity.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.jivesoftware.openfire.plugin.rest.entity;
1818

1919
import com.fasterxml.jackson.annotation.JsonProperty;
20+
import org.glassfish.jersey.internal.guava.MoreObjects;
2021

2122
import java.util.Date;
2223
import java.util.List;
@@ -350,4 +351,32 @@ public void setAdminGroups(List<String> adminGroups) {
350351
this.adminGroups = adminGroups;
351352
}
352353

354+
@Override
355+
public String toString() {
356+
return MoreObjects.toStringHelper(this)
357+
.add("roomName", roomName)
358+
.add("description", description)
359+
.add("persistent", persistent)
360+
.add("publicRoom", publicRoom)
361+
.add("registrationEnabled", registrationEnabled)
362+
.add("canAnyoneDiscoverJID", canAnyoneDiscoverJID)
363+
.add("canOccupantsChangeSubject", canOccupantsChangeSubject)
364+
.add("canOccupantsInvite", canOccupantsInvite)
365+
.add("canChangeNickname", canChangeNickname)
366+
.add("logEnabled", logEnabled)
367+
.add("loginRestrictedToNickname", loginRestrictedToNickname)
368+
.add("membersOnly", membersOnly)
369+
.add("moderated", moderated)
370+
.add("broadcastPresenceRoles", broadcastPresenceRoles)
371+
.add("owners", owners)
372+
.add("ownerGroups", ownerGroups)
373+
.add("members", members)
374+
.add("memberGroups", memberGroups)
375+
.add("outcasts", outcasts)
376+
.add("outcastGroups", outcastGroups)
377+
.add("admins", admins)
378+
.add("adminGroups", adminGroups)
379+
.toString();
380+
}
381+
353382
}

src/java/org/jivesoftware/openfire/plugin/rest/entity/MessageEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ public String getBody() {
5353
public void setBody(String body) {
5454
this.body = body;
5555
}
56+
57+
@Override
58+
public String toString() {
59+
return "MessageEntity [body=" + body + "]";
60+
}
5661
}

src/java/org/jivesoftware/openfire/plugin/rest/utils/LoggingUtils.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.slf4j.LoggerFactory;
2323

2424
import java.util.ArrayList;
25-
import java.util.Arrays;
2625

2726
public class LoggingUtils {
2827
private static final Logger AUDIT_LOG = LoggerFactory.getLogger("RestAPI-Plugin-Audit");
@@ -41,7 +40,34 @@ public enum AuditEvent {
4140
GROUPS_DELETE,
4241

4342
//JustMarried
44-
USER_CHANGE_NAME
43+
USER_CHANGE_NAME,
44+
45+
//Messages
46+
MESSAGE_BROADCAST,
47+
48+
//Message Archive
49+
MESSAGE_ARCHIVE_UNREAD_COUNT,
50+
51+
//MUC
52+
// - MUC Affiliations
53+
MUC_LIST_AFFILIATED_USERS_FOR_AFFILIATION,
54+
MUC_REPLACE_AFFILIATED_USERS_FOR_AFFILIATION,
55+
MUC_REMOVE_AFFILIATED_USER_OR_GROUP_FOR_AFFILIATION,
56+
MUC_ADD_AFFILIATED_USERS_FOR_AFFILIATION,
57+
MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_ADMIN,
58+
MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_MEMBER,
59+
MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_OUTCAST,
60+
MUC_ADD_AFFILIATED_USER_OR_GROUP_AS_OWNER,
61+
// - MUC Rooms
62+
MUC_LIST_ROOMS,
63+
MUC_GET_ROOM,
64+
MUC_DELETE_ROOM,
65+
MUC_CREATE_ROOM,
66+
MUC_UPDATE_ROOM,
67+
MUC_GET_PARTICIPANT_LIST,
68+
MUC_GET_OCCUPANT_LIST,
69+
MUC_GET_ROOM_HISTORY,
70+
MUC_INVITE_USER,
4571
;
4672
}
4773

@@ -55,11 +81,14 @@ public static void auditEvent(AuditEvent event, Object... parameters){
5581
String logMessage = "Event: " + event;
5682
logMessage += " - ";
5783
logMessage += "Parameters: + " + parameterString;
84+
logMessage += " - ";
85+
logMessage += "Caller: " + getCaller();
5886
AUDIT_LOG.info(logMessage);
5987
};
6088
}
6189

6290
private static String parseParameters(Object[] parameters) {
91+
//TODO: Does this belong here?
6392
ArrayList<String> parsed = new ArrayList<>();
6493
for (Object obj: parameters) {
6594
if(obj == null){
@@ -74,4 +103,20 @@ private static String parseParameters(Object[] parameters) {
74103
}
75104
return parsed.toString();
76105
}
106+
107+
/*
108+
* Returns the name and method of the calling class.
109+
*/
110+
private static String getCaller() {
111+
try {
112+
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
113+
for (StackTraceElement element : stackTrace) {
114+
if(element.getClassName().equals(LoggingUtils.class.getName())){
115+
continue;
116+
}
117+
return element.getClassName() + "." + element.getMethodName();
118+
}
119+
} catch (Exception ignored) {}
120+
return "unknown";
121+
}
77122
}

0 commit comments

Comments
 (0)