use of org.springframework.core.io.FileSystemResource in project ratpack by ratpack.
the class RatpackProperties method resourceToPath.
static Path resourceToPath(URL resource) {
Objects.requireNonNull(resource, "Resource URL cannot be null");
URI uri;
try {
uri = resource.toURI();
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Could not extract URI", e);
}
String scheme = uri.getScheme();
if (scheme.equals("file")) {
String path = uri.toString().substring("file:".length());
if (path.contains("//")) {
path = StringUtils.cleanPath(path.replace("//", ""));
}
return Paths.get(new FileSystemResource(path).getFile().toURI());
}
if (!scheme.equals("jar")) {
throw new IllegalArgumentException("Cannot convert to Path: " + uri);
}
String s = uri.toString();
int separator = s.indexOf("!/");
URI fileURI = URI.create(s.substring(0, separator) + "!/");
try {
FileSystems.getFileSystem(fileURI);
} catch (FileSystemNotFoundException e) {
try {
FileSystems.newFileSystem(fileURI, Collections.singletonMap("create", "true"));
} catch (IOException e1) {
throw new IllegalArgumentException("Cannot convert to Path: " + uri);
}
}
return Paths.get(fileURI);
}
use of org.springframework.core.io.FileSystemResource in project uPortal by Jasig.
the class CachingResourceLoaderImplTest method testCachedModifiedLoad.
@Test
public void testCachedModifiedLoad() throws Exception {
final Resource doc1Resouce = new FileSystemResource(doc1);
final CachingResourceLoaderImpl loader = new CachingResourceLoaderImpl();
final Ehcache cache = createMock(Ehcache.class);
final CachedResource<?> cachedResource = createMock(CachedResource.class);
final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
expect(cache.getInternalContext()).andReturn(null).anyTimes();
expect(cache.getCacheConfiguration()).andReturn(new CacheConfiguration());
expect(cache.get(doc1Resouce)).andReturn(new Element(doc1Resouce, cachedResource));
final long lastModified = doc1.lastModified();
expect(cachedResource.getResource()).andReturn(doc1Resouce);
expect(cachedResource.getLastCheckTime()).andReturn(lastModified - TimeUnit.MINUTES.toMillis(5));
expect(cachedResource.getLastLoadTime()).andReturn(lastModified - TimeUnit.MINUTES.toMillis(5));
cache.put(anyObject(Element.class));
expectLastCall();
replay(cache, cachedResource, elementsProvider);
loader.setResourceCache(cache);
loader.setResourcesElementsProvider(elementsProvider);
final CachedResource<String> cachedResource1 = loader.getResource(doc1Resouce, StringResourceBuilder.INSTANCE);
verify(cache, cachedResource, elementsProvider);
assertNotNull(cachedResource1);
final String expected = IOUtils.toString(new FileReader(doc1));
assertEquals(expected, cachedResource1.getCachedResource());
}
use of org.springframework.core.io.FileSystemResource in project uPortal by Jasig.
the class CachingResourceLoaderImplTest method testUncachedLoadNoDigest.
@Test
public void testUncachedLoadNoDigest() throws Exception {
final Resource doc1Resouce = new FileSystemResource(doc1);
final CachingResourceLoaderImpl loader = new CachingResourceLoaderImpl();
final Ehcache cache = createMock(Ehcache.class);
final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
expect(cache.getInternalContext()).andReturn(null).anyTimes();
expect(cache.getCacheConfiguration()).andReturn(new CacheConfiguration());
expect(cache.get(doc1Resouce)).andReturn(null);
expect(cache.getQuiet(doc1Resouce)).andReturn(null);
cache.put(anyObject(Element.class));
expectLastCall();
replay(cache, elementsProvider);
loader.setResourceCache(cache);
loader.setResourcesElementsProvider(elementsProvider);
final CachedResource<String> cachedResource1 = loader.getResource(doc1Resouce, StringResourceBuilder.INSTANCE);
verify(cache, elementsProvider);
assertNotNull(cachedResource1);
final String expected = IOUtils.toString(new FileReader(doc1));
assertEquals(expected, cachedResource1.getCachedResource());
}
use of org.springframework.core.io.FileSystemResource in project uPortal by Jasig.
the class CachingResourceLoaderImplTest method testCachedNotModified.
@Test
public void testCachedNotModified() throws Exception {
final Resource doc1Resouce = new FileSystemResource(doc1);
final CachingResourceLoaderImpl loader = new CachingResourceLoaderImpl();
final Ehcache cache = createMock(Ehcache.class);
final CachedResource<?> cachedResource = createMock(CachedResource.class);
final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
expect(cache.getInternalContext()).andReturn(null).anyTimes();
expect(cache.getCacheConfiguration()).andReturn(new CacheConfiguration());
final Element element = new Element("class path resource [CachingResourceLoaderImplTest_doc1.txt]", cachedResource);
expect(cache.get(doc1Resouce)).andReturn(element);
final long lastModified = doc1.lastModified();
expect(cachedResource.getResource()).andReturn(doc1Resouce);
expect(cachedResource.getLastCheckTime()).andReturn(0L);
expect(cachedResource.getLastLoadTime()).andReturn(lastModified + TimeUnit.MINUTES.toMillis(5));
expect(cachedResource.getAdditionalResources()).andReturn(Collections.EMPTY_MAP);
cachedResource.setLastCheckTime(anyLong());
cache.put(element);
expectLastCall();
replay(cache, cachedResource, elementsProvider);
loader.setResourceCache(cache);
loader.setResourcesElementsProvider(elementsProvider);
final CachedResource<String> cachedResource1 = loader.getResource(doc1Resouce, StringResourceBuilder.INSTANCE);
verify(cache, cachedResource, elementsProvider);
assertNotNull(cachedResource1);
assertTrue(cachedResource1 == cachedResource);
}
use of org.springframework.core.io.FileSystemResource in project uPortal by Jasig.
the class CachingResourceLoaderImplTest method testCachedWithinInterval.
@Test
public void testCachedWithinInterval() throws Exception {
final Resource doc1Resouce = new FileSystemResource(doc1);
final CachingResourceLoaderImpl loader = new CachingResourceLoaderImpl();
final Ehcache cache = createMock(Ehcache.class);
final CachedResource<?> cachedResource = createMock(CachedResource.class);
final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
expect(cache.getInternalContext()).andReturn(null).anyTimes();
expect(cache.getCacheConfiguration()).andReturn(new CacheConfiguration());
expect(cache.get(doc1Resouce)).andReturn(new Element(doc1Resouce, cachedResource));
expect(cachedResource.getLastCheckTime()).andReturn(System.currentTimeMillis());
replay(cache, cachedResource, elementsProvider);
loader.setResourceCache(cache);
loader.setResourcesElementsProvider(elementsProvider);
final CachedResource<String> cachedResource1 = loader.getResource(doc1Resouce, StringResourceBuilder.INSTANCE);
verify(cache, cachedResource, elementsProvider);
assertNotNull(cachedResource1);
assertTrue(cachedResource1 == cachedResource);
}
Aggregations