org.springframework.core.io.UrlResource Java Examples
The following examples show how to use
org.springframework.core.io.UrlResource.
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: BeanFactoryGenericsTests.java From spring-analysis-note with MIT License | 7 votes |
@Test public void testGenericMapResourceConstructor() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); Map<String, String> input = new HashMap<>(); input.put("4", "5"); input.put("6", "7"); rbd.getConstructorArgumentValues().addGenericArgumentValue(input); rbd.getConstructorArgumentValues().addGenericArgumentValue("http://localhost:8080"); bf.registerBeanDefinition("genericBean", rbd); GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean"); assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); }
Example #2
Source File: ConfigurationSummary.java From ldp4j with Apache License 2.0 | 6 votes |
private ResourceSource getResourceSource(Resource resource) { ResourceSource source=null; if(resource instanceof ClassPathResource) { source=ResourceSource.CLASSPATH; } else if(resource instanceof UrlResource) { source=ResourceSource.REMOTE; } else if(resource instanceof FileSystemResource) { source=ResourceSource.FILE_SYSTEM; } else if(resource instanceof InputStreamResource) { source=ResourceSource.STREAM; } else if(resource instanceof ByteArrayResource) { source=ResourceSource.RAW; } else { String type=resource.getClass().toString(); if(CLAZZ_NAME.equals(type)) { source=ResourceSource.OSGI_BUNDLE; } else { source=ResourceSource.UNKNOWN; } } return source; }
Example #3
Source File: SSLConfig.java From micro-server with Apache License 2.0 | 6 votes |
@Bean public static SSLProperties sslProperties() throws IOException { PropertiesFactoryBean factory = new PropertiesFactoryBean(); URL url = SSLConfig.class.getClassLoader().getResource("ssl.properties"); if (url != null) { Resource reource = new UrlResource(url); factory.setLocation(reource); factory.afterPropertiesSet(); Properties properties = factory.getObject(); return SSLProperties.builder() .keyStoreFile(properties.getProperty(keyStoreFile)) .keyStorePass(properties.getProperty(keyStorePass)) .trustStoreFile(properties.getProperty(trustStoreFile)) .trustStorePass(properties.getProperty(trustStorePass)) .keyStoreType(properties.getProperty(keyStoreType)) .keyStoreProvider(properties.getProperty(keyStoreProvider)) .trustStoreType(properties.getProperty(trustStoreType)) .trustStoreProvider(properties.getProperty(trustStoreProvider)) .clientAuth(properties.getProperty(clientAuth)) .ciphers(properties.getProperty(ciphers)) .protocol(properties.getProperty(protocol)).build(); } return null; }
Example #4
Source File: ResourcesFileSource.java From spring-cloud-contract with Apache License 2.0 | 6 votes |
@Override public FileSource child(String subDirectoryName) { List<FileSource> childSources = new ArrayList<>(); for (FileSource resource : this.sources) { try { UrlResource uri = new UrlResource( resource.child(subDirectoryName).getUri()); if (uri.createRelative(subDirectoryName).exists()) { childSources.add(resource.child(subDirectoryName)); } } catch (IOException e) { // Ignore } } if (!childSources.isEmpty()) { return new ResourcesFileSource(childSources.toArray(new FileSource[0])); } return this.sources[0].child(subDirectoryName); }
Example #5
Source File: DefaultListableBeanFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testDoubleArrayConstructorWithAutowiring() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("integer1", new Integer(4)); bf.registerSingleton("integer2", new Integer(5)); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertEquals(new Integer(4), ab.getIntegerArray()[0]); assertEquals(new Integer(5), ab.getIntegerArray()[1]); assertEquals(new UrlResource("http://localhost:8080"), ab.getResourceArray()[0]); assertEquals(new UrlResource("http://localhost:9090"), ab.getResourceArray()[1]); }
Example #6
Source File: BeanFactoryGenericsTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testGenericSetListFactoryMethod() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); rbd.setFactoryMethodName("createInstance"); Set<String> input = new HashSet<>(); input.add("4"); input.add("5"); List<String> input2 = new ArrayList<>(); input2.add("http://localhost:8080"); input2.add("http://localhost:9090"); rbd.getConstructorArgumentValues().addGenericArgumentValue(input); rbd.getConstructorArgumentValues().addGenericArgumentValue(input2); bf.registerBeanDefinition("genericBean", rbd); GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean"); assertTrue(gb.getIntegerSet().contains(new Integer(4))); assertTrue(gb.getIntegerSet().contains(new Integer(5))); assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); }
Example #7
Source File: BeanFactoryGenericsTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testGenericSetListConstructor() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); Set<String> input = new HashSet<String>(); input.add("4"); input.add("5"); List<String> input2 = new ArrayList<String>(); input2.add("http://localhost:8080"); input2.add("http://localhost:9090"); rbd.getConstructorArgumentValues().addGenericArgumentValue(input); rbd.getConstructorArgumentValues().addGenericArgumentValue(input2); bf.registerBeanDefinition("genericBean", rbd); GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean"); assertTrue(gb.getIntegerSet().contains(new Integer(4))); assertTrue(gb.getIntegerSet().contains(new Integer(5))); assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); }
Example #8
Source File: IgniteSpringHelperImpl.java From ignite with Apache License 2.0 | 6 votes |
/** * Creates Spring application context. Optionally excluded properties can be specified, * it means that if such a property is found in {@link org.apache.ignite.configuration.IgniteConfiguration} * then it is removed before the bean is instantiated. * For example, {@code streamerConfiguration} can be excluded from the configs that Visor uses. * * @param cfgUrl Resource where config file is located. * @param excludedProps Properties to be excluded. * @return Spring application context. * @throws IgniteCheckedException If configuration could not be read. */ public static ApplicationContext applicationContext(URL cfgUrl, final String... excludedProps) throws IgniteCheckedException { try { GenericApplicationContext springCtx = prepareSpringContext(excludedProps); new XmlBeanDefinitionReader(springCtx).loadBeanDefinitions(new UrlResource(cfgUrl)); springCtx.refresh(); return springCtx; } catch (BeansException e) { if (X.hasCause(e, ClassNotFoundException.class)) throw new IgniteCheckedException("Failed to instantiate Spring XML application context " + "(make sure all classes used in Spring configuration are present at CLASSPATH) " + "[springUrl=" + cfgUrl + ']', e); else throw new IgniteCheckedException("Failed to instantiate Spring XML application context [springUrl=" + cfgUrl + ", err=" + e.getMessage() + ']', e); } }
Example #9
Source File: DefaultListableBeanFactoryTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testDoubleArrayConstructorWithAutowiring() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("integer1", new Integer(4)); bf.registerSingleton("integer2", new Integer(5)); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertEquals(new Integer(4), ab.getIntegerArray()[0]); assertEquals(new Integer(5), ab.getIntegerArray()[1]); assertEquals(new UrlResource("http://localhost:8080"), ab.getResourceArray()[0]); assertEquals(new UrlResource("http://localhost:9090"), ab.getResourceArray()[1]); }
Example #10
Source File: FileHelper.java From spring-boot-doma2-sample with Apache License 2.0 | 6 votes |
/** * ファイルを読み込みます。 * * @param location * @param filename * @return */ public Resource loadFile(Path location, String filename) { try { Path file = location.resolve(filename); Resource resource = new UrlResource(file.toUri()); if (resource.exists() || resource.isReadable()) { return resource; } throw new FileNotFoundException("could not read file. " + filename); } catch (MalformedURLException e) { throw new IllegalArgumentException( "malformed Url resource. [location=" + location.toString() + ", filename=" + filename + "]", e); } }
Example #11
Source File: ConsulBinderTests.java From spring-cloud-consul with Apache License 2.0 | 6 votes |
/** * Launch an application in a separate JVM. * @param clz the main class to launch * @param properties the properties to pass to the application * @param args the command line arguments for the application * @return a string identifier for the application */ private String launchApplication(Class<?> clz, Map<String, String> properties, List<String> args) { Resource resource = new UrlResource( clz.getProtectionDomain().getCodeSource().getLocation()); properties.put(AppDeployer.GROUP_PROPERTY_KEY, "test-group"); properties.put("main", clz.getName()); properties.put("classpath", System.getProperty("java.class.path")); String appName = String.format("%s-%s", clz.getSimpleName(), properties.get("server.port")); AppDefinition definition = new AppDefinition(appName, properties); AppDeploymentRequest request = new AppDeploymentRequest(definition, resource, properties, args); return this.deployer.deploy(request); }
Example #12
Source File: BeanFactoryGenericsTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testGenericMapResourceFactoryMethod() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); RootBeanDefinition rbd = new RootBeanDefinition(GenericBean.class); rbd.setFactoryMethodName("createInstance"); Map<String, String> input = new HashMap<String, String>(); input.put("4", "5"); input.put("6", "7"); rbd.getConstructorArgumentValues().addGenericArgumentValue(input); rbd.getConstructorArgumentValues().addGenericArgumentValue("http://localhost:8080"); bf.registerBeanDefinition("genericBean", rbd); GenericBean<?> gb = (GenericBean<?>) bf.getBean("genericBean"); assertEquals(new Integer(5), gb.getShortMap().get(new Short("4"))); assertEquals(new Integer(7), gb.getShortMap().get(new Short("6"))); assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); }
Example #13
Source File: FileSystemStorageService.java From tensorflow-java-examples-spring with Do What The F*ck You Want To Public License | 6 votes |
@Override public Resource loadAsResource(String filename) { try { Path file = load(filename); Resource resource = new UrlResource(file.toUri()); if (resource.exists() || resource.isReadable()) { return resource; } else { throw new StorageFileNotFoundException( "Could not read file: " + filename); } } catch (MalformedURLException e) { throw new StorageFileNotFoundException("Could not read file: " + filename, e); } }
Example #14
Source File: InitPropConfigFactory.java From mPaaS with Apache License 2.0 | 6 votes |
/** * 配置文件信息获取 * * @return */ @Override public Map<String, Object> defaultConfig() { Map<String, Object> rtnMap = new HashMap<>(1); try { ClassLoader classLoader = this.getClass().getClassLoader(); Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(getPropFilePath()) : ClassLoader.getSystemResources(getPropFilePath())); while (urls.hasMoreElements()) { URL url = urls.nextElement(); UrlResource resource = new UrlResource(url); Properties properties = PropertiesLoaderUtils.loadProperties(resource); for (Map.Entry<?, ?> entry : properties.entrySet()) { rtnMap.put((String) entry.getKey(), entry.getValue()); } } } catch (IOException e) { log.error("加载初始配置错误.", e); } return rtnMap; }
Example #15
Source File: FileSystemStorageService.java From code-examples with MIT License | 6 votes |
@Override public Resource loadAsResource(String filename) { try { Path file = load(filename); Resource resource = new UrlResource(file.toUri()); if (resource.exists() || resource.isReadable()) { return resource; } else { throw new FileNotFoundException( "Could not read file: " + filename); } } catch (MalformedURLException e) { throw new FileNotFoundException("Could not read file: " + filename, e); } }
Example #16
Source File: DefaultListableBeanFactoryTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testDoubleArrayConstructorWithAutowiring() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("integer1", new Integer(4)); bf.registerSingleton("integer2", new Integer(5)); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertEquals(new Integer(4), ab.getIntegerArray()[0]); assertEquals(new Integer(5), ab.getIntegerArray()[1]); assertEquals(new UrlResource("http://localhost:8080"), ab.getResourceArray()[0]); assertEquals(new UrlResource("http://localhost:9090"), ab.getResourceArray()[1]); }
Example #17
Source File: SpringDmnConfigurationHelper.java From flowable-engine with Apache License 2.0 | 5 votes |
public static DmnEngine buildDmnEngine(URL resource) { LOGGER.debug("==== BUILDING SPRING APPLICATION CONTEXT AND DMN ENGINE ========================================="); try (GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(new UrlResource(resource))) { Map<String, DmnEngine> beansOfType = applicationContext.getBeansOfType(DmnEngine.class); if ((beansOfType == null) || beansOfType.isEmpty()) { throw new FlowableException("no " + DmnEngine.class.getName() + " defined in the application context " + resource); } DmnEngine dmnEngine = beansOfType.values().iterator().next(); LOGGER.debug("==== SPRING DMN ENGINE CREATED =================================================================="); return dmnEngine; } }
Example #18
Source File: DefaultListableBeanFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testArrayPropertyWithAutowiring() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(ArrayBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); bf.registerBeanDefinition("arrayBean", rbd); ArrayBean ab = (ArrayBean) bf.getBean("arrayBean"); assertEquals(new UrlResource("http://localhost:8080"), ab.getResourceArray()[0]); assertEquals(new UrlResource("http://localhost:9090"), ab.getResourceArray()[1]); }
Example #19
Source File: ResourcesFileSource.java From spring-cloud-contract with Apache License 2.0 | 5 votes |
private static FileSource fileOrFallbackToClasspath(Resource resource) { UrlResource file = (UrlResource) resource; try { URI uri = file.getURI(); if (compressedResource(uri)) { return new ClasspathFileSource(pathFromCompressed(uri)); } return new SingleRootFileSource(getFile(file)); } catch (IOException e) { throw new IllegalStateException(e); } }
Example #20
Source File: SpringFormConfigurationHelper.java From flowable-engine with Apache License 2.0 | 5 votes |
public static FormEngine buildFormEngine(URL resource) { LOGGER.debug("==== BUILDING SPRING APPLICATION CONTEXT AND FORM ENGINE ========================================="); try (GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(new UrlResource(resource))) { Map<String, FormEngine> beansOfType = applicationContext.getBeansOfType(FormEngine.class); if ((beansOfType == null) || beansOfType.isEmpty()) { throw new FlowableException("no " + FormEngine.class.getName() + " defined in the application context " + resource); } FormEngine formEngine = beansOfType.values().iterator().next(); LOGGER.debug("==== SPRING FORM ENGINE CREATED =================================================================="); return formEngine; } }
Example #21
Source File: BeanWrapperGenericsTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testGenericListElement() throws MalformedURLException { GenericBean<?> gb = new GenericBean<Object>(); gb.setResourceList(new ArrayList<Resource>()); BeanWrapper bw = new BeanWrapperImpl(gb); bw.setPropertyValue("resourceList[0]", "http://localhost:8080"); assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); }
Example #22
Source File: BeanFactoryGenericsTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testGenericListPropertyWithAutowiring() throws MalformedURLException { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerSingleton("resource1", new UrlResource("http://localhost:8080")); bf.registerSingleton("resource2", new UrlResource("http://localhost:9090")); RootBeanDefinition rbd = new RootBeanDefinition(GenericIntegerBean.class); rbd.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE); bf.registerBeanDefinition("genericBean", rbd); GenericIntegerBean gb = (GenericIntegerBean) bf.getBean("genericBean"); assertEquals(new UrlResource("http://localhost:8080"), gb.getResourceList().get(0)); assertEquals(new UrlResource("http://localhost:9090"), gb.getResourceList().get(1)); }
Example #23
Source File: LocalStorage.java From litemall with MIT License | 5 votes |
@Override public Resource loadAsResource(String filename) { try { Path file = load(filename); Resource resource = new UrlResource(file.toUri()); if (resource.exists() || resource.isReadable()) { return resource; } else { return null; } } catch (MalformedURLException e) { logger.error(e.getMessage(), e); return null; } }
Example #24
Source File: PathResourceResolverTests.java From java-technology-stack with MIT License | 5 votes |
@Test // SPR-12624 public void checkRelativeLocation() throws Exception { String locationUrl= new UrlResource(getClass().getResource("./test/")).getURL().toExternalForm(); Resource location = new UrlResource(locationUrl.replace("/springframework","/../org/springframework")); List<Resource> locations = singletonList(location); assertNotNull(this.resolver.resolveResource(null, "main.css", locations, null).block(TIMEOUT)); }
Example #25
Source File: XmlBeanFactoryTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testUrlResourceWithImport() { URL url = getClass().getResource(RESOURCE_CONTEXT.getPath()); DefaultListableBeanFactory xbf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(url)); // comes from "resourceImport.xml" xbf.getBean("resource1", ResourceTestBean.class); // comes from "resource.xml" xbf.getBean("resource2", ResourceTestBean.class); }
Example #26
Source File: CandidateComponentsIndexLoader.java From java-technology-stack with MIT License | 5 votes |
@Nullable private static CandidateComponentsIndex doLoadIndex(ClassLoader classLoader) { if (shouldIgnoreIndex) { return null; } try { Enumeration<URL> urls = classLoader.getResources(COMPONENTS_RESOURCE_LOCATION); if (!urls.hasMoreElements()) { return null; } List<Properties> result = new ArrayList<>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); result.add(properties); } if (logger.isDebugEnabled()) { logger.debug("Loaded " + result.size() + "] index(es)"); } int totalCount = result.stream().mapToInt(Properties::size).sum(); return (totalCount > 0 ? new CandidateComponentsIndex(result) : null); } catch (IOException ex) { throw new IllegalStateException("Unable to load indexes from location [" + COMPONENTS_RESOURCE_LOCATION + "]", ex); } }
Example #27
Source File: PathUtils.java From spring-cloud-config with Apache License 2.0 | 5 votes |
private static boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException { if (resource.getClass() != location.getClass()) { return false; } String resourcePath; String locationPath; if (resource instanceof UrlResource) { resourcePath = resource.getURL().toExternalForm(); locationPath = StringUtils.cleanPath(location.getURL().toString()); } else if (resource instanceof ClassPathResource) { resourcePath = ((ClassPathResource) resource).getPath(); locationPath = StringUtils .cleanPath(((ClassPathResource) location).getPath()); } else { resourcePath = resource.getURL().getPath(); locationPath = StringUtils.cleanPath(location.getURL().getPath()); } if (locationPath.equals(resourcePath)) { return true; } locationPath = (locationPath.endsWith("/") || locationPath.isEmpty() ? locationPath : locationPath + "/"); return (resourcePath.startsWith(locationPath) && !isInvalidEncodedPath(resourcePath)); }
Example #28
Source File: FileStorageUtil.java From fastdep with Apache License 2.0 | 5 votes |
/** * Load file resource * * @param fileName file name * @return resource */ public Resource loadFileAsResource(String fileName) { try { Path filePath = this.fileStorageBaseLocation.resolve(fileName).normalize(); Resource resource = new UrlResource(filePath.toUri()); if (resource.exists()) { return resource; } else { throw new FileNotFoundException("File not found " + fileName); } } catch (MalformedURLException ex) { throw new FileNotFoundException("File not found " + fileName, ex); } }
Example #29
Source File: BannerListener.java From mPass with Apache License 2.0 | 5 votes |
@Override public void environmentPrepared(ConfigurableEnvironment environment) { if(springApplication.getWebApplicationType() == WebApplicationType.NONE){ return; } if (executed.compareAndSet(false, true)) { String bannerConfig = environment.getProperty(SpringApplication.BANNER_LOCATION_PROPERTY); if (StringUtils.isEmpty(bannerConfig)) { URL url = BannerListener.class.getResource(BANNER_PATH); springApplication.setBanner(new ResourceBanner(new UrlResource(url))); } } }
Example #30
Source File: AppResourceCommon.java From spring-cloud-dataflow with Apache License 2.0 | 5 votes |
private String getFileNameNoExtension(UrlResource urlResource) { URI uri = getUri(urlResource); String uriPath = uri.getPath(); Assert.isTrue(StringUtils.hasText(uriPath), "URI path doesn't exist"); String lastSegment = new File(uriPath).getName(); Assert.isTrue(lastSegment.indexOf(".") != -1, "URI file name extension doesn't exist"); return lastSegment.substring(0, lastSegment.lastIndexOf(".")); }