org.springframework.core.io.WritableResource Java Examples

The following examples show how to use org.springframework.core.io.WritableResource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExtendedResourceUtils.java    From wallride with Apache License 2.0 6 votes vote down vote up
public static void write(Resource resource, File file) throws IOException {
		if (resource instanceof WritableResource) {
//			IOUtils.copy(new FileInputStream(file), ((WritableResource) resource).getOutputStream());
			try (InputStream input = new FileInputStream(file); OutputStream output = ((WritableResource) resource).getOutputStream()) {
				IOUtils.copy(input, output);
			}
			return;
		}
		FileUtils.copyFile(file, getFile(resource.getURI()));
	}
 
Example #2
Source File: ResourceLoaderAwsTest.java    From spring-cloud-aws with Apache License 2.0 6 votes vote down vote up
@Test
void testUploadFileWithRelativePath() throws Exception {
	String bucketName = this.stackResourceRegistry
			.lookupPhysicalResourceId("EmptyBucket");
	uploadFileTestFile(bucketName, "testUploadFileWithRelativePathParent",
			"hello world");
	Resource resource = this.resourceLoader.getResource(
			S3_PREFIX + bucketName + "/testUploadFileWithRelativePathParent");
	assertTrue(resource.exists());

	WritableResource childFileResource = (WritableResource) resource
			.createRelative("child");

	try (OutputStream outputStream = childFileResource.getOutputStream();
			OutputStreamWriter writer = new OutputStreamWriter(outputStream)) {
		writer.write("hello world");
	}

	this.createdObjects.add(childFileResource.getFilename());

	InputStream inputStream = childFileResource.getInputStream();
	assertNotNull(inputStream);
	assertEquals("hello world",
			FileCopyUtils.copyToString(new InputStreamReader(inputStream, "UTF-8")));
	assertEquals("hello world".length(), childFileResource.contentLength());
}
 
Example #3
Source File: GoogleStorageTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Test
public void testBucketNoBlobResourceStatuses() throws IOException {
	Assert.assertFalse(this.bucketResource.isOpen());
	Assert.assertFalse(this.bucketResource.isReadable());
	Assert.assertFalse(((WritableResource) this.bucketResource).isWritable());
	Assert.assertTrue(this.bucketResource.exists());
}
 
Example #4
Source File: GoogleStorageTests.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example #5
Source File: WebController.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/", method = RequestMethod.POST)
String writeGcs(@RequestBody String data) throws IOException {
	try (OutputStream os = ((WritableResource) this.gcsFile).getOutputStream()) {
		os.write(data.getBytes());
	}
	return "file was updated\n";
}
 
Example #6
Source File: StoreRestController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
protected void handleUpdate(HttpHeaders headers, String store, String path, InputStream content)
		throws IOException {

	ContentStoreInfo info = ContentStoreUtils.findStore(storeService, store);
	if (info == null) {
		throw new IllegalArgumentException("Not a Store");
	}

	String pathToUse = path.substring(store.length() + 1);
	Resource r = ((Store) info.getImpementation()).getResource(pathToUse);
	if (r == null) {
		throw new ResourceNotFoundException();
	}
	if (r instanceof WritableResource == false) {
		throw new UnsupportedOperationException();
	}

	if (r.exists()) {
		HeaderUtils.evaluateHeaderConditions(headers, null, new Date(r.lastModified()));
	}

	InputStream in = content;
	OutputStream out = ((WritableResource) r).getOutputStream();
	IOUtils.copy(in, out);
	IOUtils.closeQuietly(out);
	IOUtils.closeQuietly(in);
}
 
Example #7
Source File: S3StoreResource.java    From spring-content with Apache License 2.0 5 votes vote down vote up
public S3StoreResource(AmazonS3 client, String bucket, Resource delegate) {
	Assert.notNull(client, "client must be specified");
	Assert.hasText(bucket, "bucket must be specified");
	Assert.isInstanceOf(WritableResource.class, delegate);
	this.client = client;
	this.bucket = bucket;
	this.delegate = delegate;
}
 
