'Adding Cache Mongodb Panache Quarkus Reactive

im trying to speed up my mongodb panache query's using quarkus and reactive, im trying to use cache and get a better pagination but cant make it work, this are my java class:

@ApplicationScoped
@RegisterForReflection
public class BrandRepository implements ReactivePanacheMongoRepositoryBase<Brands, Integer> {

  public ReactivePanacheQuery<Brands> listBrands() {
    return Brands.findAll(Sort.by("name").ascending());
  }
}

@Path("/brand/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@RegisterForReflection
public class brand {
  private static final Logger LOG = LoggerFactory.getLogger(brand.class);

  @Inject BrandRepository br;

  @GET
  public Uni<Response> list(
      @DefaultValue("0") @QueryParam("page") Integer page,
      @DefaultValue("10") @QueryParam("page_size") Integer page_size) {
    Pagination pagination = new Pagination();
    if (page != null && page >= 1) {
      pagination.setPage(page);
      page--;
    } else {
      page = 0;
      pagination.setPage(1);
    }
    if (page_size == null || page_size <= 0) {
      page_size = 10;
    }
    pagination.setPageSize(page_size);
    return br.listBrands()
        .page(Page.of(page, page_size))
        .list()
        .map(
            b -> {
              pagination.setTotalCount(b.size());
              LOG.info("listBrands");
              return Response.ok(
                      new BrandResponseList(new Metadata("success", 200, "ok"), b, pagination))
                  .build();
            });
  }
}

The other trouble im having is to count the total of br.listBrands() and no the total after paginating.

Thank You For The Help



Solution 1:[1]

If your using the Client Credentials flow (App tokens) then Autodiscover v1 won't work. If your using delegate Access then it will work okay

One workaround is to use Autodiscoverv2 (which is what the new version of Outlook uses). This is unauthenticated so will return just the EWS Endpoint eg

        string autodiscoverv2Endpoint = $"https://outlook.office365.com/autodiscover/autodiscover.json/v1.0/{emailAddress}?Protocol=EWS";
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; AcmeInc/1.0)");
            dynamic adResponse = JObject.Parse(client.GetAsync(autodiscoverv2Endpoint).GetAwaiter().GetResult().Content.ReadAsStringAsync().GetAwaiter().GetResult());
            return adResponse.Url.ToString();
        }

If you using App Tokens against Office365 the actual need for AutoDiscover is negalible as the endpoint will always be https://outlook.office365.com/ews/exchange.asmx . If you need to support Hybrid Modern Auth and your switching between OnPrem and Hybrid Mailboxes within your code then you will need to do that, but keep in mind you will also need a different access Token when accessing OnPrem Mailboxes to those in the cloud becuase the Token Audience will change.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glen Scales