Skip to content

Commit

Permalink
Fixed caching request body
Browse files Browse the repository at this point in the history
  • Loading branch information
galovics committed Sep 17, 2024
1 parent ca3f69d commit 8ebd3bf
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 109 deletions.
2 changes: 1 addition & 1 deletion fineract-provider/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ dependencies {
implementation ('jakarta.xml.bind:jakarta.xml.bind-api') {
exclude group: 'jakarta.activation'
}
implementation ('org.apache.activemq:activemq-client-jakarta') {
implementation ('org.apache.activemq:activemq-client') {
exclude group: 'org.apache.geronimo.specs'
exclude group: 'javax.annotation', module: 'javax.annotation-api'
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import lombok.RequiredArgsConstructor;
import org.apache.fineract.cob.conditions.LoanCOBEnabledCondition;
import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
import org.apache.fineract.infrastructure.core.http.BodyCachingHttpServletRequestWrapper;
import org.apache.fineract.infrastructure.jobs.exception.LoanIdsHardLockedException;
import org.apache.fineract.useradministration.exception.UnAuthenticatedUserException;
import org.apache.http.HttpStatus;
import org.springframework.context.annotation.Conditional;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.ContentCachingRequestWrapper;

@RequiredArgsConstructor
@Conditional(LoanCOBEnabledCondition.class)
Expand Down Expand Up @@ -63,7 +63,7 @@ public void toServletResponse(HttpServletResponse response) throws IOException {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
request = new BodyCachingHttpServletRequestWrapper(request);
request = new ContentCachingRequestWrapper(request);

if (!helper.isOnApiList(request)) {
proceed(filterChain, request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
Expand All @@ -48,7 +49,6 @@
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.apache.fineract.infrastructure.core.domain.FineractPlatformTenant;
import org.apache.fineract.infrastructure.core.http.BodyCachingHttpServletRequestWrapper;
import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.portfolio.loanaccount.domain.GLIMAccountInfoRepository;
Expand All @@ -68,6 +68,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.mock.web.DelegatingServletInputStream;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

Expand Down Expand Up @@ -144,7 +145,7 @@ void shouldProceedWhenUrlDoesNotMatch() throws ServletException, IOException {

given(request.getPathInfo()).willReturn("/v1/jobs/2/inline");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));

testObj.doFilterInternal(request, response, filterChain);
verify(filterChain, times(1)).doFilter(any(HttpServletRequest.class), eq(response));
Expand All @@ -165,7 +166,7 @@ void shouldProceedWhenUrlDoesNotMatchWithInvalidLoanId() throws ServletException

given(request.getPathInfo()).willReturn("/v1/loans/invalid2LoanId/charges");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));
given(context.authenticatedUser()).willReturn(appUser);
given(fineractProperties.getQuery()).willReturn(fineractQueryProperties);
given(fineractQueryProperties.getInClauseParameterSizeLimit()).willReturn(65000);
Expand All @@ -185,7 +186,7 @@ void shouldProceedWhenUserHasBypassPermission() throws ServletException, IOExcep

given(request.getPathInfo()).willReturn("/v1/jobs/2/inline");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));
given(context.authenticatedUser()).willReturn(appUser);
given(appUser.isBypassUser()).willReturn(true);

Expand All @@ -208,7 +209,7 @@ void shouldProceedWhenLoanIsNotLockedAndNoLoanIsBehind() throws ServletException

given(request.getPathInfo()).willReturn("/v1/loans/2/charges");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));
given(loanAccountLockService.isLoanHardLocked(2L)).willReturn(false);
given(context.authenticatedUser()).willReturn(appUser);
given(fineractProperties.getQuery()).willReturn(fineractQueryProperties);
Expand All @@ -235,7 +236,7 @@ void shouldProceedWhenExternalLoanIsNotLockedAndNotBehind() throws ServletExcept
String uuid = UUID.randomUUID().toString();
given(request.getPathInfo()).willReturn("/v1/loans/external-id/" + uuid + "/charges");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));
given(loanAccountLockService.isLoanHardLocked(2L)).willReturn(false);
given(context.authenticatedUser()).willReturn(appUser);
given(loanRepository.findIdByExternalId(any())).willReturn(2L);
Expand Down Expand Up @@ -263,7 +264,7 @@ void shouldProceedWhenRescheduleLoanIsNotLockedAndNotBehind() throws ServletExce
Long resourceId = 123L;
given(request.getPathInfo()).willReturn("/v1/rescheduleloans/" + resourceId + "/charges");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));
given(loanAccountLockService.isLoanHardLocked(2L)).willReturn(false);
given(fineractProperties.getQuery()).willReturn(fineractQueryProperties);
given(fineractQueryProperties.getInClauseParameterSizeLimit()).willReturn(65000);
Expand Down Expand Up @@ -296,7 +297,7 @@ void shouldRunInlineCOBAndProceedWhenLoanIsBehind() throws ServletException, IOE
given(result.getLastClosedBusinessDate()).willReturn(businessDate.minusDays(2));
given(request.getPathInfo()).willReturn("/v1/loans/2?command=approve");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));
given(loanAccountLockService.isLoanHardLocked(2L)).willReturn(false);
given(fineractProperties.getQuery()).willReturn(fineractQueryProperties);
given(fineractQueryProperties.getInClauseParameterSizeLimit()).willReturn(65000);
Expand Down Expand Up @@ -327,7 +328,7 @@ void shouldNotRunInlineCOBAndProceedWhenLoanIsNotBehind() throws ServletExceptio
given(result.getId()).willReturn(2L);
given(request.getPathInfo()).willReturn("/v1/loans/2?command=approve");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));
given(loanAccountLockService.isLoanHardLocked(2L)).willReturn(false);
given(fineractProperties.getQuery()).willReturn(fineractQueryProperties);
given(fineractQueryProperties.getInClauseParameterSizeLimit()).willReturn(65000);
Expand All @@ -350,7 +351,7 @@ void shouldNotRunInlineCOBAndProceedWhenLoanIsBehindForLoanCreation() throws Ser

