Skip to content

Commit

Permalink
Transition from Field Injection to Constructor Injection (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
hokwaichan authored Jul 18, 2024
1 parent e986e01 commit 564c8d4
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
Expand All @@ -21,11 +20,14 @@ public class ForTestingController {
@Value("${url.api.2.1.base}/testing")
private String API_2_1_BASE;

@Autowired
private HttpRequestService httpRequestService;
private final HttpRequestService httpRequestService;

@Autowired
private UserContextService userContextService;
private final UserContextService userContextService;

public ForTestingController(HttpRequestService httpRequestService, UserContextService userContextService) {
this.httpRequestService = httpRequestService;
this.userContextService = userContextService;
}

@GetMapping(value = "/exception")
public ResponseEntity<String> throwException() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,11 +19,14 @@ public class GeneralRestController {
@Value("${url.api.2.1.base}")
private String API_2_1_BASE;

@Autowired
private HttpRequestService httpRequestService;
private final HttpRequestService httpRequestService;

@Autowired
private UserContextService userContextService;
private final UserContextService userContextService;

public GeneralRestController(HttpRequestService httpRequestService, UserContextService userContextService) {
this.httpRequestService = httpRequestService;
this.userContextService = userContextService;
}

/**
* Get the list of active announcements to display.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.apache.commons.logging.LogFactory;
import org.owasp.html.PolicyFactory;
import org.owasp.html.Sanitizers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
Expand All @@ -39,7 +38,7 @@
public class GroupingsRestController {

private static final Log logger = LogFactory.getLog(GroupingsRestController.class);
private final PolicyFactory policy;
private final PolicyFactory policy = Sanitizers.FORMATTING;;

@Value("${app.groupings.controller.uuid}")
private String uuid;
Expand All @@ -56,8 +55,7 @@ public class GroupingsRestController {
@Value("${app.api.handshake.enabled:true}")
private Boolean API_HANDSHAKE_ENABLED = true;

@Autowired
private UserContextService userContextService;
private final UserContextService userContextService;

/*
* Checks to make sure that the API is running and that there are no issues with the overrides file.
Expand All @@ -66,15 +64,15 @@ public class GroupingsRestController {
@Value("${groupings.api.check}")
private String CREDENTIAL_CHECK_USER;

@Autowired
private HttpRequestService httpRequestService;

@Autowired
private Realm realm;

// Constructor.
public GroupingsRestController() {
policy = Sanitizers.FORMATTING;
public GroupingsRestController(UserContextService userContextService, HttpRequestService httpRequestService, Realm realm) {
this.userContextService = userContextService;
this.httpRequestService = httpRequestService;
this.realm = realm;
}

/*
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/edu/hawaii/its/api/controller/OotbRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.apache.commons.logging.LogFactory;
import org.owasp.html.PolicyFactory;
import org.owasp.html.Sanitizers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
Expand All @@ -30,26 +29,28 @@
@RequestMapping("/api/groupings/ootb")
public class OotbRestController {
private static final Log logger = LogFactory.getLog(OotbRestController.class);
private final PolicyFactory policy;
private final PolicyFactory policy = Sanitizers.FORMATTING;;

@Value("${url.api.2.1.base}")
private String API_2_1_BASE;

@Autowired
private UserContextService userContextService;
private final UserContextService userContextService;

@Autowired
private OotbStaticUserAuthenticationFilter ootbAuthenticationFilter;
private final OotbStaticUserAuthenticationFilter ootbAuthenticationFilter;

@Autowired
private OotbActiveUserProfileService ootbActiveUserProfileService;
private final OotbActiveUserProfileService ootbActiveUserProfileService;

@Autowired
private HttpRequestService httpRequestService;
private final HttpRequestService httpRequestService;

// Constructor.
public OotbRestController() {
policy = Sanitizers.FORMATTING;
public OotbRestController(UserContextService userContextService,
OotbStaticUserAuthenticationFilter ootbAuthenticationFilter,
OotbActiveUserProfileService ootbActiveUserProfileService,
HttpRequestService httpRequestService) {
this.userContextService = userContextService;
this.ootbAuthenticationFilter = ootbAuthenticationFilter;
this.ootbActiveUserProfileService = ootbActiveUserProfileService;
this.httpRequestService = httpRequestService;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import org.apereo.cas.client.authentication.SimplePrincipal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

Expand All @@ -17,12 +16,15 @@
@Service
public class AuthorizationService {

@Autowired
GroupingsRestController groupingsRestController;

@Autowired
UhUuidCheckerService uhUuidCheckerService;
private static final Log logger = LogFactory.getLog(AuthorizationService.class);
private final GroupingsRestController groupingsRestController;

private final UhUuidCheckerService uhUuidCheckerService;

public AuthorizationService(GroupingsRestController groupingsRestController, UhUuidCheckerService uhUuidCheckerService) {
this.groupingsRestController = groupingsRestController;
this.uhUuidCheckerService = uhUuidCheckerService;
}

/**
* Assign roles to user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

Expand All @@ -15,8 +14,11 @@ public final class UserBuilder {

private static final Log logger = LogFactory.getLog(UserBuilder.class);

@Autowired
private AuthorizationService authorizationService;
private final AuthorizationService authorizationService;

public UserBuilder(AuthorizationService authorizationService) {
this.authorizationService = authorizationService;
}

public User make(Map<String, ?> map) {
return make(new UhAttributes(map));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jakarta.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

Expand All @@ -11,8 +10,11 @@
@Configuration
public class GrouperPropertyConfigurer {

@Autowired
private Environment env;
private final Environment env;

public GrouperPropertyConfigurer(Environment env) {
this.env = env;
}

@PostConstruct
public void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
Expand All @@ -17,8 +16,11 @@ public class Realm {

private static final Log logger = LogFactory.getLog(Realm.class);

@Autowired
private Environment environment;
private final Environment environment;

public Realm(Environment environment) {
this.environment = environment;
}

private Map<String, Boolean> profileMap = new ConcurrentHashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.apereo.cas.client.proxy.ProxyGrantingTicketStorageImpl;
import org.apereo.cas.client.session.SingleSignOutFilter;
import org.apereo.cas.client.validation.Saml11TicketValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -65,8 +64,11 @@ public class SecurityConfig {
@Value("${cas.send.renew:false}")
private boolean casSendRenew;

@Autowired
private UserBuilder userBuilder;
private final UserBuilder userBuilder;

public SecurityConfig(UserBuilder userBuilder) {
this.userBuilder = userBuilder;
}

@PostConstruct
public void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand All @@ -26,11 +25,14 @@ public class ErrorControllerAdvice {

private static final Log logger = LogFactory.getLog(ErrorControllerAdvice.class);

@Autowired
private UserContextService userContextService;
private final UserContextService userContextService;

@Autowired
private EmailService emailService;
private final EmailService emailService;

public ErrorControllerAdvice(UserContextService userContextService, EmailService emailService) {
this.userContextService = userContextService;
this.emailService = emailService;
}

@ExceptionHandler(GroupingsServiceResultException.class)
public ResponseEntity<GroupingsHTTPException> handleGroupingsServiceResultException(GroupingsServiceResultException gsre) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
Expand All @@ -35,11 +34,14 @@ public class HomeController {
@Value("${cas.logout.url}")
private String casLogoutUrl;

@Autowired
private EmailService emailService;
private final EmailService emailService;

@Autowired
private UserContextService userContextService;
private final UserContextService userContextService;

public HomeController(EmailService emailService, UserContextService userContextService) {
this.emailService = emailService;
this.userContextService = userContextService;
}

// Mapping to home.
@GetMapping(value = { "/", "/home" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
Expand All @@ -33,9 +32,8 @@ public class EmailService {

private static final Log logger = LogFactory.getLog(EmailService.class);

private JavaMailSender javaMailSender;
private final JavaMailSender javaMailSender;

@Autowired
public EmailService(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
Expand Down

0 comments on commit 564c8d4

Please sign in to comment.