Java Code Examples for org.springframework.core.io.Resource#createRelative()

The following examples show how to use org.springframework.core.io.Resource#createRelative() . These examples are extracted from open source projects. 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
@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 2
/**
 * Determine JPA's default "META-INF/orm.xml" resource for use with Spring's default
 * persistence unit, if any.
 * <p>Checks whether a "META-INF/orm.xml" file exists in the classpath and uses it
 * if it is not co-located with a "META-INF/persistence.xml" file.
 */
private Resource getOrmXmlForDefaultPersistenceUnit() {
	Resource ormXml = this.resourcePatternResolver.getResource(
			this.defaultPersistenceUnitRootLocation + DEFAULT_ORM_XML_RESOURCE);
	if (ormXml.exists()) {
		try {
			Resource persistenceXml = ormXml.createRelative(PERSISTENCE_XML_FILENAME);
			if (!persistenceXml.exists()) {
				return ormXml;
			}
		}
		catch (IOException ex) {
			// Cannot resolve relative persistence.xml file - let's assume it's not there.
			return ormXml;
		}
	}
	return null;
}
 
Example 3
/**
 * Determine JPA's default "META-INF/orm.xml" resource for use with Spring's default
 * persistence unit, if any.
 * <p>Checks whether a "META-INF/orm.xml" file exists in the classpath and uses it
 * if it is not co-located with a "META-INF/persistence.xml" file.
 */
@Nullable
private Resource getOrmXmlForDefaultPersistenceUnit() {
	Resource ormXml = this.resourcePatternResolver.getResource(
			this.defaultPersistenceUnitRootLocation + DEFAULT_ORM_XML_RESOURCE);
	if (ormXml.exists()) {
		try {
			Resource persistenceXml = ormXml.createRelative(PERSISTENCE_XML_FILENAME);
			if (!persistenceXml.exists()) {
				return ormXml;
			}
		}
		catch (IOException ex) {
			// Cannot resolve relative persistence.xml file - let's assume it's not there.
			return ormXml;
		}
	}
	return null;
}
 
Example 4
/**
 * Determine whether to register JPA's default "META-INF/orm.xml" with
 * Spring's default persistence unit, if any.
 * <p>Checks whether a "META-INF/orm.xml" file exists in the classpath and
 * uses it if it is not co-located with a "META-INF/persistence.xml" file.
 */
private boolean useOrmXmlForDefaultPersistenceUnit() {
	Resource ormXml = this.resourcePatternResolver.getResource(
			this.defaultPersistenceUnitRootLocation + DEFAULT_ORM_XML_RESOURCE);
	if (ormXml.exists()) {
		try {
			Resource persistenceXml = ormXml.createRelative(PERSISTENCE_XML_FILENAME);
			if (!persistenceXml.exists()) {
				return true;
			}
		}
		catch (IOException ex) {
			// Cannot resolve relative persistence.xml file - let's assume it's not there.
			return true;
		}
	}
	return false;
}
 
Example 5
/**
 * Find the resource under the given location.
 * <p>The default implementation checks if there is a readable
 * {@code Resource} for the given path relative to the location.
 * @param resourcePath the path to the resource
 * @param location the location to check
 * @return the resource, or {@code null} if none found
 */
@Nullable
protected Resource getResource(String resourcePath, Resource location) throws IOException {
	Resource resource = location.createRelative(resourcePath);
	if (resource.isReadable()) {
		if (checkResource(resource, location)) {
			return resource;
		}
		else if (logger.isWarnEnabled()) {
			Resource[] allowedLocations = getAllowedLocations();
			logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " +
					"but resource \"" +	resource.getURL() + "\" is neither under the " +
					"current location \"" + location.getURL() + "\" nor under any of the " +
					"allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]"));
		}
	}
	return null;
}
 
Example 6
/**
 * Find the resource under the given location.
 * <p>The default implementation checks if there is a readable
 * {@code Resource} for the given path relative to the location.
 * @param resourcePath the path to the resource
 * @param location the location to check
 * @return the resource, or {@code null} if none found
 */
