-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f48ec4
commit 6236766
Showing
1 changed file
with
18 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
import javax.annotation.Resource; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.FilterConfig; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
@@ -96,21 +97,31 @@ public class ApiRateLimitFilter extends OncePerRequestFilter { | |
@Value("${org.orcid.persistence.panoply.papiExceededRate.production:false}") | ||
private boolean enablePanoplyPapiExceededRateInProduction; | ||
|
||
@Value("${org.orcid.papi.rate.limit.ip.whiteSpaceSeparatedWhiteList:127.0.0.1}") | ||
@Value("${org.orcid.papi.rate.limit.ip.whiteSpaceSeparatedWhiteList:192.168.65.1 127.0.0.1}") | ||
private String papiWhiteSpaceSeparatedWhiteList; | ||
|
||
@Value("${org.orcid.papi.rate.limit.clientId.whiteSpaceSeparatedWhiteList}") | ||
private String papiClientIdWhiteSpaceSeparatedWhiteList; | ||
|
||
private List<String> papiIpWhiteList; | ||
private List<String> papiClientIdWhiteList; | ||
|
||
private static final String TOO_MANY_REQUESTS_MSG = "Too Many Requests - You have exceeded the daily allowance of API calls.\\n" | ||
+ "You can increase your daily quota by registering for and using Public API client credentials " | ||
+ "(https://info.orcid.org/documentation/integration-guide/registering-a-public-api-client/ )"; | ||
|
||
private static final String SUBJECT = "[ORCID] You have exceeded the daily Public API Usage Limit - "; | ||
|
||
@Value("${org.orcid.papi.rate.limit.fromEmail:[email protected]}") | ||
private String FROM_ADDRESS; | ||
|
||
@Override | ||
public void afterPropertiesSet() throws ServletException { | ||
super.afterPropertiesSet(); | ||
papiIpWhiteList = StringUtils.isNotBlank(papiWhiteSpaceSeparatedWhiteList) ? Arrays.asList(papiWhiteSpaceSeparatedWhiteList.split("\\s")) : null; | ||
papiClientIdWhiteList = StringUtils.isNotBlank(papiClientIdWhiteSpaceSeparatedWhiteList) ? Arrays.asList(papiClientIdWhiteSpaceSeparatedWhiteList.split("\\s")) : null; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
@@ -274,8 +285,7 @@ private void setPapiRateExceededItemInPanoply(PanoplyPapiDailyRateExceededItem i | |
}); | ||
} | ||
|
||
// gets actual client IP address, using the headers that the proxy server | ||
// ads | ||
// gets actual client IP address, using the headers that the proxy server adds | ||
private String getClientIpAddress(HttpServletRequest request) { | ||
String ipAddress = request.getHeader("X-FORWARDED-FOR"); | ||
if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) { | ||
|
@@ -291,29 +301,11 @@ private String getClientIpAddress(HttpServletRequest request) { | |
} | ||
|
||
private boolean isWhiteListed(String ipAddress) { | ||
List<String> papiIpWhiteList = null; | ||
if (StringUtils.isNotBlank(papiWhiteSpaceSeparatedWhiteList)) { | ||
papiIpWhiteList = Arrays.asList(papiWhiteSpaceSeparatedWhiteList.split("\\s")); | ||
} | ||
|
||
if (papiIpWhiteList != null) { | ||
return papiIpWhiteList.contains(ipAddress); | ||
|
||
} | ||
return false; | ||
return (papiIpWhiteList != null)?papiIpWhiteList.contains(ipAddress): false; | ||
} | ||
|
||
private boolean isClientIdWhiteListed(String clientId) { | ||
List<String> papiClientIdWhiteList = null; | ||
if (StringUtils.isNotBlank(papiClientIdWhiteSpaceSeparatedWhiteList)) { | ||
papiClientIdWhiteList = Arrays.asList(papiWhiteSpaceSeparatedWhiteList.split("\\s")); | ||
} | ||
|
||
if (papiClientIdWhiteList != null) { | ||
return papiClientIdWhiteList.contains(clientId); | ||
|
||
} | ||
return false; | ||
private boolean isClientIdWhiteListed(String clientId) { | ||
return (papiClientIdWhiteList != null)?papiClientIdWhiteList.contains(clientId):false; | ||
} | ||
|
||
} |