Java Code Examples for org.springframework.core.io.Resource#createRelative()
The following examples show how to use
org.springframework.core.io.Resource#createRelative() .
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: PathResourceResolver.java From lams with GNU General Public License v2.0 | 6 votes |
/** * 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 2
Source File: ResourceLoaderAwsTest.java From spring-cloud-aws with Apache License 2.0 | 6 votes |
@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: DefaultPersistenceUnitManager.java From lams with GNU General Public License v2.0 | 6 votes |
/** * 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 4
Source File: DefaultPersistenceUnitManager.java From java-technology-stack with MIT License | 6 votes |
/** * 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 5
Source File: DefaultPersistenceUnitManager.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * 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 6
Source File: PathResourceResolver.java From java-technology-stack with MIT License | 6 votes |
/** * 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 7
Source File: PathResourceResolver.java From spring-analysis-note with MIT License | 5 votes |
/** * 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 8
Source File: LocalAppStorageService.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
@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 9
Source File: ResourceTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
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 10
Source File: ResourceTests.java From spring-analysis-note with MIT License | 5 votes |
@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 File: ResourceTests.java From spring-analysis-note with MIT License | 5 votes |
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
Source File: ResourceTests.java From java-technology-stack with MIT License | 5 votes |
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 13
Source File: ResourceTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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 File: PathResourceResolver.java From java-technology-stack with MIT License | 5 votes |
/** * 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 15
Source File: ResourceTests.java From java-technology-stack with MIT License | 5 votes |
@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 16
Source File: EncodedResourceResolver.java From java-technology-stack with MIT License | 4 votes |
EncodedResource(Resource original, String coding, String extension) throws IOException { this.original = original; this.coding = coding; this.encoded = original.createRelative(original.getFilename() + extension); }
Example 17
Source File: GzipResourceResolver.java From java-technology-stack with MIT License | 4 votes |
public GzippedResource(Resource original) throws IOException { this.original = original; this.gzipped = original.createRelative(original.getFilename() + ".gz"); }
Example 18
Source File: GzipResourceResolver.java From java-technology-stack with MIT License | 4 votes |
public GzippedResource(Resource original) throws IOException { this.original = original; this.gzipped = original.createRelative(original.getFilename() + ".gz"); }
Example 19
Source File: EncodedResourceResolver.java From spring-analysis-note with MIT License | 4 votes |
EncodedResource(Resource original, String coding, String extension) throws IOException { this.original = original; this.coding = coding; this.encoded = original.createRelative(original.getFilename() + extension); }
Example 20
Source File: EncodedResourceResolver.java From spring-analysis-note with MIT License | 4 votes |
EncodedResource(Resource original, String coding, String extension) throws IOException { this.original = original; this.coding = coding; this.encoded = original.createRelative(original.getFilename() + extension); }