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 com.fasterxml.jackson.annotation.JsonProperty;
024import jakarta.annotation.Nonnull;
025import jakarta.annotation.Nullable;
026import org.springframework.messaging.Message;
027import org.springframework.messaging.MessageHeaders;
028
029import java.util.Objects;
030
031import static java.util.Objects.isNull;
032
033public abstract class BaseJsonMessage<T> implements Message<T>, IModelJson {
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 myHeaders.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        @Deprecated
069        @Nullable
070        public String getMessageKeyOrNull() {
071                return getMessageKey();
072        }
073
074        @Nullable
075        public String getMessageKey() {
076                return null;
077        }
078
079        /**
080         * Returns {@link #getMessageKey()} or {@link #getMessageKeyDefaultValue()} when {@link #getMessageKey()} returns null.
081         *
082         * @return the message key value or default
083         */
084        @Nullable
085        public String getMessageKeyOrDefault() {
086                return Objects.toString(getMessageKey(), getMessageKeyDefaultValue());
087        }
088
089        /**
090         * Provides a fallback value when the value returned by {@link #getMessageKey()} is null.
091         *
092         * @return null by default
093         */
094        @Nullable
095        protected String getMessageKeyDefaultValue() {
096                return null;
097        }
098}