You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

application-resources.asciidoc 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. ---
  2. title: Images and Other Resources
  3. order: 5
  4. layout: page
  5. ---
  6. [[application.resources]]
  7. = Images and Other Resources
  8. Web applications can display various __resources__, such as images, other
  9. embedded content, or downloadable files, that the browser has to load from the
  10. server. Image resources are typically displayed with the [classname]#Image#
  11. component or as component icons. Embedded browser frames can be displayed with [classname]#BrowserFrame#, and
  12. other content with the [classname]#Embedded# component, as described in
  13. <<../components/components-embedded#components.embedded,"Embedded
  14. Resources">>. Downloadable files are usually provided by clicking a
  15. [classname]#Link# or using the [classname]#FileDownloader# extension.
  16. There are several ways to how such resources can be provided by the web server.
  17. Static resources can be provided without having to ask for them from the
  18. application. For dynamic resources, the user application must be able to create
  19. them dynamically. The resource request interfaces in Vaadin allow applications
  20. to both refer to static resources as well as dynamically create them. The
  21. dynamic creation includes the [classname]#StreamResource# class and the
  22. [interfacename]#RequestHandler# described in
  23. <<../advanced/advanced-requesthandler#advanced.requesthandler,"Request
  24. Handlers">>.
  25. Vaadin also provides low-level facilities for retrieving the URI and other
  26. parameters of a HTTP request. We will first look into how applications can
  27. provide various kinds of resources and then look into low-level interfaces for
  28. handling URIs and parameters to provide resources and functionalities.
  29. Notice that using request handlers to create "pages" is not normally meaningful
  30. in Vaadin or in AJAX applications generally. Please see
  31. <<../architecture/architecture-technology#architecture.technology.ajax,"AJAX">>
  32. for a detailed explanation.
  33. [[application.resources.api]]
  34. == Resource Interfaces and Classes
  35. The resource classes in Vaadin are grouped under two interfaces: a generic
  36. [classname]#Resource# interface and a more specific
  37. [classname]#ConnectorResource# interface for resources provided by the servlet.
  38. [[figure.resource.classdiagram]]
  39. .Resource Interface and Class Diagram
  40. image::img/resource_classdiagram-hi.png[width=80%, scaledwidth=100%]
  41. [[application.resources.file]]
  42. == File Resources
  43. File resources are files stored anywhere in the file system. As such, they can
  44. not be retrieved by a regular URL from the server, but need to be requested
  45. through the Vaadin servlet. The use of file resources is typically necessary for
  46. persistent user data that is not packaged in the web application, which would
  47. not be persistent over redeployments.
  48. A file object that can be accessed as a file resource is defined with the
  49. standard [classname]#java.io.File# class. You can create the file either with an
  50. absolute or relative path, but the base path of the relative path depends on the
  51. installation of the web server. For example, with Apache Tomcat, the default
  52. current directory would be the installation path of Tomcat.
  53. In the following example, we provide an image resource from a file stored in the
  54. web application. Notice that the image is stored under the [filename]#WEB-INF#
  55. folder, which is a special folder that is never accessible using an URL, unlike
  56. the other folders of a web application. This is a security solution - another
  57. would be to store the resource elsewhere in the file system.
  58. [source, java]
  59. ----
  60. // Find the application directory
  61. String basepath = VaadinService.getCurrent()
  62. .getBaseDirectory().getAbsolutePath();
  63. // Image as a file resource
  64. FileResource resource = new FileResource(new File(basepath +
  65. "/WEB-INF/images/image.png"));
  66. // Show the image in the application
  67. Image image = new Image("Image from file", resource);
  68. // Let the user view the file in browser or download it
  69. Link link = new Link("Link to the image file", resource);
  70. ----
  71. In a Maven based Vaadin project the image file should be located inside [filename]#src/main/webapp/WEB-INF/images/image.png#.
  72. [[application.resources.class]]
  73. == Class Loader Resources
  74. The [classname]#ClassResource# allows resources to be loaded from the class path
  75. using Java Class Loader. Normally, the relevant class path entry is the
  76. [filename]#WEB-INF/classes# folder under the web application, where the Java
  77. compilation should compile the Java classes and copy other files from the source
  78. tree.
  79. The one-line example below loads an image resource from the application package
  80. and displays it in an [classname]#Image# component.
  81. [source, java]
  82. ----
  83. layout.addComponent(new Image(null,
  84. new ClassResource("smiley.jpg")));
  85. ----
  86. [[application.resources.theme]]
  87. == Theme Resources
  88. Theme resources of [classname]#ThemeResource# class are files, typically images,
  89. included in a theme. A theme is located with the path
  90. [filename]#VAADIN/themes/themename# in a web application. The name of a theme
  91. resource is given as the parameter for the constructor, with a path relative to
  92. the theme folder.
  93. [source, java]
  94. ----
  95. // A theme resource in the current theme ("mytheme")
  96. // Located in: VAADIN/themes/mytheme/img/themeimage.png
  97. ThemeResource resource = new ThemeResource("img/themeimage.png");
  98. // Use the resource
  99. Image image = new Image("My Theme Image", resource);
  100. ----
  101. To use theme resources, you must set the theme for the UI. See
  102. <<../themes/themes-overview.asciidoc#themes.overview,"Themes">>
  103. for more information regarding themes.
  104. [[application.resources.stream]]
  105. == Stream Resources
  106. Stream resources allow creating dynamic resource content. Charts are typical
  107. examples of dynamic images. To define a stream resource, you need to implement
  108. the [classname]#StreamResource.StreamSource# interface and its
  109. [methodname]#getStream()# method. The method needs to return an
  110. [classname]#InputStream# from which the stream can be read.
  111. The following example demonstrates the creation of a simple image in PNG image
  112. format.
  113. [source, java]
  114. ----
  115. import java.awt.image.*;
  116. public class MyImageSource implements StreamSource {
  117. ByteArrayOutputStream imagebuffer = null;
  118. int reloads = 0;
  119. // This method generates the stream contents
  120. public InputStream getStream () {
  121. // Create an image
  122. BufferedImage image = new BufferedImage (400, 400,
  123. BufferedImage.TYPE_INT_RGB);
  124. Graphics2D drawable = image.createGraphics();
  125. // Draw something static
  126. drawable.setStroke(new BasicStroke(5));
  127. drawable.setColor(Color.WHITE);
  128. drawable.fillRect(0, 0, 400, 400);
  129. drawable.setColor(Color.BLACK);
  130. drawable.drawOval(50, 50, 300, 300);
  131. // Draw something dynamic
  132. drawable.setFont(new Font("Montserrat",
  133. Font.PLAIN, 48));
  134. drawable.drawString("Reloads=" + reloads, 75, 216);
  135. reloads++;
  136. drawable.setColor(new Color(0, 165, 235));
  137. int x= (int) (200-10 + 150*Math.sin(reloads * 0.3));
  138. int y= (int) (200-10 + 150*Math.cos(reloads * 0.3));
  139. drawable.fillOval(x, y, 20, 20);
  140. try {
  141. // Write the image to a buffer
  142. imagebuffer = new ByteArrayOutputStream();
  143. ImageIO.write(image, "png", imagebuffer);
  144. // Return a stream from the buffer
  145. return new ByteArrayInputStream(
  146. imagebuffer.toByteArray());
  147. } catch (IOException e) {
  148. return null;
  149. }
  150. }
  151. }
  152. ----
  153. The content of the generated image is dynamic, as it updates the reloads counter
  154. with every call. The [classname]#ImageIO#. [methodname]#write()# method writes
  155. the image to an output stream, while we had to return an input stream, so we
  156. stored the image contents to a temporary buffer.
  157. Below we display the image with the [classname]#Image# component.
  158. [source, java]
  159. ----
  160. // Create an instance of our stream source.
  161. StreamSource imagesource = new MyImageSource();
  162. // Create a resource that uses the stream source and give it
  163. // a name. The constructor will automatically register the
  164. // resource in the application.
  165. StreamResource resource =
  166. new StreamResource(imagesource, "myimage.png");
  167. // Create an image component that gets its contents
  168. // from the resource.
  169. layout.addComponent(new Image("Image title", resource));
  170. ----
  171. The resulting image is shown in <<figure.application.resource.stream>>.
  172. [[figure.application.resource.stream]]
  173. .A stream resource
  174. image::img/application_streamresource.png[width=25%, scaledwidth=25%]
  175. Another way to create dynamic content is a request handler, described in
  176. <<../advanced/advanced-requesthandler#advanced.requesthandler,"Request
  177. Handlers">>.