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.smart; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; import java.util.Map; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Returns data from an imbedded resource to SMART api request. */ public class SmartAPIResource extends SmartAPIBase { private static final Log log = LogFactory.getLog(SmartAPIResource.class); private final String resourcePath; /** * Creates an API adapter with the specified api matching pattern and resource path. * * @param pattern API matching pattern. * @param resourcePath Path to resource. */ public SmartAPIResource(String pattern, String resourcePath) { this(pattern, resourcePath, null); } /** * Creates an API adapter with the specified api matching pattern and resource path. * * @param pattern API matching pattern. * @param resourcePath Path to resource. * @param capability Optional capability */ public SmartAPIResource(String pattern, String resourcePath, String capability) { super(pattern, ContentType.RDF, capability); this.resourcePath = resourcePath; } /** * Returns the content of the imbedded resource to the caller. */ @Override public String handleAPI(Map<String, String> params) { InputStream is = SmartAPIResource.class.getClassLoader().getResourceAsStream(resourcePath); StringWriter sw = new StringWriter(); try { IOUtils.copy(is, sw); sw.flush(); is.close(); } catch (IOException e) { log.error("Error retrieving SMART API resource.", e); throw new RuntimeException(e); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(sw); } return sw.toString(); } }