UidlWriter implements Serializable { public JsonObject createUidl(UI ui, boolean async, boolean resync) { JsonObject response = Json.createObject(); UIInternals uiInternals = ui.getInternals(); VaadinSession session = ui.getSession(); VaadinService service = session.getService(); service.runPendingAccessTasks(session); getLogger().debug("* Creating response to client"); int syncId = service.getDeploymentConfiguration().isSyncIdCheckEnabled() ? uiInternals.getServerSyncId() : -1; response.put(ApplicationConstants.SERVER_SYNC_ID, syncId); if (resync) { response.put(ApplicationConstants.RESYNCHRONIZE_ID, true); } 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(); encodeChanges(ui, stateChanges); populateDependencies(response, uiInternals.getDependencyList(), new ResolveContext(service, session.getBrowser())); if (uiInternals.getConstantPool().hasNewConstants()) { response.put("constants", uiInternals.getConstantPool().dumpConstants()); } if (stateChanges.length() != 0) { response.put("changes", stateChanges); } List<PendingJavaScriptInvocation> 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; }  JsonObject createUidl(UI ui, boolean async, boolean resync); JsonObject createUidl(UI ui, boolean async);  }
@Test public void componentDependencies_npmMode() throws Exception { UI ui = initializeUIForDependenciesTest(new TestUI()); UidlWriter uidlWriter = new UidlWriter(); addInitialComponentDependencies(ui, uidlWriter); JsonObject response = uidlWriter.createUidl(ui, false); assertFalse(response.hasKey(LoadMode.EAGER.name())); assertFalse(response.hasKey(LoadMode.INLINE.name())); assertFalse(response.hasKey(LoadMode.LAZY.name())); }
@Test public void testComponentInterfaceDependencies_npmMode() throws Exception { UI ui = initializeUIForDependenciesTest(new TestUI()); UidlWriter uidlWriter = new UidlWriter(); addInitialComponentDependencies(ui, uidlWriter); ui.add(new ActualComponent(), new SuperComponent(), new ChildComponent()); JsonObject response = uidlWriter.createUidl(ui, false); Map<String, JsonObject> dependenciesMap = getDependenciesMap(response); assertEquals(4, dependenciesMap.size()); assertDependency("childinterface1-" + CSS_STYLE_NAME, CSS_STYLE_NAME, dependenciesMap); assertDependency("childinterface2-" + CSS_STYLE_NAME, CSS_STYLE_NAME, dependenciesMap); assertDependency("child1-" + CSS_STYLE_NAME, CSS_STYLE_NAME, dependenciesMap); assertDependency("child2-" + CSS_STYLE_NAME, CSS_STYLE_NAME, dependenciesMap); }
@Test public void checkAllTypesOfDependencies_npmMode() throws Exception { UI ui = initializeUIForDependenciesTest(new TestUI()); UidlWriter uidlWriter = new UidlWriter(); addInitialComponentDependencies(ui, uidlWriter); ui.add(new ComponentWithAllDependencyTypes()); JsonObject response = uidlWriter.createUidl(ui, false); Map<LoadMode, List<JsonObject>> dependenciesMap = Stream .of(LoadMode.values()) .map(mode -> response.getArray(mode.name())) .flatMap(JsonUtils::<JsonObject> stream) .collect(Collectors.toMap( jsonObject -> LoadMode.valueOf( jsonObject.getString(Dependency.KEY_LOAD_MODE)), Collections::singletonList, (list1, list2) -> { List<JsonObject> result = new ArrayList<>(list1); result.addAll(list2); return result; })); assertThat( "Dependencies with all types of load mode should be present in this response", dependenciesMap.size(), is(LoadMode.values().length)); List<JsonObject> eagerDependencies = dependenciesMap .get(LoadMode.EAGER); assertThat("Should have an eager dependency", eagerDependencies, hasSize(1)); assertThat("Eager dependencies should not have inline contents", eagerDependencies.stream() .filter(json -> json.hasKey(Dependency.KEY_CONTENTS)) .collect(Collectors.toList()), hasSize(0)); JsonObject eagerDependency = eagerDependencies.get(0); assertEquals("eager.css", eagerDependency.getString(Dependency.KEY_URL)); assertEquals(Dependency.Type.STYLESHEET, Dependency.Type .valueOf(eagerDependency.getString(Dependency.KEY_TYPE))); List<JsonObject> lazyDependencies = dependenciesMap.get(LoadMode.LAZY); JsonObject lazyDependency = lazyDependencies.get(0); assertEquals("lazy.css", lazyDependency.getString(Dependency.KEY_URL)); assertEquals(Dependency.Type.STYLESHEET, Dependency.Type .valueOf(lazyDependency.getString(Dependency.KEY_TYPE))); List<JsonObject> inlineDependencies = dependenciesMap .get(LoadMode.INLINE); assertInlineDependencies(inlineDependencies); }
@Test public void resynchronizationRequested_responseFieldContainsResynchronize() throws Exception { UI ui = initializeUIForDependenciesTest(new TestUI()); UidlWriter uidlWriter = new UidlWriter(); JsonObject response = uidlWriter.createUidl(ui, false, true); assertTrue("Response contains resynchronize field", response.hasKey(ApplicationConstants.RESYNCHRONIZE_ID)); assertTrue("Response resynchronize field is set to true", response.getBoolean(ApplicationConstants.RESYNCHRONIZE_ID)); }