Search in sources :

Example 1 with WebComponentConfiguration

use of com.vaadin.flow.component.webcomponent.WebComponentConfiguration in project flow by vaadin.

the class WebComponentConfigurationRegistryInitializer method initialize.

@Override
@SuppressWarnings("rawtypes")
public void initialize(Set<Class<?>> classSet, VaadinContext context) throws VaadinInitializerException {
    WebComponentConfigurationRegistry instance = WebComponentConfigurationRegistry.getInstance(context);
    if (classSet == null || classSet.isEmpty()) {
        instance.setConfigurations(Collections.emptySet());
        return;
    }
    try {
        Set<WebComponentExporterFactory> factories = WebComponentExporterUtils.getFactories(classSet);
        Set<WebComponentConfiguration<? extends Component>> configurations = constructConfigurations(factories);
        validateTagNames(configurations);
        validateDistinctTagNames(configurations);
        instance.setConfigurations(configurations);
    } catch (Exception e) {
        throw new VaadinInitializerException(String.format("%s failed to collect %s implementations!", WebComponentConfigurationRegistryInitializer.class.getSimpleName(), WebComponentExporter.class.getSimpleName()), e);
    }
}
Also used : WebComponentConfiguration(com.vaadin.flow.component.webcomponent.WebComponentConfiguration) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) Component(com.vaadin.flow.component.Component) WebComponentExporter(com.vaadin.flow.component.WebComponentExporter) InvalidCustomElementNameException(com.vaadin.flow.server.InvalidCustomElementNameException) WebComponentExporterFactory(com.vaadin.flow.component.WebComponentExporterFactory)

Example 2 with WebComponentConfiguration

use of com.vaadin.flow.component.webcomponent.WebComponentConfiguration in project flow by vaadin.

the class WebComponentExporterTest method configuration_bindProxy_throwsIfExporterSharesTagWithComponent.

@Test(expected = IllegalStateException.class)
@SuppressWarnings("unchecked")
public void configuration_bindProxy_throwsIfExporterSharesTagWithComponent() {
    SharedTagExporter sharedTagExporter = new SharedTagExporter();
    WebComponentConfiguration<SharedTagComponent> sharedConfig = (WebComponentConfiguration<SharedTagComponent>) new WebComponentExporter.WebComponentConfigurationFactory().create(sharedTagExporter);
    sharedConfig.createWebComponentBinding(new MockInstantiator(), mock(Element.class), Json.createObject());
}
Also used : WebComponentConfiguration(com.vaadin.flow.component.webcomponent.WebComponentConfiguration) MockInstantiator(com.vaadin.flow.server.MockInstantiator) Element(com.vaadin.flow.dom.Element) Test(org.junit.Test)

Example 3 with WebComponentConfiguration

use of com.vaadin.flow.component.webcomponent.WebComponentConfiguration in project flow by vaadin.

the class WebComponentProviderTest method setupConfigurations.

@SuppressWarnings({ "unchecked", "rawtypes" })
@SafeVarargs
private final WebComponentConfigurationRegistry setupConfigurations(Class<? extends WebComponentExporter<? extends Component>>... exporters) {
    WebComponentConfigurationRegistry registry = setUpRegistry();
    final Set<Class<? extends WebComponentExporter<? extends Component>>> set = Stream.of(exporters).collect(Collectors.toSet());
    WebComponentExporter.WebComponentConfigurationFactory factory = new WebComponentExporter.WebComponentConfigurationFactory();
    Set<WebComponentConfiguration<? extends Component>> configurations = new HashSet<>();
    for (Class<? extends WebComponentExporter<? extends Component>> exporter : exporters) configurations.add(factory.create(new DefaultWebComponentExporterFactory(exporter).create()));
    registry.setConfigurations(configurations);
    return registry;
}
Also used : WebComponentConfiguration(com.vaadin.flow.component.webcomponent.WebComponentConfiguration) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) WebComponent(com.vaadin.flow.component.webcomponent.WebComponent) Component(com.vaadin.flow.component.Component) WebComponentExporter(com.vaadin.flow.component.WebComponentExporter) HashSet(java.util.HashSet) DefaultWebComponentExporterFactory(com.vaadin.flow.component.WebComponentExporterFactory.DefaultWebComponentExporterFactory)

