001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2025 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.server.messaging.json;
021
022import ca.uhn.fhir.model.api.IModelJson;
023import ca.uhn.fhir.rest.server.messaging.IHasPayloadMessageKey;
024import ca.uhn.fhir.rest.server.messaging.IMessage;
025import ca.uhn.fhir.rest.server.messaging.IMessageDeliveryContext;
026import com.fasterxml.jackson.annotation.JsonProperty;
027import jakarta.annotation.Nonnull;
028import org.springframework.messaging.Message;
029import org.springframework.messaging.MessageHeaders;
030
031import static java.util.Objects.isNull;
032
033public abstract class BaseJsonMessage<T> implements IMessage<T>, Message<T>, IModelJson, IMessageDeliveryContext {
034
035        @JsonProperty("headers")
036        private HapiMessageHeaders myHeaders;
037
038        /**
039         * Constructor
040         */
041        public BaseJsonMessage() {
042                super();
043                setDefaultRetryHeaders();
044        }
045
046        protected void setDefaultRetryHeaders() {
047                HapiMessageHeaders messageHeaders = new HapiMessageHeaders();
048                setHeaders(messageHeaders);
049        }
050
051        @Override
052        @Nonnull
053        public MessageHeaders getHeaders() {
054                return getHapiHeaders().toMessageHeaders();
055        }
056
057        public HapiMessageHeaders getHapiHeaders() {
058                if (isNull(myHeaders)) {
059                        setDefaultRetryHeaders();
060                }
061                return myHeaders;
062        }
063
064        public void setHeaders(HapiMessageHeaders theHeaders) {
065                myHeaders = theHeaders;
066        }
067
068        @Override
069        @Nonnull
070        public String getMessageKey() {
071                T payload = getPayload();
072                if (payload instanceof IHasPayloadMessageKey) {
073                        String payloadMessageKey = ((IHasPayloadMessageKey) payload).getPayloadMessageKey();
074                        if (payloadMessageKey != null) {
075                                return payloadMessageKey;
076                        }
077                }
078                return IMessage.super.getMessageKey();
079        }
080
081        @Override
082        public int getRetryCount() {
083                return getHapiHeaders().getRetryCount();
084        }
085}