Java Code Examples for com.day.cq.wcm.api.Page#adaptTo()

The following examples show how to use com.day.cq.wcm.api.Page#adaptTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SiteNavigation.java    From aem-core-cif-components with Apache License 2.0 6 votes vote down vote up
/**
 * Builds and returns a product page URL based on the given page, slug, and variant sku.
 * This method might return the URL of a specific subpage configured for that particular page.
 * 
 * @param page The page used to build the URL.
 * @param slug The slug of the product.
 * @param variantSku An optional sku of the variant that will be "selected" on the product page, can be null.
 * @return The product page URL.
 * @deprecated Use {@link UrlProvider#toProductUrl(SlingHttpServletRequest, Page, Map)}
 */
@Deprecated
public String toProductUrl(Page page, String slug, String variantSku) {
    Resource pageResource = page.adaptTo(Resource.class);
    boolean deepLink = !WCMMode.DISABLED.equals(WCMMode.fromRequest(request));
    if (deepLink) {
        Resource subPageResource = UrlProviderImpl.toSpecificPage(pageResource, slug);
        if (subPageResource != null) {
            pageResource = subPageResource;
        }
    }

    if (StringUtils.isNotBlank(variantSku)) {
        return String.format("%s.%s.html%s%s", pageResource.getPath(), slug, COMBINED_SKU_SEPARATOR, variantSku);
    } else {
        return String.format("%s.%s.html", pageResource.getPath(), slug);
    }
}
 
Example 2
Source File: UrlProviderImpl.java    From aem-core-cif-components with Apache License 2.0 5 votes vote down vote up
private String toUrl(SlingHttpServletRequest request, Page page, Map<String, String> params, String template, String selectorFilter) {
    if (page != null && params.containsKey(selectorFilter)) {
        Resource pageResource = page.adaptTo(Resource.class);
        boolean deepLink = !WCMMode.DISABLED.equals(WCMMode.fromRequest(request));
        if (deepLink) {
            Resource subPageResource = toSpecificPage(pageResource, params.get(selectorFilter));
            if (subPageResource != null) {
                pageResource = subPageResource;
            }
        }

        params.put(PAGE_PARAM, pageResource.getPath());
    }

    String prefix = "${", suffix = "}"; // variables have the format ${var}
    if (template.contains("{{")) {
        prefix = "{{";
        suffix = "}}"; // variables have the format {{var}}
    }

    StringSubstitutor sub = new StringSubstitutor(params, prefix, suffix);
    String url = sub.replace(template);
    url = StringUtils.substringBeforeLast(url, "#" + prefix); // remove anchor if it hasn't been substituted

    if (url.contains(prefix)) {
        LOGGER.warn("Missing params for URL substitution. Resulted URL: {}", url);
    }

    return url;
}
 
Example 3
Source File: MagentoGraphqlClient.java    From aem-core-cif-components with Apache License 2.0 4 votes vote down vote up
private MagentoGraphqlClient(Resource resource, Page page) {

        Resource configurationResource = page != null ? page.adaptTo(Resource.class) : resource;

        LOGGER.debug("Try to get a graphql client from the resource at {}", configurationResource.getPath());

        ComponentsConfiguration configuration = configurationResource.adaptTo(ComponentsConfiguration.class);
        if (configuration.size() == 0) {
            LOGGER.warn("Context configuration not found, attempt to read the configuration from the page");
            graphqlClient = configurationResource.adaptTo(GraphqlClient.class);
        } else {
            LOGGER.debug("Crafting a configuration resource and attempting to get a GraphQL client from it...");
            // The Context-Aware Configuration API does return a ValueMap with all the collected properties from /conf and /libs,
            // but if you ask it for a resource via ConfigurationResourceResolver#getConfigurationResource() you get the resource that
            // resolves first (e.g. /conf/.../settings/cloudonfigs/commerce). This resource might not contain the properties
            // we need to adapt it to a graphql client so we just craft our own resource using the value map provided above.
            Resource configResource = new ValueMapResource(configurationResource.getResourceResolver(),
                configurationResource.getPath(),
                configurationResource.getResourceType(),
                configuration.getValueMap());

            graphqlClient = configResource.adaptTo(GraphqlClient.class);
        }
        if (graphqlClient == null) {
            throw new RuntimeException("GraphQL client not available for resource " + configurationResource.getPath());
        }
        requestOptions = new RequestOptions().withGson(QueryDeserializer.getGson());

        CachingStrategy cachingStrategy = new CachingStrategy()
            .withCacheName(resource.getResourceType())
            .withDataFetchingPolicy(DataFetchingPolicy.CACHE_FIRST);
        requestOptions.withCachingStrategy(cachingStrategy);

        String storeCode;

        if (configuration.size() > 0) {
            storeCode = configuration.get(STORE_CODE_PROPERTY, String.class);
            if (storeCode == null) {
                storeCode = readFallBackConfiguration(configurationResource, STORE_CODE_PROPERTY);
            }
        } else {
            storeCode = readFallBackConfiguration(configurationResource, STORE_CODE_PROPERTY);
        }
        if (StringUtils.isNotEmpty(storeCode)) {
            Header storeHeader = new BasicHeader("Store", storeCode);
            requestOptions.withHeaders(Collections.singletonList(storeHeader));
        }
    }