Search in sources :

Example 1 with VaadinResponse

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

the class Router method initializeUI.

/**
 * Enables navigation for a new UI instance. This initializes the UI content
 * based on the location used for loading the UI and sets up the UI to be
 * updated when the user navigates to some other location.
 *
 * @param ui
 *            the UI that navigation should be set up for
 * @param initRequest
 *            the Vaadin request that bootstraps the provided UI
 */
@Override
public void initializeUI(UI ui, VaadinRequest initRequest) {
    assert getConfiguration().isConfigured();
    String pathInfo = initRequest.getPathInfo();
    final String path;
    if (pathInfo == null) {
        path = "";
    } else {
        assert pathInfo.startsWith("/");
        path = pathInfo.substring(1);
    }
    final QueryParameters queryParameters = QueryParameters.full(initRequest.getParameterMap());
    ui.getPage().getHistory().setHistoryStateChangeHandler(e -> navigate(ui, e.getLocation(), e.getTrigger()));
    Location location = new Location(path, queryParameters);
    int statusCode = navigate(ui, location, NavigationTrigger.PAGE_LOAD);
    VaadinResponse response = VaadinService.getCurrentResponse();
    if (response != null) {
        response.setStatus(statusCode);
    }
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) QueryParameters(com.vaadin.flow.router.QueryParameters) Location(com.vaadin.flow.router.Location)

Example 2 with VaadinResponse

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

the class WebComponentBootstrapHandler method createAndInitUI.

@Override
protected BootstrapContext createAndInitUI(Class<? extends UI> uiClass, VaadinRequest request, VaadinResponse response, VaadinSession session) {
    if (!canHandleRequest(request)) {
        throw new IllegalStateException("Unexpected request URL '" + getRequestUrl(request) + "' in the bootstrap handler for web " + "component UI which should handle path " + PATH_PATTERN.toString());
    }
    final String serviceUrl = getServiceUrl(request, response);
    BootstrapContext context = super.createAndInitUI(WebComponentUI.class, request, response, session);
    JsonObject config = context.getApplicationParameters();
    String pushURL = context.getSession().getConfiguration().getPushURL();
    if (pushURL == null) {
        pushURL = serviceUrl;
    } else {
        try {
            URI uri = new URI(serviceUrl);
            pushURL = uri.resolve(new URI(pushURL)).toASCIIString();
        } catch (URISyntaxException exception) {
            throw new IllegalStateException(String.format("Can't resolve pushURL '%s' based on the service URL '%s'", pushURL, serviceUrl), exception);
        }
    }
    PushConfiguration pushConfiguration = context.getUI().getPushConfiguration();
    pushConfiguration.setPushUrl(pushURL);
    assert serviceUrl.endsWith("/");
    config.put(ApplicationConstants.SERVICE_URL, serviceUrl);
    config.put(ApplicationConstants.APP_WC_MODE, true);
    WebComponentConfigurationRegistry registry = WebComponentConfigurationRegistry.getInstance(request.getService().getContext());
    JsonArray tags = registry.getConfigurations().stream().map(conf -> Json.create(conf.getTag())).collect(JsonUtils.asArray());
    config.put("webcomponents", tags);
    config.put(ApplicationConstants.DEVMODE_GIZMO_ENABLED, false);
    return context;
}
Also used : JsonArray(elemental.json.JsonArray) PwaRegistry(com.vaadin.flow.server.PwaRegistry) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) URISyntaxException(java.net.URISyntaxException) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8(com.vaadin.flow.shared.ApplicationConstants.CONTENT_TYPE_TEXT_JAVASCRIPT_UTF_8) WebComponentUI(com.vaadin.flow.component.webcomponent.WebComponentUI) EXPORT_CHUNK(com.vaadin.flow.server.frontend.FrontendUtils.EXPORT_CHUNK) Function(java.util.function.Function) ArrayList(java.util.ArrayList) BootstrapHandler(com.vaadin.flow.server.BootstrapHandler) ElementUtil(com.vaadin.flow.dom.ElementUtil) Element(org.jsoup.nodes.Element) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) OutputStreamWriter(java.io.OutputStreamWriter) Constants(com.vaadin.flow.server.Constants) URI(java.net.URI) UI(com.vaadin.flow.component.UI) VaadinSession(com.vaadin.flow.server.VaadinSession) HandlerHelper(com.vaadin.flow.server.HandlerHelper) VaadinResponse(com.vaadin.flow.server.VaadinResponse) BufferedWriter(java.io.BufferedWriter) UTF_8(java.nio.charset.StandardCharsets.UTF_8) PushConfiguration(com.vaadin.flow.component.PushConfiguration) IOException(java.io.IOException) VaadinRequest(com.vaadin.flow.server.VaadinRequest) List(java.util.List) JsonUtils(com.vaadin.flow.internal.JsonUtils) Attribute(org.jsoup.nodes.Attribute) Document(org.jsoup.nodes.Document) Writer(java.io.Writer) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) JsonObject(elemental.json.JsonObject) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) PushConfiguration(com.vaadin.flow.component.PushConfiguration) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) JsonObject(elemental.json.JsonObject) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 3 with VaadinResponse

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

