001package ca.uhn.fhir.jpa.subscription.model;
002
003/*-
004 * #%L
005 * HAPI FHIR Storage api
006 * %%
007 * Copyright (C) 2014 - 2022 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.context.FhirContext;
024import ca.uhn.fhir.interceptor.model.RequestPartitionId;
025import ca.uhn.fhir.parser.IParser;
026import ca.uhn.fhir.rest.api.EncodingEnum;
027import ca.uhn.fhir.rest.server.messaging.BaseResourceMessage;
028import ca.uhn.fhir.rest.server.messaging.IResourceMessage;
029import com.fasterxml.jackson.annotation.JsonIgnore;
030import com.fasterxml.jackson.annotation.JsonProperty;
031import org.apache.commons.lang3.builder.ToStringBuilder;
032import org.hl7.fhir.instance.model.api.IBaseResource;
033import org.hl7.fhir.instance.model.api.IIdType;
034
035import static org.apache.commons.lang3.StringUtils.isNotBlank;
036
037@SuppressWarnings("WeakerAccess")
038public class ResourceDeliveryMessage extends BaseResourceMessage implements IResourceMessage {
039
040        @JsonProperty("canonicalSubscription")
041        private CanonicalSubscription mySubscription;
042        @JsonProperty("partitionId")
043        private RequestPartitionId myPartitionId;
044        @JsonProperty("payload")
045        private String myPayloadString;
046        @JsonProperty("payloadId")
047        private String myPayloadId;
048        @JsonIgnore
049        private transient IBaseResource myPayloadDecoded;
050
051        /**
052         * Constructor
053         */
054        public ResourceDeliveryMessage() {
055                super();
056                myPartitionId = RequestPartitionId.defaultPartition();
057        }
058
059        public IBaseResource getPayload(FhirContext theCtx) {
060                IBaseResource retVal = myPayloadDecoded;
061                if (retVal == null && isNotBlank(myPayloadString)) {
062                        IParser parser = EncodingEnum.detectEncoding(myPayloadString).newParser(theCtx);
063                        retVal = parser.parseResource(myPayloadString);
064                        myPayloadDecoded = retVal;
065                }
066                return retVal;
067        }
068
069        public String getPayloadString() {
070                if (this.myPayloadString != null) {
071                        return this.myPayloadString;
072                }
073
074                return "";
075        }
076
077        public IIdType getPayloadId(FhirContext theCtx) {
078                IIdType retVal = null;
079                if (myPayloadId != null) {
080                        retVal = theCtx.getVersion().newIdType().setValue(myPayloadId);
081                }
082                return retVal;
083        }
084
085        public CanonicalSubscription getSubscription() {
086                return mySubscription;
087        }
088
089        public void setSubscription(CanonicalSubscription theSubscription) {
090                mySubscription = theSubscription;
091        }
092
093        public void setPayload(FhirContext theCtx, IBaseResource thePayload, EncodingEnum theEncoding) {
094                /*
095                 * Note that we populate the raw string but don't keep the parsed resource around when we set this. This
096                 * has two reasons:
097                 *  - If we build up a big queue of these on an in-memory queue, we aren't taking up double the memory
098                 *  - If use a serializing queue, we aren't behaving differently (and therefore possibly missing things
099                 *    in tests)
100                 */
101                myPayloadString = theEncoding.newParser(theCtx).encodeResourceToString(thePayload);
102                myPayloadId = thePayload.getIdElement().toUnqualified().getValue();
103        }
104
105        @Override
106        public String getPayloadId() {
107                return myPayloadId;
108        }
109
110        public void setPayloadId(IIdType thePayloadId) {
111                myPayloadId = null;
112                if (thePayloadId != null) {
113                        myPayloadId = thePayloadId.getValue();
114                }
115        }
116
117        public RequestPartitionId getRequestPartitionId() {
118                return myPartitionId;
119        }
120
121        public void setPartitionId(RequestPartitionId thePartitionId) {
122                myPartitionId = thePartitionId;
123        }
124
125        @Override
126        public String toString() {
127                return new ToStringBuilder(this)
128                        .append("mySubscription", mySubscription)
129                        .append("myPayloadString", myPayloadString)
130                        .append("myPayload", myPayloadDecoded)
131                        .append("myPayloadId", myPayloadId)
132                        .append("myPartitionId", myPartitionId)
133                        .append("myOperationType", getOperationType())
134                        .toString();
135        }
136
137        /**
138         * Helper method to fetch the subscription ID
139         */
140        public String getSubscriptionId(FhirContext theFhirContext) {
141                String retVal = null;
142                if (getSubscription() != null) {
143                        IIdType idElement = getSubscription().getIdElement(theFhirContext);
144                        if (idElement != null) {
145                                retVal = idElement.getValue();
146                        }
147                }
148                return retVal;
149        }
150}