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