001package ca.uhn.fhir.rest.server.messaging.json;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.model.api.IModelJson;
024import com.fasterxml.jackson.annotation.JsonProperty;
025import org.springframework.messaging.MessageHeaders;
026
027import java.util.HashMap;
028import java.util.Map;
029
030/**
031 * This class is for holding headers for BaseJsonMessages. Any serializable data can be thrown into
032 * the header map. There are also three special headers, defined by the constants in this class, which are for use
033 * in message handling retrying. There are also matching helper functions for fetching those special variables; however
034 * they can also be accessed in standard map fashion with a `get` on the map.
035 */
036public class HapiMessageHeaders implements IModelJson {
037        public static final String RETRY_COUNT_KEY = "retryCount";
038        public static final String FIRST_FAILURE_KEY = "firstFailureTimestamp";
039        public static final String LAST_FAILURE_KEY = "lastFailureTimestamp";
040
041        @JsonProperty(RETRY_COUNT_KEY)
042        private Integer myRetryCount = 0;
043        @JsonProperty(FIRST_FAILURE_KEY)
044        private Long myFirstFailureTimestamp;
045        @JsonProperty(LAST_FAILURE_KEY)
046        private Long myLastFailureTimestamp;
047
048        @JsonProperty("customHeaders")
049        private final Map<String, Object> headers;
050
051        public HapiMessageHeaders(Map<String, Object> theHeaders) {
052                headers = theHeaders;
053        }
054
055        public HapiMessageHeaders() {
056                headers = new HashMap<>();
057        }
058
059        public Integer getRetryCount() {
060                return this.myRetryCount;
061        }
062
063        public Long getFirstFailureTimestamp() {
064                return this.myFirstFailureTimestamp;
065        }
066
067        public Long getLastFailureTimestamp() {
068                return this.myLastFailureTimestamp;
069        }
070
071        public void setRetryCount(Integer theRetryCount) {
072                this.myRetryCount = theRetryCount;
073        }
074
075        public void setLastFailureTimestamp(Long theLastFailureTimestamp) {
076                this.myLastFailureTimestamp = theLastFailureTimestamp;
077        }
078
079
080        public void setFirstFailureTimestamp(Long theFirstFailureTimestamp) {
081                this.myFirstFailureTimestamp = theFirstFailureTimestamp;
082        }
083
084
085        public Map<String, Object> getCustomHeaders() {
086                return this.headers;
087        }
088
089        public MessageHeaders toMessageHeaders() {
090                Map<String, Object> returnedHeaders = new HashMap<>(this.headers);
091                returnedHeaders.put(RETRY_COUNT_KEY, myRetryCount);
092                returnedHeaders.put(FIRST_FAILURE_KEY, myFirstFailureTimestamp);
093                returnedHeaders.put(LAST_FAILURE_KEY, myLastFailureTimestamp);
094                return new MessageHeaders(returnedHeaders);
095        }
096
097}