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:com.wavemaker.tools.project.AbstractStudioFileSystem.java

@Override
public Resource repacePattern(Resource resource, String oldPattern, String newPattern) throws IOException {
    InputStream is = resource.getInputStream();
    String content = org.apache.commons.io.IOUtils.toString(is, ServerConstants.DEFAULT_ENCODING);
    content = content.replaceAll(oldPattern, newPattern);
    is.close();//w  w w  .  java 2 s  .co m
    OutputStream os = getOutputStream(resource);
    os.write(content.getBytes(ServerConstants.DEFAULT_ENCODING));
    os.close();

    return resource;
}

From source file:ch.sbb.releasetrain.jsfbootadapter.FileDownloadUtil.java

@RequestMapping(value = "/static/**", method = RequestMethod.GET)
public void getFile(HttpServletResponse response, HttpServletRequest request) {
    try {/*ww w.  j  av a2 s . co m*/

        String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        PathMatchingResourcePatternResolver res = new PathMatchingResourcePatternResolver();

        Resource template = res.getResource(path);

        if (!template.exists()) {
            log.info("file n/a... " + path);
            return;
        }

        org.apache.commons.io.IOUtils.copy(template.getInputStream(), response.getOutputStream());
        response.flushBuffer();

    } catch (IOException ex) {
        log.info("Error writing file to output stream", ex);
    }
}

From source file:com.hillert.botanic.model.Image.java

public Image(String name, Resource data, Plant plant) {
    this.name = name;
    this.plant = plant;

    InputStream is = null;/* ww  w  . java  2s.  c  o m*/
    try {
        is = data.getInputStream();
        this.image = StreamUtils.copyToByteArray(is);
        is.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:io.pivotal.poc.claimcheck.LocalFileClaimCheckStore.java

@Override
public String save(Resource resource) {
    String id = idGenerator.generateId().toString();
    try {/*from   ww  w .j  a v  a 2  s  .c o m*/
        File outputFile = new File(this.directory, id);
        FileCopyUtils.copy(new InputStreamReader(resource.getInputStream()), new FileWriter(outputFile));
        log.info("saved '{}' to {}", id, outputFile.getAbsolutePath());
    } catch (IOException e) {
        throw new IllegalStateException("failed to write file", e);
    }
    return id;
}

From source file:org.web4thejob.module.JobletInstallerImpl.java

@Override
@SuppressWarnings("unchecked")
public <E extends Exception> List<E> install(List<Joblet> joblets) {
    List<E> exceptions = new ArrayList<E>();

    try {//from   www  . j  a  v a 2s . c  om

        final Configuration configuration = new Configuration();
        configuration.setProperty(AvailableSettings.DIALECT,
                connInfo.getProperty(DatasourceProperties.DIALECT));
        configuration.setProperty(AvailableSettings.DRIVER, connInfo.getProperty(DatasourceProperties.DRIVER));
        configuration.setProperty(AvailableSettings.URL, connInfo.getProperty(DatasourceProperties.URL));
        configuration.setProperty(AvailableSettings.USER, connInfo.getProperty(DatasourceProperties.USER));
        configuration.setProperty(AvailableSettings.PASS, connInfo.getProperty(DatasourceProperties.PASSWORD));

        final ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        if (StringUtils.hasText(connInfo.getProperty(DatasourceProperties.SCHEMA_SYNTAX))) {
            String schemaSyntax = connInfo.getProperty(DatasourceProperties.SCHEMA_SYNTAX);
            Connection connection = serviceRegistry.getService(ConnectionProvider.class).getConnection();

            for (Joblet joblet : joblets) {
                for (String schema : joblet.getSchemas()) {
                    Statement statement = connection.createStatement();
                    statement.executeUpdate(schemaSyntax.replace("%s", schema));
                    statement.close();
                }
            }

            if (!connection.getAutoCommit()) {
                connection.commit();
            }
        }

        for (Joblet joblet : joblets) {
            for (Resource resource : joblet.getResources()) {
                configuration.addInputStream(resource.getInputStream());
            }
        }

        SchemaExport schemaExport = new SchemaExport(serviceRegistry, configuration);
        schemaExport.execute(Target.EXPORT, SchemaExport.Type.CREATE);
        exceptions.addAll(schemaExport.getExceptions());

    } catch (Exception e) {
        exceptions.add((E) e);
    }

    return exceptions;

}

From source file:com.epam.ta.reportportal.core.jasper.JasperReportRender.java

@Autowired
public JasperReportRender(ResourceLoader resourceLoader) throws JRException, IOException {
    Resource reportTemplate = resourceLoader.getResource(REPORT_JRXML_TEMPLATE);
    com.google.common.base.Preconditions.checkArgument(reportTemplate.exists());
    InputStream inputStream = reportTemplate.getInputStream();
    JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
    this.jasperReport = JasperCompileManager.compileReport(jasperDesign);

}

From source file:demo.FleetLocationTests.java

private void saveJson(Resource resource) throws IOException, JsonParseException, JsonMappingException {
    if (this.repository.count() == 0) {
        List<Location> value = this.mapper.readValue(resource.getInputStream(),
                new TypeReference<List<Location>>() {
                });/*from ww w .  j a  va2  s  . co  m*/
        assertEquals(4, value.size());
        this.repository.save(value);
    }
}

From source file:com.knitml.core.xml.PluggableSchemaResolver.java

public InputSource resolveEntity(String publicId, String systemId) throws IOException {
    if (logger.isTraceEnabled()) {
        logger.trace("Trying to resolve XML entity with public id [" + publicId + "] and system id [" + systemId
                + "]");
    }/*from ww w .  j  a  v a  2  s  .c  o  m*/
    if (systemId != null) {
        String resourceLocation = getSchemaMapping(systemId);
        if (resourceLocation != null) {
            Resource resource = new ClassPathResource(resourceLocation, this.classLoader);
            InputSource source = new InputSource(resource.getInputStream());
            source.setPublicId(publicId);
            source.setSystemId(systemId);
            if (logger.isDebugEnabled()) {
                logger.debug("Found XML schema [" + systemId + "] in classpath: " + resourceLocation);
            }
            return source;
        }
    }
    return null;
}

From source file:com.gopivotal.spring.xd.module.jdbc.JdbcTasklet.java

private String scriptToString(Resource resource, String encoding) throws Exception {

    InputStream stream = resource.getInputStream();

    BufferedReader reader = new BufferedReader(
            (StringUtils.hasText(encoding) ? new InputStreamReader(stream, encoding)
                    : new InputStreamReader(stream)));

    String message = "";

    try {/*from   w ww  . j  ava2  s  . co m*/
        message = IOUtils.toString(reader);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        IOUtils.closeQuietly(reader);
    }
    return message;
}

From source file:de.ep3.ftpc.view.designer.UIDesigner.java

/**
 * Loads a (buffered) image from the resources directory provided.
 *
 * @param dirName The directory name under the resource/drawable directory.
 * @param fileName The file name under the directory name provided before.
 * @return A hopefully nice image.//from w w w .j av a  2 s.  c o  m
 */
public BufferedImage getDefaultImage(String dirName, String fileName) {
    String filePath = "drawable" + File.separator + dirName + File.separator + fileName;

    Resource res = App.getContext().getResource(filePath);

    BufferedImage image;

    try {
        image = ImageIO.read(res.getInputStream());
    } catch (IOException e) {
        // Don't blame this checked-to-unchecked exception conversion. This is by design (fail fast and early).
        throw new IllegalStateException("Unable to load image file " + fileName + " (" + e.getMessage() + ")");
    }

    return image;
}