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.

ExternalResource.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal;
  5. import java.io.Serializable;
  6. import java.net.URL;
  7. import com.vaadin.service.FileTypeResolver;
  8. /**
  9. * <code>ExternalResource</code> implements source for resources fetched from
  10. * location specified by URL:s. The resources are fetched directly by the client
  11. * terminal and are not fetched trough the terminal adapter.
  12. *
  13. * @author Vaadin Ltd.
  14. * @version
  15. * @VERSION@
  16. * @since 3.0
  17. */
  18. @SuppressWarnings("serial")
  19. public class ExternalResource implements Resource, Serializable {
  20. /**
  21. * Url of the download.
  22. */
  23. private String sourceURL = null;
  24. /**
  25. * MIME Type for the resource
  26. */
  27. private String mimeType = null;
  28. /**
  29. * Creates a new download component for downloading directly from given URL.
  30. *
  31. * @param sourceURL
  32. * the source URL.
  33. */
  34. public ExternalResource(URL sourceURL) {
  35. if (sourceURL == null) {
  36. throw new RuntimeException("Source must be non-null");
  37. }
  38. this.sourceURL = sourceURL.toString();
  39. }
  40. /**
  41. * Creates a new download component for downloading directly from given URL.
  42. *
  43. * @param sourceURL
  44. * the source URL.
  45. * @param mimeType
  46. * the MIME Type
  47. */
  48. public ExternalResource(URL sourceURL, String mimeType) {
  49. this(sourceURL);
  50. this.mimeType = mimeType;
  51. }
  52. /**
  53. * Creates a new download component for downloading directly from given URL.
  54. *
  55. * @param sourceURL
  56. * the source URL.
  57. */
  58. public ExternalResource(String sourceURL) {
  59. if (sourceURL == null) {
  60. throw new RuntimeException("Source must be non-null");
  61. }
  62. this.sourceURL = sourceURL.toString();
  63. }
  64. /**
  65. * Creates a new download component for downloading directly from given URL.
  66. *
  67. * @param sourceURL
  68. * the source URL.
  69. * @param mimeType
  70. * the MIME Type
  71. */
  72. public ExternalResource(String sourceURL, String mimeType) {
  73. this(sourceURL);
  74. this.mimeType = mimeType;
  75. }
  76. /**
  77. * Gets the URL of the external resource.
  78. *
  79. * @return the URL of the external resource.
  80. */
  81. public String getURL() {
  82. return sourceURL;
  83. }
  84. /**
  85. * Gets the MIME type of the resource.
  86. *
  87. * @see com.vaadin.terminal.Resource#getMIMEType()
  88. */
  89. @Override
  90. public String getMIMEType() {
  91. if (mimeType == null) {
  92. mimeType = FileTypeResolver.getMIMEType(getURL().toString());
  93. }
  94. return mimeType;
  95. }
  96. /**
  97. * Sets the MIME type of the resource.
  98. */
  99. public void setMIMEType(String mimeType) {
  100. this.mimeType = mimeType;
  101. }
  102. }