use of com.vaadin.flow.server.StreamRegistration in project flow by vaadin.
the class StreamResourceRegistryTest method registerTwoResourcesWithSameName_resourcesHasDifferentURI.
@Test
public void registerTwoResourcesWithSameName_resourcesHasDifferentURI() {
StreamResourceRegistry registry = session.getResourceRegistry();
StreamResource resource1 = new StreamResource("name", () -> makeEmptyStream());
StreamRegistration registration1 = registry.registerResource(resource1);
StreamResource resource2 = new StreamResource("name", () -> makeEmptyStream());
StreamRegistration registration2 = registry.registerResource(resource2);
Assert.assertNotEquals("Two different resource are registered to the same URI", registration1.getResourceUri(), registration2.getResourceUri());
registration1.unregister();
Assert.assertTrue("Second resource is not found after first resource has been unregistered", registry.getResource(registration2.getResourceUri()).isPresent());
}
use of com.vaadin.flow.server.StreamRegistration in project flow by vaadin.
the class StreamResourceRegistryTest method getResourceUrlIsEncoded.
@Test
public void getResourceUrlIsEncoded() throws UnsupportedEncodingException {
StreamResourceRegistry registry = session.getResourceRegistry();
StreamResource resource = new StreamResource("a?b=c d&e", () -> makeEmptyStream());
StreamRegistration registration = registry.registerResource(resource);
URI url = registration.getResourceUri();
String suffix = URLEncoder.encode(resource.getName(), StandardCharsets.UTF_8.name());
Assert.assertTrue("Resource url is not encoded", url.toString().endsWith(suffix));
}
use of com.vaadin.flow.server.StreamRegistration in project flow by vaadin.
the class ElementAttributeMap method unregisterResource.
private void unregisterResource(String attribute) {
ensureResourceRegistrations();
ensurePendingRegistrations();
StreamRegistration registration = resourceRegistrations.remove(attribute);
Registration handle = pendingRegistrations.remove(attribute);
if (handle != null) {
handle.remove();
}
if (registration != null) {
registration.unregister();
}
if (resourceRegistrations.isEmpty()) {
resourceRegistrations = null;
}
if (pendingRegistrations.isEmpty()) {
pendingRegistrations = null;
}
}
use of com.vaadin.flow.server.StreamRegistration in project flow by vaadin.
the class ElementAttributeMap method registerResource.
private void registerResource(String attribute, AbstractStreamResource resource) {
ensureResourceRegistrations();
ensurePendingRegistrations();
assert !resourceRegistrations.containsKey(attribute);
StreamRegistration registration = getSession().getResourceRegistry().registerResource(resource);
resourceRegistrations.put(attribute, registration);
Registration handle = pendingRegistrations.remove(attribute);
if (handle != null) {
handle.remove();
}
pendingRegistrations.put(attribute, getNode().addDetachListener(// see ElementAttributeMap#deferRegistration
new Command() {
@Override
public void execute() {
ElementAttributeMap.this.unsetResource(attribute);
}
}));
}
use of com.vaadin.flow.server.StreamRegistration in project flow by vaadin.
the class DependencyUI method init.
@Override
protected void init(VaadinRequest request) {
getElement().appendChild(ElementFactory.createDiv("This test initially loads a stylesheet which makes all text red and a JavaScript which listens to body clicks"));
getElement().appendChild(ElementFactory.createHr());
add(new JsResourceComponent());
Element jsOrder = ElementFactory.createButton("Load js").setAttribute("id", "loadJs");
StreamRegistration foo = getSession().getResourceRegistry().registerResource(getJsResource());
jsOrder.addEventListener("click", e -> {
getPage().addJavaScript("base://" + foo.getResourceUri().toString());
});
Element allBlue = ElementFactory.createButton("Load 'everything blue' stylesheet").setAttribute("id", "loadBlue");
allBlue.addEventListener("click", e -> {
add(new AllBlueImportantComponent());
});
getElement().appendChild(jsOrder, allBlue, ElementFactory.createHr());
}
Aggregations