Search in sources :

Example 1 with CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8

use of com.vaadin.flow.shared.ApplicationConstants.CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8 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

Component (com.vaadin.flow.component.Component)1 WebComponentConfiguration (com.vaadin.flow.component.webcomponent.WebComponentConfiguration)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 WebComponentConfigurationRegistry (com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry)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 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Supplier (java.util.function.Supplier)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 IOUtils (org.apache.commons.io.IOUtils)1 LoggerFactory (org.slf4j.LoggerFactory)1