-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add UCUM support * Add changelog * Some cleanup * Test fix * Add flywayDB callback * Add hooks to schema migrator
- Loading branch information
1 parent
f94f2fd
commit 3d5a8bb
Showing
19 changed files
with
540 additions
and
98 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
hapi-fhir-base/src/main/java/ca/uhn/fhir/util/ClasspathUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package ca.uhn.fhir.util; | ||
|
||
import ca.uhn.fhir.context.FhirContext; | ||
import ca.uhn.fhir.rest.api.EncodingEnum; | ||
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; | ||
import com.google.common.base.Charsets; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.io.input.BOMInputStream; | ||
import org.hl7.fhir.instance.model.api.IBaseResource; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.function.Function; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
/** | ||
* Use this API with caution, it may change! | ||
*/ | ||
public class ClasspathUtil { | ||
|
||
private static final Logger ourLog = LoggerFactory.getLogger(ClasspathUtil.class); | ||
|
||
public static String loadResource(String theClasspath) { | ||
Function<InputStream, InputStream> streamTransform = t -> t; | ||
return loadResource(theClasspath, streamTransform); | ||
} | ||
|
||
/** | ||
* Load a classpath resource, throw an {@link InternalErrorException} if not found | ||
*/ | ||
@Nonnull | ||
public static InputStream loadResourceAsStream(String theClasspath) { | ||
InputStream retVal = ClasspathUtil.class.getResourceAsStream(theClasspath); | ||
if (retVal == null) { | ||
throw new InternalErrorException("Unable to find classpath resource: " + theClasspath); | ||
} | ||
return retVal; | ||
} | ||
|
||
/** | ||
* Load a classpath resource, throw an {@link InternalErrorException} if not found | ||
*/ | ||
@Nonnull | ||
public static String loadResource(String theClasspath, Function<InputStream, InputStream> theStreamTransform) { | ||
InputStream stream = ClasspathUtil.class.getResourceAsStream(theClasspath); | ||
try { | ||
if (stream == null) { | ||
throw new IOException("Unable to find classpath resource: " + theClasspath); | ||
} | ||
try { | ||
InputStream newStream = theStreamTransform.apply(stream); | ||
return IOUtils.toString(newStream, Charsets.UTF_8); | ||
} finally { | ||
stream.close(); | ||
} | ||
} catch (IOException e) { | ||
throw new InternalErrorException(e); | ||
} | ||
} | ||
|
||
@Nonnull | ||
public static String loadCompressedResource(String theClasspath) { | ||
Function<InputStream, InputStream> streamTransform = t -> { | ||
try { | ||
return new GZIPInputStream(t); | ||
} catch (IOException e) { | ||
throw new InternalErrorException(e); | ||
} | ||
}; | ||
return loadResource(theClasspath, streamTransform); | ||
} | ||
|
||
@Nonnull | ||
public static <T extends IBaseResource> T loadResource(FhirContext theCtx, Class<T> theType, String theClasspath) { | ||
String raw = loadResource(theClasspath); | ||
return EncodingEnum.detectEncodingNoDefault(raw).newParser(theCtx).parseResource(theType, raw); | ||
} | ||
|
||
public static void close(InputStream theInput) { | ||
try { | ||
if (theInput != null) { | ||
theInput.close(); | ||
} | ||
} catch (IOException e) { | ||
ourLog.debug("Closing InputStream threw exception", e); | ||
} | ||
} | ||
|
||
public static Function<InputStream, InputStream> withBom() { | ||
return t -> new BOMInputStream(t); | ||
} | ||
|
||
public static byte[] loadResourceAsByteArray(String theClasspath) { | ||
InputStream stream = loadResourceAsStream(theClasspath); | ||
try { | ||
return IOUtils.toByteArray(stream); | ||
} catch (IOException e) { | ||
throw new InternalErrorException(e); | ||
} finally { | ||
close(stream); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
hapi-fhir-base/src/test/java/ca/uhn/fhir/util/ClasspathUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ca.uhn.fhir.util; | ||
|
||
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; | ||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class ClasspathUtilTest { | ||
|
||
@Test | ||
public void testLoadResourceNotFound() { | ||
try { | ||
ClasspathUtil.loadResource("/FOOOOOO"); | ||
} catch (InternalErrorException e) { | ||
assertEquals("Unable to find classpath resource: /FOOOOOO", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLoadResourceAsStreamNotFound() { | ||
try { | ||
ClasspathUtil.loadResourceAsStream("/FOOOOOO"); | ||
} catch (InternalErrorException e) { | ||
assertEquals("Unable to find classpath resource: /FOOOOOO", e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Should not throw any exception | ||
*/ | ||
@Test | ||
public void testClose_Null() { | ||
ClasspathUtil.close(null); | ||
} | ||
|
||
/** | ||
* Should not throw any exception | ||
*/ | ||
@Test | ||
public void testClose_Ok() { | ||
ClasspathUtil.close(new ByteArrayInputStream(new byte[]{0,1,2})); | ||
} | ||
|
||
|
||
/** | ||
* Should not throw any exception | ||
*/ | ||
@Test | ||
public void testClose_ThrowException() throws IOException { | ||
InputStream is = mock(InputStream.class); | ||
doThrow(new IOException("FOO")).when(is).close(); | ||
ClasspathUtil.close(is); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/4_3_0/1824-add-ucum-support.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
type: add | ||
issue: 1824 | ||
title: Native support for UCUM has been added to the validation stack, meaning that UCUM codes can be validated | ||
at runtime without the need for any external validation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.