Example usage for org.springframework.core.io Resource getInputStream

List of usage examples for org.springframework.core.io Resource getInputStream

Introduction

In this page you can find the example usage for org.springframework.core.io Resource getInputStream.

Prototype

InputStream getInputStream() throws IOException;

Source Link

Document

Return an InputStream for the content of an underlying resource.

Usage

From source file:corner.orm.spring.CornerPropertiesPersister.java

/**
 * resoure???/* www.  j a  va 2  s .c o  m*/
 * @param props ?
 * @param location ?
 * @throws IOException ?
 */
private void loadImportConfigFile(Properties props, Resource location) throws IOException {
    InputStream is = null;
    try {
        is = location.getInputStream();
        if (location.getFilename().endsWith(PropertiesLoaderSupport.XML_FILE_EXTENSION)) {
            this.loadFromXml(props, is);
        } else {

            super.load(props, is);

        }
    } catch (IOException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Could not load properties from " + location + ": " + ex.getMessage());
        }

    } finally {
        if (is != null) {
            is.close();
        }
    }

}

From source file:com.mitchellbosecke.pebble.spring.PebbleTemplateLoader.java

@Override
public Reader getReader(String resourceName) throws LoaderException {
    resourceName = getFullyQualifiedResourceName(resourceName);
    Resource resource = resourceLoader.getResource(resourceName);
    if (resource.exists()) {
        try {/*w w w.ja v a  2 s .  c  om*/
            return new InputStreamReader(resource.getInputStream(), charset);
        } catch (IOException e) {
            throw new LoaderException(e, "Failed to load template: " + resourceName);
        }
    }
    throw new LoaderException(null, "No template exists named: " + resourceName);
}

From source file:com.ullgren.pontus.mule.module.cb2java.Copybook2JavaExtensionModule.java

/**
 * Custom processor//from w  ww. ja va  2s. co m
 *
 * {@sample.xml ../../../doc/Copybook2JavaExtension-connector.xml.sample copybook2javaextension:copybook-to-record}
 *
 * @param content Content to be processed
 * @return Some string
 * @throws IOException 
 */
@Processor
public List<Record> copybookToRecord(@Payload final InputStream input) throws IOException {
    Resource resource = new ClassPathResource(getCopybook());
    // Parse copybook
    Copybook copybook = CopybookParser.parse(this.copybookName, resource.getInputStream());
    // Parse data
    List<Record> results = copybook.parseData(input);
    return results;
}

From source file:com.jrzmq.core.utils.PropertiesUtil.java

/**
 * , Spring Resource?.//from  w  w  w  . j  a v  a 2s .  c  om
 */
