Skip to content

Commit 55a614a

Browse files
authored
[ZEPPELIN-5933] Polish jetty (#4621)
* Rewrite index.html handling * Polish initialize of the Rest API * Don't break the WebApp Classloader Isolation * Use default SessionHandler * Add simple session tests
1 parent cb8a4f5 commit 55a614a

File tree

9 files changed

+352
-320
lines changed

9 files changed

+352
-320
lines changed

zeppelin-server/src/main/java/org/apache/zeppelin/server/HtmlAddonResource.java

Lines changed: 0 additions & 173 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.util.HashSet;
20+
import java.util.Set;
21+
22+
import javax.ws.rs.core.Application;
23+
24+
import org.apache.zeppelin.rest.AdminRestApi;
25+
import org.apache.zeppelin.rest.ClusterRestApi;
26+
import org.apache.zeppelin.rest.ConfigurationsRestApi;
27+
import org.apache.zeppelin.rest.CredentialRestApi;
28+
import org.apache.zeppelin.rest.HeliumRestApi;
29+
import org.apache.zeppelin.rest.InterpreterRestApi;
30+
import org.apache.zeppelin.rest.LoginRestApi;
31+
import org.apache.zeppelin.rest.NotebookRepoRestApi;
32+
import org.apache.zeppelin.rest.NotebookRestApi;
33+
import org.apache.zeppelin.rest.SecurityRestApi;
34+
import org.apache.zeppelin.rest.SessionRestApi;
35+
import org.apache.zeppelin.rest.ZeppelinRestApi;
36+
import org.apache.zeppelin.rest.exception.WebApplicationExceptionMapper;
37+
38+
public class RestApiApplication extends Application {
39+
@Override
40+
public Set<Class<?>> getClasses() {
41+
Set<Class<?>> s = new HashSet<>();
42+
s.add(AdminRestApi.class);
43+
s.add(ClusterRestApi.class);
44+
s.add(ConfigurationsRestApi.class);
45+
s.add(CredentialRestApi.class);
46+
s.add(HeliumRestApi.class);
47+
s.add(InterpreterRestApi.class);
48+
s.add(LoginRestApi.class);
49+
s.add(NotebookRepoRestApi.class);
50+
s.add(NotebookRestApi.class);
51+
s.add(SecurityRestApi.class);
52+
s.add(SessionRestApi.class);
53+
s.add(ZeppelinRestApi.class);
54+
55+
// add ExceptionMapper
56+
s.add(WebApplicationExceptionMapper.class);
57+
return s;
58+
}
59+
}

0 commit comments

Comments
 (0)