Skip to content

Commit 3660b02

Browse files
committed
Fix NPE when accept header is missing
1 parent 9a883cd commit 3660b02

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

core/src/main/java/org/fao/geonet/web/LocaleRedirects.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import java.util.*;
4646

47+
import javax.annotation.Nullable;
4748
import javax.servlet.http.HttpServletRequest;
4849
import javax.servlet.http.HttpSession;
4950

@@ -113,10 +114,14 @@ public ModelAndView redirectRootPath(final HttpServletRequest request,
113114
) throws ResourceNotFoundException {
114115
String lang = lang(langParam, langCookie, langHeader);
115116

116-
if (checkPortalExist(portal, !accept.startsWith(ACCEPT_VALUE))) {
117+
// Throw exception for non-HTML requests instead of redirecting if portal is not found
118+
boolean throwExceptionOnMissingPortal = accept != null && !accept.startsWith(ACCEPT_VALUE);
119+
120+
if (checkPortalExist(portal, throwExceptionOnMissingPortal)) {
117121
return redirectURL(createServiceUrl(request, _homeRedirectUrl, lang, portal));
118122
} else {
119-
if (sourceRepository.findByType(SourceType.portal, null).size() == 0) {
123+
List<Source> portals = sourceRepository.findByType(SourceType.portal, null);
124+
if (portals == null || portals.isEmpty()) {
120125
return redirectURL(createServiceUrl(request, _homeRedirectUrl, lang, NodeInfo.DEFAULT_NODE));
121126
}
122127
// Redirect to list of portal page if more than one or the default if only one
@@ -147,10 +152,14 @@ public ModelAndView redirectPortalPath(final HttpServletRequest request,
147152
final String langHeader) throws ResourceNotFoundException {
148153
String lang = lang(langParam, langCookie, langHeader);
149154

150-
if (checkPortalExist(portal, !accept.startsWith(ACCEPT_VALUE))) {
155+
// Throw exception for non-HTML requests instead of redirecting if portal is not found
156+
boolean throwExceptionOnMissingPortal = accept != null && !accept.startsWith(ACCEPT_VALUE);
157+
158+
if (checkPortalExist(portal, throwExceptionOnMissingPortal)) {
151159
return redirectURL(createServiceUrl(request, _homeRedirectUrl, lang, portal));
152160
} else {
153-
if (sourceRepository.findByType(SourceType.subportal, null).size() == 0) {
161+
List<Source> subPortals = sourceRepository.findByType(SourceType.subportal, null);
162+
if (subPortals == null || subPortals.isEmpty()) {
154163
return redirectURL(createServiceUrl(request, _homeRedirectUrl, lang, NodeInfo.DEFAULT_NODE));
155164
}
156165
// Redirect to list of portal page if more than one or the default if only one

0 commit comments

Comments
 (0)