View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * Wrapper for the HTML element "embed".
35   *
36   * @author Ahmed Ashour
37   * @author Ronald Brill
38   * @author Frank Danek
39   */
40  public class HtmlEmbed extends HtmlElement {
41  
42      /** The HTML tag represented by this element. */
43      public static final String TAG_NAME = "embed";
44  
45      /**
46       * Creates a new instance.
47       *
48       * @param qualifiedName the qualified name of the element type to instantiate
49       * @param page the page that contains this element
50       * @param attributes the initial attributes
51       */
52      HtmlEmbed(final String qualifiedName, final SgmlPage page,
53              final Map<String, DomAttr> attributes) {
54          super(qualifiedName, page, attributes);
55      }
56  
57      /**
58       * {@inheritDoc}
59       * Checks the src url against the Content-Security-Policy (object-src).
60       */
61      @Override
62      protected void onAddedToPage() {
63          super.onAddedToPage();
64          ContentSecurityPolicy.allowsObject(this, getAttributeDirect("src"));
65      }
66  
67      /**
68       * {@inheritDoc}
69       * Checks the new src url against the Content-Security-Policy (object-src).
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       * Saves this content as the specified file.
88       * @param file the file to save to
89       * @throws IOException if an IO error occurs
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      * {@inheritDoc}
107      */
108     @Override
109     public DisplayStyle getDefaultStyleDisplay() {
110         return DisplayStyle.INLINE;
111     }
112 }