use of com.vaadin.flow.server.VaadinServletService in project flow by vaadin.
the class UidlRequestHandlerTest method writeSessionExpired.
@Test
public void writeSessionExpired() throws Exception {
ApplicationConfiguration config = Mockito.mock(ApplicationConfiguration.class);
Mockito.when(config.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
Mockito.when(config.getBuildFolder()).thenReturn(".");
VaadinContext context = new MockVaadinContext();
Mockito.when(config.getContext()).thenReturn(context);
VaadinService service = new VaadinServletService(null, new DefaultDeploymentConfiguration(config, getClass(), new Properties()));
when(request.getService()).thenReturn(service);
when(request.getParameter(ApplicationConstants.REQUEST_TYPE_PARAMETER)).thenReturn(RequestType.UIDL.getIdentifier());
boolean result = handler.handleSessionExpired(request, response);
Assert.assertTrue("Result should be true", result);
String responseContent = CommunicationUtil.getStringWhenWriteBytesOffsetLength(outputStream);
// response shouldn't contain async
Assert.assertEquals("Invalid response", "for(;;);[{\"meta\":{\"sessionExpired\":true}}]", responseContent);
}
use of com.vaadin.flow.server.VaadinServletService in project flow by vaadin.
the class BundleFilterInitializerTest method init.
@Before
public void init() throws MalformedURLException {
event = Mockito.mock(ServiceInitEvent.class);
VaadinServletService service = Mockito.mock(VaadinServletService.class);
VaadinServlet servlet = Mockito.mock(VaadinServlet.class);
Mockito.when(event.getSource()).thenReturn(service);
Mockito.when(service.getServlet()).thenReturn(servlet);
Mockito.when(service.getDeploymentConfiguration()).thenReturn(new DefaultDeploymentConfiguration(BundleFilterInitializerTest.class, new Properties(), (base, consumer) -> {
}) {
@Override
public boolean isProductionMode() {
return true;
}
});
Mockito.doAnswer(invocation -> {
dependencyFilterAddHandler.accept(invocation.getArgumentAt(0, DependencyFilter.class));
return null;
}).when(event).addDependencyFilter(Mockito.any(DependencyFilter.class));
Mockito.doAnswer(invocation -> {
return inputStreamProducer.apply(invocation.getArgumentAt(0, String.class));
}).when(service).getResourceAsStream("/frontend-es6/vaadin-flow-bundle-manifest.json");
Mockito.doAnswer(invocation -> {
return resourceProducer.apply(invocation.getArgumentAt(0, String.class));
}).when(service).getResource(Mockito.anyString());
}
use of com.vaadin.flow.server.VaadinServletService in project flow by vaadin.
the class BundleFilterInitializer method readBundleDependencies.
private Map<String, Set<String>> readBundleDependencies(ServiceInitEvent event, VaadinUriResolver es6ContextPathResolver) {
VaadinServletService servlet = ((VaadinServletService) event.getSource());
String es6Base = es6ContextPathResolver.resolveVaadinUri(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX);
if (!es6Base.endsWith("/")) {
es6Base += '/';
}
String bundleManifestContextPath = es6Base + FLOW_BUNDLE_MANIFEST;
try (InputStream bundleManifestStream = servlet.getResourceAsStream(bundleManifestContextPath)) {
if (bundleManifestStream == null) {
getLogger().info("Bundling disabled: Flow bundle manifest '{}' was not found in servlet context", bundleManifestContextPath);
return Collections.emptyMap();
}
JsonObject bundlesToUrlsContained = Json.parse(IOUtils.toString(bundleManifestStream, StandardCharsets.UTF_8));
Map<String, Set<String>> importToBundle = new HashMap<>();
for (String bundlePath : bundlesToUrlsContained.keys()) {
JsonArray bundledFiles = bundlesToUrlsContained.getArray(bundlePath);
for (int i = 0; i < bundledFiles.length(); i++) {
String bundledFile = bundledFiles.getString(i);
if (servlet.getResource(es6ContextPathResolver.resolveVaadinUri(es6Base + bundlePath)) == null) {
throw new IllegalArgumentException(String.format("Failed to find bundle at context path '%s', specified in manifest '%s'. " + "Remove file reference from the manifest to disable bundle usage or add the bundle to the context path specified.", bundlePath, bundleManifestContextPath));
}
importToBundle.computeIfAbsent(bundledFile, key -> new HashSet<>()).add(bundlePath);
}
}
return importToBundle;
} catch (IOException e) {
throw new UncheckedIOException(String.format("Failed to read bundle manifest file at context path '%s'", bundleManifestContextPath), e);
}
}
use of com.vaadin.flow.server.VaadinServletService in project flow by vaadin.
the class DefaultTemplateParserTest method setUp.
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
VaadinServletRequest request = Mockito.mock(VaadinServletRequest.class);
VaadinSession session = Mockito.mock(VaadinSession.class);
factory = (VaadinUriResolverFactory) vaadinRequest -> resolver;
Mockito.when(session.getAttribute(VaadinUriResolverFactory.class)).thenReturn(factory);
Mockito.when(service.getDependencyFilters()).thenReturn(Collections.emptyList());
WrappedHttpSession wrappedSession = Mockito.mock(WrappedHttpSession.class);
HttpSession httpSession = Mockito.mock(HttpSession.class);
Mockito.when(wrappedSession.getHttpSession()).thenReturn(httpSession);
servlet = new VaadinServlet() {
@Override
protected VaadinServletService createServletService() throws ServletException, ServiceException {
return service;
}
@Override
public ServletContext getServletContext() {
return servletContext;
}
};
Mockito.when(service.getServlet()).thenReturn(servlet);
Mockito.when(resolver.resolveVaadinUri("/bar.html")).thenReturn("bar.html");
Mockito.when(resolver.resolveVaadinUri("/bar1.html")).thenReturn("bar1.html");
Mockito.when(resolver.resolveVaadinUri("/bundle.html")).thenReturn("bundle.html");
Mockito.when(request.getWrappedSession()).thenReturn(wrappedSession);
Mockito.when(request.getServletPath()).thenReturn("");
Mockito.when(servletContext.getResourceAsStream("/bar.html")).thenReturn(new ByteArrayInputStream("<dom-module id='bar'></dom-module>".getBytes(StandardCharsets.UTF_8)));
Mockito.when(servletContext.getResourceAsStream("/bar1.html")).thenReturn(new ByteArrayInputStream("<dom-module id='foo'></dom-module>".getBytes(StandardCharsets.UTF_8)));
Mockito.when(servletContext.getResourceAsStream("/bundle.html")).thenReturn(getBundle(), getBundle(), getBundle());
CurrentInstance.set(VaadinRequest.class, request);
CurrentInstance.set(VaadinSession.class, session);
CurrentInstance.set(VaadinService.class, service);
}
use of com.vaadin.flow.server.VaadinServletService 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;
}
Aggregations