use of com.vaadin.flow.server.VaadinSession in project flow by vaadin.
the class SerializationTest method testVaadinSession.
public void testVaadinSession() throws Exception {
VaadinSession session = new VaadinSession(null);
session = serializeAndDeserialize(session);
assertNotNull("Pending access queue was not recreated after deserialization", session.getPendingAccessQueue());
}
use of com.vaadin.flow.server.VaadinSession in project flow by vaadin.
the class CustomElementRegistryInitializerTest method setup.
@Before
public void setup() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Field customElements = CustomElementRegistry.class.getDeclaredField("customElements");
customElements.setAccessible(true);
customElements.set(CustomElementRegistry.getInstance(), new AtomicReference<>());
customElementRegistryInitializer = new CustomElementRegistryInitializer();
VaadinSession session = Mockito.mock(VaadinSession.class);
UI ui = new UI() {
@Override
public VaadinSession getSession() {
return session;
}
};
VaadinService service = Mockito.mock(VaadinService.class);
when(session.getService()).thenReturn(service);
DefaultInstantiator instantiator = new DefaultInstantiator(service);
when(service.getInstantiator()).thenReturn(instantiator);
UI.setCurrent(ui);
}
use of com.vaadin.flow.server.VaadinSession 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.lock();
configuration = (DeploymentConfiguration) session.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(servlet.getClass(), initParameters, this::scanForResources);
} else {
configuration = originalConfiguration;
}
session.setAttribute(name, configuration);
}
} finally {
session.unlock();
}
CurrentInstance.set(DeploymentConfiguration.class, configuration);
} else {
configuration = originalConfiguration;
}
}
return configuration;
}
use of com.vaadin.flow.server.VaadinSession in project flow by vaadin.
the class UidlWriter method createUidl.
/**
* Creates a JSON object containing all pending changes to the given UI.
*
* @param ui
* The {@link UI} whose changes to write
* @param async
* True if this message is sent by the server asynchronously,
* false if it is a response to a client message.
* @return JSON object containing the UIDL response
*/
public JsonObject createUidl(UI ui, boolean async) {
JsonObject response = Json.createObject();
UIInternals uiInternals = ui.getInternals();
VaadinSession session = ui.getSession();
VaadinService service = session.getService();
// Purge pending access calls as they might produce additional changes
// to write out
service.runPendingAccessTasks(session);
// Paints components
getLogger().debug("* Creating response to client");
int syncId = service.getDeploymentConfiguration().isSyncIdCheckEnabled() ? uiInternals.getServerSyncId() : -1;
response.put(ApplicationConstants.SERVER_SYNC_ID, syncId);
int nextClientToServerMessageId = uiInternals.getLastProcessedClientToServerId() + 1;
response.put(ApplicationConstants.CLIENT_TO_SERVER_ID, nextClientToServerMessageId);
SystemMessages messages = ui.getSession().getService().getSystemMessages(ui.getLocale(), null);
JsonObject meta = new MetadataWriter().createMetadata(ui, false, async, messages);
if (meta.keys().length > 0) {
response.put("meta", meta);
}
JsonArray stateChanges = Json.createArray();
JsonObject templates = Json.createObject();
encodeChanges(ui, stateChanges, templates);
populateDependencies(response, session, uiInternals.getDependencyList());
if (uiInternals.getConstantPool().hasNewConstants()) {
response.put("constants", uiInternals.getConstantPool().dumpConstants());
}
if (stateChanges.length() != 0) {
response.put("changes", stateChanges);
}
if (templates.keys().length > 0) {
response.put("templates", templates);
}
List<JavaScriptInvocation> executeJavaScriptList = uiInternals.dumpPendingJavaScriptInvocations();
if (!executeJavaScriptList.isEmpty()) {
response.put(JsonConstants.UIDL_KEY_EXECUTE, encodeExecuteJavaScriptList(executeJavaScriptList));
}
if (ui.getSession().getService().getDeploymentConfiguration().isRequestTiming()) {
response.put("timings", createPerformanceData(ui));
}
uiInternals.incrementServerId();
return response;
}
use of com.vaadin.flow.server.VaadinSession in project flow by vaadin.
the class CustomUIClassLoaderTest method testWithDefaultClassLoader.
/**
* Tests that a UI class can be loaded even if no classloader has been
* provided.
*
* @throws Exception
* if thrown
*/
public void testWithDefaultClassLoader() throws Exception {
VaadinSession application = createStubApplication();
application.setConfiguration(createConfigurationMock());
Class<? extends UI> uiClass = BootstrapHandler.getUIClass(createRequestMock(getClass().getClassLoader()));
assertEquals(MyUI.class, uiClass);
}
Aggregations