Search in sources :

Example 31 with FileSystemResource

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);
}
Also used : FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) URISyntaxException(java.net.URISyntaxException) FileSystemResource(org.springframework.core.io.FileSystemResource) IOException(java.io.IOException) URI(java.net.URI)

Example 32 with FileSystemResource

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());
}
Also used : ResourcesElementsProvider(org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider) Element(net.sf.ehcache.Element) LoadedResource(org.apereo.portal.utils.cache.resource.LoadedResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) CachedResource(org.apereo.portal.utils.cache.resource.CachedResource) Ehcache(net.sf.ehcache.Ehcache) FileReader(java.io.FileReader) FileSystemResource(org.springframework.core.io.FileSystemResource) CachingResourceLoaderImpl(org.apereo.portal.utils.cache.resource.CachingResourceLoaderImpl) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Test(org.junit.Test)

Example 33 with FileSystemResource

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());
}
Also used : ResourcesElementsProvider(org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider) Element(net.sf.ehcache.Element) LoadedResource(org.apereo.portal.utils.cache.resource.LoadedResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) CachedResource(org.apereo.portal.utils.cache.resource.CachedResource) Ehcache(net.sf.ehcache.Ehcache) FileReader(java.io.FileReader) FileSystemResource(org.springframework.core.io.FileSystemResource) CachingResourceLoaderImpl(org.apereo.portal.utils.cache.resource.CachingResourceLoaderImpl) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Test(org.junit.Test)

Example 34 with FileSystemResource

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);
}
Also used : ResourcesElementsProvider(org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider) Element(net.sf.ehcache.Element) LoadedResource(org.apereo.portal.utils.cache.resource.LoadedResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) CachedResource(org.apereo.portal.utils.cache.resource.CachedResource) Ehcache(net.sf.ehcache.Ehcache) FileSystemResource(org.springframework.core.io.FileSystemResource) CachingResourceLoaderImpl(org.apereo.portal.utils.cache.resource.CachingResourceLoaderImpl) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Test(org.junit.Test)

Example 35 with FileSystemResource

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);
}
Also used : ResourcesElementsProvider(org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider) Element(net.sf.ehcache.Element) LoadedResource(org.apereo.portal.utils.cache.resource.LoadedResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) CachedResource(org.apereo.portal.utils.cache.resource.CachedResource) Ehcache(net.sf.ehcache.Ehcache) FileSystemResource(org.springframework.core.io.FileSystemResource) CachingResourceLoaderImpl(org.apereo.portal.utils.cache.resource.CachingResourceLoaderImpl) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Test(org.junit.Test)

Aggregations

FileSystemResource (org.springframework.core.io.FileSystemResource)148 File (java.io.File)77 Test (org.junit.Test)44 Resource (org.springframework.core.io.Resource)42 Before (org.junit.Before)27 ClassPathResource (org.springframework.core.io.ClassPathResource)16 IOException (java.io.IOException)12 FileWriter (java.io.FileWriter)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 Properties (java.util.Properties)8 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)8 FileOutputStream (java.io.FileOutputStream)7 URL (java.net.URL)7 InputStreamResource (org.springframework.core.io.InputStreamResource)7 UrlResource (org.springframework.core.io.UrlResource)7 Date (java.util.Date)5 FilesystemResourceStorageDao (org.opennms.netmgt.dao.support.FilesystemResourceStorageDao)5 FileReader (java.io.FileReader)4 OutputStreamWriter (java.io.OutputStreamWriter)4