use of com.vaadin.flow.server.VaadinResponse in project flow by vaadin.
the class WebComponentBootstrapHandlerTest method getMockResponse.
private VaadinResponse getMockResponse(ByteArrayOutputStream stream) throws IOException {
VaadinResponse response = Mockito.mock(VaadinResponse.class);
VaadinService service = Mockito.mock(VaadinService.class);
VaadinContext context = Mockito.mock(VaadinContext.class);
Mockito.when(response.getOutputStream()).thenReturn(stream);
Mockito.when(response.getService()).thenReturn(service);
Mockito.when(service.getContext()).thenReturn(context);
Mockito.when(context.getAttribute(eq(WebComponentConfigurationRegistry.class), any())).thenReturn(Mockito.mock(WebComponentConfigurationRegistry.class));
return response;
}
use of com.vaadin.flow.server.VaadinResponse in project flow by vaadin.
the class WebComponentBootstrapHandlerTest method writeBootstrapPage_noExportChunk.
@Test
public void writeBootstrapPage_noExportChunk() throws IOException, ServiceException {
TestWebComponentBootstrapHandler handler = new TestWebComponentBootstrapHandler();
VaadinServletService service = new MockVaadinServletService();
initLookup(service);
VaadinSession session = new MockVaadinSession(service);
session.lock();
session.setConfiguration(service.getDeploymentConfiguration());
MockDeploymentConfiguration config = (MockDeploymentConfiguration) service.getDeploymentConfiguration();
config.setApplicationOrSystemProperty(SERVLET_PARAMETER_STATISTICS_JSON, VAADIN_SERVLET_RESOURCES + "config/stats_no_export.json");
config.setEnableDevServer(false);
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
Mockito.when(request.getService()).thenReturn(service);
Mockito.when(request.getServletPath()).thenReturn("/");
VaadinResponse response = getMockResponse(null);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Mockito.when(response.getOutputStream()).thenReturn(stream);
handler.synchronizedHandleRequest(session, request, response);
// no "export" chunk, expect "bundle" in result instead
String result = stream.toString(StandardCharsets.UTF_8.name());
Assert.assertTrue(result.contains("VAADIN/build/vaadin-bundle-1111.cache.js"));
}
use of com.vaadin.flow.server.VaadinResponse 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;
}
use of com.vaadin.flow.server.VaadinResponse in project flow by vaadin.
the class CsrfIndexHtmlRequestListener method ensureCsrfTokenCookieIsSet.
private void ensureCsrfTokenCookieIsSet(VaadinRequest request, VaadinResponse response) {
if (isSpringCsrfTokenPresent(request)) {
return;
}
final String csrfCookieValue = Optional.ofNullable(request.getCookies()).map(Arrays::stream).orElse(Stream.empty()).filter(cookie -> cookie.getName().equals(ApplicationConstants.CSRF_TOKEN)).findFirst().map(Cookie::getValue).orElse(null);
if (csrfCookieValue != null && !csrfCookieValue.isEmpty()) {
return;
}
/*
* Despite section 6 of RFC 4122, this particular use of UUID *is*
* adequate for security capabilities. Type 4 UUIDs contain 122 bits of
* random data, and UUID.randomUUID() is defined to use a
* cryptographically secure random generator.
*/
final String csrfToken = UUID.randomUUID().toString();
Cookie csrfCookie = new Cookie(ApplicationConstants.CSRF_TOKEN, csrfToken);
csrfCookie.setSecure(request.isSecure());
String path = request.getContextPath();
if (path == null || path.isEmpty()) {
path = "/";
}
csrfCookie.setPath(path);
csrfCookie.setHttpOnly(false);
response.addCookie(csrfCookie);
}
Aggregations