001package ca.uhn.fhir.rest.server.messaging;
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
023
024
025import ca.uhn.fhir.model.api.IModelJson;
026import com.fasterxml.jackson.annotation.JsonProperty;
027import org.apache.commons.lang3.Validate;
028
029import javax.annotation.Nullable;
030import java.util.HashMap;
031import java.util.Map;
032import java.util.Optional;
033
034@SuppressWarnings("WeakerAccess")
035public abstract class BaseResourceMessage implements IResourceMessage, IModelJson {
036
037        @JsonProperty("operationType")
038        protected BaseResourceModifiedMessage.OperationTypeEnum myOperationType;
039
040        @JsonProperty("attributes")
041        private Map<String, String> myAttributes;
042
043        @JsonProperty("transactionId")
044        private String myTransactionId;
045
046        @JsonProperty("mediaType")
047        private String myMediaType;
048
049        /**
050         * Returns an attribute stored in this message.
051         * <p>
052         * Attributes are just a spot for user data of any kind to be
053         * added to the message for pasing along the subscription processing
054         * pipeline (typically by interceptors). Values will be carried from the beginning to the end.
055         * </p>
056         * <p>
057         * Note that messages are designed to be passed into queueing systems
058         * and serialized as JSON. As a result, only strings are currently allowed
059         * as values.
060         * </p>
061         */
062        public Optional<String> getAttribute(String theKey) {
063                Validate.notBlank(theKey);
064                if (myAttributes == null) {
065                        return Optional.empty();
066                }
067                return Optional.ofNullable(myAttributes.get(theKey));
068        }
069
070        /**
071         * Sets an attribute stored in this message.
072         * <p>
073         * Attributes are just a spot for user data of any kind to be
074         * added to the message for passing along the subscription processing
075         * pipeline (typically by interceptors). Values will be carried from the beginning to the end.
076         * </p>
077         * <p>
078         * Note that messages are designed to be passed into queueing systems
079         * and serialized as JSON. As a result, only strings are currently allowed
080         * as values.
081         * </p>
082         *
083         * @param theKey   The key (must not be null or blank)
084         * @param theValue The value (must not be null)
085         */
086        public void setAttribute(String theKey, String theValue) {
087                Validate.notBlank(theKey);
088                Validate.notNull(theValue);
089                if (myAttributes == null) {
090                        myAttributes = new HashMap<>();
091                }
092                myAttributes.put(theKey, theValue);
093        }
094
095        /**
096         * Copies any attributes from the given message into this messsage.
097         *
098         * @see #setAttribute(String, String)
099         * @see #getAttribute(String)
100         */
101        public void copyAdditionalPropertiesFrom(BaseResourceMessage theMsg) {
102                if (theMsg.myAttributes != null) {
103                        if (myAttributes == null) {
104                                myAttributes = new HashMap<>();
105                        }
106                        myAttributes.putAll(theMsg.myAttributes);
107                }
108        }
109
110        /**
111         * Returns the {@link OperationTypeEnum} that is occurring to the Resource of the message
112         *
113         * @return the operation type.
114         */
115        public BaseResourceModifiedMessage.OperationTypeEnum getOperationType() {
116                return myOperationType;
117        }
118
119        /**
120         * Sets the {@link OperationTypeEnum} occuring to the resource of the message.
121         *
122         * @param theOperationType The operation type to set.
123         */
124        public void setOperationType(BaseResourceModifiedMessage.OperationTypeEnum theOperationType) {
125                myOperationType = theOperationType;
126        }
127
128        /**
129         * Retrieve the transaction ID related to this message.
130         *
131         * @return the transaction ID, or null.
132         */
133        @Nullable
134        public String getTransactionId() {
135                return myTransactionId;
136        }
137
138        /**
139         * Adds a transaction ID to this message. This ID can be used for many purposes. For example, performing tracing
140         * across asynchronous hooks, tying data together, or downstream logging purposes.
141         *
142         * One current internal implementation uses this field to tie back MDM processing results (which are asynchronous)
143         * to the original transaction log that caused the MDM processing to occur.
144         *
145         * @param theTransactionId An ID representing a transaction of relevance to this message.
146         */
147        public void setTransactionId(String theTransactionId) {
148                myTransactionId = theTransactionId;
149        }
150
151        public String getMediaType() {
152                return myMediaType;
153        }
154
155        public void setMediaType(String theMediaType) {
156                myMediaType = theMediaType;
157        }
158
159        public enum OperationTypeEnum {
160                CREATE,
161                UPDATE,
162                DELETE,
163                MANUALLY_TRIGGERED,
164                TRANSACTION
165        }
166}