How do I retrieve all products from a specific site or catalog?

While we don't have an individual method that takes in a siteId parameter and returns all the products, but we do have the ability to filter by site. 

By default, we use Hibernate filters to constrain queries based on site or catalog (depending on the context). The constraints for the filters are completely controlled by the BroadleafRequestContext (BRC). If the BRC#getSite() is set to 1 then behind the scenes the Hibernate filter adds a constrains of SITE_DISC = 1 (this is a over-simplified example).

To help support dynamic filtering of site, catalog, or profile we have a utility called IdentityExecutionUtils. This utility can be used to call a query within the desired identity context (e.g. with a siteId = 1). Below is a snippet of code that shows an example of how it can be done. If you view the IdentityExecutionUtils code you will see that various parameters can be passed in for site, catalog, and/or profile.

    /**
     * Finds all products based on the passed in siteId
     * 
     * @param siteId
     * @return List<Product>
     */
    public List<Product> findProductsBySite(Long siteId) {
        Site mySite = siteService.retrieveNonPersistentSiteById(siteId);
        List<Product> list = null;
        try {
            list = IdentityExecutionUtils.runOperationByIdentifier(new IdentityOperation<List<Product>, Exception>() {

                @Override
                public List<Product> execute() throws Exception {
                    //The findAllProducts will be constrained based on the mySite value.  
                    //Behind the scenes the BroadleafRequestContext is changed, the query executed (in the new context), and then BRC is restored
                    List<Product> allProducts = catalogService.findAllProducts();
                    return allProducts;
                }
            }, mySite);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

The following example searches by catalog ;

    /**
     * Finds all products based on the passed in catalogId
     * 
     * @param catalogId
     * @return List<Product>
     */
    public List<Product> findProductsByCatalog(Long catalogId) {
        
        Catalog searchCatalog = siteService.findCatalogById(catalogId);
        Site owningSite = ((MultiTenantCatalog) searchCatalog).getOwningSite();
        List<Product> list = null;
        try {
            list = IdentityExecutionUtils.runOperationByIdentifier(new IdentityOperation<List<Product>, Exception>() {

                @Override
                public List<Product> execute() throws Exception {
                    //The findAllProducts will be constrained based on the mySite value.  
                    //Behind the scenes the BroadleafRequestContext is changed, the query executed (in the new context), and then BRC is restored
                    List<Product> allProducts = catalogService.findAllProducts();
                    return allProducts;
                }
            }, owningSite, searchCatalog);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

The examples above would need to be cleaned up some (i.e. better error handling) but it gives the basic approach for programatic context filtering (while not impacting the existing BRC context).