com.vaadin.flow.server.ServiceContextUriResolver

Here are the examples of the java api com.vaadin.flow.server.ServiceContextUriResolver taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

18 Source : VertxVaadinService.java
with MIT License
from mcollovati

/**
 * Created by marco on 16/07/16.
 */
public clreplaced VertxVaadinService extends VaadinService {

    private static final Logger logger = LoggerFactory.getLogger(VertxVaadinService.clreplaced);

    private final transient VertxVaadin vertxVaadin;

    private final ServiceContextUriResolver contextResolver = new ServiceContextUriResolver();

    public VertxVaadinService(VertxVaadin vertxVaadin, DeploymentConfiguration deploymentConfiguration) {
        super(deploymentConfiguration);
        this.vertxVaadin = vertxVaadin;
    }

    public Vertx getVertx() {
        return vertxVaadin.vertx();
    }

    public VaadinOptions getVaadinOptions() {
        return vertxVaadin.config();
    }

    public ServletContext getServletContext() {
        return vertxVaadin.servletContext();
    }

    @Override
    public void ensureAccessQueuePurged(VaadinSession session) {
        Context context = getVertx().getOrCreateContext();
        // Ensure commands are executed in vertx context
        context.runOnContext(ev -> super.ensureAccessQueuePurged(session));
    }

    @Override
    protected RouteRegistry getRouteRegistry() {
        return ApplicationRouteRegistry.getInstance(vertxVaadin.servletContext());
    }

    @Override
    protected PwaRegistry getPwaRegistry() {
        return PwaRegistry.getInstance(vertxVaadin.servletContext());
    }

    @Override
    protected List<RequestHandler> createRequestHandlers() throws ServiceException {
        List<RequestHandler> handlers = super.createRequestHandlers();
        // TODO: removed because of explicit cast to servlet; should be handled at router level?
        handlers.removeIf(FaviconHandler.clreplaced::isInstance);
        handlers.replaceAll(RequestHandlerReplacements::replace);
        handlers.add(0, VertxBootstrapHandler.patchIfNeeded());
        return handlers;
    }

    @Override
    public String getMimeType(String resourceName) {
        return MimeMapping.getMimeTypeForFilename(resourceName);
    }

    @Override
    public boolean ensurePushAvailable() {
        return vertxVaadin.config().supportsSockJS();
    }

    @Override
    public String getMainDivId(VaadinSession session, VaadinRequest request) {
        String appId;
        // Have to check due to VertxBootstrapHandler tricks
        if (request instanceof VertxVaadinRequest) {
            appId = ((VertxVaadinRequest) request).getRoutingContext().mountPoint();
        } else {
            appId = request.getContextPath();
        }
        if (appId == null || "".equals(appId) || "/".equals(appId)) {
            appId = "ROOT";
        }
        appId = appId.replaceAll("[^a-zA-Z0-9]", "");
        // Add hashCode to the end, so that it is still (sort of)
        // predictable, but indicates that it should not be used in CSS
        // and
        // such:
        int hashCode = appId.hashCode();
        if (hashCode < 0) {
            hashCode = -hashCode;
        }
        appId = appId + "-" + hashCode;
        return appId;
    }

    // From VaadinServletService
    @Override
    protected boolean requestCanCreateSession(VaadinRequest request) {
        if (isOtherRequest(request)) {
            /*
             * I.e URIs that are not RPC calls or static (theme) files.
             */
            return true;
        }
        return false;
    }

    // From VaadinServletService
    private boolean isOtherRequest(VaadinRequest request) {
        return request.getParameter(ApplicationConstants.REQUEST_TYPE_PARAMETER) == null;
    }

    @Override
    public String getServiceName() {
        return vertxVaadin.serviceName();
    }

    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public URL getStaticResource(String url) {
        return tryResolveFile(url);
    }

    @Override
    public URL getResource(String url, WebBrowser browser, AbstractTheme theme) {
        return tryGetResource(getThemedOrRawPath(url, browser, theme));
    }

    @Override
    public InputStream getResourcereplacedtream(String url, WebBrowser browser, AbstractTheme theme) {
        return tryGetResourcereplacedtream(getThemedOrRawPath(url, browser, theme));
    }

    @Override
    public String resolveResource(String url, WebBrowser browser) {
        Objects.requireNonNull(url, "Url cannot be null");
        Objects.requireNonNull(browser, "Browser cannot be null");
        String frontendRootUrl;
        DeploymentConfiguration config = getDeploymentConfiguration();
        if (browser.isEs6Supported()) {
            frontendRootUrl = config.getEs6FrontendPrefix();
        } else {
            frontendRootUrl = config.getEs5FrontendPrefix();
        }
        return contextResolver.resolveVaadinUri(url, frontendRootUrl);
    }

    @Override
    public Optional<String> getThemedUrl(String url, WebBrowser browser, AbstractTheme theme) {
        if (theme != null && !resolveResource(url, browser).equals(getThemedOrRawPath(url, browser, theme))) {
            return Optional.of(theme.translateUrl(url));
        }
        return Optional.empty();
    }

    @Override
    protected VaadinContext constructVaadinContext() {
        return new VertxVaadinContext(getVertx());
    }

    private String getThemedOrRawPath(String url, WebBrowser browser, AbstractTheme theme) {
        String resourcePath = resolveResource(url, browser);
        return getThemeResourcePath(resourcePath, theme).filter(p -> Objects.nonNull(tryGetResource(p))).orElse(resourcePath);
    }

    private URL tryGetResource(String path) {
        logger.trace("Try to resolve path {}", path);
        URL url = tryResolveFile(path);
        if (url == null && !path.startsWith("META-INF/resources/")) {
            logger.trace("Path {} not found, try into /META-INF/resources/", path);
            url = tryResolveFile("/META-INF/resources/" + path);
        }
        if (url == null) {
            logger.trace("Path {} not found into META-INF/resources/, try with webjars", path);
            url = vertxVaadin.webJars.getWebJarResourcePath(path).map(this::tryResolveFile).orElse(null);
        }
        return url;
    }

    private URL tryResolveFile(String path) {
        FileSystem fileSystem = getVertx().fileSystem();
        String relativePath = makePathRelative(path);
        if (fileSystem.existsBlocking(relativePath)) {
            try {
                return new FileResolver().resolveFile(relativePath).toURI().toURL();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private Optional<String> getThemeResourcePath(String path, AbstractTheme theme) {
        return Optional.ofNullable(theme).map(t -> t.translateUrl(path)).filter(p -> !p.equals(path));
    }

    public InputStream tryGetResourcereplacedtream(String path) {
        logger.trace("Try to resolve path {}", path);
        String relativePath = makePathRelative(path);
        FileSystem fileSystem = getVertx().fileSystem();
        if (fileSystem.existsBlocking(relativePath)) {
            return new BufferInputStreamAdapter(fileSystem.readFileBlocking(relativePath));
        }
        if (!path.startsWith("/META-INF/resources")) {
            logger.trace("Path {} not found, try into /META-INF/resources/", path);
            InputStream is = tryGetResourcereplacedtream("/META-INF/resources/" + relativePath);
            if (is != null) {
                return is;
            }
        }
        logger.trace("Path {} not found into META-INF/resources/, try with webjars", path);
        return vertxVaadin.webJars.getWebJarResourcePath(path).filter(fileSystem::existsBlocking).map(fileSystem::readFileBlocking).map(BufferInputStreamAdapter::new).orElse(null);
    }

    private String makePathRelative(String path) {
        String relativePath = path;
        if (path.startsWith("/")) {
            relativePath = relativePath.substring(1);
        // relativePath = "." + relativePath;
        }
        return relativePath;
    }

    /**
     * Gets a relative path you can use to refer to the context root.
     *
     * @param request the request for which the location should be determined
     * @return A relative path to the context root. Never ends with a slash (/).
     */
    @Override
    public String getContextRootRelativePath(VaadinRequest request) {
        return getCancelingRelativePath("/") + "/";
    }

    // Just to avoid direct calls to VaadinServletService
    // from outside VertxVaadinService
    public static String getCancelingRelativePath(String servletPath) {
        return ServletHelper.getCancelingRelativePath(servletPath);
    }
}