Search in sources :

Example 6 with ClientLibrary

use of com.adobe.granite.ui.clientlibs.ClientLibrary in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ClientLibraryCategoriesDataSourceServlet method getCategoryResourceList.

private List<Resource> getCategoryResourceList(@NotNull SlingHttpServletRequest request, LibraryType libraryType) {
    List<Resource> categoryResourceList = new ArrayList<>();
    HashSet<String> clientLibraryCategories = new HashSet<String>();
    for (ClientLibrary library : htmlLibraryManager.getLibraries().values()) {
        for (String category : library.getCategories()) {
            clientLibraryCategories.add(category);
        }
    }
    if (libraryType != null) {
        Collection<ClientLibrary> clientLibraries = htmlLibraryManager.getLibraries(clientLibraryCategories.toArray(new String[clientLibraryCategories.size()]), libraryType, true, true);
        clientLibraryCategories.clear();
        for (ClientLibrary library : clientLibraries) {
            for (String category : library.getCategories()) {
                clientLibraryCategories.add(category);
            }
        }
    }
    for (String category : clientLibraryCategories) {
        categoryResourceList.add(new CategoryResource(category, request.getResourceResolver()));
    }
    return categoryResourceList;
}
Also used : ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 7 with ClientLibrary

use of com.adobe.granite.ui.clientlibs.ClientLibrary in project acs-aem-commons by Adobe-Consulting-Services.

the class OptionsServletTest method testWithNormalType.

@Test
public void testWithNormalType() throws Exception {
    Map<String, ClientLibrary> libraries = new HashMap<String, ClientLibrary>();
    String jsOnlyCategory1 = RandomStringUtils.randomAlphanumeric(5);
    String jsOnlyCategory2 = RandomStringUtils.randomAlphanumeric(5);
    String bothCategory1 = RandomStringUtils.randomAlphanumeric(5);
    String bothCategory2 = RandomStringUtils.randomAlphanumeric(5);
    addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js" }, new String[] { jsOnlyCategory1, jsOnlyCategory2 });
    addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js" }, new String[] { jsOnlyCategory2 });
    addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js", "css" }, new String[] { bothCategory1, bothCategory2 });
    addLibrary(libraries, RandomStringUtils.random(10), new String[] { "js", "css" }, new String[] { bothCategory2 });
    when(manager.getLibraries()).thenReturn(libraries);
    MockSlingHttpServletRequest request = new MockSlingHttpServletRequest("/apps/acs-commons/components/utilities/designer/clientlibsmanager/options", "js", "json", null, null);
    MockSlingHttpServletResponse response = new MockSlingHttpServletResponse();
    servlet.doGet(request, response);
    assertEquals("application/json", response.getContentType());
    JSONArray array = new JSONArray(response.getOutput().toString());
    assertEquals(4, array.length());
}
Also used : ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary) HashMap(java.util.HashMap) MockSlingHttpServletRequest(org.apache.sling.commons.testing.sling.MockSlingHttpServletRequest) JSONArray(org.apache.sling.commons.json.JSONArray) MockSlingHttpServletResponse(org.apache.sling.commons.testing.sling.MockSlingHttpServletResponse) Test(org.junit.Test)

Example 8 with ClientLibrary

use of com.adobe.granite.ui.clientlibs.ClientLibrary in project acs-aem-commons by Adobe-Consulting-Services.

the class DynamicClassicUiClientLibraryServletTest method setup.

@Before
public void setup() throws Exception {
    writer = new StringWriter();
    when(response.getWriter()).thenReturn(new PrintWriter(writer));
    when(request.getResourceResolver()).thenReturn(resourceResolver);
    when(resourceResolver.map(any(SlingHttpServletRequest.class), anyString())).then(i -> {
        SlingHttpServletRequest request = i.getArgumentAt(0, SlingHttpServletRequest.class);
        String path = i.getArgumentAt(1, String.class);
        if (request != null && StringUtils.isNotBlank(request.getContextPath())) {
            return request.getContextPath().concat(path);
        } else {
            return path;
        }
    });
    when(htmlLibraryManager.getLibraries(any(String[].class), any(LibraryType.class), eq(true), eq(true))).thenAnswer(i -> {
        Set<ClientLibrary> result = new HashSet<>();
        for (String category : i.getArgumentAt(0, String[].class)) {
            if (category.equals(LIMIT.id)) {
                result.add(LIMIT.getClientLibrary());
            } else if (category.equals(PLACEHOLDER.id)) {
                result.add(PLACEHOLDER.getClientLibrary());
            } else if (category.equals(CUSTOM.id)) {
                result.add(CUSTOM.getClientLibrary());
            }
        }
        return result;
    });
    when(htmlLibraryManager.isMinifyEnabled()).thenReturn(false);
}
Also used : StringWriter(java.io.StringWriter) ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary) LibraryType(com.adobe.granite.ui.clientlibs.LibraryType) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 9 with ClientLibrary

