Example usage for org.springframework.core.io ClassPathResource getInputStream

List of usage examples for org.springframework.core.io ClassPathResource getInputStream

Introduction

In this page you can find the example usage for org.springframework.core.io ClassPathResource getInputStream.

Prototype

@Override
public InputStream getInputStream() throws IOException 

Source Link

Document

This implementation opens an InputStream for the given class path resource.

Usage

From source file:org.projectforge.database.XmlDump.java

/**
 * @return Only for test cases.// ww  w .  j a v a 2 s  .c  o  m
 */
public XStreamSavingConverter restoreDatabaseFromClasspathResource(final String path, final String encoding) {
    final ClassPathResource cpres = new ClassPathResource(path);
    Reader reader;
    try {
        InputStream in;
        if (path.endsWith(".gz") == true) {
            in = new GZIPInputStream(cpres.getInputStream());
        } else {
            in = cpres.getInputStream();
        }
        reader = new InputStreamReader(in, encoding);
    } catch (final IOException ex) {
        log.error(ex.getMessage(), ex);
        throw new RuntimeException(ex);
    }
    return restoreDatabase(reader);
}

From source file:org.sakaiproject.tool.assessment.qti.helper.AuthoringXml.java

/**
 * get template as stream using spring's ClassPathResource 
 * @param templateName// w w w  .  j  av a  2 s . co m
 * @param context
 * @return
 */
