1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21 import java.net.URL;
22 import java.nio.file.Files;
23 import java.util.Map;
24
25 import org.apache.commons.io.IOUtils;
26 import org.htmlunit.SgmlPage;
27 import org.htmlunit.WebClient;
28 import org.htmlunit.WebRequest;
29 import org.htmlunit.WebResponse;
30 import org.htmlunit.html.csp.ContentSecurityPolicy;
31 import org.htmlunit.util.StringUtils;
32
33
34
35
36
37
38
39
40 public class HtmlEmbed extends HtmlElement {
41
42
43 public static final String TAG_NAME = "embed";
44
45
46
47
48
49
50
51
52 HtmlEmbed(final String qualifiedName, final SgmlPage page,
53 final Map<String, DomAttr> attributes) {
54 super(qualifiedName, page, attributes);
55 }
56
57
58
59
60
61 @Override
62 protected void onAddedToPage() {
63 super.onAddedToPage();
64 ContentSecurityPolicy.allowsObject(this, getAttributeDirect("src"));
65 }
66
67
68
69
70
71 @Override
72 protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String value,
73 final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
74 final String qualifiedNameLC = StringUtils.toRootLowerCase(qualifiedName);
75 final boolean changed = "src".equals(qualifiedNameLC)
76 && !getAttributeNS(namespaceURI, qualifiedNameLC).equals(value);
77
78 super.setAttributeNS(namespaceURI, qualifiedName, value,
79 notifyAttributeChangeListeners, notifyMutationObservers);
80
81 if (changed && isAttachedToPage()) {
82 ContentSecurityPolicy.allowsObject(this, value);
83 }
84 }
85
86
87
88
89
90
91 public void saveAs(final File file) throws IOException {
92 final HtmlPage page = (HtmlPage) getPage();
93 final WebClient webclient = page.getWebClient();
94
95 final URL url = page.getFullyQualifiedUrl(getAttributeDirect(SRC_ATTRIBUTE));
96 final WebRequest request = new WebRequest(url, page.getCharset(), page.getUrl());
97 final WebResponse webResponse = webclient.loadWebResponse(request);
98
99 try (OutputStream fos = Files.newOutputStream(file.toPath());
100 InputStream content = webResponse.getContentAsStream()) {
101 IOUtils.copy(content, fos);
102 }
103 }
104
105
106
107
108 @Override
109 public DisplayStyle getDefaultStyleDisplay() {
110 return DisplayStyle.INLINE;
111 }
112 }