use of com.adobe.granite.ui.clientlibs.ClientLibrary in project acs-aem-commons by Adobe-Consulting-Services.

the class OptionsServlet method doGet.

@Override
@SuppressWarnings({ "squid:S3776", "squid:S1141" })
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    final JsonWriter writer = new JsonWriter(response.getWriter());
    writer.beginArray();
    String type = request.getRequestPathInfo().getSelectorString();
    if (type != null) {
        try {
            Set<String> categories = new TreeSet<String>();
            LibraryType libraryType = LibraryType.valueOf(type.toUpperCase());
            Map<String, ClientLibrary> libraries = libraryManager.getLibraries();
            for (ClientLibrary library : libraries.values()) {
                if (library.getTypes() != null && library.getTypes().contains(libraryType)) {
                    String[] libraryCats = library.getCategories();
                    if (libraryCats != null) {
                        for (String cat : libraryCats) {
                            categories.add(cat);
                        }
                    }
                }
            }
            for (String cat : categories) {
                writer.beginObject();
                writer.name("value");
                writer.value(cat);
                writer.name("text");
                writer.value(cat);
                writer.endObject();
            }
        } catch (IllegalArgumentException e) {
        // no matching type. no need to log, just return empty array.
        }
    }
    writer.endArray();
    writer.close();
}
Also used : ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary) TreeSet(java.util.TreeSet) LibraryType(com.adobe.granite.ui.clientlibs.LibraryType) JsonWriter(com.google.gson.stream.JsonWriter)

Example 10 with ClientLibrary

use of com.adobe.granite.ui.clientlibs.ClientLibrary in project acs-aem-commons by Adobe-Consulting-Services.

the class AbstractDynamicClientLibraryServlet method doGet.

@Override
protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
    ResourceResolver resourceResolver = request.getResourceResolver();
    response.setContentType("application/json");
    JsonWriter writer = new JsonWriter(response.getWriter());
    writer.beginObject();
    writer.name("js");
    writer.beginArray();
    if (!excludeAll) {
        Collection<ClientLibrary> libraries = htmlLibraryManager.getLibraries(categories, LibraryType.JS, true, true);
        for (ClientLibrary library : libraries) {
            writer.value(resourceResolver.map(request, library.getIncludePath(LibraryType.JS, htmlLibraryManager.isMinifyEnabled())));
        }
    }
    writer.endArray();
    writer.name("css");
    writer.beginArray();
    if (!excludeAll) {
        Collection<ClientLibrary> libraries = htmlLibraryManager.getLibraries(categories, LibraryType.CSS, true, true);
        for (ClientLibrary library : libraries) {
            writer.value(resourceResolver.map(request, library.getIncludePath(LibraryType.CSS, htmlLibraryManager.isMinifyEnabled())));
        }
    }
    writer.endArray();
    writer.endObject();
}
Also used : ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) JsonWriter(com.google.gson.stream.JsonWriter)

Aggregations

ClientLibrary (com.adobe.granite.ui.clientlibs.ClientLibrary)12 LibraryType (com.adobe.granite.ui.clientlibs.LibraryType)4 PrintWriter (java.io.PrintWriter)3 HashSet (java.util.HashSet)3 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)3 Resource (org.apache.sling.api.resource.Resource)3 MockHtmlLibraryManager (com.adobe.cq.wcm.core.components.testing.MockHtmlLibraryManager)2 HtmlLibrary (com.adobe.granite.ui.clientlibs.HtmlLibrary)2 HtmlLibraryManager (com.adobe.granite.ui.clientlibs.HtmlLibraryManager)2 JsonWriter (com.google.gson.stream.JsonWriter)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 MockPersistenceStrategy (com.adobe.cq.wcm.core.components.testing.MockPersistenceStrategy)1 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 LinkedHashSet (java.util.LinkedHashSet)1