Search in sources :

Example 26 with VaadinServletRequest

use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.

the class TestApplicationServiceInitListener method getBaseUrl.

private static String getBaseUrl(IndexHtmlResponse indexHtmlResponse) {
    VaadinServletRequest request = (VaadinServletRequest) indexHtmlResponse.getVaadinRequest();
    String scheme = request.getScheme() + "://";
    String serverName = request.getServerName();
    String serverPort = (request.getServerPort() == 80) ? "" : ":" + request.getServerPort();
    String contextPath = request.getContextPath();
    return scheme + serverName + serverPort + contextPath;
}
Also used : VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest)

Example 27 with VaadinServletRequest

use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.

the class PageView method beforeEnter.

@Override
public void beforeEnter(BeforeEnterEvent event) {
    Input input = new Input();
    input.setId("input");
    input.clear();
    Div updateButton = new Div();
    updateButton.setId("button");
    updateButton.setText("Update page title");
    updateButton.addClickListener(e -> {
        getPage().setTitle(input.getValue());
    });
    Div overrideButton = new Div();
    overrideButton.setId("override");
    overrideButton.setText("Triggers two updates");
    overrideButton.addClickListener(e -> {
        getPage().setTitle(input.getValue());
        getPage().setTitle("OVERRIDDEN");
    });
    Div reloadButton = new Div();
    reloadButton.setId("reload");
    reloadButton.setText("Reloads the page");
    reloadButton.addClickListener(e -> {
        getPage().reload();
    });
    VaadinServletRequest request = (VaadinServletRequest) VaadinRequest.getCurrent();
    HttpServletRequest httpServletRequest = request.getHttpServletRequest();
    String url = httpServletRequest.getRequestURI().replace(PageView.class.getName(), BaseHrefView.class.getName());
    Div setLocationButton = new Div();
    setLocationButton.setId("setLocation");
    setLocationButton.setText("Set page location");
    setLocationButton.addClickListener(e -> getPage().setLocation(url));
    Div openButton = new Div();
    openButton.setId("open");
    openButton.setText("Open url in a new tab");
    openButton.addClickListener(e -> getPage().open(url));
    IFrame frame = new IFrame();
    frame.setId("newWindow");
    frame.setName("newWindow");
    Div openButton2 = new Div();
    openButton2.setId("openInIFrame");
    openButton2.setText("Open url in an IFrame");
    openButton2.addClickListener(e -> getPage().open(url, "newWindow"));
    add(input, updateButton, overrideButton, reloadButton, setLocationButton, openButton, openButton2, frame);
    add(new NativeButton("page.fetchURL", onClickEvent -> {
        getUI().ifPresent(ui -> ui.getPage().fetchCurrentURL(currentUrl -> {
            LoggerFactory.getLogger(PageView.class.getName()).info(currentUrl.toString());
        }));
    }));
}
Also used : Div(com.vaadin.flow.component.html.Div) HttpServletRequest(javax.servlet.http.HttpServletRequest) Input(com.vaadin.flow.component.html.Input) HttpServletRequest(javax.servlet.http.HttpServletRequest) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) IFrame(com.vaadin.flow.component.html.IFrame) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) LoggerFactory(org.slf4j.LoggerFactory) Div(com.vaadin.flow.component.html.Div) NativeButton(com.vaadin.flow.component.html.NativeButton) VaadinRequest(com.vaadin.flow.server.VaadinRequest) ViewTestLayout(com.vaadin.flow.uitest.servlet.ViewTestLayout) Route(com.vaadin.flow.router.Route) NativeButton(com.vaadin.flow.component.html.NativeButton) Input(com.vaadin.flow.component.html.Input) IFrame(com.vaadin.flow.component.html.IFrame) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest)

Example 28 with VaadinServletRequest

use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.

the class TrackMessageSizeView method findMethodImplementation.

private String findMethodImplementation() {
    String filename = "/VAADIN/static/push/vaadinPush.js";
    VaadinRequest request = VaadinRequest.getCurrent();
    HttpServletRequest httpServletRequest = ((VaadinServletRequest) request).getHttpServletRequest();
    String jsPath = httpServletRequest.getRequestURL().toString().replace(httpServletRequest.getRequestURI(), filename);
    String content = getFileContent(jsPath);
    if (content == null) {
        log("Can't find " + jsPath);
        return null;
    }
    // Find the function inside the script content
    int startIndex = content.indexOf("function _trackMessageSize");
    if (startIndex == -1) {
        log("function not found");
        return null;
    }
    // Assumes there's a /** comment before the next function
    int endIndex = content.indexOf("/**", startIndex);
    if (endIndex == -1) {
        log("End of function not found");
        return null;
    }
    content = content.substring(startIndex, endIndex);
    content = content.replaceAll("jQuery", "jQueryVaadin");
    return content;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) VaadinRequest(com.vaadin.flow.server.VaadinRequest)

Example 29 with VaadinServletRequest

use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.

the class FaviconHandler method handleRequest.

@Override
public boolean handleRequest(VaadinSession session, VaadinRequest request, VaadinResponse response) throws IOException {
    VaadinServletRequest httpRequest = (VaadinServletRequest) request;
    boolean isFavicon = httpRequest.getContextPath().isEmpty() && httpRequest.getServletPath().isEmpty() && "/favicon.ico".equals(httpRequest.getPathInfo());
    if (isFavicon) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
    return isFavicon;
}
Also used : VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest)

