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.

FileDownloaderUI.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright 2012 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components;
  17. import java.awt.image.BufferedImage;
  18. import java.io.BufferedOutputStream;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. import javax.imageio.ImageIO;
  28. import com.vaadin.server.ClassResource;
  29. import com.vaadin.server.ConnectorResource;
  30. import com.vaadin.server.FileDownloader;
  31. import com.vaadin.server.FileResource;
  32. import com.vaadin.server.StreamResource;
  33. import com.vaadin.server.VaadinRequest;
  34. import com.vaadin.server.VaadinResponse;
  35. import com.vaadin.tests.components.embedded.EmbeddedPdf;
  36. import com.vaadin.ui.AbstractComponent;
  37. import com.vaadin.ui.Button;
  38. import com.vaadin.ui.Button.ClickEvent;
  39. import com.vaadin.ui.Button.ClickListener;
  40. import com.vaadin.ui.Component;
  41. import com.vaadin.ui.CssLayout;
  42. import com.vaadin.ui.HorizontalLayout;
  43. import com.vaadin.ui.Label;
  44. import com.vaadin.ui.Layout;
  45. import com.vaadin.ui.NativeButton;
  46. public class FileDownloaderUI extends AbstractTestUIWithLog {
  47. private AbstractComponent firstDownloadComponent;
  48. @Override
  49. protected void setup(VaadinRequest request) {
  50. List<Class<? extends Component>> components = new ArrayList<Class<? extends Component>>();
  51. components.add(Button.class);
  52. components.add(NativeButton.class);
  53. components.add(CssLayout.class);
  54. components.add(Label.class);
  55. ConnectorResource resource;
  56. resource = new StreamResource(new StreamResource.StreamSource() {
  57. @Override
  58. public InputStream getStream() {
  59. try {
  60. BufferedImage img = getImage2("demo.png");
  61. ByteArrayOutputStream imagebuffer = new ByteArrayOutputStream();
  62. ImageIO.write(img, "png", imagebuffer);
  63. Thread.sleep(5000);
  64. return new ByteArrayInputStream(imagebuffer.toByteArray());
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. return null;
  68. }
  69. }
  70. }, "demo.png");
  71. addComponents("Dynamic image", resource, components);
  72. try {
  73. File hugeFile = File.createTempFile("huge", ".txt");
  74. hugeFile.deleteOnExit();
  75. BufferedOutputStream os = new BufferedOutputStream(
  76. new FileOutputStream(hugeFile));
  77. int writeAtOnce = 1024 * 1024;
  78. byte[] b = new byte[writeAtOnce];
  79. for (int i = 0; i < 5l * 1024l * 1024l; i += writeAtOnce) {
  80. os.write(b);
  81. }
  82. os.close();
  83. resource = new FileResource(hugeFile);
  84. addComponents("Huge text file", resource, components);
  85. } catch (IOException e) {
  86. e.printStackTrace();
  87. }
  88. resource = new ClassResource(new EmbeddedPdf().getClass(), "test.pdf");
  89. addComponents("Class resource pdf", resource, components);
  90. Button downloadUtf8File = new Button("Download UTF-8 named file");
  91. FileDownloader fd = new FileDownloader(new ClassResource(
  92. new EmbeddedPdf().getClass(), "åäö-日本語.pdf"));
  93. fd.setOverrideContentType(false);
  94. fd.extend(downloadUtf8File);
  95. addComponent(downloadUtf8File);
  96. addComponent(new Button("Remove first download button",
  97. new ClickListener() {
  98. @Override
  99. public void buttonClick(ClickEvent event) {
  100. Layout parent = (Layout) firstDownloadComponent
  101. .getParent();
  102. parent.removeComponent(firstDownloadComponent);
  103. }
  104. }));
  105. addComponent(new Button(
  106. "Detach FileDownloader from first download button",
  107. new ClickListener() {
  108. @Override
  109. public void buttonClick(ClickEvent event) {
  110. FileDownloader e = (FileDownloader) firstDownloadComponent
  111. .getExtensions().iterator().next();
  112. e.remove();
  113. log("FileDownload detached");
  114. }
  115. }));
  116. }
  117. public void addComponents(String caption, ConnectorResource resource,
  118. List<Class<? extends Component>> components) {
  119. HorizontalLayout layout = new HorizontalLayout();
  120. layout.setCaption(caption);
  121. for (Class<? extends Component> cls : components) {
  122. try {
  123. AbstractComponent c = (AbstractComponent) cls.newInstance();
  124. if (firstDownloadComponent == null) {
  125. firstDownloadComponent = c;
  126. }
  127. c.setId(cls.getName() + caption.replace(" ", ""));
  128. c.setCaption(cls.getName());
  129. c.setDescription(resource.getMIMEType() + " / "
  130. + resource.getClass());
  131. c.setWidth("100px");
  132. c.setHeight("100px");
  133. layout.addComponent(c);
  134. new FileDownloader(resource).extend(c);
  135. if (c instanceof Button) {
  136. ((Button) c).addClickListener(new ClickListener() {
  137. @Override
  138. public void buttonClick(ClickEvent event) {
  139. }
  140. });
  141. }
  142. } catch (Exception e) {
  143. System.err.println("Could not instatiate " + cls.getName());
  144. }
  145. }
  146. addComponent(layout);
  147. }
  148. private static final String DYNAMIC_IMAGE_NAME = "requestImage.png";
  149. @Override
  150. public boolean handleConnectorRequest(VaadinRequest request,
  151. VaadinResponse response, String path) throws IOException {
  152. if (DYNAMIC_IMAGE_NAME.equals(path)) {
  153. // Create an image, draw the "text" parameter to it and output it to
  154. // the browser.
  155. String text = request.getParameter("text");
  156. if (text == null) {
  157. text = DYNAMIC_IMAGE_NAME;
  158. }
  159. BufferedImage bi = getImage(text);
  160. response.setContentType("image/png");
  161. response.setHeader("Content-Disposition", "attachment; filename=\""
  162. + path + "\"");
  163. ImageIO.write(bi, "png", response.getOutputStream());
  164. return true;
  165. } else {
  166. return super.handleConnectorRequest(request, response, path);
  167. }
  168. }
  169. private BufferedImage getImage(String text) {
  170. BufferedImage bi = new BufferedImage(150, 30,
  171. BufferedImage.TYPE_3BYTE_BGR);
  172. bi.getGraphics()
  173. .drawChars(text.toCharArray(), 0, text.length(), 10, 20);
  174. return bi;
  175. }
  176. private BufferedImage getImage2(String text) {
  177. BufferedImage bi = new BufferedImage(200, 200,
  178. BufferedImage.TYPE_INT_RGB);
  179. bi.getGraphics()
  180. .drawChars(text.toCharArray(), 0, text.length(), 10, 20);
  181. return bi;
  182. }
  183. }