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