Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CB-5587 override default jetty session handler to add ability to use … #2880

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.jetty.server.session.DefaultSessionCache;
import org.eclipse.jetty.server.session.DefaultSessionIdManager;
import org.eclipse.jetty.server.session.NullSessionDataStore;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
Expand Down Expand Up @@ -184,7 +183,7 @@
@NotNull ServletContextHandler servletContextHandler
) {
// Init sessions persistence
SessionHandler sessionHandler = new SessionHandler();
CBSessionHandler sessionHandler = new CBSessionHandler(application);

Check warning on line 186 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBJettyServer.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBJettyServer.java#L186

Distance between variable sessionHandler declaration and its first usage is 4, but allowed 3. Consider making that variable final if you still need to store its value in advance (before method calls that might have side effects on the original value).
var maxIdleTime = application.getMaxSessionIdleTime();
int intMaxIdleSeconds;
if (maxIdleTime > Integer.MAX_VALUE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
//scf.setDomain(domain);
//scf.setMaxAge(CB_SESSION_LIFE_TIME);
cookieConfig.setPath(CBApplication.getInstance().getRootURI());
cookieConfig.setSecure(application.getServerURL().startsWith("https"));
// cookieConfig.setSecure(application.getServerURL().startsWith("https"));

Check warning on line 42 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBServerContextListener.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBServerContextListener.java#L42

Comment has incorrect indentation level 0, expected is 8, indentation should be the same level as line 43.
cookieConfig.setHttpOnly(true);
cookieConfig.setName(CBConstants.CB_SESSION_COOKIE_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
*
* Licensed 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 io.cloudbeaver.server.jetty;

import io.cloudbeaver.server.CBApplication;
import jakarta.servlet.SessionCookieConfig;
import org.eclipse.jetty.http.Syntax;
import org.eclipse.jetty.server.session.SessionHandler;

public class CBSessionHandler extends SessionHandler {
private final CBCookieConfig cbCookieConfig;
private final CBApplication<?> application;

public CBSessionHandler(CBApplication<?> application) {
this.cbCookieConfig = new CBCookieConfig();
this.application = application;
}


@Override
public SessionCookieConfig getSessionCookieConfig() {
return this.cbCookieConfig;
}


//mostly copy of org.eclipse.jetty.server.session.CookieConfig but allows to use dynamic setSecure flag
public final class CBCookieConfig implements SessionCookieConfig {
public CBCookieConfig() {
}

public String getComment() {
return CBSessionHandler.this._sessionComment;
}

public String getDomain() {
return CBSessionHandler.this._sessionDomain;
}

public int getMaxAge() {
return CBSessionHandler.this._maxCookieAge;
}

public String getName() {
return CBSessionHandler.this._sessionCookie;
}

public String getPath() {
return CBSessionHandler.this._sessionPath;
}

public boolean isHttpOnly() {
return CBSessionHandler.this._httpOnly;
}

public boolean isSecure() {

Check warning on line 69 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java#L69

Missing a Javadoc comment.
var serverUrl = CBSessionHandler.this.application.getServerURL();
return serverUrl != null && serverUrl.startsWith("https://");
}

public void setComment(String comment) {

Check warning on line 74 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java#L74

Missing a Javadoc comment.
if (CBSessionHandler.this._context != null && CBSessionHandler.this._context.getContextHandler()
.isAvailable()) {
throw new IllegalStateException("CookieConfig cannot be set after ServletContext is started");
} else {
CBSessionHandler.this._sessionComment = comment;
}
}

public void setDomain(String domain) {

Check warning on line 83 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java#L83

Missing a Javadoc comment.
if (CBSessionHandler.this._context != null && CBSessionHandler.this._context.getContextHandler()
.isAvailable()) {
throw new IllegalStateException("CookieConfig cannot be set after ServletContext is started");
} else {
CBSessionHandler.this._sessionDomain = domain;
}
}

public void setHttpOnly(boolean httpOnly) {

Check warning on line 92 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java#L92

Missing a Javadoc comment.
if (CBSessionHandler.this._context != null && CBSessionHandler.this._context.getContextHandler()
.isAvailable()) {
throw new IllegalStateException("CookieConfig cannot be set after ServletContext is started");
} else {
CBSessionHandler.this._httpOnly = httpOnly;
}
}

public void setMaxAge(int maxAge) {

Check warning on line 101 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java#L101

Missing a Javadoc comment.
if (CBSessionHandler.this._context != null && CBSessionHandler.this._context.getContextHandler()
.isAvailable()) {
throw new IllegalStateException("CookieConfig cannot be set after ServletContext is started");
} else {
CBSessionHandler.this._maxCookieAge = maxAge;
}
}

public void setName(String name) {

Check warning on line 110 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java#L110

Missing a Javadoc comment.
if (CBSessionHandler.this._context != null && CBSessionHandler.this._context.getContextHandler()
.isAvailable()) {
throw new IllegalStateException("CookieConfig cannot be set after ServletContext is started");
} else if ("".equals(name)) {
throw new IllegalArgumentException("Blank cookie name");
} else {
if (name != null) {
Syntax.requireValidRFC2616Token(name, "Bad Session cookie name");
}

CBSessionHandler.this._sessionCookie = name;
}
}

public void setPath(String path) {

Check warning on line 125 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java#L125

Missing a Javadoc comment.
if (CBSessionHandler.this._context != null && CBSessionHandler.this._context.getContextHandler()
.isAvailable()) {
throw new IllegalStateException("CookieConfig cannot be set after ServletContext is started");
} else {
CBSessionHandler.this._sessionPath = path;
}
}

public void setSecure(boolean secure) {

Check warning on line 134 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/jetty/CBSessionHandler.java#L134

Missing a Javadoc comment.
if (CBSessionHandler.this._context != null && CBSessionHandler.this._context.getContextHandler()
.isAvailable()) {
throw new IllegalStateException("CookieConfig cannot be set after ServletContext is started");
} else {
CBSessionHandler.this._secureCookies = secure;
}
}
}


}
Loading