protected Resource getResource(String resourcePath, Resource location) throws IOException {
	Resource resource = location.createRelative(resourcePath);
	if (resource.exists() && resource.isReadable()) {
		if (checkResource(resource, location)) {
			return resource;
		}
		else if (logger.isTraceEnabled()) {
			logger.trace("Resource path=\"" + resourcePath + "\" was successfully resolved " +
					"but resource=\"" +	resource.getURL() + "\" is neither under the " +
					"current location=\"" + location.getURL() + "\" nor under any of the " +
					"allowed locations=" + Arrays.asList(getAllowedLocations()));
		}
	}
	return null;
}
 
Example 7
@Test
public void testServletContextResourceWithRelativePath() throws IOException {
	MockServletContext sc = new MockServletContext();
	Resource resource = new ServletContextResource(sc, "dir/");
	Resource relative = resource.createRelative("subdir");
	assertEquals(new ServletContextResource(sc, "dir/subdir"), relative);
}
 
Example 8
/**
 * Find the resource under the given location.
 * <p>The default implementation checks if there is a readable
 * {@code Resource} for the given path relative to the location.
 * @param resourcePath the path to the resource
 * @param location the location to check
 * @return the resource, or empty {@link Mono} if none found
 */
protected Mono<Resource> getResource(String resourcePath, Resource location) {
	try {
		if (location instanceof ClassPathResource) {
			resourcePath = UriUtils.decode(resourcePath, StandardCharsets.UTF_8);
		}
		Resource resource = location.createRelative(resourcePath);
		if (resource.isReadable()) {
			if (checkResource(resource, location)) {
				return Mono.just(resource);
			}
			else if (logger.isWarnEnabled()) {
				Resource[] allowedLocations = getAllowedLocations();
				logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " +
						"but resource \"" + resource.getURL() + "\" is neither under the " +
						"current location \"" + location.getURL() + "\" nor under any of the " +
						"allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]"));
			}
		}
		return Mono.empty();
	}
	catch (IOException ex) {
		if (logger.isDebugEnabled()) {
			String error = "Skip location [" + location + "] due to error";
			if (logger.isTraceEnabled()) {
				logger.trace(error, ex);
			}
			else {
				logger.debug(error + ": " + ex.getMessage());
			}
		}
		return Mono.error(ex);
	}
}
 
Example 9
@Override
public Resource getAppResource( App app, String pageName )
    throws IOException
{
    List<Resource> locations = Lists.newArrayList(
        resourceLoader.getResource( "file:" + getAppFolderPath() + "/" + app.getFolderName() + "/" ),
        resourceLoader.getResource( "classpath*:/apps/" + app.getFolderName() + "/" )
    );

    for ( Resource location : locations )
    {
        Resource resource = location.createRelative( pageName );

        if ( resource.exists() && resource.isReadable() )
        {
            File file = resource.getFile();

            // Make sure that file resolves into path app folder
            if ( file != null && file.toPath().startsWith( getAppFolderPath() ) )
            {
                return resource;
            }
        }
    }

    return null;
}
 
Example 10
Source Project: spring-analysis-note   File: ResourceTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testServletContextResourceWithRelativePath() throws IOException {
	MockServletContext sc = new MockServletContext();
	Resource resource = new ServletContextResource(sc, "dir/");
	Resource relative = resource.createRelative("subdir");
	assertEquals(new ServletContextResource(sc, "dir/subdir"), relative);
}
 
Example 11
Source Project: spring-analysis-note   File: ResourceTests.java    License: MIT License 5 votes vote down vote up
private void doTestResource(Resource resource) throws IOException {
	assertEquals("Resource.class", resource.getFilename());
	assertTrue(resource.getURL().getFile().endsWith("Resource.class"));

	Resource relative1 = resource.createRelative("ClassPathResource.class");
	assertEquals("ClassPathResource.class", relative1.getFilename());
	assertTrue(relative1.getURL().getFile().endsWith("ClassPathResource.class"));
	assertTrue(relative1.exists());

	Resource relative2 = resource.createRelative("support/ResourcePatternResolver.class");
	assertEquals("ResourcePatternResolver.class", relative2.getFilename());
	assertTrue(relative2.getURL().getFile().endsWith("ResourcePatternResolver.class"));
	assertTrue(relative2.exists());
}
 
