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);
}
}
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;
}
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());
}
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);
}
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");
}
Aggregations