public InputStream getTemplateInputStream(String templateName) {
    InputStream is = null;

    try {
        ClassPathResource resource = new ClassPathResource(TEMPLATE_PATH + qtiPath + "/" + templateName);
        is = resource.getInputStream();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    return is;
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlUtil.java

/**
 * Read document from within spring context path
 *
 * @param path path/* w w  w .jav  a2 s  .  co  m*/
 *
 * @return Document
 */
public static Document readDocument(String path) {
    if (log.isDebugEnabled()) {
        log.debug("readDocument(String " + path + ")");
    }

    Document document = null;
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    ClassPathResource resource = new ClassPathResource(path);

    try {

        InputStream inputStream = resource.getInputStream();

        setDocumentBuilderFactoryFeatures(builderFactory);
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        document = documentBuilder.parse(inputStream);
    } catch (ParserConfigurationException e) {
        log.error(e.getMessage(), e);
    } catch (SAXException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    return document;
}

From source file:org.springframework.cloud.stream.app.aws.AwsIntegrationTestStackRule.java

@Override
protected void before() throws Throwable {
    try {/*www .  j a  v a 2 s . com*/
        String awsCredentialsDir = System.getProperty("aws.credentials.path");
        File awsCredentialsFile = new File(awsCredentialsDir, "aws.credentials.properties");
        Properties awsCredentials = new Properties();
        awsCredentials.load(new FileReader(awsCredentialsFile));
        String accessKey = awsCredentials.getProperty("cloud.aws.credentials.accessKey");
        String secretKey = awsCredentials.getProperty("cloud.aws.credentials.secretKey");
        this.cloudFormation = new AmazonCloudFormationClient(new BasicAWSCredentials(accessKey, secretKey));

        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(new ClassPathResource("application.yml"));
        Properties applicationProperties = yamlPropertiesFactoryBean.getObject();

        this.stackName = applicationProperties.getProperty("cloud.aws.stack.name");

        after();

        ClassPathResource stackTemplate = new ClassPathResource("AwsIntegrationTestTemplate.json");
        String templateBody = FileCopyUtils.copyToString(new InputStreamReader(stackTemplate.getInputStream()));

        this.cloudFormation.createStack(new CreateStackRequest().withTemplateBody(templateBody)
                .withOnFailure(OnFailure.DELETE).withStackName(this.stackName));

        waitForCompletion();

        System.setProperty("cloud.aws.credentials.accessKey", accessKey);
        System.setProperty("cloud.aws.credentials.secretKey", secretKey);
    } catch (Exception e) {
        if (!(e instanceof AssumptionViolatedException)) {
            Assume.assumeTrue("Can't perform AWS integration test because of: " + e.getMessage(), false);
        } else {
            throw e;
        }
    }
}

From source file:org.springframework.security.ldap.server.ApacheDSContainerTests.java

@Test
public void startWithLdapOverSslWithWrongPassword() throws Exception {
    final ClassPathResource keyStoreResource = new ClassPathResource(
            "/org/springframework/security/ldap/server/spring.keystore");
    final File temporaryKeyStoreFile = new File(temporaryFolder.getRoot(), "spring.keystore");
    FileCopyUtils.copy(keyStoreResource.getInputStream(), new FileOutputStream(temporaryKeyStoreFile));

    assertThat(temporaryKeyStoreFile).isFile();

    ApacheDSContainer server = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");

    List<Integer> ports = getDefaultPorts(1);
    server.setPort(ports.get(0));/*w ww .ja  v  a  2  s.com*/

    server.setLdapOverSslEnabled(true);
    server.setKeyStoreFile(temporaryKeyStoreFile);
    server.setCertificatePassord("incorrect-password");

    try {
        server.afterPropertiesSet();
        fail("Expected a RuntimeException to be thrown.");
    } catch (RuntimeException e) {
        assertThat(e).hasMessage("Server startup failed");
        assertThat(e).hasRootCauseInstanceOf(UnrecoverableKeyException.class);
    }
}

From source file:org.springframework.security.ldap.server.ApacheDSContainerTests.java

/**
 * This test starts an LDAP server using LDAPs (LDAP over SSL). A self-signed certificate is being used, which was
 * previously generated with:/*w  ww  .  ja va 2 s.  c  o m*/
 *
 * <pre>
 * {@code
 * keytool -genkey -alias spring -keyalg RSA -keystore spring.keystore -validity 3650 -storetype JKS \
 * -dname "CN=localhost, OU=Spring, O=Pivotal, L=Kailua-Kona, ST=HI, C=US" -keypass spring -storepass spring
 * }
 * </pre>
 * @throws Exception
 */
@Test
public void startWithLdapOverSsl() throws Exception {

    final ClassPathResource keyStoreResource = new ClassPathResource(
            "/org/springframework/security/ldap/server/spring.keystore");
    final File temporaryKeyStoreFile = new File(temporaryFolder.getRoot(), "spring.keystore");
    FileCopyUtils.copy(keyStoreResource.getInputStream(), new FileOutputStream(temporaryKeyStoreFile));

    assertThat(temporaryKeyStoreFile).isFile();

    ApacheDSContainer server = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");

    List<Integer> ports = getDefaultPorts(1);
    server.setPort(ports.get(0));

    server.setLdapOverSslEnabled(true);
    server.setKeyStoreFile(temporaryKeyStoreFile);
    server.setCertificatePassord("spring");

    try {
        server.afterPropertiesSet();
    } finally {
        try {
            server.destroy();
        } catch (Throwable t) {
        }
    }
}

From source file:piecework.ui.PageContextTest.java

@Test
public void testSerializeToJson() throws Exception {
    ClassPathResource expectedResource = new ClassPathResource("piecework/ui/PageContext.json");

    ProcessInstance processInstance = new ProcessInstance.Builder().processInstanceId("1")
            .processDefinitionKey("Test").processDefinitionLabel("Testing Process")
            .processInstanceLabel("A Simple Test").build();

    PageContext context = new PageContext.Builder().applicationTitle("Test application").pageTitle("Test page")
            .assetsUrl("http://localhost/resources").resource(processInstance)
            .user(new User.Builder().userId("123").visibleId("jtest").displayName("Jill Test").build()).build();

    ObjectMapper mapper = new ObjectMapper();

    String expected = IOUtils.toString(expectedResource.getInputStream());
    String actual = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(context);
    Assert.assertEquals(expected, actual);
}