Skip to content

Commit

Permalink
fixed out of bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
CriMDev97 committed Nov 17, 2023
1 parent adef63d commit 549e150
Showing 1 changed file with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,49 @@
import it.pagopa.interop.signalhub.pull.service.service.SignalService;
import it.pagopa.interop.signalhub.pull.service.utils.Utility;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.method.P;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

import static it.pagopa.interop.signalhub.pull.service.exception.ExceptionTypeEnum.NO_AUTH_FOUNDED;


@RestController
@AllArgsConstructor
public class SignalController implements GatewayApi {
private SignalService signalService;

@Autowired
private SignalHubPullConfig signalHubPullConfig;


@Override
public Mono<ResponseEntity<PaginationSignal>> getRequest(String eserviceId, Long indexSignal, Long size, ServerWebExchange exchange) {


final Long finalSize = (size == 0 || size > signalHubPullConfig.getMaxNumberPage()) ? signalHubPullConfig.getMaxNumberPage() : size;
final HttpStatus finalStatus = size > signalHubPullConfig.getMaxNumberPage() ? HttpStatus.PARTIAL_CONTENT : HttpStatus.OK;

return Utility.getPrincipalFromSecurityContext()
.switchIfEmpty(Mono.error(new PDNDGenericException(NO_AUTH_FOUNDED, NO_AUTH_FOUNDED.getMessage(), HttpStatus.UNAUTHORIZED)))
.flatMapMany(principalAgreement -> {
return this.signalService.pullSignal(principalAgreement.getPrincipalId(), eserviceId, indexSignal,
size == 0 || size > signalHubPullConfig.getMaxNumberPage() ? signalHubPullConfig.getMaxNumberPage() : size);
})
.flatMapMany(principalAgreement -> this.signalService.pullSignal(principalAgreement.getPrincipalId(), eserviceId, indexSignal, finalSize))
.collectList()
.map(list -> {
PaginationSignal paginationSignal = new PaginationSignal();
paginationSignal.setSignals(list);
paginationSignal.setLastIndexSignal(list.get(list.size()-1).getIndexSignal());
return ResponseEntity.status(size > signalHubPullConfig.getMaxNumberPage() ? HttpStatus.PARTIAL_CONTENT : HttpStatus.OK)
.body(paginationSignal);
});
.map(list -> ResponseEntity.status(finalStatus)
.body(toPagination(list))
);
}







private PaginationSignal toPagination(List<Signal> list) {
PaginationSignal paginationSignal = new PaginationSignal();
paginationSignal.setSignals(list);
if (list == null || list.isEmpty()) paginationSignal.setLastIndexSignal(null);
else {
paginationSignal.setLastIndexSignal(list.get(list.size()-1).getIndexSignal());
}
return paginationSignal;
}



Expand Down

0 comments on commit 549e150

Please sign in to comment.