Skip to content

Commit

Permalink
Made application version checking optional
Browse files Browse the repository at this point in the history
  • Loading branch information
kamfosica committed May 9, 2022
1 parent 988b7fc commit e73a1d9
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/spring.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up JDK
uses: actions/setup-java@v3
with:
distribution: 'corretto'
distribution: 'adopt'
java-version: '16'
cache: 'maven'

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>org.cga</groupId>
<artifactId>sctp</artifactId>
<packaging>pom</packaging>
<version>1.4.8</version>
<version>1.4.9</version>

<parent>
<groupId>org.springframework.boot</groupId>
Expand Down
2 changes: 1 addition & 1 deletion sctp-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sctp</artifactId>
<groupId>org.cga</groupId>
<version>1.4.8</version>
<version>1.4.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.cga.sctp.mobile.MobileApplicationService;
import org.cga.sctp.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
Expand Down Expand Up @@ -82,36 +83,8 @@ public class ApiConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private MobileApplicationService mobileApplicationService;

/* @Bean
OperationCustomizer customize() {
return (operation, handlerMethod) -> {
operation.responses(new ApiResponses()
.addApiResponse("Mobile Application Version", new ApiResponse()
.addHeaderObject(
AppConstants.APP_VERSION_CODE_HEADER, new Header()
.schema(new IntegerSchema())
.description("Current application version code")
)
.addHeaderObject(
AppConstants.APP_VERSION_UPDATE_AVAILABLE_HEADER, new Header()
.schema(new BooleanSchema())
.description("Whether an update is available")
)
.addHeaderObject(
AppConstants.APP_VERSION_UPDATE_TIME_HEADER, new Header()
.schema(new DateTimeSchema())
.description("When the new application became available")
)
.addHeaderObject(
AppConstants.APP_VERSION_UPDATE_MANDATORY, new Header()
.schema(new BooleanSchema())
.description("Whether the update is mandatory or not")
)
)
);
return operation;
};
}*/
@Value("${api.checkAppVersion:false}")
private boolean checkAppVersion;

@Override
public void configure(WebSecurity web) throws Exception {
Expand Down Expand Up @@ -148,7 +121,7 @@ public void configure(WebSecurity web) throws Exception {

@Bean
public AppVersionFilter apiVersionFilter() {
return new AppVersionFilter(mobileApplicationService, gson, ALLOW_LIST);
return new AppVersionFilter(mobileApplicationService, checkAppVersion, gson, ALLOW_LIST);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ public void setContextPath(String contextPath) {

@Bean("customGson")
public Gson gson() {
return new GsonBuilder()
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
.create();
return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter())
.registerTypeAdapter(LocalDate.class, new LocalDateAdapter()).create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@
public class AppVersionFilter extends GenericFilterBean {

private final Gson gson;
private final boolean filterEnabled;
private final AtomicReference<AppVersion> appVersion;
private final List<AntPathRequestMatcher> ignoredPathMatchers;
private final MobileApplicationService mobileApplicationService;

public AppVersionFilter(MobileApplicationService mobileApplicationService, Gson gson, String... ignoredPaths) {
public AppVersionFilter(MobileApplicationService mobileApplicationService, boolean filterEnabled, Gson gson, String... ignoredPaths) {
this.gson = gson;
this.appVersion = new AtomicReference<>();
this.ignoredPathMatchers = new LinkedList<>();
Expand All @@ -73,9 +74,14 @@ public AppVersionFilter(MobileApplicationService mobileApplicationService, Gson
this.ignoredPathMatchers.add(new AntPathRequestMatcher(pathPattern));
}
}
this.filterEnabled = filterEnabled;
this.refreshVersion();
}

public AppVersionFilter(MobileApplicationService mobileApplicationService, Gson gson, String... ignoredPaths) {
this(mobileApplicationService, true, gson, ignoredPaths);
}

private boolean ignorePath(HttpServletRequest request) {
for (AntPathRequestMatcher requestMatcher : ignoredPathMatchers) {
if (requestMatcher.matches(request)) {
Expand All @@ -86,16 +92,22 @@ private boolean ignorePath(HttpServletRequest request) {
}

@Override
public void doFilter(
ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

int applicationVersionCode = -1;
AppVersion version = appVersion.get();
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;

if (!filterEnabled) {
response.addHeader(AppConstants.APP_VERSION_CODE_HEADER, version.getVersionCode().toString());
response.addHeader(AppConstants.APP_VERSION_UPDATE_TIME_HEADER, version.getUpdatedAt().toString());
response.addHeader(AppConstants.APP_VERSION_UPDATE_AVAILABLE_HEADER, "false");
response.addHeader(AppConstants.APP_VERSION_UPDATE_MANDATORY, version.getMandatoryUpdate().toString());
filterChain.doFilter(request, response);
return;
}

if (ignorePath(request)) {
filterChain.doFilter(request, response);
return;
Expand Down
2 changes: 1 addition & 1 deletion sctp-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sctp</artifactId>
<groupId>org.cga</groupId>
<version>1.4.8</version>
<version>1.4.9</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion sctp-mis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>sctp</artifactId>
<groupId>org.cga</groupId>
<version>1.4.8</version>
<version>1.4.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down

0 comments on commit e73a1d9

Please sign in to comment.