Java tutorial
/** * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. * If a copy of the MPL was not distributed with this file, You can obtain one at * http://mozilla.org/MPL/2.0/. * * This Source Code Form is also subject to the terms of the Health-Related Additional * Disclaimer of Warranty and Limitation of Liability available at * http://www.carewebframework.org/licensing/disclaimer. */ package org.carewebframework.cal.api; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.UriDt; import ca.uhn.fhir.rest.client.GenericClient; import org.apache.http.impl.client.CloseableHttpClient; import org.carewebframework.api.spring.SpringUtil; import org.carewebframework.fhir.client.FhirContext; import org.carewebframework.fhir.client.HttpClientProxy; /** * FHIR client utility methods. */ public class ClientUtil { private static GenericClient fhirClient; public static FhirContext getFhirContext() { return SpringUtil.getBean("fhirContext", FhirContext.class); } public static void registerHttpClient(String pattern, CloseableHttpClient client) { ((HttpClientProxy) getFhirContext().getRestfulClientFactory().getHttpClient()).registerHttpClient(pattern, client); } public static GenericClient getFhirClient() { if (fhirClient == null) { fhirClient = SpringUtil.getBean("fhirClient", GenericClient.class); } return fhirClient; } /** * Returns the default FHIR service root url. * * @return Default FHIR service root url. */ public static String getServiceRoot() { return getFhirClient().getUrlBase(); } /** * For urls without a service root, prepends the default service root. * * @param url URL to expand. * @return URL with a service root prepended. */ public static String expandURL(String url) { return url.matches("^.+:/") ? url : getServiceRoot() + url; } /** * Returns a resource of the specified type given a resource reference. If the resource has not * been previously fetched, it will be fetched from the server. If the referenced resource is * not of the specified type, null is returned. * * @param reference A resource reference. * @param clazz The desired resource class. * @return The corresponding resource. */ @SuppressWarnings("unchecked") public static <T extends IResource> T getResource(ResourceReferenceDt reference, Class<T> clazz) { IResource resource = getResource(reference); return clazz.isInstance(resource) ? (T) resource : null; } /** * Returns a resource given a resource reference. If the resource has not been previously * fetched, it will be fetched from the server. * * @param reference A resource reference. * @return The corresponding resource. */ public static IResource getResource(ResourceReferenceDt reference) { if (reference.isEmpty()) { return null; } if (reference.getResource() != null) { return reference.getResource(); } IdDt resourceId = reference.getReference(); if (resourceId == null) { throw new IllegalStateException("Reference has no resource ID defined"); } String resourceUrl = resourceId.getValue(); if (!resourceUrl.startsWith("http")) { resourceUrl = getServiceRoot() + resourceUrl; } IResource resource = getFhirClient().read(new UriDt(resourceUrl)); reference.setResource(resource); return resource; } /** * Enforce static class. */ private ClientUtil() { }; }