Example #8
Source File: DefaultJpaStoreImpl.java    From spring-content with Apache License 2.0 5 votes vote down vote up
@Override
public S setContent(S entity, InputStream content) {
	Resource resource = getResource(entity);
	if (resource == null) {
		UUID contentId = UUID.randomUUID();
		Object convertedId = convertToExternalContentIdType(entity, contentId);
		resource = this.getResource((SID)convertedId);
		BeanUtils.setFieldWithAnnotation(entity, ContentId.class,
				convertedId);
	}
	OutputStream os = null;
	long contentLen = -1L;
	try {
		if (resource instanceof WritableResource) {
			os = ((WritableResource) resource).getOutputStream();
			contentLen = IOUtils.copyLarge(content, os);
		}
	}
	catch (IOException e) {
		logger.error(format("Unexpected error setting content for entity %s", entity), e);
		throw new StoreAccessException(format("Setting content for entity %s", entity), e);
	}
	finally {
		IOUtils.closeQuietly(content);
		IOUtils.closeQuietly(os);
	}

	waitForCommit((BlobResource) resource);

	BeanUtils.setFieldWithAnnotation(entity, ContentId.class,
			((BlobResource) resource).getId());
	BeanUtils.setFieldWithAnnotation(entity, ContentLength.class, contentLen);

	return entity;
}
 
Example #9
Source File: CloudS3Application.java    From building-microservices with Apache License 2.0 5 votes vote down vote up
@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())));
}
 
Example #10
Source File: ExtendedResourceUtils.java    From wallride with Apache License 2.0 5 votes vote down vote up
public static void write(Resource resource, MultipartFile file) throws IOException {
		if (resource instanceof WritableResource) {
//			IOUtils.copy(file.getInputStream(), ((WritableResource) resource).getOutputStream());
			try (InputStream input = file.getInputStream(); OutputStream output = ((WritableResource) resource).getOutputStream()) {
				IOUtils.copy(input, output);
			}
			return;
		}
		FileUtils.copyInputStreamToFile(file.getInputStream(), getFile(resource.getURI()));
	}
 
Example #11
Source File: ResourceLoaderAwsTest.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
@Test
void testUploadFileWithMoreThenFiveMegabytes() throws Exception {
	String bucketName = this.stackResourceRegistry
			.lookupPhysicalResourceId("EmptyBucket");
	Resource resource = this.resourceLoader.getResource(
			S3_PREFIX + bucketName + "/testUploadFileWithMoreThenFiveMegabytes");
	assertTrue(WritableResource.class.isInstance(resource));
	WritableResource writableResource = (WritableResource) resource;
	OutputStream outputStream = writableResource.getOutputStream();
	for (int i = 0; i < (1024 * 1024 * 6); i++) {
		outputStream.write("c".getBytes("UTF-8"));
	}
	outputStream.close();
	this.createdObjects.add("testUploadFileWithMoreThenFiveMegabytes");
}
 
Example #12
Source File: GoogleStorageTests.java    From spring-cloud-gcp with Apache License 2.0 4 votes vote down vote up
@Test
public void testBucketOutputStream() throws IOException {
	this.expectedEx.expect(IllegalStateException.class);
	this.expectedEx.expectMessage("Cannot open an output stream to a bucket: 'gs://test-spring/'");
	((WritableResource) this.bucketResource).getOutputStream();
}
 
Example #13
Source File: S3StoreResource.java    From spring-content with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isWritable() {
	return ((WritableResource) delegate).isWritable();
}
 
Example #14
Source File: S3StoreResource.java    From spring-content with Apache License 2.0 4 votes vote down vote up
@Override
public OutputStream getOutputStream() throws IOException {
	return ((WritableResource) delegate).getOutputStream();
}
 
Example #15
Source File: SpringCloudS3.java    From tutorials with MIT License 4 votes vote down vote up
public void uploadFileToS3(File file, String s3Url) throws IOException {
    WritableResource resource = (WritableResource) resourceLoader.getResource(s3Url);
    try (OutputStream outputStream = resource.getOutputStream()) {
        Files.copy(file.toPath(), outputStream);
    }
}