given(request.getPathInfo()).willReturn("/v1/loans");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));

given(context.authenticatedUser()).willReturn(appUser);

Expand All @@ -368,7 +369,7 @@ void shouldNotRunInlineCOBForCatchUp() throws ServletException, IOException {

given(request.getPathInfo()).willReturn("/v1/loans/catch-up");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));

given(context.authenticatedUser()).willReturn(appUser);

Expand All @@ -387,7 +388,7 @@ void shouldRejectWhenLoanIsHardLocked() throws ServletException, IOException {

given(request.getPathInfo()).willReturn("/v1/loans/2/charges");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));
given(loanAccountLockService.isLoanHardLocked(2L)).willReturn(true);
given(response.getWriter()).willReturn(writer);
given(context.authenticatedUser()).willReturn(appUser);
Expand All @@ -409,7 +410,7 @@ void shouldRejectWhenGlimLoanIsHardLocked() throws ServletException, IOException

given(request.getPathInfo()).willReturn("/v1/loans/glimAccount/2");
given(request.getMethod()).willReturn(HTTPMethods.POST.value());
given(request.getInputStream()).willReturn(new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(new byte[0]));
given(request.getInputStream()).willReturn(new DelegatingServletInputStream(new ByteArrayInputStream(new byte[0])));
given(glimAccountInfoRepository.findOneByIsAcceptingChildAndApplicationId(true, BigDecimal.valueOf(2))).willReturn(glimAccount);
given(glimAccount.getChildLoan()).willReturn(Collections.singleton(loan));
given(loan.getId()).willReturn(loanId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
package org.apache.fineract.infrastructure.jobs.filter;

import jakarta.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.apache.fineract.cob.loan.RetrieveLoanIdService;
import org.apache.fineract.cob.service.InlineLoanCOBExecutorServiceImpl;
import org.apache.fineract.cob.service.LoanAccountLockService;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.apache.fineract.infrastructure.core.http.BodyCachingHttpServletRequestWrapper;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.portfolio.loanaccount.domain.GLIMAccountInfoRepository;
import org.apache.fineract.portfolio.loanaccount.domain.LoanRepository;
Expand All @@ -39,6 +39,7 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.DelegatingServletInputStream;

@ExtendWith(MockitoExtension.class)
public class LoanCOBFilterHelperTest {
Expand Down Expand Up @@ -118,9 +119,8 @@ public void testCOBFilterUnescapedChars() throws IOException {

HttpServletRequest httpServletRequest = Mockito.mock(HttpServletRequest.class);
Mockito.when(httpServletRequest.getPathInfo()).thenReturn("/v1/batches/endpoint");
BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream inputStream = new BodyCachingHttpServletRequestWrapper.CachedBodyServletInputStream(
json.getBytes(Charset.forName("UTF-8")));
Mockito.when(httpServletRequest.getInputStream()).thenReturn(inputStream);
Mockito.when(httpServletRequest.getInputStream())
.thenReturn(new DelegatingServletInputStream(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))));
List<Long> loanIds = helper.calculateRelevantLoanIds(httpServletRequest);
Assertions.assertEquals(0, loanIds.size());
}
Expand Down

0 comments on commit 8ebd3bf

Please sign in to comment.