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.alibaba.ims.platform.dict.DictManager.java

public static void init() {
    Resource resource = null;
    try {/*from   ww  w.j a  v a2 s  . c o m*/
        resource = new PathMatchingResourcePatternResolver().getResources("classpath:conf/dict.properties")[0];
        LinkedProperties properties = new LinkedProperties();
        properties.load(resource.getInputStream());

        for (Enumeration<Object> e = properties.keys(); e.hasMoreElements();) {
            String key = (String) e.nextElement();
            put(key, properties.getProperty(key));
        }
    } catch (Exception e) {
        logger.error("Init data dict error.", e);
    } finally {
    }
}

From source file:fr.mby.saml2.sp.impl.helper.SecurityHelper.java

/**
 * Build a certificate from PEM resource.
 * //from  ww w  . j  av  a2s.  c  o m
 * @param certificate
 *            the PEM resource
 * @param type
 *            the certificate type
 * @return the java.security.cert.Certificate
 * @throws CertificateException
 * @throws IOException
 */
public static Certificate buildCertificate(final Resource certificate, final String type)
        throws CertificateException, IOException {
    Certificate result = null;

    final CertificateFactory certFactory = CertificateFactory.getInstance(type);
    result = certFactory.generateCertificate(certificate.getInputStream());

    return result;
}

From source file:com.iisigroup.cap.mvc.i18n.MessageBundleScriptCreator.java

/**
 * ? i18n //from   w w w.ja v a 2s  . c om
 *
 * @param i18nPath
 *            i18nPath
 * @return Properties
 */
private static Properties loadProperties(String i18nPath) {
    Properties prop = new Properties();
    Locale locale = null;
    try {
        locale = CapSecurityContext.getLocale();
        if (locale == null) {
            locale = Locale.getDefault();
        }
    } catch (Exception e) {
        locale = Locale.getDefault();
    }
    String i18nFile = null;
    InputStream is = null;
    try {
        i18nFile = new StringBuffer("classpath:/i18n/").append(i18nPath).append("_").append(locale.toString())
                .append(".properties").toString();
        Resource rs = CapAppContext.getApplicationContext().getResource(i18nFile);
        if (rs != null) {
            is = rs.getInputStream();
            prop.load(is);
        } else {
            i18nFile = new StringBuffer("classpath:/i18n/").append(i18nPath).append("_").append(".properties")
                    .toString();
            rs = CapAppContext.getApplicationContext().getResource(i18nFile);
            if (rs != null) {
                is = rs.getInputStream();
                prop.load(is);
            }
        }

    } catch (Exception e) {
        LOGGER.error("can't load " + i18nPath);
    } finally {
        IOUtils.closeQuietly(is);
    }
    return prop;
}

From source file:io.cloudine.rhq.plugins.worker.ResourceUtils.java

/**
 *  ?  {@link org.springframework.core.io.Resource}? ? ?  .
 *
 * @param resource //w ww.  j a  va  2  s  . c o m
 * @return {@link org.springframework.core.io.Resource}? ?  
 * @throws java.io.IOException ??    
 */
public static byte[] getResourceByteContents(Resource resource) throws IOException {
    InputStream inputStream = resource.getInputStream();
    int size = inputStream.available();
    byte[] bytes = new byte[size];
    inputStream.read(bytes);
    return bytes;
}

From source file:grails.plugin.searchable.internal.SearchableUtils.java

private static Map loadMetadata(Resource resource) {
    try {//  www. jav  a 2 s . c  o m
        Properties meta = new Properties();
        meta.load(resource.getInputStream());
        return meta;
    } catch (IOException e) {
        //            GrailsUtil.deepSanitize(e);
        log.warn("No application metadata file found at " + resource);
    }
    return null;
}

From source file:io.cloudine.rhq.plugins.worker.ResourceUtils.java

/**
 *  ?  {@link org.springframework.core.io.Resource}? ? ? .
 *
 * @param location  //from  w w w  .java2s. c  om
 * @return {@link org.springframework.core.io.Resource}? ? 
 * @throws java.io.IOException     
 */