Example 4 with WebComponentConfiguration

use of com.vaadin.flow.component.webcomponent.WebComponentConfiguration in project flow by vaadin.

the class WebComponentProvider method synchronizedHandleRequest.

@Override
public boolean synchronizedHandleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    String pathInfo = request.getPathInfo();
    final ComponentInfo componentInfo = new ComponentInfo(pathInfo);
    if (!componentInfo.hasExtension()) {
        LoggerFactory.getLogger(WebComponentProvider.class).info("Received web-component request without extension " + "information (.js/.html) with request path {}", pathInfo);
        return false;
    }
    if (componentInfo.getTag() == null) {
        LoggerFactory.getLogger(WebComponentProvider.class).info("Received web-component request for non-custom element with request path {}", pathInfo);
        return false;
    }
    if (componentInfo.isHTML()) {
        LoggerFactory.getLogger(WebComponentProvider.class).info("Received web-component request for html component in npm" + " mode with request path {}", pathInfo);
        return false;
    }
    WebComponentConfigurationRegistry registry = WebComponentConfigurationRegistry.getInstance(request.getService().getContext());
    Optional<WebComponentConfiguration<? extends Component>> optionalWebComponentConfiguration = registry.getConfiguration(componentInfo.tag);
    if (optionalWebComponentConfiguration.isPresent()) {
        WebComponentConfiguration<? extends Component> webComponentConfiguration = optionalWebComponentConfiguration.get();
        String generated;
        Supplier<String> responder;
        response.setContentType(CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8);
        responder = () -> generateNPMResponse(webComponentConfiguration.getTag(), request, response);
        if (cache == null) {
            generated = responder.get();
        } else {
            generated = cache.computeIfAbsent(componentInfo.tag, moduleTag -> responder.get());
        }
        IOUtils.write(generated, response.getOutputStream(), StandardCharsets.UTF_8);
    } else {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "No web component for " + Optional.ofNullable(componentInfo.tag).orElse("<null>"));
    }
    return true;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) Component(com.vaadin.flow.component.Component) VaadinResponse(com.vaadin.flow.server.VaadinResponse) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) HttpServletResponse(javax.servlet.http.HttpServletResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LoggerFactory(org.slf4j.LoggerFactory) CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8(com.vaadin.flow.shared.ApplicationConstants.CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8) IOException(java.io.IOException) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Supplier(java.util.function.Supplier) StandardCharsets(java.nio.charset.StandardCharsets) Serializable(java.io.Serializable) IOUtils(org.apache.commons.io.IOUtils) Matcher(java.util.regex.Matcher) WebComponentConfiguration(com.vaadin.flow.component.webcomponent.WebComponentConfiguration) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) SynchronizedRequestHandler(com.vaadin.flow.server.SynchronizedRequestHandler) WebComponentConfiguration(com.vaadin.flow.component.webcomponent.WebComponentConfiguration) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) Component(com.vaadin.flow.component.Component)

Aggregations

WebComponentConfiguration (com.vaadin.flow.component.webcomponent.WebComponentConfiguration)4 Component (com.vaadin.flow.component.Component)3 WebComponentConfigurationRegistry (com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry)3 WebComponentExporter (com.vaadin.flow.component.WebComponentExporter)2 WebComponentExporterFactory (com.vaadin.flow.component.WebComponentExporterFactory)1 DefaultWebComponentExporterFactory (com.vaadin.flow.component.WebComponentExporterFactory.DefaultWebComponentExporterFactory)1 WebComponent (com.vaadin.flow.component.webcomponent.WebComponent)1 Element (com.vaadin.flow.dom.Element)1 InvalidCustomElementNameException (com.vaadin.flow.server.InvalidCustomElementNameException)1 MockInstantiator (com.vaadin.flow.server.MockInstantiator)1 SynchronizedRequestHandler (com.vaadin.flow.server.SynchronizedRequestHandler)1 VaadinRequest (com.vaadin.flow.server.VaadinRequest)1 VaadinResponse (com.vaadin.flow.server.VaadinResponse)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8 (com.vaadin.flow.shared.ApplicationConstants.CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 StandardCharsets (java.nio.charset.StandardCharsets)1 HashSet (java.util.HashSet)1 Optional (java.util.Optional)1