Search in sources :

Example 1 with StreamResource

use of com.vaadin.flow.server.StreamResource 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 StreamResource

use of com.vaadin.flow.server.StreamResource 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 StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class StreamResourceTest method setContentType.

@Test
public void setContentType() {
    StreamResource resource = new StreamResource("foo", () -> makeEmptyStream());
    resource.setContentType("bar");
    Assert.assertNotNull(resource.getContentTypeResolver());
    assertContentType(resource, resource.getContentTypeResolver());
}
Also used : StreamResource(com.vaadin.flow.server.StreamResource) Test(org.junit.Test)

Example 4 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class StreamResourceView method onAttach.

@Override
protected void onAttach(AttachEvent attachEvent) {
    StreamResource resource = new StreamResource("filename", () -> new ByteArrayInputStream("Hello world".getBytes(StandardCharsets.UTF_8)));
    Anchor download = new Anchor("", "Download file");
    download.setHref(resource);
    download.setId("download");
    add(download);
}
Also used : Anchor(com.vaadin.flow.component.html.Anchor) StreamResource(com.vaadin.flow.server.StreamResource) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 5 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_attachElement_resourceIsRegistered.

@Test
public void setResourceAttribute_attachElement_resourceIsRegistered() throws URISyntaxException {
    UI ui = createUI();
    UI.setCurrent(ui);
    StreamResource resource = createEmptyResource("resource");
    Element element = ElementFactory.createDiv();
    element.setAttribute("foo", resource);
    ui.getElement().appendChild(element);
    Assert.assertTrue(element.hasAttribute("foo"));
    String uri = element.getAttribute("foo");
    Optional<StreamResource> res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, new URI(uri));
    Assert.assertTrue(res.isPresent());
}
Also used : UI(com.vaadin.flow.component.UI) StreamResource(com.vaadin.flow.server.StreamResource) Element(com.vaadin.flow.dom.Element) URI(java.net.URI) Test(org.junit.Test)

Aggregations

StreamResource (com.vaadin.flow.server.StreamResource)26 Test (org.junit.Test)24 UI (com.vaadin.flow.component.UI)14 Element (com.vaadin.flow.dom.Element)13 URI (java.net.URI)12 WeakReference (java.lang.ref.WeakReference)9 StreamRegistration (com.vaadin.flow.server.StreamRegistration)4 StreamResourceRegistry (com.vaadin.flow.server.StreamResourceRegistry)4 Anchor (com.vaadin.flow.component.html.Anchor)1 ContentTypeResolver (com.vaadin.flow.function.ContentTypeResolver)1 AbstractStreamResource (com.vaadin.flow.server.AbstractStreamResource)1 StreamReceiver (com.vaadin.flow.server.StreamReceiver)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1