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.

AbstractMedia.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.ui;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;
  25. import org.jsoup.nodes.Attributes;
  26. import org.jsoup.nodes.Element;
  27. import org.jsoup.nodes.Node;
  28. import com.vaadin.server.ConnectorResource;
  29. import com.vaadin.server.DownloadStream;
  30. import com.vaadin.server.Resource;
  31. import com.vaadin.server.ResourceReference;
  32. import com.vaadin.server.VaadinRequest;
  33. import com.vaadin.server.VaadinResponse;
  34. import com.vaadin.server.VaadinSession;
  35. import com.vaadin.shared.communication.URLReference;
  36. import com.vaadin.shared.ui.AbstractMediaState;
  37. import com.vaadin.shared.ui.MediaControl;
  38. import com.vaadin.shared.ui.PreloadMode;
  39. import com.vaadin.ui.declarative.DesignAttributeHandler;
  40. import com.vaadin.ui.declarative.DesignContext;
  41. /**
  42. * Abstract base class for the HTML5 media components.
  43. *
  44. * @author Vaadin Ltd
  45. */
  46. public abstract class AbstractMedia extends AbstractComponent {
  47. @Override
  48. protected AbstractMediaState getState() {
  49. return (AbstractMediaState) super.getState();
  50. }
  51. @Override
  52. protected AbstractMediaState getState(boolean markAsDirty) {
  53. return (AbstractMediaState) super.getState(markAsDirty);
  54. }
  55. /**
  56. * Sets a single media file as the source of the media component.
  57. *
  58. * @param source
  59. */
  60. public void setSource(Resource source) {
  61. clearSources();
  62. addSource(source);
  63. }
  64. private void clearSources() {
  65. getState().sources.clear();
  66. getState().sourceTypes.clear();
  67. }
  68. /**
  69. * Adds an alternative media file to the sources list. Which of the sources
  70. * is used is selected by the browser depending on which file formats it
  71. * supports. See
  72. * <a href="http://en.wikipedia.org/wiki/HTML5_video#Table">wikipedia</a>
  73. * for a table of formats supported by different browsers.
  74. *
  75. * @param source
  76. */
  77. public void addSource(Resource source) {
  78. if (source != null) {
  79. List<URLReference> sources = getState().sources;
  80. sources.add(new ResourceReference(source, this,
  81. Integer.toString(sources.size())));
  82. getState().sourceTypes.add(source.getMIMEType());
  83. }
  84. }
  85. @Override
  86. public boolean handleConnectorRequest(VaadinRequest request,
  87. VaadinResponse response, String path) throws IOException {
  88. Matcher matcher = Pattern.compile("(\\d+)(/.*)?").matcher(path);
  89. if (!matcher.matches()) {
  90. return super.handleConnectorRequest(request, response, path);
  91. }
  92. DownloadStream stream;
  93. VaadinSession session = getSession();
  94. session.lock();
  95. try {
  96. List<URLReference> sources = getState().sources;
  97. int sourceIndex = Integer.parseInt(matcher.group(1));
  98. if (sourceIndex < 0 || sourceIndex >= sources.size()) {
  99. getLogger().log(Level.WARNING,
  100. "Requested source index {0} is out of bounds",
  101. sourceIndex);
  102. return false;
  103. }
  104. URLReference reference = sources.get(sourceIndex);
  105. ConnectorResource resource = (ConnectorResource) ResourceReference
  106. .getResource(reference);
  107. stream = resource.getStream();
  108. } finally {
  109. session.unlock();
  110. }
  111. stream.writeResponse(request, response);
  112. return true;
  113. }
  114. private Logger getLogger() {
  115. return Logger.getLogger(AbstractMedia.class.getName());
  116. }
  117. /**
  118. * Set multiple sources at once. Which of the sources is used is selected by
  119. * the browser depending on which file formats it supports. See
  120. * <a href="http://en.wikipedia.org/wiki/HTML5_video#Table">wikipedia</a>
  121. * for a table of formats supported by different browsers.
  122. *
  123. * @param sources
  124. */
  125. public void setSources(Resource... sources) {
  126. clearSources();
  127. for (Resource source : sources) {
  128. addSource(source);
  129. }
  130. }
  131. /**
  132. * @return The sources pointed to in this media.
  133. */
  134. public List<Resource> getSources() {
  135. List<Resource> sources = new ArrayList<>();
  136. for (URLReference ref : getState(false).sources) {
  137. sources.add(((ResourceReference) ref).getResource());
  138. }
  139. return sources;
  140. }
  141. /**
  142. * Sets whether or not the browser should show native media controls.
  143. *
  144. * @param showControls
  145. */
  146. public void setShowControls(boolean showControls) {
  147. getState().showControls = showControls;
  148. }
  149. /**
  150. * @return true if the browser is to show native media controls.
  151. */
  152. public boolean isShowControls() {
  153. return getState(false).showControls;
  154. }
  155. /**
  156. * Sets the alternative text to be displayed if the browser does not support
  157. * HTML5. This text is rendered as HTML if
  158. * {@link #setHtmlContentAllowed(boolean)} is set to true. With HTML
  159. * rendering, this method can also be used to implement fallback to a
  160. * flash-based player, see the <a href=
  161. * "https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Using_Flash"
  162. * >Mozilla Developer Network</a> for details.
  163. *
  164. * @param altText
  165. */
  166. public void setAltText(String altText) {
  167. getState().altText = altText;
  168. }
  169. /**
  170. * @return The text/html that is displayed when a browser doesn't support
  171. * HTML5.
  172. */
  173. public String getAltText() {
  174. return getState(false).altText;
  175. }
  176. /**
  177. * Sets the preload attribute that is intended to provide a hint to the
  178. * browser how the media should be preloaded. Valid values are 'none',
  179. * 'metadata', 'preload', see the <a href=
  180. * "https://developer.mozilla.org/en/docs/Web/HTML/Element/video#attr-preload">
  181. * Mozilla Developer Network</a> for details.
  182. *
  183. * @param preload
  184. * preload mode
  185. * @since 7.7.11
  186. */
  187. public void setPreload(final PreloadMode preload) {
  188. getState().preload = preload;
  189. }
  190. /**
  191. * @return the configured media preload value
  192. * @since 7.7.11
  193. */
  194. public PreloadMode getPreload() {
  195. return getState(false).preload;
  196. }
  197. /**
  198. * Enables or disables looping.
  199. *
  200. * @param loop
  201. * if true, enable looping
  202. * @since 7.7.11
  203. */
  204. public void setLoop(final boolean loop) {
  205. getState().loop = loop;
  206. }
  207. /**
  208. * @return true if looping is enabled
  209. * @since 7.7.11
  210. */
  211. public boolean isLoop() {
  212. return getState(false).loop;
  213. }
  214. /**
  215. * Set whether the alternative text ({@link #setAltText(String)}) is
  216. * rendered as HTML or not.
  217. *
  218. * @param htmlContentAllowed
  219. */
  220. public void setHtmlContentAllowed(boolean htmlContentAllowed) {
  221. getState().htmlContentAllowed = htmlContentAllowed;
  222. }
  223. /**
  224. * @return true if the alternative text ({@link #setAltText(String)}) is to
  225. * be rendered as HTML.
  226. */
  227. public boolean isHtmlContentAllowed() {
  228. return getState(false).htmlContentAllowed;
  229. }
  230. /**
  231. * Sets whether the media is to automatically start playback when enough
  232. * data has been loaded.
  233. *
  234. * @param autoplay
  235. */
  236. public void setAutoplay(boolean autoplay) {
  237. getState().autoplay = autoplay;
  238. }
  239. /**
  240. * @return true if the media is set to automatically start playback.
  241. */
  242. public boolean isAutoplay() {
  243. return getState(false).autoplay;
  244. }
  245. /**
  246. * Set whether to mute the audio or not.
  247. *
  248. * @param muted
  249. */
  250. public void setMuted(boolean muted) {
  251. getState().muted = muted;
  252. }
  253. /**
  254. * @return true if the audio is muted.
  255. */
  256. public boolean isMuted() {
  257. return getState(false).muted;
  258. }
  259. /**
  260. * Pauses the media.
  261. */
  262. public void pause() {
  263. getRpcProxy(MediaControl.class).pause();
  264. }
  265. /**
  266. * Starts playback of the media.
  267. */
  268. public void play() {
  269. getRpcProxy(MediaControl.class).play();
  270. }
  271. @Override
  272. public void writeDesign(Element design, DesignContext designContext) {
  273. super.writeDesign(design, designContext);
  274. String altText = getAltText();
  275. if (altText != null && !altText.isEmpty()) {
  276. design.append(altText);
  277. }
  278. for (Resource r : getSources()) {
  279. Attributes attr = design.appendElement("source").attributes();
  280. DesignAttributeHandler.writeAttribute("href", attr, r, null,
  281. Resource.class, designContext);
  282. }
  283. }
  284. @Override
  285. public void readDesign(Element design, DesignContext designContext) {
  286. super.readDesign(design, designContext);
  287. String altText = "";
  288. for (Node child : design.childNodes()) {
  289. if (child instanceof Element
  290. && ((Element) child).tagName().equals("source")
  291. && child.hasAttr("href")) {
  292. addSource(DesignAttributeHandler.readAttribute("href",
  293. child.attributes(), Resource.class));
  294. } else {
  295. altText += child.toString();
  296. }
  297. }
  298. altText = altText.trim();
  299. if (!altText.isEmpty()) {
  300. setAltText(altText);
  301. }
  302. }
  303. @Override
  304. protected Collection<String> getCustomAttributes() {
  305. Collection<String> result = super.getCustomAttributes();
  306. result.add("alt-text");
  307. return result;
  308. }
  309. }