Skip to content

Commit

Permalink
chore: refactor embedded Jetty main class, remove deprecated features (
Browse files Browse the repository at this point in the history
…#17810)

Signed-off-by: Morten Svanaes <msvanaes@dhis2.org>
  • Loading branch information
netroms committed Jun 20, 2024
1 parent 47cb506 commit 67977c0
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
import org.hisp.dhis.i18n.I18nManager;
import org.hisp.dhis.render.RenderService;
import org.hisp.dhis.security.RequiresAuthority;
import org.hisp.dhis.user.CurrentUserUtil;
import org.hisp.dhis.user.UserDetails;
import org.hisp.dhis.webapi.mvc.annotation.ApiVersion;
import org.hisp.dhis.webapi.service.ContextService;
import org.hisp.dhis.webapi.utils.ContextUtils;
Expand All @@ -73,10 +71,6 @@
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -117,34 +111,10 @@ public class AppController {

@GetMapping(value = "/menu", produces = ContextUtils.CONTENT_TYPE_JSON)
public @ResponseBody Map<String, List<WebModule>> getWebModules(HttpServletRequest request) {
checkForEmbeddedJettyRuntime(request);

String contextPath = HttpServletRequestPaths.getContextPath(request);
return Map.of("modules", getAccessibleAppMenu(contextPath));
}

/**
* Checks if we are running in embedded Jetty mode. If so, we need to set the SecurityContext
* manually from the session object SPRING_SECURITY_CONTEXT. This is done for compatibility with
* the old Struts action, which is not 100% ported yet. To be removed when application is ported
* away from Struts
*/
private static void checkForEmbeddedJettyRuntime(HttpServletRequest request) {
Object springSecurityContext = request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
if (springSecurityContext != null) {
SecurityContextImpl context = (SecurityContextImpl) springSecurityContext;
Authentication authentication = context.getAuthentication();

UserDetails currentUserDetails = CurrentUserUtil.getCurrentUserDetails();

if (authentication != null && currentUserDetails == null) {
SecurityContext newContext = SecurityContextHolder.createEmptyContext();
newContext.setAuthentication(authentication);
SecurityContextHolder.setContext(context);
}
}
}

private List<WebModule> getAccessibleAppMenu(String contextPath) {
List<WebModule> modules = appMenuManager.getAccessibleWebModules();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,20 @@
*/
package org.hisp.dhis.web.jetty;

import org.hisp.dhis.security.Authorities;
import org.hisp.dhis.security.SystemAuthoritiesProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;

/**
* Configuration class for a simple startup timer for embedded Jetty.
*
* @author Morten Svanæs <msvanaes@dhis2.org>
*/
@Configuration
@Order(100)
@ComponentScan(basePackages = {"org.hisp.dhis"})
@Profile("embeddedJetty")
public class SpringConfiguration {

@Primary
@Bean("org.hisp.dhis.security.SystemAuthoritiesProvider")
public SystemAuthoritiesProvider systemAuthoritiesProvider() {
return Authorities::getAllAuthorities;
}
public class JettyStartupTimerSpringConfiguration {

@Bean("org.hisp.dhis.web.embeddedjetty.StartupFinishedRoutine")
public StartupFinishedRoutine startupFinishedRoutine() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,13 @@
*
* @author Morten Svanæs <msvanaes@dhis2.org>
*/
public class JettyEmbeddedCoreWeb extends EmbeddedJettyBase {
private static final int DEFAULT_HTTP_PORT = 9090;
public class Main extends EmbeddedJettyBase {

private static final int DEFAULT_HTTP_PORT = 9090;
private static final String SERVER_HOSTNAME_OR_IP = "localhost";

private static final Long ELAPSED_SINCE_START = System.currentTimeMillis();

public JettyEmbeddedCoreWeb() {
public Main() {
super();
}

Expand All @@ -68,15 +67,7 @@ public static void main(String[] args) throws Exception {
setDefaultPropertyValue("jetty.host", SERVER_HOSTNAME_OR_IP);
setDefaultPropertyValue("jetty.http.port", String.valueOf(DEFAULT_HTTP_PORT));

/*
* This property is very import, this will instruct Spring to use
* special Spring config classes adapted to running in embedded Jetty.
*
* @see org.hisp.dhis.web.embeddedjetty.SpringConfiguration
*/
setDefaultPropertyValue("spring.profiles.active", "embeddedJetty");

JettyEmbeddedCoreWeb jettyEmbeddedCoreWeb = new JettyEmbeddedCoreWeb();
Main jettyEmbeddedCoreWeb = new Main();
jettyEmbeddedCoreWeb.printBanner("DHIS2 API Server");
jettyEmbeddedCoreWeb.startJetty();
}
Expand Down Expand Up @@ -111,7 +102,7 @@ public ServletContextHandler getServletContextHandler() {

private static AnnotationConfigWebApplicationContext getWebApplicationContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(SpringConfiguration.class);
context.register(JettyStartupTimerSpringConfiguration.class);
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public void execute() throws Exception {
log.info(
String.format(
"DHIS2 API Server Startup Finished In %s Seconds! Running on port: %s",
(JettyEmbeddedCoreWeb.getElapsedMsSinceStart() / 1000),
System.getProperty("jetty.http.port")));
(Main.getElapsedMsSinceStart() / 1000), System.getProperty("jetty.http.port")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
import org.springframework.util.StringUtils;

/**
* This code is a modified version of the original code from Spring Boot project.
* This code is a modified version of the original code from Spring Boot project. It serves as the
* main entry point for the embedded server. It starts an embedded Tomcat server
*
* @author Phillip Webb
* @author Andy Wilkinson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.hisp.dhis.system.util.HttpHeadersBuilder;
import org.hisp.dhis.web.jetty.JettyEmbeddedCoreWeb;
import org.hisp.dhis.web.jetty.Main;
import org.hisp.dhis.webapi.controller.security.LoginRequest;
import org.hisp.dhis.webapi.controller.security.LoginResponse;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -98,7 +98,7 @@ static void setup() throws Exception {
new Thread(
() -> {
try {
JettyEmbeddedCoreWeb.main(null);
Main.main(null);
} catch (InterruptedException ignored) {
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down

0 comments on commit 67977c0

Please sign in to comment.