use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.
the class DefaultApplicationConfigurationFactory method getTokenFileFromClassloader.
/**
* Gets token file from the classpath using the provided {@code context}.
* <p>
* The {@code contextClass} may be a class which is defined in the Web
* Application module/bundle and in this case it may be used to get Web
* Application resources. Also a {@link VaadinContext} {@code context}
* instance may be used to get a context of the Web Application (since the
* {@code contextClass} may be a class not from Web Application module). In
* WAR case it doesn't matter which class is used to get the resources (Web
* Application classes or e.g. "flow-server" classes) since they are loaded
* by the same {@link ClassLoader}. But in OSGi "flow-server" module classes
* can't be used to get Web Application resources since they are in
* different bundles.
*
* @param context
* a VaadinContext which may provide information how to get token
* file for the web application
* @return the token file content
* @throws IOException
* if I/O fails during access to the token file
*/
protected String getTokenFileFromClassloader(VaadinContext context) throws IOException {
String tokenResource = VAADIN_SERVLET_RESOURCES + TOKEN_FILE;
Lookup lookup = context.getAttribute(Lookup.class);
ResourceProvider resourceProvider = lookup.lookup(ResourceProvider.class);
List<URL> resources = resourceProvider.getApplicationResources(tokenResource);
// Accept resource that doesn't contain
// 'jar!/META-INF/Vaadin/config/flow-build-info.json'
URL resource = resources.stream().filter(url -> !url.getPath().endsWith("jar!/" + tokenResource)).findFirst().orElse(null);
if (resource == null && !resources.isEmpty()) {
return getPossibleJarResource(context, resources);
}
return resource == null ? null : FrontendUtils.streamToString(resource.openStream());
}
use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.
the class DefaultApplicationConfigurationFactoryTest method create_tokenFileIsReadFromClassloader_externalStatsFileIsReadFromTokenFile_predefinedContext.
@Test
public void create_tokenFileIsReadFromClassloader_externalStatsFileIsReadFromTokenFile_predefinedContext() throws MalformedURLException, IOException {
VaadinContext context = Mockito.mock(VaadinContext.class);
VaadinConfig config = Mockito.mock(VaadinConfig.class);
ResourceProvider resourceProvider = mockResourceProvider(config, context);
String content = "{ 'externalStatsFile':true }";
mockClassPathTokenFile(resourceProvider, content);
DefaultApplicationConfigurationFactory factory = new DefaultApplicationConfigurationFactory();
ApplicationConfiguration configuration = factory.create(context);
List<String> propertyNames = Collections.list(configuration.getPropertyNames());
Assert.assertTrue(propertyNames.contains(Constants.EXTERNAL_STATS_FILE));
Assert.assertTrue(configuration.getBooleanProperty(Constants.EXTERNAL_STATS_FILE, false));
Assert.assertFalse(configuration.isProductionMode());
Assert.assertFalse(configuration.enableDevServer());
}
use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.
the class DefaultApplicationConfigurationFactoryTest method mockResourceProvider.
private ResourceProvider mockResourceProvider(VaadinConfig config, VaadinContext context) {
Mockito.when(config.getVaadinContext()).thenReturn(context);
Mockito.when(context.getContextParameterNames()).thenReturn(Collections.emptyEnumeration());
Mockito.when(config.getConfigParameterNames()).thenReturn(Collections.emptyEnumeration());
ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
Mockito.when(context.getAttribute(ApplicationConfiguration.class)).thenReturn(appConfig);
Mockito.when(context.getAttribute(Mockito.eq(ApplicationConfiguration.class), Mockito.any())).thenReturn(appConfig);
Lookup lookup = Mockito.mock(Lookup.class);
ResourceProvider resourceProvider = Mockito.mock(ResourceProvider.class);
Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(resourceProvider);
Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
return resourceProvider;
}
use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.
the class DefaultApplicationConfigurationFactoryTest method getTokenFileFromClassloader_tokenFileIsRead_checkWebpackGeneratedFromContext.
@Test
public void getTokenFileFromClassloader_tokenFileIsRead_checkWebpackGeneratedFromContext() throws IOException {
VaadinContext context = Mockito.mock(VaadinContext.class);
VaadinConfig config = Mockito.mock(VaadinConfig.class);
ResourceProvider resourceProvider = mockResourceProvider(config, context);
String path = VAADIN_SERVLET_RESOURCES + TOKEN_FILE;
String content = "{ 'foo':'bar' }";
mockClassPathTokenFile(resourceProvider, content);
DefaultApplicationConfigurationFactory factory = new DefaultApplicationConfigurationFactory();
String tokenFileContent = factory.getTokenFileFromClassloader(context);
Mockito.verify(resourceProvider).getApplicationResource(FrontendUtils.WEBPACK_GENERATED);
Mockito.verify(resourceProvider).getApplicationResources(path);
Assert.assertEquals(content, tokenFileContent.trim());
}
use of com.vaadin.flow.di.ResourceProvider in project flow by vaadin.
the class DefaultApplicationConfigurationFactoryTest method create_tokenFileIsReadFromClassloader_externalStatsUrlIsReadFromTokenFile_predefinedContext.
@Test
public void create_tokenFileIsReadFromClassloader_externalStatsUrlIsReadFromTokenFile_predefinedContext() throws IOException {
VaadinContext context = Mockito.mock(VaadinContext.class);
VaadinConfig config = Mockito.mock(VaadinConfig.class);
ResourceProvider resourceProvider = mockResourceProvider(config, context);
mockClassPathTokenFile(resourceProvider, "{ 'externalStatsUrl': 'http://my.server/static/stats.json'}");
DefaultApplicationConfigurationFactory factory = new DefaultApplicationConfigurationFactory();
ApplicationConfiguration configuration = factory.create(context);
List<String> propertyNames = Collections.list(configuration.getPropertyNames());
Assert.assertTrue(propertyNames.contains(Constants.EXTERNAL_STATS_URL));
Assert.assertTrue(configuration.getBooleanProperty(Constants.EXTERNAL_STATS_FILE, false));
Assert.assertEquals("http://my.server/static/stats.json", configuration.getStringProperty(Constants.EXTERNAL_STATS_URL, null));
Assert.assertFalse(configuration.isProductionMode());
Assert.assertFalse(configuration.enableDevServer());
}
Aggregations