Search in sources :

Example 16 with Page

use of com.day.cq.wcm.api.Page in project acs-aem-commons by Adobe-Consulting-Services.

the class TemplateUtilTest method test_that_correct_template_returns_true.

@Test
public void test_that_correct_template_returns_true() {
    Page page = mock(Page.class);
    ValueMap properties = createTemplateValueMap(TMPL_FAKE);
    when(page.getProperties()).thenReturn(properties);
    assertThat(TemplateUtil.hasTemplate(page, TMPL_FAKE), is(true));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Page(com.day.cq.wcm.api.Page) Test(org.junit.Test)

Example 17 with Page

use of com.day.cq.wcm.api.Page in project acs-aem-commons by Adobe-Consulting-Services.

the class TemplateUtilTest method test_that_null_properties_always_returns_false.

@Test
public void test_that_null_properties_always_returns_false() {
    Page page = mock(Page.class);
    // implicit page.getProperties() == null
    assertThat(TemplateUtil.hasTemplate(page, TMPL_FAKE), is(false));
}
Also used : Page(com.day.cq.wcm.api.Page) Test(org.junit.Test)

Example 18 with Page

use of com.day.cq.wcm.api.Page in project acs-aem-commons by Adobe-Consulting-Services.

the class TemplateUtilTest method test_that_null_template_always_returns_false.

@Test
public void test_that_null_template_always_returns_false() {
    Page page = mock(Page.class);
    ValueMap properties = createTemplateValueMap(TMPL_FAKE);
    when(page.getProperties()).thenReturn(properties);
    assertThat(TemplateUtil.hasTemplate(page, null), is(false));
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Page(com.day.cq.wcm.api.Page) Test(org.junit.Test)

Example 19 with Page

use of com.day.cq.wcm.api.Page in project acs-aem-commons by Adobe-Consulting-Services.

the class DispatcherFlusherServlet method doPost.

@Override
@SuppressWarnings("squid:S3776")
protected final void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    final Resource resource = request.getResource();
    final ResourceResolver resourceResolver = request.getResourceResolver();
    final PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    final Page currentPage = pageManager.getContainingPage(resource);
    final ValueMap properties = resource.getValueMap();
    /* Properties */
    final String[] paths = properties.get("paths", new String[0]);
    final ReplicationActionType replicationActionType = ReplicationActionType.valueOf(properties.get("replicationActionType", ReplicationActionType.ACTIVATE.name()));
    final List<FlushResult> overallResults = new ArrayList<FlushResult>();
    boolean caughtException = false;
    ResourceResolver flushingResourceResolver = null;
    try {
        if (paths.length > 0) {
            if (flushWithAdminResourceResolver) {
                // Use the admin resource resolver for replication to ensure all
                // replication permission checks are OK
                // Make sure to close this resource resolver
                flushingResourceResolver = resourceResolverFactory.getServiceResourceResolver(AUTH_INFO);
            } else {
                // Use the HTTP Request's resource resolver; don't close this resource resolver
                flushingResourceResolver = resourceResolver;
            }
            final Map<Agent, ReplicationResult> results = dispatcherFlusher.flush(flushingResourceResolver, replicationActionType, true, paths);
            for (final Map.Entry<Agent, ReplicationResult> entry : results.entrySet()) {
                final Agent agent = entry.getKey();
                final ReplicationResult result = entry.getValue();
                overallResults.add(new FlushResult(agent, result));
            }
        }
    } catch (ReplicationException e) {
        log.error("Replication exception occurred during Dispatcher Flush request.", e);
        caughtException = true;
    } catch (LoginException e) {
        log.error("Could not obtain an Admin Resource Resolver during Dispatcher Flush request.", e);
        caughtException = true;
    } finally {
        if (flushWithAdminResourceResolver && flushingResourceResolver != null) {
            // Close the admin resource resolver if opened by this servlet
            flushingResourceResolver.close();
        }
    }
    if (request.getRequestPathInfo().getExtension().equals("json")) {
        response.setContentType("application/json");
        JSONWriter writer = new JSONWriter(response.getWriter());
        try {
            writer.object();
            for (final FlushResult result : overallResults) {
                writer.key(result.agentId);
                writer.value(result.success);
            }
            writer.endObject();
        } catch (JSONException e) {
            throw new ServletException("Unable to output JSON data", e);
        }
    } else {
        String suffix;
        if (caughtException) {
            suffix = "replication-error";
        } else {
            suffix = StringUtils.join(overallResults, '/');
        }
        response.sendRedirect(request.getContextPath() + currentPage.getPath() + ".html/" + suffix);
    }
}
Also used : JSONWriter(org.apache.sling.commons.json.io.JSONWriter) Agent(com.day.cq.replication.Agent) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) JSONException(org.apache.sling.commons.json.JSONException) Page(com.day.cq.wcm.api.Page) ServletException(javax.servlet.ServletException) ReplicationResult(com.day.cq.replication.ReplicationResult) PageManager(com.day.cq.wcm.api.PageManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) ReplicationException(com.day.cq.replication.ReplicationException) ValueMap(org.apache.sling.api.resource.ValueMap) Map(java.util.Map) ReplicationActionType(com.day.cq.replication.ReplicationActionType)

Example 20 with Page

use of com.day.cq.wcm.api.Page in project acs-aem-commons by Adobe-Consulting-Services.

the class PagesReferenceProvider method findReferences.

@Override
public List<Reference> findReferences(Resource resource) {
    List<Reference> references = new ArrayList<Reference>();
    ResourceResolver resolver = resource.getResourceResolver();
    PageManager pageManager = resolver.adaptTo(PageManager.class);
    Set<Page> pages = new HashSet<Page>();
    search(resource, pages, pageManager);
    for (Page page : pages) {
        Resource contentResource = page.getContentResource();
        if (contentResource != null && !contentResource.getPath().equals(resource.getPath())) {
            references.add(getReference(page));
        }
    }
    return references;
}
Also used : PageManager(com.day.cq.wcm.api.PageManager) Reference(com.day.cq.wcm.api.reference.Reference) ArrayList(java.util.ArrayList) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) Page(com.day.cq.wcm.api.Page) HashSet(java.util.HashSet)

Aggregations

Page (com.day.cq.wcm.api.Page)100 Resource (org.apache.sling.api.resource.Resource)45 PageManager (com.day.cq.wcm.api.PageManager)34 Test (org.junit.jupiter.api.Test)22 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)18 ValueMap (org.apache.sling.api.resource.ValueMap)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)12 SlingBindings (org.apache.sling.api.scripting.SlingBindings)12 NotNull (org.jetbrains.annotations.NotNull)10 Test (org.junit.Test)9 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Optional (java.util.Optional)6 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)6 Nullable (org.jetbrains.annotations.Nullable)6 LinkHandler (com.adobe.cq.wcm.core.components.internal.link.LinkHandler)5 Template (com.day.cq.wcm.api.Template)5 StringUtils (org.apache.commons.lang3.StringUtils)5 Before (org.junit.Before)5