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