001package ca.uhn.fhir.util;
002
003/*-
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2020 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.FhirContext;
024import ca.uhn.fhir.rest.api.EncodingEnum;
025import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
026import com.google.common.base.Charsets;
027import org.apache.commons.io.IOUtils;
028import org.apache.commons.io.input.BOMInputStream;
029import org.hl7.fhir.instance.model.api.IBaseResource;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033import javax.annotation.Nonnull;
034import java.io.IOException;
035import java.io.InputStream;
036import java.util.function.Function;
037import java.util.zip.GZIPInputStream;
038
039/**
040 * Use this API with caution, it may change!
041 */
042public class ClasspathUtil {
043
044        private static final Logger ourLog = LoggerFactory.getLogger(ClasspathUtil.class);
045
046        public static String loadResource(String theClasspath) {
047                Function<InputStream, InputStream> streamTransform = t -> t;
048                return loadResource(theClasspath, streamTransform);
049        }
050
051        /**
052         * Load a classpath resource, throw an {@link InternalErrorException} if not found
053         */
054        @Nonnull
055        public static InputStream loadResourceAsStream(String theClasspath) {
056                InputStream retVal = ClasspathUtil.class.getResourceAsStream(theClasspath);
057                if (retVal == null) {
058                        throw new InternalErrorException("Unable to find classpath resource: " + theClasspath);
059                }
060                return retVal;
061        }
062
063        /**
064         * Load a classpath resource, throw an {@link InternalErrorException} if not found
065         */
066        @Nonnull
067        public static String loadResource(String theClasspath, Function<InputStream, InputStream> theStreamTransform) {
068                InputStream stream = ClasspathUtil.class.getResourceAsStream(theClasspath);
069                try {
070                        if (stream == null) {
071                                throw new IOException("Unable to find classpath resource: " + theClasspath);
072                        }
073                        try {
074                                InputStream newStream = theStreamTransform.apply(stream);
075                                return IOUtils.toString(newStream, Charsets.UTF_8);
076                        } finally {
077                                stream.close();
078                        }
079                } catch (IOException e) {
080                        throw new InternalErrorException(e);
081                }
082        }
083
084        @Nonnull
085        public static String loadCompressedResource(String theClasspath) {
086                Function<InputStream, InputStream> streamTransform = t -> {
087                        try {
088                                return new GZIPInputStream(t);
089                        } catch (IOException e) {
090                                throw new InternalErrorException(e);
091                        }
092                };
093                return loadResource(theClasspath, streamTransform);
094        }
095
096        @Nonnull
097        public static <T extends IBaseResource> T loadResource(FhirContext theCtx, Class<T> theType, String theClasspath) {
098                String raw = loadResource(theClasspath);
099                return EncodingEnum.detectEncodingNoDefault(raw).newParser(theCtx).parseResource(theType, raw);
100        }
101
102        public static void close(InputStream theInput) {
103                try {
104                        if (theInput != null) {
105                                theInput.close();
106                        }
107                } catch (IOException e) {
108                        ourLog.debug("Closing InputStream threw exception", e);
109                }
110        }
111
112        public static Function<InputStream, InputStream> withBom() {
113                return t -> new BOMInputStream(t);
114        }
115
116        public static byte[] loadResourceAsByteArray(String theClasspath) {
117                InputStream stream = loadResourceAsStream(theClasspath);
118                try {
119                        return IOUtils.toByteArray(stream);
120                } catch (IOException e) {
121                        throw new InternalErrorException(e);
122                } finally {
123                        close(stream);
124                }
125        }
126}