Skip to content

Commit

Permalink
Merge branch 'devel' into dbeaver/pro#3393-udbt-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kseniaguzeeva authored Oct 15, 2024
2 parents c094379 + 94ea41c commit 5dce204
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,12 @@ public WSEventController getEventController() {
public boolean isEnvironmentVariablesAccessible() {
return false;
}

protected void closeResource(String name, Runnable closeFunction) {
try {
closeFunction.run();
} catch (Exception e) {
log.error("Failed close " + name, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
package io.cloudbeaver.model.app;

import io.cloudbeaver.server.WebServerPreferenceStore;
import org.jkiss.code.NotNull;

import java.util.Map;

/**
* Web server configuration.
* Contains only server configuration properties.
Expand All @@ -27,4 +32,12 @@ default String getRootURI() {
return "";
}

/**
* @return the setting values that will be used in {@link WebServerPreferenceStore}
*/
@NotNull
default Map<String, Object> getProductSettings() {
return Map.of();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@
*/
package io.cloudbeaver.server;

import io.cloudbeaver.utils.WebAppUtils;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.model.impl.preferences.AbstractPreferenceStore;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;

import java.io.IOException;
import java.util.Map;

public class CBPreferenceStore extends AbstractPreferenceStore {
@NotNull
private final CBPlatform cbPlatform;
public class WebServerPreferenceStore extends AbstractPreferenceStore {
private final DBPPreferenceStore parentStore;

public CBPreferenceStore(
@NotNull CBPlatform cbPlatform,
public WebServerPreferenceStore(
@NotNull DBPPreferenceStore parentStore
) {
this.cbPlatform = cbPlatform;
this.parentStore = parentStore;
}

Expand Down Expand Up @@ -188,7 +185,7 @@ public void save() throws IOException {
}

private Map<String, Object> productConf() {
var app = cbPlatform.getApplication();
return app.getProductConfiguration();
var app = WebAppUtils.getWebApplication();
return app.getServerConfiguration().getProductSettings();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class CBPlatform extends BaseGQLPlatform {
@Nullable
private static GQLApplicationAdapter application = null;

private CBPreferenceStore preferenceStore;
private WebServerPreferenceStore preferenceStore;
protected final List<DBPDriver> applicableDrivers = new ArrayList<>();

public static CBPlatform getInstance() {
Expand All @@ -77,7 +77,7 @@ public static void setApplication(@NotNull GQLApplicationAdapter application) {
protected synchronized void initialize() {
long startTime = System.currentTimeMillis();
log.info("Initialize web platform...: ");
this.preferenceStore = new CBPreferenceStore(this, WebPlatformActivator.getInstance().getPreferences());
this.preferenceStore = new WebServerPreferenceStore(WebPlatformActivator.getInstance().getPreferences());
super.initialize();
refreshApplicableDrivers();

Expand Down
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.app.DBPPlatform;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.PeriodicJob;

public class SessionStateJob extends PeriodicSystemJob {
public class SessionStateJob extends PeriodicJob {
private static final Log log = Log.getLog(SessionStateJob.class);
private static final int PERIOD_MS = 30_000; // once per 30 seconds
private final WebSessionManager sessionManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.app.DBPPlatform;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.PeriodicJob;

/**
* WebSessionMonitorJob
*/
public class WebSessionMonitorJob extends PeriodicSystemJob {
public class WebSessionMonitorJob extends PeriodicJob {
private static final Log log = Log.getLog(WebSessionMonitorJob.class);
private static final int MONITOR_INTERVAL = 10000; // once per 10 seconds
private final WebSessionManager sessionManager;
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
Loading

0 comments on commit 5dce204

Please sign in to comment.