Search in sources :

Example 26 with ResourceProvider

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());
}
Also used : ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup) URL(java.net.URL)

Example 27 with ResourceProvider

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());
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) VaadinConfig(com.vaadin.flow.server.VaadinConfig) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Test(org.junit.Test)

Example 28 with ResourceProvider

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;
}
Also used : ResourceProvider(com.vaadin.flow.di.ResourceProvider) Lookup(com.vaadin.flow.di.Lookup)

Example 29 with 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());
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) VaadinConfig(com.vaadin.flow.server.VaadinConfig) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Test(org.junit.Test)

Example 30 with ResourceProvider

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());
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) VaadinConfig(com.vaadin.flow.server.VaadinConfig) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Test(org.junit.Test)

Aggregations

ResourceProvider (com.vaadin.flow.di.ResourceProvider)30 Lookup (com.vaadin.flow.di.Lookup)22 Test (org.junit.Test)11 URL (java.net.URL)10 VaadinContext (com.vaadin.flow.server.VaadinContext)8 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)5 File (java.io.File)5 IOException (java.io.IOException)5 Before (org.junit.Before)5 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4 VaadinConfig (com.vaadin.flow.server.VaadinConfig)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 Properties (java.util.Properties)4 Instantiator (com.vaadin.flow.di.Instantiator)3 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)3 ServletContext (javax.servlet.ServletContext)3 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 StaticFileServer (com.vaadin.flow.server.StaticFileServer)2