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 9.1KB

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