-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #329 from adobe/dev
Sync dev to main before release
- Loading branch information
Showing
22 changed files
with
982 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
core/src/main/java/com/venia/core/models/commerce/services/CommerceComponentModelFinder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
~ Copyright 2021 Adobe | ||
~ | ||
~ Licensed 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 com.venia.core.models.commerce.services; | ||
|
||
import com.adobe.cq.commerce.core.components.models.product.Product; | ||
import com.adobe.cq.commerce.core.components.models.productlist.ProductList; | ||
import com.drew.lang.annotations.Nullable; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.models.factory.ModelFactory; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** | ||
* This service allows to traverse a {@link Resource} tree looking for a {@link Resource} of a set of particular resource types and if | ||
* found adapting them to given adapter type. This helps for example finding the product component on the page and return the Product model | ||
* from it. | ||
*/ | ||
@Component( | ||
service = com.venia.core.models.commerce.services.CommerceComponentModelFinder.class) | ||
public class CommerceComponentModelFinder { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(com.venia.core.models.commerce.services.CommerceComponentModelFinder.class); | ||
private static final Collection<String> PRODUCT_RTS = Collections.singleton("core/cif/components/commerce/product/v1/product"); | ||
private static final Collection<String> PRODUCT_LIST_RTS = Arrays.asList( | ||
"core/cif/components/commerce/productlist/v2/productlist", | ||
"core/cif/components/commerce/productlist/v1/productlist"); | ||
|
||
@Reference | ||
private ModelFactory modelFactory; | ||
|
||
@Nullable | ||
public Product findProductComponentModel(SlingHttpServletRequest request, Resource root) { | ||
return findComponentModel(request, root, PRODUCT_RTS, Product.class); | ||
} | ||
|
||
@Nullable | ||
public ProductList findProductListComponentModel(SlingHttpServletRequest request, Resource root) { | ||
return findComponentModel(request, root, PRODUCT_LIST_RTS, ProductList.class); | ||
} | ||
|
||
@Nullable | ||
public <T> T findComponentModel(SlingHttpServletRequest request, Resource root, Collection<String> resourceTypes, | ||
Class<T> adapterType) { | ||
Resource componentResource = findChildResourceWithType(root, resourceTypes); | ||
if (componentResource != null) { | ||
return modelFactory.getModelFromWrappedRequest(request, componentResource, adapterType); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private Resource findChildResourceWithType(Resource fromResource, Collection<String> resourceTypes) { | ||
if (fromResource == null) { | ||
return null; | ||
} | ||
|
||
LOGGER.debug("Looking for child resource type '{}' from {}", resourceTypes, fromResource.getPath()); | ||
|
||
for (Resource child : fromResource.getChildren()) { | ||
for (String resourceType : resourceTypes) { | ||
if (child.isResourceType(resourceType)) { | ||
LOGGER.debug("Found child resource type '{}' at {}", resourceType, child.getPath()); | ||
return child; | ||
} | ||
} | ||
|
||
Resource resource = findChildResourceWithType(child, resourceTypes); | ||
if (resource != null) { | ||
return resource; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
144 changes: 144 additions & 0 deletions
144
core/src/main/java/com/venia/core/models/commerce/servlets/CatalogPageErrorFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.venia.core.models.commerce.servlets; | ||
|
||
import com.adobe.cq.commerce.core.components.models.common.SiteStructure; | ||
import com.adobe.cq.commerce.core.components.models.product.Product; | ||
import com.adobe.cq.commerce.core.components.models.productlist.ProductList; | ||
import com.adobe.cq.commerce.core.components.models.retriever.AbstractCategoryRetriever; | ||
import com.adobe.cq.commerce.core.components.models.retriever.AbstractProductRetriever; | ||
import com.day.cq.wcm.api.Page; | ||
import com.day.cq.wcm.api.PageManager; | ||
import com.day.cq.wcm.api.PageManagerFactory; | ||
import com.venia.core.models.commerce.services.CommerceComponentModelFinder; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.SlingHttpServletResponse; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.scripting.SlingBindings; | ||
import org.apache.sling.scripting.core.ScriptHelper; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.servlet.*; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
@Component( | ||
service = {Filter.class}, | ||
property = { | ||
"sling.filter.scope=REQUEST", | ||
"sling.filter.resourceTypes=cq:Page", | ||
"sling.filter.resourceTypes=core/cif/components/structure/page/v1/page", | ||
"sling.filter.resourceTypes=core/cif/components/structure/page/v2/page", | ||
"sling.filter.resourceTypes=core/cif/components/structure/page/v3/page", | ||
"sling.filter.extensions=html", | ||
"sling.filter.extensions=json", | ||
"sling.filter.resource.pattern=/content(/.+)?", | ||
"service.ranking:Integer=-4000" | ||
}) | ||
public class CatalogPageErrorFilter implements Filter { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(CatalogPageErrorFilter.class); | ||
|
||
@Reference | ||
private PageManagerFactory pageManagerFactory; | ||
|
||
@Reference | ||
private CommerceComponentModelFinder commerceModelFinder; | ||
|
||
public CatalogPageErrorFilter() { | ||
} | ||
|
||
private BundleContext bundleContext; | ||
|
||
@Activate | ||
protected void activate(BundleContext bundleContext) { | ||
this.bundleContext = bundleContext; | ||
} | ||
|
||
public void init(FilterConfig filterConfig) throws ServletException { | ||
} | ||
|
||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | ||
if (servletRequest instanceof SlingHttpServletRequest && servletResponse instanceof SlingHttpServletResponse) { | ||
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) servletRequest; | ||
SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) servletResponse; | ||
PageManager pageManager = pageManagerFactory.getPageManager(slingRequest.getResourceResolver()); | ||
Page currentPage = pageManager.getContainingPage(slingRequest.getResource()); | ||
boolean removeSlingScriptHelperFromBindings = false; | ||
if (currentPage != null) { | ||
// Get the SiteStructure model | ||
SiteStructure siteStructure = slingRequest.adaptTo(SiteStructure.class); | ||
if (siteStructure.isProductPage(currentPage)) { | ||
// add the SlingScriptHelper to the bindings if it is not there yet | ||
removeSlingScriptHelperFromBindings = addSlingScriptHelperIfNeeded(slingRequest, slingResponse); | ||
Product product = commerceModelFinder.findProductComponentModel(slingRequest, currentPage.getContentResource()); | ||
if (product != null) { | ||
AbstractProductRetriever productRetriever = product.getProductRetriever(); | ||
// force GraphQL query execution | ||
if (productRetriever != null && !product.getFound() && productRetriever.hasErrors()) { | ||
slingResponse.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Commerce application not reachable"); | ||
return; | ||
} | ||
} | ||
} else if (siteStructure.isCategoryPage(currentPage)) { | ||
// add the SlingScriptHelper to the bindings if it is not there yet | ||
removeSlingScriptHelperFromBindings = addSlingScriptHelperIfNeeded(slingRequest, slingResponse); | ||
ProductList productList = commerceModelFinder.findProductListComponentModel(slingRequest, currentPage.getContentResource()); | ||
if (productList != null) { | ||
// Get the AbstractCategoryRetriever model | ||
AbstractCategoryRetriever categoryRetriever = productList.getCategoryRetriever(); | ||
if ((categoryRetriever != null && | ||
// force GraphQL query execution for category | ||
categoryRetriever.fetchCategory() == null && categoryRetriever.hasErrors()) || | ||
// force GraphQL query execution for products | ||
productList.getSearchResultsSet().hasErrors()) { | ||
slingResponse.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Commerce application not reachable"); | ||
return; | ||
} | ||
} | ||
} | ||
if (removeSlingScriptHelperFromBindings) { | ||
// remove the ScriptHelper if we added it before | ||
SlingBindings slingBindings = getSlingBindings(slingRequest); | ||
if (slingBindings != null) { | ||
slingBindings.remove("sling"); | ||
} | ||
} | ||
} | ||
} | ||
filterChain.doFilter(servletRequest, servletResponse); | ||
} | ||
|
||
public void destroy() { | ||
} | ||
|
||
/** | ||
* The {@link com.venia.core.models.commerce.services.CommerceComponentModelFinder} uses | ||
* {@link org.apache.sling.models.factory.ModelFactory#getModelFromWrappedRequest(SlingHttpServletRequest, Resource, Class)} | ||
* to obtain the model of either {@link Product} or {@link ProductList}. That method invokes all | ||
* {@link org.apache.sling.scripting.api.BindingsValuesProvider} | ||
* while creating the wrapped request. In AEM 6.5 they are not executed lazily and depend on some existing bindings on construction of | ||
* which one requires the SlingScriptHelper. | ||
* | ||
* @param slingRequest | ||
*/ | ||
private boolean addSlingScriptHelperIfNeeded(SlingHttpServletRequest slingRequest, SlingHttpServletResponse slingResponse) { | ||
SlingBindings slingBindings = getSlingBindings(slingRequest); | ||
if (slingBindings != null && slingBindings.getSling() == null) { | ||
slingBindings.put("sling", new ScriptHelper(bundleContext, null, slingRequest, slingResponse)); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static SlingBindings getSlingBindings(SlingHttpServletRequest slingRequest) { | ||
Object attr = slingRequest.getAttribute(SlingBindings.class.getName()); | ||
if (attr == null) { | ||
attr = new SlingBindings(); | ||
slingRequest.setAttribute(SlingBindings.class.getName(), attr); | ||
} | ||
return attr instanceof SlingBindings ? (SlingBindings) attr : null; | ||
} | ||
} |
Oops, something went wrong.