001package ca.uhn.fhir.jpa.subscription.module.subscriber; 002 003/*- 004 * #%L 005 * HAPI FHIR Subscription Server 006 * %% 007 * Copyright (C) 2014 - 2020 University Health Network 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 com.fasterxml.jackson.annotation.JsonAutoDetect; 024import com.fasterxml.jackson.annotation.JsonInclude; 025import com.fasterxml.jackson.annotation.JsonProperty; 026import org.apache.commons.lang3.Validate; 027 028import java.util.HashMap; 029import java.util.Map; 030import java.util.Optional; 031 032@SuppressWarnings("WeakerAccess") 033@JsonInclude(JsonInclude.Include.NON_NULL) 034@JsonAutoDetect(creatorVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, getterVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE, setterVisibility = JsonAutoDetect.Visibility.NONE) 035public abstract class BaseResourceMessage implements IResourceMessage { 036 037 @JsonProperty("attributes") 038 private Map<String, String> myAttributes; 039 040 /** 041 * Returns an attribute stored in this message. 042 * <p> 043 * Attributes are just a spot for user data of any kind to be 044 * added to the message for pasing along the subscription processing 045 * pipeline (typically by interceptors). Values will be carried from the beginning to the end. 046 * </p> 047 * <p> 048 * Note that messages are designed to be passed into queueing systems 049 * and serialized as JSON. As a result, only strings are currently allowed 050 * as values. 051 * </p> 052 */ 053 public Optional<String> getAttribute(String theKey) { 054 Validate.notBlank(theKey); 055 if (myAttributes == null) { 056 return Optional.empty(); 057 } 058 return Optional.ofNullable(myAttributes.get(theKey)); 059 } 060 061 /** 062 * Sets an attribute stored in this message. 063 * <p> 064 * Attributes are just a spot for user data of any kind to be 065 * added to the message for passing along the subscription processing 066 * pipeline (typically by interceptors). Values will be carried from the beginning to the end. 067 * </p> 068 * <p> 069 * Note that messages are designed to be passed into queueing systems 070 * and serialized as JSON. As a result, only strings are currently allowed 071 * as values. 072 * </p> 073 * 074 * @param theKey The key (must not be null or blank) 075 * @param theValue The value (must not be null) 076 */ 077 public void setAttribute(String theKey, String theValue) { 078 Validate.notBlank(theKey); 079 Validate.notNull(theValue); 080 if (myAttributes == null) { 081 myAttributes = new HashMap<>(); 082 } 083 myAttributes.put(theKey, theValue); 084 } 085 086 /** 087 * Copies any attributes from the given message into this messsage. 088 * 089 * @see #setAttribute(String, String) 090 * @see #getAttribute(String) 091 */ 092 public void copyAdditionalPropertiesFrom(BaseResourceMessage theMsg) { 093 if (theMsg.myAttributes != null) { 094 if (myAttributes == null) { 095 myAttributes = new HashMap<>(); 096 } 097 myAttributes.putAll(theMsg.myAttributes); 098 } 099 } 100}