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 org.apache.camel.CamelContext;
19 import org.apache.camel.Exchange;
20 import org.apache.camel.support.DefaultMessage;
21 import org.apache.camel.util.ObjectHelper;
22
23 import org.json.JSONObject;
24
25 public class HippoEventMessage extends DefaultMessage {
26
27 public HippoEventMessage(final Exchange exchange) {
28 super(exchange);
29 }
30
31 public HippoEventMessage(final CamelContext camelContext) {
32 super(camelContext);
33 }
34
35 @Override
36 public String toString() {
37 if (getBody() != null) {
38 return "HippoEventMessage[event: " + getBody() + "]";
39 }
40
41 return "HippoEventMessage@" + ObjectHelper.getIdentityHashCode(this);
42 }
43
44 @Override
45 public void copyFrom(org.apache.camel.Message that) {
46 if (that == this) {
47 // the same instance so do not need to copy
48 return;
49 }
50
51 // must initialize headers before we set the JmsMessage to avoid Camel
52 // populating it before we do the copy
53 getHeaders().clear();
54
55 // copy body and fault flag
56
57 Object body = that.getBody();
58
59 if (body != null && body instanceof JSONObject) {
60 setBody(JSONObject.valueToString(body));
61 } else {
62 setBody(body);
63 }
64
65 // we have already cleared the headers
66 if (that.hasHeaders()) {
67 getHeaders().putAll(that.getHeaders());
68 }
69
70 }
71 }