View Javadoc
1   /*
2    * Copyright 2024 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  
21  import net.sf.json.JSONArray;
22  import net.sf.json.JSONObject;
23  
24  import org.hippoecm.repository.standardworkflow.FolderWorkflowEvent;
25  import org.onehippo.cms7.event.HippoEvent;
26  import org.onehippo.cms7.event.HippoSecurityEvent;
27  import org.onehippo.repository.events.HippoWorkflowEvent;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * 
33   */
34  public final class HippoEventConverter {
35  
36      private static final Logger log = LoggerFactory.getLogger(HippoEventConverter.class);
37  
38      private HippoEventConverter() {
39      }
40  
41      public static JSONObject toJSONObject(final HippoEvent event) {
42          JSONObject eventJson = new JSONObject();
43  
44          if (event != null) {
45              eventJson.put("_eventClassName", event.getClass().getName());
46  
47              eventJson.put("action", event.action());
48              eventJson.put("application", event.application());
49              eventJson.put("category", event.category());
50              eventJson.put("sealed", event.isSealed());
51              eventJson.put("message", event.message());
52              eventJson.put("result", event.result());
53              eventJson.put("system", event.system());
54              eventJson.put("timestamp", event.timestamp());
55              eventJson.put("user", event.user());
56  
57              if (event instanceof HippoSecurityEvent) {
58                  HippoSecurityEvent secEvent = (HippoSecurityEvent) event;
59                  eventJson.put("success", secEvent.success());
60              }
61  
62              if (event instanceof HippoWorkflowEvent) {
63                  HippoWorkflowEvent wfEvent = (HippoWorkflowEvent) event;
64  
65                  List<String> arguments = wfEvent.arguments();
66                  JSONArray jsonArgs = new JSONArray();
67                  if (arguments != null) {
68                      jsonArgs.addAll(arguments);
69                  }
70  
71                  eventJson.put("arguments", jsonArgs);
72                  eventJson.put("className", wfEvent.className());
73  
74                  if (wfEvent.exception() != null) {
75                      eventJson.put("exception", wfEvent.exception().toString());
76                  }
77  
78                  eventJson.put("interaction", wfEvent.interaction());
79                  eventJson.put("interactionId", wfEvent.interactionId());
80                  eventJson.put("methodName", wfEvent.action());
81                  eventJson.put("returnType", wfEvent.returnType());
82                  eventJson.put("returnValue", wfEvent.returnValue());
83                  eventJson.put("subjectId", wfEvent.subjectId());
84                  eventJson.put("subjectPath", wfEvent.subjectPath());
85                  eventJson.put("workflowCategory", wfEvent.workflowCategory());
86                  eventJson.put("workflowName", wfEvent.workflowName());
87  
88                  try {
89                      eventJson.put("documentType", wfEvent.documentType());
90                  } catch (Throwable th) {
91                      log.warn("HippoWorkflowEvent#documentType() is not supported in the current module.");
92                  }
93  
94                  // Add deprecated properties as well for now.
95                  eventJson.put("documentPath", wfEvent.subjectPath());
96                  eventJson.put("handleUuid", wfEvent.subjectId());
97  
98                  if (wfEvent instanceof FolderWorkflowEvent) {
99                      FolderWorkflowEvent fwfEvent = (FolderWorkflowEvent) wfEvent;
100                     eventJson.put("type", fwfEvent.type().toString());
101                 }
102             }
103 
104             final Map<String, Object> attributes = event.getValues();
105             String key;
106             Object value;
107 
108             for (Map.Entry<String, Object> entry : attributes.entrySet()) {
109                 key = entry.getKey();
110 
111                 if (!eventJson.has(key)) {
112                     value = entry.getValue();
113 
114                     if (value == null) {
115                         eventJson.put(key, null);
116                     } else {
117                         eventJson.put(key, value.toString());
118                     }
119                 }
120             }
121         }
122 
123         return eventJson;
124     }
125 }