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())));
}
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();
}
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;
}
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);
}
}
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();
}
Aggregations