Search in sources :

Example 1 with StreamRegistration

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());
}
Also used : StreamResourceRegistry(com.vaadin.flow.server.StreamResourceRegistry) StreamResource(com.vaadin.flow.server.StreamResource) StreamRegistration(com.vaadin.flow.server.StreamRegistration) Test(org.junit.Test)

Example 2 with StreamRegistration

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));
}
Also used : StreamResourceRegistry(com.vaadin.flow.server.StreamResourceRegistry) StreamResource(com.vaadin.flow.server.StreamResource) StreamRegistration(com.vaadin.flow.server.StreamRegistration) URI(java.net.URI) Test(org.junit.Test)

Example 3 with StreamRegistration

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;
    }
}
Also used : StreamRegistration(com.vaadin.flow.server.StreamRegistration) Registration(com.vaadin.flow.shared.Registration) StreamRegistration(com.vaadin.flow.server.StreamRegistration)

Example 4 with StreamRegistration

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);
        }
    }));
}
Also used : StreamRegistration(com.vaadin.flow.server.StreamRegistration) Command(com.vaadin.flow.server.Command) Registration(com.vaadin.flow.shared.Registration) StreamRegistration(com.vaadin.flow.server.StreamRegistration)

Example 5 with StreamRegistration

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());
}
Also used : StreamRegistration(com.vaadin.flow.server.StreamRegistration) Element(com.vaadin.flow.dom.Element)

Aggregations

StreamRegistration (com.vaadin.flow.server.StreamRegistration)6 StreamResource (com.vaadin.flow.server.StreamResource)2 StreamResourceRegistry (com.vaadin.flow.server.StreamResourceRegistry)2 Registration (com.vaadin.flow.shared.Registration)2 Test (org.junit.Test)2 Element (com.vaadin.flow.dom.Element)1 AbstractStreamResource (com.vaadin.flow.server.AbstractStreamResource)1 Command (com.vaadin.flow.server.Command)1 URI (java.net.URI)1