@@ -91,7 +91,6 @@ public class Ortb2RequestFactory {
9191 private static final ConditionalLogger EMPTY_ACCOUNT_LOGGER = new ConditionalLogger ("empty_account" , logger );
9292 private static final ConditionalLogger UNKNOWN_ACCOUNT_LOGGER = new ConditionalLogger ("unknown_account" , logger );
9393
94- private final boolean enforceValidAccount ;
9594 private final int timeoutAdjustmentFactor ;
9695 private final double logSamplingRate ;
9796 private final List <String > blacklistedAccounts ;
@@ -110,8 +109,7 @@ public class Ortb2RequestFactory {
110109 private final Metrics metrics ;
111110 private final Clock clock ;
112111
113- public Ortb2RequestFactory (boolean enforceValidAccount ,
114- int timeoutAdjustmentFactor ,
112+ public Ortb2RequestFactory (int timeoutAdjustmentFactor ,
115113 double logSamplingRate ,
116114 List <String > blacklistedAccounts ,
117115 UidsCookieService uidsCookieService ,
@@ -133,7 +131,6 @@ public Ortb2RequestFactory(boolean enforceValidAccount,
133131 throw new IllegalArgumentException ("Expected timeout adjustment factor should be in [0, 100]." );
134132 }
135133
136- this .enforceValidAccount = enforceValidAccount ;
137134 this .timeoutAdjustmentFactor = timeoutAdjustmentFactor ;
138135 this .logSamplingRate = logSamplingRate ;
139136 this .blacklistedAccounts = Objects .requireNonNull (blacklistedAccounts );
@@ -457,20 +454,26 @@ private String validateIfAccountBlacklisted(String accountId) {
457454 return accountId ;
458455 }
459456
460- private Future <Account > loadAccount (Timeout timeout ,
461- HttpRequestContext httpRequest ,
462- String accountId ) {
463-
464- final Future <Account > accountFuture = StringUtils .isBlank (accountId )
465- ? responseForEmptyAccount (httpRequest )
466- : applicationSettings .getAccountById (accountId , timeout )
467- .compose (this ::ensureAccountActive ,
468- exception -> accountFallback (exception , accountId , httpRequest ));
457+ private Future <Account > loadAccount (Timeout timeout , HttpRequestContext httpRequest , String accountId ) {
458+ if (StringUtils .isBlank (accountId )) {
459+ EMPTY_ACCOUNT_LOGGER .warn (accountErrorMessage ("Account not specified" , httpRequest ), logSamplingRate );
460+ }
469461
470- return accountFuture
462+ return applicationSettings .getAccountById (accountId , timeout )
463+ .compose (this ::ensureAccountActive )
464+ .recover (exception -> wrapFailure (exception , accountId , httpRequest ))
471465 .onFailure (ignored -> metrics .updateAccountRequestRejectedByInvalidAccountMetrics (accountId ));
472466 }
473467
468+ private Future <Account > ensureAccountActive (Account account ) {
469+ final String accountId = account .getId ();
470+
471+ return account .getStatus () == AccountStatus .inactive
472+ ? Future .failedFuture (
473+ new UnauthorizedAccountException ("Account %s is inactive" .formatted (accountId ), accountId ))
474+ : Future .succeededFuture (account );
475+ }
476+
474477 /**
475478 * Extracts publisher id either from {@link BidRequest}.app.publisher or {@link BidRequest}.site.publisher.
476479 * If neither is present returns empty string.
@@ -505,48 +508,26 @@ private String parentAccountIdFromExtPublisher(ExtPublisher extPublisher) {
505508 return extPublisherPrebid != null ? StringUtils .stripToNull (extPublisherPrebid .getParentAccount ()) : null ;
506509 }
507510
508- private Future <Account > responseForEmptyAccount (HttpRequestContext httpRequest ) {
509- EMPTY_ACCOUNT_LOGGER .warn (accountErrorMessage ("Account not specified" , httpRequest ), logSamplingRate );
510- return responseForUnknownAccount (StringUtils .EMPTY );
511- }
512-
513- private static String accountErrorMessage (String message , HttpRequestContext httpRequest ) {
514- return "%s, Url: %s and Referer: %s" .formatted (
515- message ,
516- httpRequest .getAbsoluteUri (),
517- httpRequest .getHeaders ().get (HttpUtil .REFERER_HEADER ));
518- }
519-
520- private Future <Account > accountFallback (Throwable exception ,
521- String accountId ,
522- HttpRequestContext httpRequest ) {
523-
524- if (exception instanceof PreBidException ) {
511+ private Future <Account > wrapFailure (Throwable exception , String accountId , HttpRequestContext httpRequest ) {
512+ if (exception instanceof UnauthorizedAccountException ) {
513+ return Future .failedFuture (exception );
514+ } else if (exception instanceof PreBidException ) {
525515 UNKNOWN_ACCOUNT_LOGGER .warn (accountErrorMessage (exception .getMessage (), httpRequest ), 100 );
526516 } else {
527517 metrics .updateAccountRequestRejectedByFailedFetch (accountId );
528518 logger .warn ("Error occurred while fetching account: {0}" , exception .getMessage ());
529519 logger .debug ("Error occurred while fetching account" , exception );
530520 }
531521
532- // hide all errors occurred while fetching account
533- return responseForUnknownAccount (accountId );
534- }
535-
536- private Future <Account > responseForUnknownAccount (String accountId ) {
537- return enforceValidAccount
538- ? Future .failedFuture (new UnauthorizedAccountException (
539- "Unauthorized account id: " + accountId , accountId ))
540- : Future .succeededFuture (Account .empty (accountId ));
522+ return Future .failedFuture (
523+ new UnauthorizedAccountException ("Unauthorized account id: " + accountId , accountId ));
541524 }
542525
543- private Future <Account > ensureAccountActive (Account account ) {
544- final String accountId = account .getId ();
545-
546- return account .getStatus () == AccountStatus .inactive
547- ? Future .failedFuture (new UnauthorizedAccountException (
548- "Account %s is inactive" .formatted (accountId ), accountId ))
549- : Future .succeededFuture (account );
526+ private static String accountErrorMessage (String message , HttpRequestContext httpRequest ) {
527+ return "%s, Url: %s and Referer: %s" .formatted (
528+ message ,
529+ httpRequest .getAbsoluteUri (),
530+ httpRequest .getHeaders ().get (HttpUtil .REFERER_HEADER ));
550531 }
551532
552533 private ExtRequest enrichExtRequest (ExtRequest ext , Account account ) {
0 commit comments