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

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