Skip to content

Commit

Permalink
CB-5713 fix action links (#2991)
Browse files Browse the repository at this point in the history
  • Loading branch information
yagudin10 authored Oct 15, 2024
1 parent 21fe547 commit d49342c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.cloudbeaver.model.session.WebSession;
import io.cloudbeaver.service.DBWServletHandler;
import io.cloudbeaver.utils.WebAppUtils;
import jakarta.servlet.Servlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -43,7 +44,7 @@ protected void createActionFromParams(WebSession session, HttpServletRequest req
action.saveInSession(session);

// Redirect to home
response.sendRedirect("/");
response.sendRedirect(WebAppUtils.getWebApplication().getServerConfiguration().getRootURI());
}

protected abstract String getActionConsole();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io.cloudbeaver.server.servlets.CBImageServlet;
import io.cloudbeaver.server.servlets.CBStaticServlet;
import io.cloudbeaver.server.servlets.CBStatusServlet;
import io.cloudbeaver.server.servlets.ProxyResourceHandler;
import io.cloudbeaver.server.websockets.CBJettyWebSocketManager;
import io.cloudbeaver.service.DBWServiceBindingServlet;
import io.cloudbeaver.service.DBWServiceBindingWebSocket;
Expand Down Expand Up @@ -101,11 +100,12 @@ public void runServer() {
String rootURI = serverConfiguration.getRootURI();
servletContextHandler.setContextPath(rootURI);

ServletHolder staticServletHolder = new ServletHolder("static", new CBStaticServlet());
ServletHolder staticServletHolder = new ServletHolder(
"static", new CBStaticServlet(Path.of(serverConfiguration.getContentRoot()))
);
staticServletHolder.setInitParameter("dirAllowed", "false");
staticServletHolder.setInitParameter("cacheControl", "public, max-age=" + CBStaticServlet.STATIC_CACHE_SECONDS);
servletContextHandler.addServlet(staticServletHolder, "/");
servletContextHandler.insertHandler(new ProxyResourceHandler(Path.of(serverConfiguration.getContentRoot())));

if (Files.isSymbolicLink(contentRootPath)) {
servletContextHandler.addAliasCheck(new CBSymLinkContentAllowedAliasChecker(contentRootPath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.ee10.servlet.DefaultServlet;
import org.eclipse.jetty.http.HttpHeader;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.auth.SMAuthInfo;
import org.jkiss.dbeaver.model.auth.SMAuthProvider;
import org.jkiss.dbeaver.model.security.SMAuthProviderCustomConfiguration;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.IOUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

@WebServlet(urlPatterns = "/")
Expand All @@ -54,6 +63,13 @@ public class CBStaticServlet extends DefaultServlet {

private static final Log log = Log.getLog(CBStaticServlet.class);

@NotNull
private final Path contentRoot;

public CBStaticServlet(@NotNull Path contentRoot) {
this.contentRoot = contentRoot;
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
for (WebServletHandlerDescriptor handler : WebHandlerRegistry.getInstance().getServletHandlers()) {
Expand Down Expand Up @@ -83,7 +99,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
} catch (DBWebException e) {
log.error("Error reading websession", e);
}
super.doGet(request, response);
patchStaticContentIfNeeded(request, response);
}

private void performAutoLoginIfNeeded(HttpServletRequest request, WebSession webSession) {
Expand Down Expand Up @@ -177,4 +193,40 @@ private boolean processSessionStart(HttpServletRequest request, HttpServletRespo
return false;
}

private void patchStaticContentIfNeeded(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pathInContext = request.getServletPath();

if ("/".equals(pathInContext)) {
pathInContext = "index.html";
}

if (pathInContext == null || !pathInContext.endsWith("index.html")
&& !pathInContext.endsWith("sso.html")
&& !pathInContext.endsWith("ssoError.html")
) {
super.doGet(request, response);
return;
}

if (pathInContext.startsWith("/")) {
pathInContext = pathInContext.substring(1);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
var filePath = contentRoot.resolve(pathInContext);
try (InputStream fis = Files.newInputStream(filePath)) {
IOUtils.copyStream(fis, baos);
}
String indexContents = baos.toString(StandardCharsets.UTF_8);
CBServerConfig serverConfig = CBApplication.getInstance().getServerConfiguration();
indexContents = indexContents
.replace("{ROOT_URI}", serverConfig.getRootURI())
.replace("{STATIC_CONTENT}", serverConfig.getStaticContent());
byte[] indexBytes = indexContents.getBytes(StandardCharsets.UTF_8);

// Disable cache for index.html
response.setHeader(HttpHeader.CACHE_CONTROL.toString(), "no-cache, no-store, must-revalidate");
response.setHeader(HttpHeader.EXPIRES.toString(), "0");
response.getOutputStream().write(ByteBuffer.wrap(indexBytes));
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class LocalServletHandler extends AbstractActionServletHandler {

@Override
public boolean handleRequest(Servlet servlet, HttpServletRequest request, HttpServletResponse response) throws DBException, IOException {
if (URI_PREFIX.equals(WebAppUtils.removeSideSlashes(request.getPathInfo()))) {
if (URI_PREFIX.equals(WebAppUtils.removeSideSlashes(request.getServletPath()))) {
try {
WebSession webSession = CBPlatform.getInstance().getSessionManager().getWebSession(request, response, true);
createActionFromParams(webSession, request, response);
Expand Down

0 comments on commit d49342c

Please sign in to comment.