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.util;
16  
17  import java.io.IOException;
18  
19  import jakarta.servlet.ServletException;
20  import jakarta.servlet.http.HttpServlet;
21  import jakarta.servlet.http.HttpServletRequest;
22  import jakarta.servlet.http.HttpServletResponse;
23  
24  /**
25   * A servlet that wraps static content.
26   *
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   */
30  public abstract class ServletContentWrapper extends HttpServlet {
31  
32      private final String content_;
33  
34      /**
35       * Creates an instance.
36       *
37       * @param content the HTML content of this servlet
38       */
39      public ServletContentWrapper(final String content) {
40          content_ = content;
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      @Override
47      protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
48          throws ServletException, IOException {
49          response.setContentType(MimeType.TEXT_HTML);
50          response.getWriter().write(content_);
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      @Override
57      protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
58          throws ServletException, IOException {
59          doGet(request, response);
60      }
61  
62      /**
63       * Helper.
64       * @return the content
65       */
66      protected String getContent() {
67          return content_;
68      }
69  
70      /**
71       * Helper.
72       * @return the length of the content
73       */
74      protected int getContentLength() {
75          return content_.length();
76      }
77  }