Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
import org.apache.fineract.infrastructure.core.domain.ExternalId;
import org.apache.fineract.infrastructure.core.filters.ClientIpHolder;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.useradministration.domain.AppUser;

Expand Down Expand Up @@ -137,6 +138,9 @@ public class CommandSource extends AbstractPersistableCustom<Long> {
@Column(name = "loan_external_id", length = 100)
private ExternalId loanExternalId;

@Column(name = "client_ip", nullable = true)
private String clientIp;

@Column(name = "is_sanitized", nullable = false)
private boolean sanitized;

Expand All @@ -162,6 +166,7 @@ public static CommandSource fullEntryFrom(final CommandWrapper wrapper, final Js
.transactionId(command.getTransactionId()) //
.creditBureauId(command.getCreditBureauId()) //
.organisationCreditBureauId(command.getOrganisationCreditBureauId()) //
.clientIp(ClientIpHolder.getClientIp()) //
.loanExternalId(command.getLoanExternalId()).sanitized(sanitized).build(); //
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class FineractProperties {

private FineractCorrelationProperties correlation;

private FineractGeolocationProperties geolocation;

private FineractPartitionedJob partitionedJob;

private FineractRemoteJobMessageHandlerProperties remoteJobMessageHandler;
Expand Down Expand Up @@ -153,6 +155,13 @@ public static class FineractCorrelationProperties {
private String headerName;
}

@Getter
@Setter
public static class FineractGeolocationProperties {

private boolean enabled;
}

@Getter
@Setter
public static class FineractPartitionedJob {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,22 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.data;

import java.math.BigDecimal;
package org.apache.fineract.infrastructure.core.filters;

/**
* Immutable data object representing currency.
*/
public class MoneyData {
public class ClientIpHolder {

private final String code;
private final BigDecimal amount;
private final int decimalPlaces;
private static final ThreadLocal<String> clientIpHolder = new ThreadLocal<>();

public MoneyData(final String code, final BigDecimal amount, final int decimalPlaces) {
this.code = code;
this.amount = amount;
this.decimalPlaces = decimalPlaces;
public static void setClientIp(String ip) {
clientIpHolder.set(ip);
}

public String getCode() {
return this.code;
public static String getClientIp() {
return clientIpHolder.get();
}

public BigDecimal getAmount() {
return this.amount;
public static void clear() {
clientIpHolder.remove();
}

public int getDecimalPlaces() {
return this.decimalPlaces;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fineract.infrastructure.core.filters;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.infrastructure.core.config.FineractProperties;
import org.springframework.web.filter.OncePerRequestFilter;

@RequiredArgsConstructor
@Slf4j
public class GeolocationHeaderFilter extends OncePerRequestFilter {

private final FineractProperties fineractProperties;

private static final String[] IP_HEADER_CANDIDATES = { "X-Forwarded-For", "Proxy-Client-IP", "WL-Proxy-Client-IP",
"HTTP_X_FORWARDED_FOR", "HTTP_X_FORWARDED", "HTTP_X_CLUSTER_CLIENT_IP", "HTTP_CLIENT_IP", "HTTP_FORWARDED_FOR",
"HTTP_FORWARDED", "HTTP_VIA", "REMOTE_ADDR" };

public static String getClientIpAddress(HttpServletRequest request) {
for (String header : IP_HEADER_CANDIDATES) {
String ip = request.getHeader(header);
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
log.debug("SEND IP : {}", ip);
return ip;
}
}
log.debug("getRemoteAddr method : {}", request.getRemoteAddr());
return request.getRemoteAddr();
}

@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain)
throws IOException, ServletException {
FineractProperties.FineractGeolocationProperties geolocationProperties = fineractProperties.getGeolocation();
if (geolocationProperties.isEnabled()) {
handleClientIp(request, response, filterChain, geolocationProperties);
} else {
filterChain.doFilter(request, response);
}

}

private void handleClientIp(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain,
FineractProperties.FineractGeolocationProperties geolocationProperties) throws IOException, ServletException {
try {
String clientIpAddress = getClientIpAddress(request);
if (StringUtils.isNotBlank(clientIpAddress)) {
log.info("Found Client IP in header : {}", clientIpAddress);
ClientIpHolder.setClientIp(clientIpAddress);
}
filterChain.doFilter(request, response);
} catch (Exception e) {
e.printStackTrace();
} finally {
ClientIpHolder.clear();
}
}

@Override
protected boolean isAsyncDispatch(final HttpServletRequest request) {
return false;
}

@Override
protected boolean shouldNotFilterErrorDispatch() {
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import java.util.UUID;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.command.core.CommandPipeline;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.organisation.monetary.command.CurrencyUpdateCommand;
import org.apache.fineract.organisation.monetary.data.CurrencyConfigurationData;
import org.apache.fineract.organisation.monetary.data.CurrencyUpdateRequest;
import org.apache.fineract.organisation.monetary.data.CurrencyUpdateResponse;
import org.apache.fineract.organisation.monetary.service.OrganisationCurrencyReadPlatformService;
import org.springframework.stereotype.Component;

@Path("/v1/currencies")
@Component
@Tag(name = "Currency", description = "Application related configuration around viewing/updating the currencies permitted for use within the MFI.")
@RequiredArgsConstructor
public class CurrenciesApiResource {

private final OrganisationCurrencyReadPlatformService readPlatformService;
private final CommandPipeline commandPipeline;

@GET
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Retrieve Currency Configuration", description = """
Returns the list of currencies permitted for use AND the list of currencies not selected (but available for selection).

Example Requests:

currencies
currencies?fields=selectedCurrencyOptions
""")
public CurrencyConfigurationData retrieveCurrencies() {
return readPlatformService.retrieveCurrencyConfiguration();
}

@PUT
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Update Currency Configuration", description = "Updates the list of currencies permitted for use.")
public CurrencyUpdateResponse updateCurrencies(CurrencyUpdateRequest request) {
final var command = new CurrencyUpdateCommand();

command.setId(UUID.randomUUID());
command.setCreatedAt(DateUtils.getAuditOffsetDateTime());
command.setPayload(request);

final Supplier<CurrencyUpdateResponse> response = commandPipeline.send(command);

return response.get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.command;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.organisation.monetary.data.CurrencyUpdateRequest;

@Data
@EqualsAndHashCode(callSuper = true)
public class CurrencyUpdateCommand extends Command<CurrencyUpdateRequest> {}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,25 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.monetary.data.request;
package org.apache.fineract.organisation.monetary.data;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

public record CurrencyRequest(List<String> currencies) implements Serializable {
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CurrencyConfigurationData implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

private List<CurrencyData> selectedCurrencyOptions;
private List<CurrencyData> currencyOptions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,29 @@
*/
package org.apache.fineract.organisation.monetary.data;

import java.io.Serial;
import java.io.Serializable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.apache.fineract.infrastructure.core.config.MapstructMapperConfig;
import org.apache.fineract.organisation.monetary.domain.MonetaryCurrency;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Immutable data object representing currency.
*/
@Getter
@EqualsAndHashCode
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CurrencyData implements Serializable {

private final String code;
private final String name;
private final int decimalPlaces;
private final Integer inMultiplesOf;
private final String displaySymbol;
private final String nameCode;
private final String displayLabel;
@Serial
private static final long serialVersionUID = 1L;

private String code;
private String name;
private int decimalPlaces;
private Integer inMultiplesOf;
private String displaySymbol;
private String nameCode;
private String displayLabel;

public static CurrencyData blank() {
return new CurrencyData("", "", 0, 0, "", "");
Expand Down Expand Up @@ -90,11 +93,4 @@ private String generateDisplayLabel() {
return builder.toString();
}

@org.mapstruct.Mapper(config = MapstructMapperConfig.class)
public interface Mapper {

default CurrencyData map(MonetaryCurrency source) {
return source.toData();
}
}
}
Loading
Loading