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.attachment;
16  
17  import java.io.Serializable;
18  
19  import org.htmlunit.Page;
20  import org.htmlunit.WebResponse;
21  import org.htmlunit.http.ContentDisposition;
22  import org.htmlunit.http.ContentDispositionParser;
23  import org.htmlunit.util.MimeType;
24  
25  /**
26   * <p>A handler for attachments, which represent pages received from the server which contain
27   * {@code Content-Disposition=attachment} headers. Normally pages are loaded inline: clicking on
28   * a link, for example, loads the linked page in the current window. Attached pages are different
29   * in that they are intended to be loaded outside of this flow: clicking on a link prompts the
30   * user to either save the linked page, or open it outside the current window, but does not
31   * load the page in the current window.</p>
32   *
33   * <p>HtmlUnit complies with the semantics described above when an <code>AttachmentHandler</code> has
34   * been registered with the {@link org.htmlunit.WebClient} via
35   * {@link org.htmlunit.WebClient#setAttachmentHandler(AttachmentHandler)}. When
36   * no attachment handler has been registered with the <code>WebClient</code>, the semantics described
37   * above to not apply, and attachments are loaded inline. By default, <code>AttachmentHandler</code>s
38   * are not registered with new <code>WebClient</code> instances.</p>
39   *
40   * @author Bruce Chapman
41   * @author Sudhan Moghe
42   * @author Daniel Gredler
43   * @author Ronald Brill
44   * @author Alex Gorbatovsky
45   * @author Lai Quang Duong
46   * @see org.htmlunit.WebClient#setAttachmentHandler(AttachmentHandler)
47   * @see org.htmlunit.WebClient#getAttachmentHandler()
48   * @see <a href="http://www.ietf.org/rfc/rfc2183.txt">RFC 2183</a>
49   */
50  @FunctionalInterface
51  public interface AttachmentHandler extends Serializable {
52  
53      /**
54       * Handles the specified attached page. This is some kind of information
55       * that the page was handled as attachment.
56       * This method will only be called if {@link #handleAttachment(WebResponse, String)}
57       * has returned false for the response.
58       * @param page an attached page, which doesn't get loaded inline
59       * @param attachmentFilename the filename to use for the attachment or {@code null} if unspecified
60       */
61      void handleAttachment(Page page, String attachmentFilename);
62  
63      /**
64       * Process the specified attachment. If this method returns false,
65       * the client will open a new window with a page created from this
66       * response as content.
67       * Overwrite this method (and return true) if you do not need to create
68       * a new window for the response.
69       * @param response the response to process
70       * @param attachmentFilename the filename to use for the attachment or {@code null} if unspecified
71       * @return {@code true} if the specified response represents is handled by this method
72       * @see <a href="http://www.ietf.org/rfc/rfc2183.txt">RFC 2183</a>
73       */
74      default boolean handleAttachment(final WebResponse response, final String attachmentFilename) {
75          return false;
76      }
77  
78      /**
79       * Returns {@code true} if the specified response represents an attachment.
80       *
81       * <p>The browsers handle a response as attachment (download), if</p>
82       * <ul>
83       * <li>the disposition type of the {@code Content-Disposition} header is {@code attachment}
84       * (case-insensitive, white space around the type is ignored; see {@link ContentDispositionParser}),</li>
85       * <li>or the disposition type is unknown, e.g. {@code foo} or {@code attachmentfoo}; RFC 6266 says
86       * that unknown types have to be handled like {@code attachment},</li>
87       * <li>or the content type is {@code application/octet-stream} (case-insensitive, parameters like
88       * {@code charset} are ignored). The browsers can not display this content type, so they offer it
89       * for download whatever the {@code Content-Disposition} header is.</li>
90       * </ul>
91       * <p>A response is displayed in the window, if the disposition type is {@code inline} or if the header
92       * has no type at all (e.g. {@code ; filename=foo.txt}), and the content type is not octet-stream.</p>
93       *
94       * <p>Overwrite this method to handle more responses as attachment, e.g. all PDF files:</p>
95       * <pre>
96       * &#64;Override
97       * public boolean isAttachment(final WebResponse response) {
98       *     return AttachmentHandler.super.isAttachment(response)
99       *             || "application/pdf".equals(response.getContentType());
100      * }
101      * </pre>
102      *
103      * <p>The parsed {@code Content-Disposition} header is available from
104      * {@link WebResponse#getContentDisposition()}; e.g. to handle also inline responses
105      * with a file name as attachment:</p>
106      * <pre>
107      * &#64;Override
108      * public boolean isAttachment(final WebResponse response) {
109      *     final ContentDisposition contentDisposition = response.getContentDisposition();
110      *     return AttachmentHandler.super.isAttachment(response)
111      *             || (contentDisposition != null &amp;&amp; contentDisposition.getFilename() != null);
112      * }
113      * </pre>
114      *
115      * @param response the response to check
116      * @return {@code true} if the specified response represents an attachment, {@code false} otherwise
117      * @see <a href="http://www.ietf.org/rfc/rfc2183.txt">RFC 2183</a>
118      */
119     default boolean isAttachment(final WebResponse response) {
120         final ContentDisposition contentDisposition = response.getContentDisposition();
121         // unknown types are handled like 'attachment', 'inline' and an empty type are not
122         if (contentDisposition != null
123                 && !contentDisposition.getType().isEmpty() && !contentDisposition.isInline()) {
124             return true;
125         }
126 
127         // content type 'application/octet-stream' is handled like an attachment by the browsers; this is
128         // also true if the content disposition is 'inline' or has no type
129         // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#applicationoctet-stream
130         // They treat it as if the Content-Disposition header was set to attachment, and propose a "Save As" dialog.
131         // the parameters (e.g. charset) of the content type are ignored
132         final String contentType = response.getContentType();
133         return MimeType.APPLICATION_OCTET_STREAM.equalsIgnoreCase(contentType.trim());
134     }
135 }