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:fr.insalyon.creatis.vip.api.rest.config.BaseVIPSpringIT.java

protected String getResourceAsString(String pathFromClasspath) throws IOException {
    Resource resource = resourceLoader.getResource("classpath:" + pathFromClasspath);
    return IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8);
}

From source file:com.nerve.commons.repository.tools.PropertiesLoader.java

/**
 * properties, ???.//ww w  . j  ava  2  s .com
 * Spring Resource?, ?UTF-8.
 *
 * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
 */
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 {
            if (is != null)
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    return props;
}

From source file:org.jasig.portlet.widget.service.GoogleGadgetServiceTest.java

@Test
public void testListGadgetsForCategory() throws IOException {

    Resource categoryPage = context
            .getResource("org/jasig/portlet/widget/service/gadgetListingFinanceCategory.html");
    doReturn(categoryPage.getInputStream()).when(service).getStreamFromUrl(anyString());

    GadgetList list = service.getGadgets(null, "finance", 0);
    assertEquals(24, list.getGadgets().size());
    assertEquals(1813, list.getTotal());

    GadgetEntry first = list.getGadgets().get(0);
    assertEquals("Stock Charts", first.getName());
    assertEquals("http://igoogle.stockmarketstudio.com/StockCharts/thumbnail.png", first.getImageUrl());
    assertEquals("http://hosting.gmodules.com/ig/gadgets/file/113570023379904426818/StockCharts.xml",
            first.getConfigUrl());/* w ww  .j av  a 2 s. c  o  m*/

}

From source file:com.jaxio.celerio.Brand.java

public Brand() {
    brandingPath = System.getProperty("user.home") + "/.celerio/branding.properties";
    logoPath = System.getProperty("user.home") + "/.celerio/" + logoFilename;
    try {/*from w  w w  . j av a  2 s  .  c o  m*/

        Properties p = new Properties();
        File f = new File(brandingPath);
        if (f.exists()) {
            p.load(new FileInputStream(f));
            companyName = p.getProperty("company_name", companyName);
            companyUrl = p.getProperty("company_url", companyUrl);
            footer = p.getProperty("footer", footer);
            rootPackage = p.getProperty("root_package", rootPackage);
        } else {
            p.setProperty("root_package", rootPackage);
            p.setProperty("company_name", companyName);
            p.setProperty("company_url", companyUrl);
            p.setProperty("footer", footer);
            if (!f.getParentFile().exists()) {
                f.getParentFile().mkdirs();
            }
            p.store(new FileOutputStream(f), "CELERIO BRANDING");
        }

        // copy logo if not present
        File logo = new File(logoPath);
        if (!logo.exists()) {
            PathMatchingResourcePatternResolver o = new PathMatchingResourcePatternResolver();
            Resource defaultBrandLogo = o.getResource("classpath:/brand-logo.png");
            new FileOutputStream(logo).write(IOUtils.toByteArray(defaultBrandLogo.getInputStream()));
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

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

@Test
public void testSchemaLinkAndValidationSuccessThenUnlink() throws IOException {
    registerType("TestType");
    Resource r = resolver.getResource("classpath:TestTypeSchema.json");
    String jsonSchema = IOUtils.toString(r.getInputStream(), Charset.defaultCharset());
    JSONObject schemaObj = new JSONObject(jsonSchema);
    com.fns.grivet.model.Class c = schemaService.linkSchema(schemaObj);
    String type = c.getName();/*from   ww  w  . j a va 2 s. c om*/
    Assertions.assertEquals("TestType", type);
    Assertions.assertTrue(c.isValidatable());
    JsonAssert.assertJsonEquals(c.getJsonSchema(), jsonSchema);

    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());

    c = schemaService.unlinkSchema(type);
    Assertions.assertFalse(c.isValidatable());
    Assertions.assertNull(c.getJsonSchema());
}

From source file:com.zaubersoftware.mule.module.jenkins.template.velocity.VelocityTemplate.java

/**
 * Creates the VelocityTemplate.//from   w  w  w . j a  v  a 2 s.  c o  m
 *
 */
public VelocityTemplate(final Resource content, final String charset) {
    Validate.notNull(content);
    Validate.notNull(charset);

    InputStream is = null;
    try {
        is = content.getInputStream();
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        IOUtils.copy(is, os);
        os.close();
        message = os.toString(charset);
    } catch (final IOException e) {
        throw new UnhandledException(e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.springmodules.cache.config.CacheManagerAndProviderFacadeParserTests.java

private String getResourceContent(Resource resource) throws Exception {
    InputStream inputStream = resource.getInputStream();

    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    for (int n; (n = inputStream.read(b)) != -1;) {
        out.append(new String(b, 0, n));
    }/*from   w  ww . j  av a 2  s  . c  o m*/
    return out.toString();
}

From source file:org.codehaus.griffon.commons.DefaultGriffonContext.java

protected Map<Object, Object> loadMetadata() {
    final Properties meta = new Properties();
    Resource r = new ClassPathResource(PROJECT_META_FILE, getClassLoader());
    try {//w  w w.j  ava 2  s .  c  o  m
        meta.load(r.getInputStream());
    } catch (IOException e) {
        StackTraceUtils.deepSanitize(e);
        log.warn("No application metadata file found at " + r);
    }
    if (System.getProperty(Environment.KEY) != null) {
        meta.setProperty(Environment.KEY, System.getProperty(Environment.KEY));
    }
    return Collections.unmodifiableMap(meta);
}

From source file:de.iteratec.iteraplan.xmi.XmiImport.java

/**
 * Imports the initial XMI data.//from ww w.  j  a va  2s  .  c  om
 * 
 * @param importAttributeTypes flag whether basic attribute types should be imported or not
 * @param initializeHistoryData Set to true if history base data should be initialized as well
 * @throws IOException if the {@code iteraplanData.xmi} file will be not found
 */
public void importInitialData(boolean importAttributeTypes, HistoryInitialization initializeHistoryData)
        throws IOException {
    LOGGER.info("Start Initial Data Import");

    ConfigurableApplicationContext context = prepareEnvironment(initializeHistoryData);
    try {
        XmiImportService xmiDeserializer = context.getBean(XmiImportService.class);

        Resource importFile = new ClassPathResource("de/iteratec/iteraplan/xmi/iteraplanData.xmi");
        xmiDeserializer.importInitialXmi(importFile.getInputStream(), importAttributeTypes);

        LOGGER.info("XMI data imported successfuly");
    } finally {
        context.close();
    }
}

From source file:de.tudarmstadt.ukp.csniper.webapp.analysis.ExamplesRepository.java

public String getExample(String aTool, String aKey) {
    Resource res = getExamplesForTool(aTool).get(aKey);
    if (res == null) {
        return "NO SCRIPT FOUND";
    } else {//  ww  w. j a va  2  s .  c  o  m
        try {
            return IOUtils.toString(res.getInputStream(), "UTF-8");
        } catch (IOException e) {
            return "ERROR LOADING SCRIPT: " + e.getMessage();
        }
    }
}