001package ca.uhn.fhir.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2023 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.BaseRuntimeChildDefinition;
024import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
025import ca.uhn.fhir.context.FhirContext;
026import ca.uhn.fhir.context.FhirVersionEnum;
027import ca.uhn.fhir.context.RuntimeResourceDefinition;
028import org.hl7.fhir.instance.model.api.IBase;
029import org.hl7.fhir.instance.model.api.IBaseBinary;
030import org.hl7.fhir.instance.model.api.IBaseReference;
031import org.hl7.fhir.instance.model.api.IPrimitiveType;
032
033import java.util.List;
034
035public class BinaryUtil {
036
037        private BinaryUtil() {
038                // non instantiable
039        }
040
041        /**
042         * Fetches the base64Binary value of Binary.data (or Binary.content on versions of
043         * FHIR before R4), creating it if it does not already exist.
044         */
045        @SuppressWarnings("unchecked")
046        public static IPrimitiveType<byte[]> getOrCreateData(FhirContext theContext, IBaseBinary theBinary) {
047                String elementName = "content";
048                if (theContext.getVersion().getVersion().isEqualOrNewerThan(FhirVersionEnum.R4)) {
049                        elementName = "data";
050                }
051
052                BaseRuntimeChildDefinition entryChild = AttachmentUtil.getChild(theContext, theBinary, elementName);
053                List<IBase> entries = entryChild.getAccessor().getValues(theBinary);
054                return entries
055                        .stream()
056                        .map(t -> (IPrimitiveType<byte[]>) t)
057                        .findFirst()
058                        .orElseGet(() -> {
059                                IPrimitiveType<byte[]> binary = AttachmentUtil.newPrimitive(theContext, "base64Binary", null);
060                                entryChild.getMutator().setValue(theBinary, binary);
061                                return binary;
062                        });
063        }
064
065
066        public static IBaseReference getSecurityContext(FhirContext theCtx, IBaseBinary theBinary) {
067                RuntimeResourceDefinition def = theCtx.getResourceDefinition("Binary");
068                BaseRuntimeChildDefinition child = def.getChildByName("securityContext");
069                IBaseReference retVal = null;
070                if (child != null) {
071                        List<IBase> values = child.getAccessor().getValues(theBinary);
072                        if (values.size() > 0) {
073                                retVal = (IBaseReference) values.get(0);
074                        }
075                }
076                return retVal;
077        }
078
079        public static IBaseBinary newBinary(FhirContext theCtx) {
080                return (IBaseBinary) theCtx.getResourceDefinition("Binary").newInstance();
081        }
082
083        public static void setSecurityContext(FhirContext theCtx, IBaseBinary theBinary, String theSecurityContext) {
084                RuntimeResourceDefinition def = theCtx.getResourceDefinition("Binary");
085                BaseRuntimeChildDefinition child = def.getChildByName("securityContext");
086
087                BaseRuntimeElementDefinition<?> referenceDef = theCtx.getElementDefinition("reference");
088                IBaseReference reference = (IBaseReference) referenceDef.newInstance();
089                child.getMutator().addValue(theBinary, reference);
090
091                reference.setReference(theSecurityContext);
092        }
093
094        public static void setData(FhirContext theCtx, IBaseBinary theBinary, byte[] theBytes, String theContentType) {
095                getOrCreateData(theCtx, theBinary).setValue(theBytes);
096
097                String elementName = "contentType";
098                BaseRuntimeChildDefinition entryChild = AttachmentUtil.getChild(theCtx, theBinary, elementName);
099                List<IBase> entries = entryChild.getAccessor().getValues(theBinary);
100                IPrimitiveType<String> contentTypeElement = entries
101                        .stream()
102                        .map(t -> (IPrimitiveType<String>) t)
103                        .findFirst()
104                        .orElseGet(() -> {
105                                IPrimitiveType<String> stringType = AttachmentUtil.newPrimitive(theCtx, "code", null);
106                                entryChild.getMutator().setValue(theBinary, stringType);
107                                return stringType;
108                        });
109                contentTypeElement.setValue(theContentType);
110
111        }
112}