private Properties loadProperties(String... resourcesPaths) {
    Properties props = new Properties();

    for (String location : resourcesPaths) {

        logger.debug("Loading properties file from path:{}", location);

        InputStream is = null;
        try {
            Resource resource = resourceLoader.getResource(location);
            is = resource.getInputStream();

            propertiesPersister.load(props, new InputStreamReader(is, "UTF-8"));
        } catch (IOException ex) {
            logger.info("Could not load properties from path:{}, {} ", location, ex.getMessage());
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return props;
}

From source file:org.carewebframework.smart.ui.SmartLocator.java

/**
 * Synthesizes a plugin definition from a SMART manifest resource.
 *
 * @param resource A resource that represents a SMART manifest in JSON format.
 * @return A plugin definition./*ww w .  ja  va2s  .  c  om*/
 */
private PluginDefinition toDefinition(Resource resource) {
    try {
        SmartManifest manifest = SmartContainerServices.manifestFromStream(resource.getInputStream());

        if (!"ui".equals(manifest.getValue("mode"))) {
            return null;
        }

        PluginDefinition definition = new PluginDefinition();
        String name = manifest.getValue("name");
        definition.setId(StrUtil.xlate(manifest.getValue("id"), " @", "__"));
        definition.setName(name);
        definition.setUrl(manifest.getValue("index"));
        definition.setClazz(SmartPlugin.class);
        definition.setDescription(manifest.getValue("description"));
        definition.setSource("SMART Platform");
        definition.setCreator(manifest.getValue("author"));
        definition.setVersion(manifest.getValue("version"));
        definition.setIcon(manifest.getValue("icon"));
        definition.setCategory("SMART apps");
        definition.getResources().add(new SmartResource(manifest));
        log.info("Found SMART Manifest for " + name);
        return definition;
    } catch (Exception e) {
        log.error("Error loading SMART manifest: " + resource, e);
    }

    return null;
}

From source file:com.dec.common.utils.PropertiesLoader.java

/**
 * , Spring Resource.//from  w w  w.j a v a2 s  . c  o  m
 */
private Properties loadProperties(String... resourcesPaths) {
    Properties props = new Properties();

    for (String location : resourcesPaths) {

        // logger.debug("Loading properties file from:" + location);

        InputStream is = null;
        try {
            Resource resource = resourceLoader.getResource(location);
            is = resource.getInputStream();
            props.load(is);
        } catch (IOException ex) {
            logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return props;
}

From source file:com.vilt.minium.jasmine.MiniumJasmineTestRunner.java

protected Object parseJson(RhinoContext rhinoContext, Resource resource) throws ParseException, IOException {
    String json = IOUtils.toString(resource.getInputStream());
    return new JsonParser(rhinoContext.getJsContext(), rhinoContext.getJsScope()).parseValue(json);
}

From source file:com.fns.grivet.service.EntityServiceTest.java

@Test
public void testCreateThenFindByType() throws IOException {
    registerType("TestType");
    Resource r = resolver.getResource("classpath:TestTypeData.json");
    String json = IOUtils.toString(r.getInputStream(), Charset.defaultCharset());
    JSONObject payload = new JSONObject(json);

    entityService.create("TestType", payload);

    String result = entityService.findByCreatedTime("TestType", LocalDateTime.now().minusSeconds(3),
            LocalDateTime.now(), null);
    JSONArray resultAsJsonArray = new JSONArray(result);
    JsonAssert.assertJsonEquals(payload.toString(), resultAsJsonArray.get(0).toString());
}

From source file:org.muhia.app.psi.integ.config.ke.shared.SharedWsClientConfiguration.java

@Bean(name = "sharedSecureHttpClient")
public CloseableHttpClient secureHttpClient() {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    try {//from   www.j av a2  s  .  c o m
        /*
        TODO: Modify to accept only specific certificates, test implementation is as below,
        TODO: need to find a way of determining if server url is https or not
        TODO: Whether we have imported the certificate or not
        */
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        Resource resource = loaderService.getResource(properties.getSharedKeystorePath());
        keyStore.load(resource.getInputStream(),
                hasher.getDecryptedValue(properties.getSharedKeystorePassword()).toCharArray());
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
                .loadKeyMaterial(keyStore,
                        hasher.getDecryptedValue(properties.getSharedKeystorePassword()).toCharArray())
                .build();
        //            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();

        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout(properties.getSharedTransportConnectionTimeout())
                .setConnectionRequestTimeout(properties.getSharedTransportConnectionRequestTimeout())
                .setSocketTimeout(properties.getSharedTransportReadTimeout()).build();
        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
                sharedDataDTO.getTransportUsername(), sharedDataDTO.getTransportPassword());
        provider.setCredentials(AuthScope.ANY, credentials);

        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
        connManager.setMaxTotal(properties.getSharedPoolMaxHost());
        connManager.setDefaultMaxPerRoute(properties.getSharedPoolDefaultmaxPerhost());
        connManager.setValidateAfterInactivity(properties.getSharedPoolValidateAfterInactivity());
        httpClient = HttpClientBuilder.create().setSSLContext(sslContext)
                .setSSLHostnameVerifier(new NoopHostnameVerifier()).setDefaultRequestConfig(config)
                .setDefaultCredentialsProvider(provider).setConnectionManager(connManager)
                .evictExpiredConnections().addInterceptorFirst(new RemoveHttpHeadersInterceptor()).build();
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException | CertificateException
            | IOException | UnrecoverableKeyException e) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, e.getMessage(), e);
    }
    return httpClient;

}

From source file:com.fns.grivet.service.EntityServiceTest.java

@Test
public void testCreateThenFindByTypeVariant() throws IOException {
    registerType("TestType2");
    Resource r = resolver.getResource("classpath:TestTypeData2.json");
    String json = IOUtils.toString(r.getInputStream(), Charset.defaultCharset());
    JSONObject payload = new JSONObject(json);

    entityService.create("TestType2", payload);

    String result = entityService.findByCreatedTime("TestType2", LocalDateTime.now().minusSeconds(3),
            LocalDateTime.now(), null);
    JSONArray resultAsJsonArray = new JSONArray(result);
    JsonAssert.assertJsonEquals(payload.toString(), resultAsJsonArray.get(0).toString());
}