public static String getResourceTextContents(String location) throws IOException {
    Resource resource = resourceLoader.getResource(location);
    InputStream inputStream = resource.getInputStream();
    return getResourceTextContents(inputStream);
}

From source file:org.shept.util.JarUtils.java

/**
 * Copy resources from a classPath, typically within a jar file 
 * to a specified destination, typically a resource directory in
 * the projects webApp directory (images, sounds, e.t.c. )
 * /*from   ww  w  .  ja v  a 2  s. co m*/
 * Copies resources only if the destination file does not exist and
 * the specified resource is available.
 * 
 * @param resources Array of String resources (filenames) to be copied
 * @param cpr ClassPathResource specifying the source location on the classPath (maybe within a jar)
 * @param webAppDestPath Full path String to the fileSystem destination directory   
 * @throws IOException when copying on copy error
 */
public static void copyResources(String[] resources, ClassPathResource cpr, String webAppDestPath)
        throws IOException {
    String dstPath = webAppDestPath; //  + "/" + jarPathInternal(cpr.getURL());
    File dir = new File(dstPath);
    dir.mkdirs();
    for (int i = 0; i < resources.length; i++) {
        File dstFile = new File(dstPath, resources[i]); //    (StringUtils.applyRelativePath(dstPath, images[i]));
        Resource fileRes = cpr.createRelative(resources[i]);
        if (!dstFile.exists() && fileRes.exists()) {
            FileOutputStream fos = new FileOutputStream(dstFile);
            FileCopyUtils.copy(fileRes.getInputStream(), fos);
            logger.info("Successfully copied file " + fileRes.getFilename() + " from " + cpr.getPath() + " to "
                    + dstFile.getPath());
        }
    }
}

From source file:org.jspringbot.keyword.expression.ELUtils.java

public static String resource(String resourceAsText) throws Exception {
    ResourceEditor editor = new ResourceEditor();
    editor.setAsText(resourceAsText);/*from   ww  w . j  a  v  a2 s .co m*/

    Resource resource = (Resource) editor.getValue();
    String resourceString = IOUtils.toString(resource.getInputStream());

    return replaceVars(resourceString);
}

From source file:com.hmsinc.epicenter.classifier.ClassifierFactory.java

/**
 * Creates the actual classifier.//from w ww  .  j a va  2 s .  co m
 * 
 * @param resource
 * @return classificationEngine
 * @throws Exception
 */
private static ClassificationEngine instantiateClassifier(final Resource resource) throws Exception {

    final Unmarshaller u = JAXBContext.newInstance("com.hmsinc.epicenter.classifier.config")
            .createUnmarshaller();

    // Enable validation
    u.setSchema(schema);

    final InputStream is = resource.getInputStream();
    final ClassifierConfig config = (ClassifierConfig) u.unmarshal(is);
    is.close();

    Validate.notNull(config, "Configuration was null!");
    Validate.notNull(config.getImplementation(), "No implementation was specified.");

    final Class<?> implementation = Class.forName(config.getImplementation());
    Validate.isTrue(ClassificationEngine.class.isAssignableFrom(implementation),
            "Implementation must be an instance of ClassificationEngine (was: " + config.getImplementation()
                    + ")");

    final ClassificationEngine engine = (ClassificationEngine) implementation.newInstance();
    engine.init(config);

    return engine;
}

From source file:com.ktds.ldap.populator.LdapTestUtils.java

public static void loadLdif(DefaultDirectoryService directoryService, Resource ldifFile) throws IOException {
    File tempFile = File.createTempFile("spring_ldap_test", ".ldif");
    try {//ww w. j  a va2s  .  c om
        InputStream inputStream = ldifFile.getInputStream();
        IOUtils.copy(inputStream, new FileOutputStream(tempFile));
        LdifFileLoader fileLoader = new LdifFileLoader(directoryService.getSession(),
                tempFile.getAbsolutePath());
        fileLoader.execute();
    } finally {
        try {
            tempFile.delete();
        } catch (Exception e) {
            // Ignore this
        }
    }
}