|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.zeppelin.server; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.URL; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | + |
| 23 | +import javax.servlet.ServletException; |
| 24 | +import javax.servlet.http.HttpServlet; |
| 25 | +import javax.servlet.http.HttpServletRequest; |
| 26 | +import javax.servlet.http.HttpServletResponse; |
| 27 | + |
| 28 | +import org.apache.commons.io.IOUtils; |
| 29 | +import org.apache.zeppelin.conf.ZeppelinConfiguration; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +public class IndexHtmlServlet extends HttpServlet { |
| 34 | + |
| 35 | + private static final Logger LOGGER = LoggerFactory.getLogger(IndexHtmlServlet.class); |
| 36 | + |
| 37 | + /** |
| 38 | + * |
| 39 | + */ |
| 40 | + private static final long serialVersionUID = 1L; |
| 41 | + |
| 42 | + private static final String TAG_BODY_OPENING = "<body"; // ignore bracket here to support potential html attributes |
| 43 | + private static final String TAG_BODY_CLOSING = "</body>"; |
| 44 | + private static final String TAG_HEAD_CLOSING = "</head>"; |
| 45 | + private static final String TAG_HTML_CLOSING = "</html>"; |
| 46 | + |
| 47 | + final String bodyAddon; |
| 48 | + final String headAddon; |
| 49 | + |
| 50 | + public IndexHtmlServlet(ZeppelinConfiguration conf) { |
| 51 | + this.bodyAddon = conf.getHtmlBodyAddon(); |
| 52 | + this.headAddon = conf.getHtmlHeadAddon(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 57 | + throws ServletException, IOException { |
| 58 | + try { |
| 59 | + URL indexResource = getServletContext().getResource("/index.html"); |
| 60 | + String content = IOUtils.toString(indexResource, StandardCharsets.UTF_8); |
| 61 | + // read original content from resource |
| 62 | + if (bodyAddon != null) { |
| 63 | + if (content.contains(TAG_BODY_CLOSING)) { |
| 64 | + content = content.replace(TAG_BODY_CLOSING, bodyAddon + TAG_BODY_CLOSING); |
| 65 | + } else if (content.contains(TAG_HTML_CLOSING)) { |
| 66 | + content = content.replace(TAG_HTML_CLOSING, bodyAddon + TAG_HTML_CLOSING); |
| 67 | + } else { |
| 68 | + content = content + bodyAddon; |
| 69 | + } |
| 70 | + } |
| 71 | + // process head addon |
| 72 | + if (headAddon != null) { |
| 73 | + if (content.contains(TAG_HEAD_CLOSING)) { |
| 74 | + content = content.replace(TAG_HEAD_CLOSING, headAddon + TAG_HEAD_CLOSING); |
| 75 | + } else if (content.contains(TAG_BODY_OPENING)) { |
| 76 | + content = content.replace(TAG_BODY_OPENING, headAddon + TAG_BODY_OPENING); |
| 77 | + } else { |
| 78 | + LOGGER.error( |
| 79 | + "Unable to process Head html addon. Could not find proper anchor in index.html."); |
| 80 | + } |
| 81 | + } |
| 82 | + resp.setContentType("text/html"); |
| 83 | + resp.setStatus(HttpServletResponse.SC_OK); |
| 84 | + resp.getWriter().append(content); |
| 85 | + } catch (IOException e) { |
| 86 | + LOGGER.error("Error rendering index.html.", e); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments