001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2024 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.Nullable;
025import org.springframework.messaging.Message;
026import org.springframework.messaging.MessageHeaders;
027
028import static java.util.Objects.isNull;
029import static org.apache.commons.lang3.StringUtils.defaultString;
030
031public abstract class BaseJsonMessage<T> implements Message<T>, IModelJson {
032
033        private static final long serialVersionUID = 1L;
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        public MessageHeaders getHeaders() {
053                return myHeaders.toMessageHeaders();
054        }
055
056        public HapiMessageHeaders getHapiHeaders() {
057                if (isNull(myHeaders)) {
058                        setDefaultRetryHeaders();
059                }
060                return myHeaders;
061        }
062
063        public void setHeaders(HapiMessageHeaders theHeaders) {
064                myHeaders = theHeaders;
065        }
066
067        @Deprecated
068        @Nullable
069        public String getMessageKeyOrNull() {
070                return getMessageKey();
071        }
072
073        @Nullable
074        public String getMessageKey() {
075                return null;
076        }
077
078        /**
079         * Returns {@link #getMessageKey()} or {@link #getMessageKeyDefaultValue()} when {@link #getMessageKey()} returns null.
080         *
081         * @return the message key value or default
082         */
083        @Nullable
084        public String getMessageKeyOrDefault() {
085                return defaultString(getMessageKey(), getMessageKeyDefaultValue());
086        }
087
088        /**
089         * Provides a fallback value when the value returned by {@link #getMessageKey()} is null.
090         *
091         * @return null by default
092         */
093        @Nullable
094        protected String getMessageKeyDefaultValue() {
095                return null;
096        }
097}