Example 30 with VaadinServletRequest

use of com.vaadin.flow.server.VaadinServletRequest in project flow by vaadin.

the class ApplicationRunnerServlet method findDeploymentConfiguration.

private DeploymentConfiguration findDeploymentConfiguration(DeploymentConfiguration originalConfiguration) throws Exception {
    // First level of cache
    DeploymentConfiguration configuration = CurrentInstance.get(DeploymentConfiguration.class);
    if (configuration == null) {
        // Not in cache, try to find a VaadinSession to get it from
        VaadinSession session = VaadinSession.getCurrent();
        if (session == null) {
            /*
                 * There's no current session, request or response when serving
                 * static resources, but there's still the current request
                 * maintained by ApplicationRunnerServlet, and there's most
                 * likely also a HttpSession containing a VaadinSession for that
                 * request.
                 */
            HttpServletRequest currentRequest = VaadinServletService.getCurrentServletRequest();
            if (currentRequest != null) {
                HttpSession httpSession = currentRequest.getSession(false);
                if (httpSession != null) {
                    Map<Class<?>, CurrentInstance> oldCurrent = CurrentInstance.setCurrent((VaadinSession) null);
                    try {
                        VaadinServletService service = (VaadinServletService) VaadinService.getCurrent();
                        session = service.findVaadinSession(new VaadinServletRequest(currentRequest, service));
                    } finally {
                        /*
                             * Clear some state set by findVaadinSession to
                             * avoid accidentally depending on it when coding on
                             * e.g. static request handling.
                             */
                        CurrentInstance.restoreInstances(oldCurrent);
                        currentRequest.removeAttribute(VaadinSession.class.getName());
                    }
                }
            }
        }
        if (session != null) {
            String name = ApplicationRunnerServlet.class.getName() + ".deploymentConfiguration";
            try {
                session.getLockInstance().lock();
                /*
                     * Read attribute using reflection to bypass
                     * VaadinSesison.getAttribute which would cause an infinite
                     * loop when checking the production mode setting for
                     * determining whether to check that the session is locked.
                     */
                Field attributesField = VaadinSession.class.getDeclaredField("attributes");
                attributesField.setAccessible(true);
                Attributes sessionAttributes = (Attributes) attributesField.get(session);
                configuration = (DeploymentConfiguration) sessionAttributes.getAttribute(name);
                if (configuration == null) {
                    ApplicationRunnerServlet servlet = (ApplicationRunnerServlet) VaadinServlet.getCurrent();
                    Class<?> classToRun;
                    try {
                        classToRun = servlet.getClassToRun();
                    } catch (ClassNotFoundException e) {
                        /*
                             * This happens e.g. if the UI class defined in the
                             * URL is not found or if this servlet just serves
                             * static resources while there's some other servlet
                             * that serves the UI (e.g. when using /run-push/).
                             */
                        return originalConfiguration;
                    }
                    CustomDeploymentConfiguration customDeploymentConfiguration = classToRun.getAnnotation(CustomDeploymentConfiguration.class);
                    if (customDeploymentConfiguration != null) {
                        Properties initParameters = new Properties(originalConfiguration.getInitParameters());
                        for (Conf entry : customDeploymentConfiguration.value()) {
                            initParameters.put(entry.name(), entry.value());
                        }
                        initParameters.put(VaadinSession.UI_PARAMETER, getApplicationRunnerApplicationClassName(request.get()));
                        configuration = new DefaultDeploymentConfiguration(ApplicationConfiguration.get(getService().getContext()), servlet.getClass(), initParameters);
                    } else {
                        configuration = originalConfiguration;
                    }
                    sessionAttributes.setAttribute(name, configuration);
                }
            } finally {
                session.getLockInstance().unlock();
            }
            CurrentInstance.set(DeploymentConfiguration.class, configuration);
        } else {
            configuration = originalConfiguration;
        }
    }
    return configuration;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) Conf(com.vaadin.flow.uitest.servlet.CustomDeploymentConfiguration.Conf) HttpSession(javax.servlet.http.HttpSession) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Attributes(com.vaadin.flow.server.Attributes) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) HttpServletRequest(javax.servlet.http.HttpServletRequest) Field(java.lang.reflect.Field) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration)

Aggregations

VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)40 Test (org.junit.Test)19 VaadinSession (com.vaadin.flow.server.VaadinSession)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 VaadinServletService (com.vaadin.flow.server.VaadinServletService)8 IOException (java.io.IOException)7 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)6 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)6 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)6 VaadinRequest (com.vaadin.flow.server.VaadinRequest)6 VaadinResponse (com.vaadin.flow.server.VaadinResponse)6 UI (com.vaadin.flow.component.UI)5 Element (org.jsoup.nodes.Element)5 RouteConfiguration (com.vaadin.flow.router.RouteConfiguration)3 SessionExpiredException (com.vaadin.flow.server.SessionExpiredException)3 VaadinService (com.vaadin.flow.server.VaadinService)3 InvalidUIDLSecurityKeyException (com.vaadin.flow.server.communication.ServerRpcHandler.InvalidUIDLSecurityKeyException)3 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)3 JsonException (elemental.json.JsonException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3