Search in sources :

Example 1 with WritableResource

use of org.springframework.core.io.WritableResource in project microservices by pwillhan.

the class CloudS3Application method resourceAccess.

@PostConstruct
public void resourceAccess() throws IOException {
    String location = "s3://" + this.bucket + "/file.txt";
    WritableResource writeableResource = (WritableResource) this.resourceLoader.getResource(location);
    FileCopyUtils.copy("Hello World!", new OutputStreamWriter(writeableResource.getOutputStream()));
    Resource resource = this.resourceLoader.getResource(location);
    System.out.println(FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())));
}
Also used : WritableResource(org.springframework.core.io.WritableResource) InputStreamReader(java.io.InputStreamReader) WritableResource(org.springframework.core.io.WritableResource) Resource(org.springframework.core.io.Resource) OutputStreamWriter(java.io.OutputStreamWriter) PostConstruct(javax.annotation.PostConstruct)

Example 2 with WritableResource

use of org.springframework.core.io.WritableResource in project pac4j by pac4j.

the class SAML2ClientConfiguration method internalInit.

@Override
protected void internalInit() {
    CommonHelper.assertNotNull("keystoreResource", this.keystoreResource);
    CommonHelper.assertNotBlank("keystorePassword", this.keystorePassword);
    CommonHelper.assertNotBlank("privateKeyPassword", this.privateKeyPassword);
    CommonHelper.assertNotNull("identityProviderMetadataResource", this.identityProviderMetadataResource);
    if (!this.keystoreResource.exists()) {
        if (this.keystoreResource instanceof WritableResource) {
            LOGGER.warn("Provided keystoreResource does not exist. Creating one for: {}", this.keystoreResource);
            createKeystore();
        } else {
            throw new TechnicalException("Provided keystoreResource does not exist and cannot be created");
        }
    }
    final BasicSignatureSigningConfiguration config = DefaultSecurityConfigurationBootstrap.buildDefaultSignatureSigningConfiguration();
    this.blackListedSignatureSigningAlgorithms = new ArrayList<>(config.getBlacklistedAlgorithms());
    this.signatureAlgorithms = new ArrayList<>(config.getSignatureAlgorithms());
    this.signatureReferenceDigestMethods = new ArrayList<>(config.getSignatureReferenceDigestMethods());
    this.signatureReferenceDigestMethods.remove("http://www.w3.org/2001/04/xmlenc#sha512");
    this.signatureCanonicalizationAlgorithm = config.getSignatureCanonicalizationAlgorithm();
}
Also used : WritableResource(org.springframework.core.io.WritableResource) TechnicalException(org.pac4j.core.exception.TechnicalException) BasicSignatureSigningConfiguration(org.opensaml.xmlsec.impl.BasicSignatureSigningConfiguration)

Example 3 with WritableResource

use of org.springframework.core.io.WritableResource in project pac4j by pac4j.

the class SAML2FileSystemMetadataGenerator method storeMetadata.

@Override
public boolean storeMetadata(final String metadata, final Resource metadataResource, final boolean force) throws Exception {
    if (metadataResource == null || CommonHelper.isBlank(metadata)) {
        logger.info("No metadata or resource is provided");
        return false;
    }
    if (!(metadataResource instanceof WritableResource)) {
        logger.warn("Unable to store metadata, as resource is not writable");
        return false;
    }
    if (metadataResource.exists() && !force) {
        logger.info("Metadata file already exists at {}.", metadataResource.getFile());
    } else {
        logger.info("Writing metadata to {}", metadataResource.getFilename());
        final var parent = metadataResource.getFile().getParentFile();
        if (parent != null) {
            logger.debug("Attempting to create directory structure for: {}", parent.getCanonicalPath());
            if (!parent.exists() && !parent.mkdirs()) {
                logger.warn("Could not construct the directory structure for metadata: {}", parent.getCanonicalPath());
            }
        }
        final var transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        final var result = new StreamResult(new StringWriter());
        final var source = new StreamSource(new StringReader(metadata));
        transformer.transform(source, result);
        final var destination = WritableResource.class.cast(metadataResource);
        try (var spMetadataOutputStream = destination.getOutputStream()) {
            spMetadataOutputStream.write(result.getWriter().toString().getBytes(StandardCharsets.UTF_8));
        }
        if (destination.exists()) {
            if (isSignMetadata()) {
                getMetadataSigner().sign(metadataResource.getFile());
            }
            return true;
        }
    }
    return false;
}
Also used : WritableResource(org.springframework.core.io.WritableResource) StreamResult(javax.xml.transform.stream.StreamResult) StringWriter(java.io.StringWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader)

Example 4 with WritableResource

use of org.springframework.core.io.WritableResource in project opennms by OpenNMS.

the class JaxbResourceConfiguration method save.

public void save(final T config) throws ConfigurationResourceException {
    final Resource r = getResource();
    if (!(r instanceof WritableResource)) {
        throw new ConfigurationResourceException("Resource " + r + " is not writable!");
    }
    final WritableResource resource = (WritableResource) r;
    OutputStream os = null;
    OutputStreamWriter osw = null;
    try {
        os = resource.getOutputStream();
        osw = new OutputStreamWriter(os);
        JaxbUtils.marshal(config, osw);
    } catch (final IOException e) {
        throw new ConfigurationResourceException("Failed to write to " + r, e);
    } catch (final Exception e) {
        throw new ConfigurationResourceException("Failed to marshal configuration " + getClassType() + " to resource " + r, e);
    } finally {
        IOUtils.closeQuietly(osw);
        IOUtils.closeQuietly(os);
    }
}
Also used : WritableResource(org.springframework.core.io.WritableResource) OutputStream(java.io.OutputStream) ConfigurationResource(org.opennms.core.config.api.ConfigurationResource) WritableResource(org.springframework.core.io.WritableResource) Resource(org.springframework.core.io.Resource) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) ConfigurationResourceException(org.opennms.core.config.api.ConfigurationResourceException) ConfigurationResourceException(org.opennms.core.config.api.ConfigurationResourceException) IOException(java.io.IOException)

Example 5 with WritableResource

use of org.springframework.core.io.WritableResource in project spring-cloud-gcp by spring-cloud.

the class GoogleStorageTests method testWritable.

@Test
public void testWritable() throws Exception {
    WriteChannel writeChannel = mock(WriteChannel.class);
    when(this.mockStorage.writer(any(BlobInfo.class))).thenReturn(writeChannel);
    Assert.assertTrue(this.remoteResource instanceof WritableResource);
    WritableResource writableResource = (WritableResource) this.remoteResource;
    Assert.assertTrue(writableResource.isWritable());
    writableResource.getOutputStream();
}
Also used : WritableResource(org.springframework.core.io.WritableResource) WriteChannel(com.google.cloud.WriteChannel) BlobInfo(com.google.cloud.storage.BlobInfo) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

WritableResource (org.springframework.core.io.WritableResource)5 OutputStreamWriter (java.io.OutputStreamWriter)2 Resource (org.springframework.core.io.Resource)2 WriteChannel (com.google.cloud.WriteChannel)1 BlobInfo (com.google.cloud.storage.BlobInfo)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 PostConstruct (javax.annotation.PostConstruct)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 Test (org.junit.Test)1 ConfigurationResource (org.opennms.core.config.api.ConfigurationResource)1 ConfigurationResourceException (org.opennms.core.config.api.ConfigurationResourceException)1 BasicSignatureSigningConfiguration (org.opensaml.xmlsec.impl.BasicSignatureSigningConfiguration)1 TechnicalException (org.pac4j.core.exception.TechnicalException)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1