the class HeartbeatHandlerTest method synchronizedHandleRequest_uiPresent_setLastHeartbeatTimestampIsCalledOnce.

@Test
public void synchronizedHandleRequest_uiPresent_setLastHeartbeatTimestampIsCalledOnce() throws IOException {
    VaadinService service = mock(VaadinService.class);
    VaadinSession session = mock(VaadinSession.class);
    VaadinRequest request = mock(VaadinRequest.class);
    VaadinResponse response = mock(VaadinResponse.class);
    UI ui = mock(UI.class);
    UIInternals uiInternals = mock(UIInternals.class);
    when(ui.getInternals()).thenReturn(uiInternals);
    when(session.getService()).thenReturn(service);
    when(service.findUI(request)).thenReturn(ui);
    HeartbeatHandler handler = new HeartbeatHandler();
    handler.synchronizedHandleRequest(session, request, response);
    Mockito.verify(ui.getInternals(), times(1)).setLastHeartbeatTimestamp(anyLong());
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinSession(com.vaadin.flow.server.VaadinSession) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) UIInternals(com.vaadin.flow.component.internal.UIInternals) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Test(org.junit.Test)

Example 4 with VaadinResponse

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

the class WebpackHandlerTest method avoidStoringPortOfFailingWebPackDevServer_failWebpackStart_startWebPackSucessfullyAfter.

@Test
public void avoidStoringPortOfFailingWebPackDevServer_failWebpackStart_startWebPackSucessfullyAfter() throws Exception {
    handler = startWebpack();
    waitForDevServer();
    removeDevModeHandlerInstance();
    // dev mode handler should fail because of non-existent npm
    // folder: it
    // means the port number should not have been written
    // use non-existent folder for as npmFolder, it should fail the
    // validation (which means server instance won't be reused)
    WebpackHandler newhHandler = new WebpackHandler(lookup, 0, new File(npmFolder, UUID.randomUUID().toString()), CompletableFuture.completedFuture(null));
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    Mockito.when(response.getOutputStream()).thenReturn(new ByteArrayOutputStream());
    boolean proceed = true;
    Throwable cause = null;
    while (proceed) {
        try {
            proceed = newhHandler.handleRequest(Mockito.mock(VaadinSession.class), Mockito.mock(VaadinRequest.class), response);
        } catch (IllegalStateException ise) {
            proceed = false;
            cause = ise.getCause();
        }
    }
    Assert.assertNotNull(cause);
    Assert.assertTrue(cause instanceof ExecutionFailedException);
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) ExecutionFailedException(com.vaadin.flow.server.ExecutionFailedException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) AbstractDevModeTest(com.vaadin.base.devserver.startup.AbstractDevModeTest) Test(org.junit.Test)

Example 5 with VaadinResponse

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

the class WebpackHandlerTest method devModeNotReady_handleRequest_returnsHtml.

@Test
public void devModeNotReady_handleRequest_returnsHtml() throws Exception {
    handler = startWebpack();
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Mockito.when(response.getOutputStream()).thenReturn(stream);
    handler.handleRequest(Mockito.mock(VaadinSession.class), Mockito.mock(VaadinRequest.class), response);
    String devModeNotReadyContents = stream.toString("UTF-8");
    Document document = Jsoup.parse(devModeNotReadyContents);
    Assert.assertTrue("expected a head child", document.head().children().size() > 0);
    Assert.assertTrue("expected a body child", document.body().children().size() > 0);
    Mockito.verify(response).setContentType("text/html;charset=utf-8");
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinRequest(com.vaadin.flow.server.VaadinRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.jsoup.nodes.Document) AbstractDevModeTest(com.vaadin.base.devserver.startup.AbstractDevModeTest) Test(org.junit.Test)

Aggregations

VaadinResponse (com.vaadin.flow.server.VaadinResponse)24 Test (org.junit.Test)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 VaadinRequest (com.vaadin.flow.server.VaadinRequest)8 VaadinSession (com.vaadin.flow.server.VaadinSession)8 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)7 Document (org.jsoup.nodes.Document)7 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)6 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)6 VaadinService (com.vaadin.flow.server.VaadinService)6 Element (org.jsoup.nodes.Element)6 UI (com.vaadin.flow.component.UI)3 Location (com.vaadin.flow.router.Location)3 VaadinServletService (com.vaadin.flow.server.VaadinServletService)3 WebComponentConfigurationRegistry (com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry)3 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)3 Optional (java.util.Optional)3 AbstractDevModeTest (com.vaadin.base.devserver.startup.AbstractDevModeTest)2 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 RouteConfiguration (com.vaadin.flow.router.RouteConfiguration)2