View Javadoc
1   /*
2    * Copyright 2025 Bloomreach B.V. (https://www.bloomreach.com)
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.onehippo.forge.camel.component.hippo;
17  
18  import java.util.List;
19  import java.util.Map;
20  import java.util.Optional;
21  
22  import org.json.JSONArray;
23  import org.json.JSONObject;
24  
25  import org.hippoecm.repository.standardworkflow.FolderWorkflowEvent;
26  import org.onehippo.cms7.event.HippoEvent;
27  import org.onehippo.cms7.event.HippoSecurityEvent;
28  import org.onehippo.repository.events.HippoWorkflowEvent;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * 
34   */
35  public final class HippoEventConverter {
36  
37      private static final Logger log = LoggerFactory.getLogger(HippoEventConverter.class);
38  
39      private HippoEventConverter() {
40      }
41  
42      public static JSONObject toJSONObject(final HippoEvent event) {
43          JSONObject eventJson = new JSONObject();
44  
45          if (event != null) {
46              eventJson.put("_eventClassName", event.getClass().getName());
47  
48              eventJson.put("action", event.action());
49              eventJson.put("application", event.application());
50              eventJson.put("category", event.category());
51              eventJson.put("sealed", event.isSealed());
52              eventJson.put("message", event.message());
53              eventJson.put("result", event.result());
54              eventJson.put("system", event.system());
55              eventJson.put("timestamp", event.timestamp());
56              eventJson.put("user", event.user());
57  
58              if (event instanceof HippoSecurityEvent) {
59                  HippoSecurityEvent secEvent = (HippoSecurityEvent) event;
60                  eventJson.put("success", secEvent.success());
61              }
62  
63              if (event instanceof HippoWorkflowEvent) {
64                  HippoWorkflowEvent wfEvent = (HippoWorkflowEvent) event;
65  
66                  List<String> arguments = wfEvent.arguments();
67                  JSONArray jsonArgs = new JSONArray();
68                  if (arguments != null) {
69                      jsonArgs.putAll(arguments);
70                  }
71  
72                  eventJson.put("arguments", jsonArgs);
73                  eventJson.put("className", wfEvent.className());
74  
75                  if (wfEvent.exception() != null) {
76                      eventJson.put("exception", wfEvent.exception().toString());
77                  }
78  
79                  eventJson.put("interaction", wfEvent.interaction());
80                  eventJson.put("interactionId", wfEvent.interactionId());
81                  eventJson.put("methodName", wfEvent.action());
82                  eventJson.put("returnType", wfEvent.returnType());
83                  eventJson.put("returnValue", wfEvent.returnValue());
84                  eventJson.put("subjectId", wfEvent.subjectId());
85                  eventJson.put("subjectPath", wfEvent.subjectPath());
86                  eventJson.put("workflowCategory", wfEvent.workflowCategory());
87                  eventJson.put("workflowName", wfEvent.workflowName());
88  
89                  try {
90                      eventJson.put("documentType", wfEvent.documentType());
91                  } catch (Throwable th) {
92                      log.warn("HippoWorkflowEvent#documentType() is not supported in the current module.");
93                  }
94  
95                  // Add deprecated properties as well for now.
96                  eventJson.put("documentPath", wfEvent.subjectPath());
97                  eventJson.put("handleUuid", wfEvent.subjectId());
98  
99                  if (wfEvent instanceof FolderWorkflowEvent) {
100                     FolderWorkflowEvent fwfEvent = (FolderWorkflowEvent) wfEvent;
101                     eventJson.put("type", fwfEvent.type().toString());
102                 }
103             }
104 
105             final Map<String, Object> attributes = event.getValues();
106             String key;
107             Object value;
108 
109             for (Map.Entry<String, Object> entry : attributes.entrySet()) {
110                 key = entry.getKey();
111 
112                 if (!eventJson.has(key)) {
113                     value = entry.getValue();
114 
115                     if (value == null) {
116                         eventJson.put(key, Optional.empty());
117                     } else {
118                         eventJson.put(key, value.toString());
119                     }
120                 }
121             }
122         }
123 
124         return eventJson;
125     }
126 }