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:uk.ac.kcl.rowmappers.DocumentRowMapper.java

private void mapDBFields(Document doc, ResultSet rs) throws SQLException, IOException {
    //add additional query fields for ES export
    ResultSetMetaData meta = rs.getMetaData();

    int colCount = meta.getColumnCount();

    for (int col = 1; col <= colCount; col++) {
        Object value = rs.getObject(col);
        if (value != null) {
            String colLabel = meta.getColumnLabel(col).toLowerCase();
            if (!fieldsToIgnore.contains(colLabel)) {
                DateTime dateTime;/*w w w  .  j  av a2s  .c  om*/
                //map correct SQL time types
                switch (meta.getColumnType(col)) {
                case 91:
                    Date dt = (Date) value;
                    dateTime = new DateTime(dt.getTime());
                    doc.getAssociativeArray().put(meta.getColumnLabel(col).toLowerCase(),
                            eSCompatibleDateTimeFormatter.print(dateTime));
                    break;
                case 93:
                    Timestamp ts = (Timestamp) value;
                    dateTime = new DateTime(ts.getTime());
                    doc.getAssociativeArray().put(meta.getColumnLabel(col).toLowerCase(),
                            eSCompatibleDateTimeFormatter.print(dateTime));
                    break;
                default:
                    doc.getAssociativeArray().put(meta.getColumnLabel(col).toLowerCase(), rs.getString(col));
                    break;
                }
            }
        }

        //map binary content from FS or database if required (as per docman reader)
        if (value != null && meta.getColumnLabel(col).equalsIgnoreCase(binaryContentFieldName)) {
            switch (binaryContentSource) {
            case "database":
                doc.setBinaryContent(rs.getBytes(col));
                break;
            case "fileSystemWithDBPath":
                Resource resource = context.getResource(pathPrefix + rs.getString(col));
                doc.setBinaryContent(IOUtils.toByteArray(resource.getInputStream()));
                break;
            default:
                break;
            }
        }
    }
}

From source file:com.github.yihtserns.spring.groovy.GroovyBeanDefinitionReader.java

@Override
public int loadBeanDefinitions(final Resource resource) throws BeanDefinitionStoreException {
    final Map<String, Object> vars = new HashMap<String, Object>();

    try {/* w  ww.j  a v  a2 s. c  om*/
        final Script script = new GroovyShell().parse(new InputStreamReader(resource.getInputStream()));
        script.setBinding(new Binding(vars));

        GroovyCategorySupport.use(SpringCategory.class, new Closure(null) {

            public void doCall() throws IOException {
                script.run();
            }
        });

        BeanDefinitionRegistry registry = getRegistry();
        int beanCount = 0;
        for (Map.Entry<String, Object> entry : vars.entrySet()) {
            String varName = entry.getKey();
            Object varValue = entry.getValue();

            if (varValue instanceof BeanDefinition) {
                beanCount++;
                registry.registerBeanDefinition(varName, (BeanDefinition) varValue);
            }
        }

        return beanCount;
    } catch (CompilationFailedException ex) {
        throw new BeanDefinitionStoreException("Unable to load Groovy script", ex);
    } catch (IOException ex) {
        throw new BeanDefinitionStoreException("Unable to load resource", ex);
    }
}

From source file:com.greglturnquist.HomeController.java

private ResponseEntity<?> getRawImage() {
    try {//from  ww w.ja  va 2s . c o m
        Resource file = resourceLoader.getResource("file:upload-dir/keep-calm-and-learn-javascript.jpg");
        return ResponseEntity.ok().contentLength(file.contentLength()).contentType(MediaType.IMAGE_JPEG)
                .body(new InputStreamResource(file.getInputStream()));
    } catch (IOException e) {
        return ResponseEntity.badRequest().body("Couldn't find it => " + e.getMessage());
    }
}

From source file:at.gv.egiz.bku.spring.ConfigurationFactoryBean.java

