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.uuweaver.ucore.util.PropertiesLoader.java

/**
 * , Spring Resource?.//from w ww. j a va 2  s  .  co m
 */
private Properties loadProperties(String... resourcesPaths) {
    Properties props = new Properties();

    for (String location : resourcesPaths) {

        logger.debug("Loading properties file from path:{}", location);

        InputStream is = null;
        try {
            try {
                Resource resource = resourceLoader.getResource(location);
                is = resource.getInputStream();
                props.load(is);
            } finally {
                if (is != null) {
                    is.close();
                }
            }
        } catch (IOException ex) {
            logger.info("Could not load properties from path:{}, {} ", location, ex.getMessage());
        }
    }
    return props;
}

From source file:edu.cmu.lti.oaqa.framework.eval.gs.KeytermGoldStandardFilePersistenceProvider.java

@Override
public boolean initialize(ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams)
        throws ResourceInitializationException {
    boolean ret = super.initialize(aSpecifier, aAdditionalParams);
    String dataset = (String) getParameterValue("DataSet");
    Pattern lineSyntaxPattern = Pattern.compile((String) getParameterValue("LineSyntax"));
    try {//from  ww  w  .  j  a v a 2 s.  c o m
        Resource[] resources = resolver.getResources((String) getParameterValue("PathPattern"));
        for (Resource resource : resources) {
            Scanner scanner = new Scanner(resource.getInputStream());
            while (scanner.findInLine(lineSyntaxPattern) != null) {
                MatchResult result = scanner.match();
                DatasetSequenceId id = new DatasetSequenceId(dataset, result.group(1));
                if (!id2gsSpans.containsKey(id)) {
                    id2gsSpans.put(id, new ArrayList<String>());
                }
                id2gsSpans.get(id).add(result.group(4));
                if (scanner.hasNextLine()) {
                    scanner.nextLine();
                } else {
                    break;
                }
            }
            scanner.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:es.itecban.deployment.security.client.ws.RoleManager.java

public void setPropertiesResource(Resource propertiesResource) throws Exception {
    this.propertiesResource = propertiesResource;
    Properties p = new Properties();
    p.load(propertiesResource.getInputStream());
    Enumeration keys = p.keys();/* w  w  w . j a v  a  2  s  .c o  m*/
    if (keys != null) {
        while (keys.hasMoreElements()) {
            String rname = (String) keys.nextElement();
            String roles = p.getProperty(rname);
            String[] ritems = roles.split(",");
            if (ritems == null) {
                ritems = new String[] { DEFAULT_USER };
            }
            if (ritems.length > 1) {
                String[] temp = new String[ritems.length - 1];
                for (int i = 0; i < temp.length; i++) {
                    temp[i] = ritems[i + 1].trim();
                }
            }
            //Add the user to the users list
            if (!this.users.containsKey(rname))
                this.users.put(rname, ritems[0]);
            //Add roles to the users            
            this.roleMap.put(rname, ritems);
        }
    }
}

From source file:com.retech.mqhcore.utils.PropertiesLoader.java

/**
 * , Spring Resource?./*from   w  ww  . j  a v  a  2s.c om*/
 */
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 {
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return props;
}

From source file:org.carewebframework.api.spring.LocalPropertySource.java

private void loadProperties() {
    if (sources != null && properties == null) {
        properties = new Properties();

        for (String source : sources) {
            try {
                for (Resource resource : appContext.getResources(source)) {
                    InputStream is = null;
                    try {
                        is = resource.getInputStream();
                        properties.load(is);
                    } catch (IOException e) {
                        IOUtils.closeQuietly(is);
                        log.error("Error loading properties from " + resource.getFilename(), e);
                    }//www  . j a v a 2s  .  c  om
                }
            } catch (IOException e) {
                log.error("Error load property resources.", e);
            }
        }
    }
}

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

protected void registerType(String type) throws IOException {
    Resource r = resolver.getResource(String.format("classpath:%s.json", type));
    String json = IOUtils.toString(r.getInputStream(), Charset.defaultCharset());
    JSONObject payload = new JSONObject(json);
    classRegistryService.register(payload);
}

From source file:fi.helsinki.opintoni.integration.leiki.LeikiMockClient.java

public <T> List<T> getLeikiCategoryResponse(Resource resource,
        TypeReference<LeikiCategoryResponse<T>> typeReference) {
    try {//from  ww w .  j  a  v  a 2 s  .c  o m
        LeikiCategoryResponse<T> response = objectMapper.readValue(resource.getInputStream(), typeReference);
        return response.data.matches.get(0).match;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.meruvian.yama.social.linkedin.LinkedInService.java

@Override
public User createUser(Connection<?> connection) {
    LinkedIn linkedIn = (LinkedIn) connection.getApi();
    LinkedInProfile profile = linkedIn.profileOperations().getUserProfile();

    String randomUsername = RandomStringUtils.randomAlphanumeric(6);

    User user = new User();
    user.setUsername(StringUtils.join(profile.getFirstName(), profile.getLastName(), randomUsername));
    user.getName().setFirst(profile.getFirstName());
    user.getName().setLast(profile.getLastName());
    user.setEmail(profile.getEmailAddress());

    if (StringUtils.isBlank(profile.getEmailAddress())) {
        user.setEmail(StringUtils.join(profile.getId(), "@linkedin.com"));
    }/*from  www . j  a v  a  2s.c  o m*/

    user.setPassword(RandomStringUtils.randomAlphanumeric(8));

    FileInfo fileInfo = new FileInfo();
    try {
        Resource resource = linkedIn.restOperations().getForObject(profile.getProfilePictureUrl(),
                Resource.class);
        fileInfo.setDataBlob(resource.getInputStream());
        user.setFileInfo(fileInfo);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    return user;
}

From source file:org.openlmis.fulfillment.Resource2DbTest.java

@Test
public void insertToDbFromCsvShouldCloseInputStream() throws IOException {
    // given//from   www.  ja  va2s  . c om
    Resource resource = mock(Resource.class);
    InputStream inputStream = spy(IOUtils.toInputStream("some data"));
    when(resource.getInputStream()).thenReturn(inputStream);
    when(template.batchUpdate(any(String.class), any(List.class))).thenReturn(new int[] { 1 });

    // when
    resource2Db.insertToDbFromCsv("sometable", resource);

    // then
    verify(inputStream, times(1)).close();
    assertFalse(resource.isOpen());
}

From source file:com.appglu.impl.AbstractAppGluApiTest.java

protected String readCompactedJsonResource(Resource resource) {
    StringBuilder resourceText = new StringBuilder();
    try {/*w  ww.j  a  v  a 2s.  co  m*/
        BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
        while (reader.ready()) {
            resourceText.append(reader.readLine().trim().replace("\n", ""));
        }
    } catch (IOException e) {
    }
    return resourceText.toString();
}