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:org.osiam.OsiamHome.java

private void copyToHome(Resource resource, Path osiamHomeDir) throws IOException {
    String pathUnderHome = resource.getURL().toString().replaceFirst(".*home/", "");
    Path target = osiamHomeDir.resolve(pathUnderHome);
    Files.createDirectories(target.getParent());
    Files.copy(resource.getInputStream(), target, StandardCopyOption.REPLACE_EXISTING);
}

From source file:com.tdclighthouse.prototype.utils.Configuration.java

public Configuration(String systemPropertyName, String defaultPropertiesFileLocation) throws IOException {
    String propertyFilePath = System.getProperties().getProperty(systemPropertyName);
    Resource location = null;
    if (StringUtils.isNotBlank(propertyFilePath)) {
        location = new FileSystemResource(propertyFilePath);
    } else {//ww w  .jav  a2 s  .  com
        location = new ClassPathResource(defaultPropertiesFileLocation);
    }
    properties = new Properties();
    properties.load(location.getInputStream());
}

From source file:uta.ak.usttmp.common.textmining.FileExcludeStopWord.java

public FileExcludeStopWord() {

    //Load stopword file
    InputStreamReader isr = null;
    try {// ww w. jav a  2 s  . co  m
        stopWordsList = new CopyOnWriteArraySet<>();
        Resource res = new ClassPathResource("StopWordTable2.txt");
        //                File stopwords=res.getFile();
        //      File stopwords=new File("/Users/zhangcong/dev/corpus/StopWordTable2.txt");
        isr = new InputStreamReader(res.getInputStream());
        BufferedReader stops = null;
        try {
            String tempString = null;
            stops = new BufferedReader(isr);
            tempString = stops.readLine();
            while ((tempString = stops.readLine()) != null) {
                if (!tempString.isEmpty()) {
                    stopWordsList.add(tempString.toLowerCase().trim());
                }
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (IOException ex) {
        Logger.getLogger(FileExcludeStopWord.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            isr.close();
        } catch (IOException ex) {
            Logger.getLogger(FileExcludeStopWord.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.haulmont.cuba.core.sys.AbstractWebAppContextLoader.java

protected void initAppProperties(ServletContext sc) {
    // get properties from web.xml
    String appProperties = sc.getInitParameter(APP_PROPS_PARAM);
    if (appProperties != null) {
        StrTokenizer tokenizer = new StrTokenizer(appProperties);
        for (String str : tokenizer.getTokenArray()) {
            int i = str.indexOf("=");
            if (i < 0)
                continue;
            String name = StringUtils.substring(str, 0, i);
            String value = StringUtils.substring(str, i + 1);
            if (!StringUtils.isBlank(name)) {
                AppContext.setProperty(name, value);
            }//from  w  ww. ja  v  a2s. co m
        }
    }

    // get properties from a set of app.properties files defined in web.xml
    String propsConfigName = getAppPropertiesConfig(sc);
    if (propsConfigName == null)
        throw new IllegalStateException(APP_PROPS_CONFIG_PARAM + " servlet context parameter not defined");

    final Properties properties = new Properties();

    DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
    StrTokenizer tokenizer = new StrTokenizer(propsConfigName);
    tokenizer.setQuoteChar('"');
    for (String str : tokenizer.getTokenArray()) {
        log.trace("Processing properties location: {}", str);
        str = StrSubstitutor.replaceSystemProperties(str);
        InputStream stream = null;
        try {
            if (ResourceUtils.isUrl(str) || str.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX)) {
                Resource resource = resourceLoader.getResource(str);
                if (resource.exists())
                    stream = resource.getInputStream();
            } else {
                stream = sc.getResourceAsStream(str);
            }

            if (stream != null) {
                log.info("Loading app properties from {}", str);
                try (Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
                    properties.load(reader);
                }
            } else {
                log.trace("Resource {} not found, ignore it", str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }

    for (Object key : properties.keySet()) {
        AppContext.setProperty((String) key, properties.getProperty((String) key).trim());
    }
}

From source file:com.hp.security.jauth.admin.controller.BaseController.java

@RequestMapping("getResource")
public void getResource(String path, HttpServletRequest request, HttpServletResponse response)
        throws IOException, TemplateException {
    if (null != path) {
        if (path.endsWith(".js")) {
            response.setContentType("text/js");
        } else if (path.endsWith(".css")) {
            response.setContentType("text/css");
        } else if (path.endsWith(".gif")) {
            response.setContentType("image/gif");
        } else if (path.endsWith(".jpg") || path.endsWith(".jpeg")) {
            response.setContentType("image/jpeg");
        } else if (path.endsWith(".png")) {
            response.setContentType("image/png");
        } else {/*from w  w w. j  a  v  a  2s .c  om*/
            response.setContentType("text/html");
        }
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource resource = resolver.getResource("jauth/resources/" + path);
        ServletOutputStream out = response.getOutputStream();
        InputStream is = resource.getInputStream();
        int read = 0;
        byte[] buffer = new byte[8192];
        while ((read = is.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        out.flush();
        out.close();
    }
}

From source file:org.jasig.schedassist.impl.caldav.xml.ReportResponseHandlerImplTest.java

@Test
public void testProblemResponse1() throws IOException {
    Resource controlExample = new ClassPathResource("caldav-examples/report-response-problem1.xml");

    ReportResponseHandlerImpl handler = new ReportResponseHandlerImpl();
    List<CalendarWithURI> calendars = handler.extractCalendars(controlExample.getInputStream());
    Assert.assertEquals(5, calendars.size());

    for (CalendarWithURI withUri : calendars) {
        Assert.assertNotNull(withUri.getCalendar());
        Assert.assertNotNull(withUri.getEtag());
        Assert.assertNotNull(withUri.getUri());
    }//from  ww  w  .  j av a 2  s  .  c  om
}

From source file:com.enonic.cms.core.structure.SitePropertiesServiceImpl.java

private SiteProperties loadSiteProperties(final SiteKey siteKey) {
    final Properties properties = new Properties(defaultProperties);
    properties.setProperty("sitekey", String.valueOf(siteKey));

    final String relativePathToCmsHome = "/config/site-" + siteKey + ".properties";
    boolean custom = false;
    try {/*from ww w  .j av  a 2s  .com*/
        String resourcePath = this.homeDir.toURI().toURL() + relativePathToCmsHome;
        Resource resource = resourceLoader.getResource(resourcePath);
        boolean useCustomProperties = resource.exists();
        if (useCustomProperties) {
            InputStream stream = resource.getInputStream();
            properties.load(stream);
            properties.setProperty("customSiteProperties", "true");
            custom = true;
            stream.close();
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to load site properties file: " + relativePathToCmsHome, e);
    }

    properties.setProperty(SitePropertyNames.URL_DEFAULT_CHARACTER_ENCODING, this.characterEncoding);

    final SiteProperties siteProperties = new SiteProperties(siteKey, properties);
    sitePropertiesMap.put(siteKey, siteProperties);

    if (custom) {
        LOG.info("Loaded custom properties for site #{}", siteKey);
    } else {
        LOG.info("Loaded default properties for site #{}", siteKey);
    }

    return siteProperties;
}

From source file:com.wavemaker.runtime.WMAppContext.java

private WMAppContext(ServletContextEvent event) {
    this.context = event.getServletContext();
    this.appName = this.context.getServletContextName();
    if (this.appName == null) {
        this.appName = "Project Name";
    }/*from  w  w w .j  a  va2  s.  c  om*/

    // In Studio, the tenant field and def tenant ID is injected by ProjectManager when a project opens
    if (!this.appName.equals(DataServiceConstants.WAVEMAKER_STUDIO)) {
        // Store types.js contents in memory
        try {
            Resource typesResource = new ServletContextResource(this.context, "/types.js");
            String s = IOUtils.toString(typesResource.getInputStream());
            this.typesObj = new JSONObject(s.substring(11));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        // Set up multi-tenant info
        Resource appPropsResource = null;
        try {
            appPropsResource = new ServletContextResource(this.context,
                    "/WEB-INF/" + CommonConstants.APP_PROPERTY_FILE);
        } catch (WMRuntimeException re) {
            return;
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        if (!appPropsResource.exists()) {
            return;
        }

        Properties props;

        try {
            props = new Properties();
            InputStream is = appPropsResource.getInputStream();
            props.load(is);
            is.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return;
        }

        this.tenantFieldName = props.getProperty(DataServiceConstants.TENANT_FIELD_PROPERTY_NAME);
        this.tenantColumnName = props.getProperty(DataServiceConstants.TENANT_COLUMN_PROPERTY_NAME);
        this.defaultTenantID = Integer
                .parseInt(props.getProperty(DataServiceConstants.DEFAULT_TENANT_ID_PROPERTY_NAME));
    }
}

From source file:de.hybris.platform.acceleratorservices.process.strategies.impl.DefaultEmailTemplateTranslationStrategy.java

protected Map loadPropertyfile(final String path) {
    final Properties properties = new Properties();
    Reader reader = null;/*from w w  w .  j a  va2 s  . c  o  m*/
    try {
        final Resource propertyResource = getApplicationContext().getResource(path);
        if (propertyResource != null && (propertyResource.exists()) && (propertyResource.isReadable())) {
            reader = new InputStreamReader(new BOMInputStream(propertyResource.getInputStream()), "UTF-8");
            properties.load(reader);
        }
    } catch (final IOException e) {
        throw new RendererException("Problem during rendering", e);
    } finally {
        IOUtils.closeQuietly(reader);
    }

    return properties;
}