protected Configuration getVersionConfiguration() throws IOException {

    Map<String, Object> map = new HashMap<String, Object>();
    map.put(MOCCA_IMPLEMENTATIONNAME_PROPERTY, "MOCCA");

    // implementation version
    String version = null;/*from  w  w w  .j  a v  a  2 s  .co  m*/
    try {
        Resource resource = resourceLoader.getResource("META-INF/MANIFEST.MF");
        Manifest properties = new Manifest(resource.getInputStream());
        Attributes attributes = properties.getMainAttributes();
        // TODO: replace by Implementation-Version ?
        version = attributes.getValue("Implementation-Build");
    } catch (Exception e) {
        log.warn("Failed to get implemenation version from manifest. {}", e.getMessage());
    }

    if (version == null) {
        version = "UNKNOWN";
    }
    map.put(MOCCA_IMPLEMENTATIONVERSION_PROPERTY, version);

    // signature layout
    try {
        String classContainer = JarLocation.get(CreateXMLSignatureCommandImpl.class);
        URL manifestUrl = new URL("jar:" + classContainer + "!/META-INF/MANIFEST.MF");
        log.debug(manifestUrl.toString());
        Manifest manifest = new Manifest(manifestUrl.openStream());
        Attributes attributes = manifest.getMainAttributes();
        String signatureLayout = attributes.getValue("SignatureLayout");
        if (signatureLayout != null) {
            map.put(SIGNATURE_LAYOUT_PROPERTY, signatureLayout);
        }
    } catch (Exception e) {
        log.warn("Failed to get signature layout from manifest.", e);
    }

    return new MapConfiguration(map);
}

From source file:com.enonic.cms.business.SitePropertiesServiceImpl.java

public void afterPropertiesSet() throws Exception {
    Resource resource = resourceLoader
            .getResource("classpath:com/enonic/cms/business/render/site-default.properties");
    try {/*from   ww  w  .j  a  v  a2 s  .c o  m*/
        defaultProperties = new Properties();
        defaultProperties.load(resource.getInputStream());
    } catch (IOException e) {
        throw new RuntimeException("Failed to load site-default.properties", e);
    }
}

From source file:com.greglturnquist.springagram.fileservice.s3.ApplicationController.java

@RequestMapping(method = RequestMethod.GET, value = "/files/{filename}")
public ResponseEntity<?> getFile(@PathVariable String filename) throws IOException {

    Resource file = this.fileService.findOne(filename);

    try {/*  w  w  w.j a  v  a2s . co  m*/
        return ResponseEntity.ok().contentLength(file.contentLength()).contentType(MediaType.IMAGE_JPEG)
                .body(new InputStreamResource(file.getInputStream()));
    } catch (IOException e) {
        return ResponseEntity.badRequest().body("Couldn't process the request");
    }
}

From source file:org.redisson.spring.cache.RedissonSpringCacheManager.java

@Override
public void afterPropertiesSet() throws Exception {
    if (configLocation == null) {
        return;/*from   w w w . j a va  2 s.c om*/
    }

    Resource resource = resourceLoader.getResource(configLocation);
    try {
        this.configMap = CacheConfig.fromJSON(resource.getInputStream());
    } catch (IOException e) {
        // try to read yaml
        try {
            this.configMap = CacheConfig.fromYAML(resource.getInputStream());
        } catch (IOException e1) {
            throw new BeanDefinitionStoreException(
                    "Could not parse cache configuration at [" + configLocation + "]", e1);
        }
    }
}

From source file:com.azaptree.services.spring.application.config.SpringApplicationServiceConfig.java

public SpringApplicationServiceConfig(final String xmlLocation)
        throws IOException, ClassNotFoundException, JAXBException {
    Assert.hasText(xmlLocation, "xmlResourcePath is required");
    final PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
    final Resource resource = resourceResolver.getResource(xmlLocation);

    try (InputStream xml = resource.getInputStream()) {
        init(xml);//from   ww  w .  j a  va 2 s.  com
    }
}

From source file:org.synyx.hades.dao.config.TypeFilterParserUnitTest.java

@Before
public void setUp() throws SAXException, IOException, ParserConfigurationException {

    parser = new TypeFilterParser(classLoader, context);

    Resource sampleXmlFile = new ClassPathResource("config/type-filter-test.xml");

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/* w ww  .j av a 2 s .  c  o m*/

    documentElement = factory.newDocumentBuilder().parse(sampleXmlFile.getInputStream()).getDocumentElement();
}