Skip to content

Commit

Permalink
[ZEPPELIN-5933] Polish jetty (apache#4621)
Browse files Browse the repository at this point in the history
* Rewrite index.html handling

* Polish initialize of the Rest API

* Don't break the WebApp Classloader Isolation

* Use default SessionHandler

* Add simple session tests
  • Loading branch information
Reamer authored Jun 27, 2023
1 parent cb8a4f5 commit 55a614a
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 320 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.zeppelin.server;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.zeppelin.conf.ZeppelinConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IndexHtmlServlet extends HttpServlet {

private static final Logger LOGGER = LoggerFactory.getLogger(IndexHtmlServlet.class);

/**
*
*/
private static final long serialVersionUID = 1L;

private static final String TAG_BODY_OPENING = "<body"; // ignore bracket here to support potential html attributes
private static final String TAG_BODY_CLOSING = "</body>";
private static final String TAG_HEAD_CLOSING = "</head>";
private static final String TAG_HTML_CLOSING = "</html>";

final String bodyAddon;
final String headAddon;

public IndexHtmlServlet(ZeppelinConfiguration conf) {
this.bodyAddon = conf.getHtmlBodyAddon();
this.headAddon = conf.getHtmlHeadAddon();
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
URL indexResource = getServletContext().getResource("/index.html");
String content = IOUtils.toString(indexResource, StandardCharsets.UTF_8);
// read original content from resource
if (bodyAddon != null) {
if (content.contains(TAG_BODY_CLOSING)) {
content = content.replace(TAG_BODY_CLOSING, bodyAddon + TAG_BODY_CLOSING);
} else if (content.contains(TAG_HTML_CLOSING)) {
content = content.replace(TAG_HTML_CLOSING, bodyAddon + TAG_HTML_CLOSING);
} else {
content = content + bodyAddon;
}
}
// process head addon
if (headAddon != null) {
if (content.contains(TAG_HEAD_CLOSING)) {
content = content.replace(TAG_HEAD_CLOSING, headAddon + TAG_HEAD_CLOSING);
} else if (content.contains(TAG_BODY_OPENING)) {
content = content.replace(TAG_BODY_OPENING, headAddon + TAG_BODY_OPENING);
} else {
LOGGER.error(
"Unable to process Head html addon. Could not find proper anchor in index.html.");
}
}
resp.setContentType("text/html");
resp.setStatus(HttpServletResponse.SC_OK);
resp.getWriter().append(content);
} catch (IOException e) {
LOGGER.error("Error rendering index.html.", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.zeppelin.server;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

import org.apache.zeppelin.rest.AdminRestApi;
import org.apache.zeppelin.rest.ClusterRestApi;
import org.apache.zeppelin.rest.ConfigurationsRestApi;
import org.apache.zeppelin.rest.CredentialRestApi;
import org.apache.zeppelin.rest.HeliumRestApi;
import org.apache.zeppelin.rest.InterpreterRestApi;
import org.apache.zeppelin.rest.LoginRestApi;
import org.apache.zeppelin.rest.NotebookRepoRestApi;
import org.apache.zeppelin.rest.NotebookRestApi;
import org.apache.zeppelin.rest.SecurityRestApi;
import org.apache.zeppelin.rest.SessionRestApi;
import org.apache.zeppelin.rest.ZeppelinRestApi;
import org.apache.zeppelin.rest.exception.WebApplicationExceptionMapper;

public class RestApiApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<>();
s.add(AdminRestApi.class);
s.add(ClusterRestApi.class);
s.add(ConfigurationsRestApi.class);
s.add(CredentialRestApi.class);
s.add(HeliumRestApi.class);
s.add(InterpreterRestApi.class);
s.add(LoginRestApi.class);
s.add(NotebookRepoRestApi.class);
s.add(NotebookRestApi.class);
s.add(SecurityRestApi.class);
s.add(SessionRestApi.class);
s.add(ZeppelinRestApi.class);

// add ExceptionMapper
s.add(WebApplicationExceptionMapper.class);
return s;
}
}
Loading

0 comments on commit 55a614a

Please sign in to comment.