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.3KB

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