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;
}
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());
}
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);
}
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();
}
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();
}
Aggregations