Example 12
/**
 * Find the resource under the given location.
 * <p>The default implementation checks if there is a readable
 * {@code Resource} for the given path relative to the location.
 * @param resourcePath the path to the resource
 * @param location the location to check
 * @return the resource, or empty {@link Mono} if none found
 */
protected Mono<Resource> getResource(String resourcePath, Resource location) {
	try {
		Resource resource = location.createRelative(resourcePath);
		if (resource.isReadable()) {
			if (checkResource(resource, location)) {
				return Mono.just(resource);
			}
			else if (logger.isWarnEnabled()) {
				Resource[] allowedLocations = getAllowedLocations();
				logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " +
						"but resource \"" + resource.getURL() + "\" is neither under the " +
						"current location \"" + location.getURL() + "\" nor under any of the " +
						"allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]"));
			}
		}
		return Mono.empty();
	}
	catch (IOException ex) {
		if (logger.isDebugEnabled()) {
			String error = "Skip location [" + location + "] due to error";
			if (logger.isTraceEnabled()) {
				logger.trace(error, ex);
			}
			else {
				logger.debug(error + ": " + ex.getMessage());
			}
		}
		return Mono.error(ex);
	}
}
 
Example 13
Source Project: java-technology-stack   File: ResourceTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testServletContextResourceWithRelativePath() throws IOException {
	MockServletContext sc = new MockServletContext();
	Resource resource = new ServletContextResource(sc, "dir/");
	Resource relative = resource.createRelative("subdir");
	assertEquals(new ServletContextResource(sc, "dir/subdir"), relative);
}
 
Example 14
Source Project: java-technology-stack   File: ResourceTests.java    License: MIT License 5 votes vote down vote up
private void doTestResource(Resource resource) throws IOException {
	assertEquals("Resource.class", resource.getFilename());
	assertTrue(resource.getURL().getFile().endsWith("Resource.class"));

	Resource relative1 = resource.createRelative("ClassPathResource.class");
	assertEquals("ClassPathResource.class", relative1.getFilename());
	assertTrue(relative1.getURL().getFile().endsWith("ClassPathResource.class"));
	assertTrue(relative1.exists());

	Resource relative2 = resource.createRelative("support/ResourcePatternResolver.class");
	assertEquals("ResourcePatternResolver.class", relative2.getFilename());
	assertTrue(relative2.getURL().getFile().endsWith("ResourcePatternResolver.class"));
	assertTrue(relative2.exists());
}
 
Example 15
private void doTestResource(Resource resource) throws IOException {
	assertEquals("Resource.class", resource.getFilename());
	assertTrue(resource.getURL().getFile().endsWith("Resource.class"));

	Resource relative1 = resource.createRelative("ClassPathResource.class");
	assertEquals("ClassPathResource.class", relative1.getFilename());
	assertTrue(relative1.getURL().getFile().endsWith("ClassPathResource.class"));
	assertTrue(relative1.exists());

	Resource relative2 = resource.createRelative("support/ResourcePatternResolver.class");
	assertEquals("ResourcePatternResolver.class", relative2.getFilename());
	assertTrue(relative2.getURL().getFile().endsWith("ResourcePatternResolver.class"));
	assertTrue(relative2.exists());
}
 
Example 16
EncodedResource(Resource original, String coding, String extension) throws IOException {
	this.original = original;
	this.coding = coding;
	this.encoded = original.createRelative(original.getFilename() + extension);
}
 
Example 17
EncodedResource(Resource original, String coding, String extension) throws IOException {
	this.original = original;
	this.coding = coding;
	this.encoded = original.createRelative(original.getFilename() + extension);
}
 
Example 18
public GzippedResource(Resource original) throws IOException {
	this.original = original;
	this.gzipped = original.createRelative(original.getFilename() + ".gz");
}
 
Example 19
EncodedResource(Resource original, String coding, String extension) throws IOException {
	this.original = original;
	this.coding = coding;
	this.encoded = original.createRelative(original.getFilename() + extension);
}
 
Example 20
public GzippedResource(Resource original) throws IOException {
	this.original = original;
	this.gzipped = original.createRelative(original.getFilename() + ".gz");
}