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());
}
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));
}
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());
}
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);
}
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());
}
Aggregations