-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transfer LinkInspector settings from the 'OSGi console' to the 'Grani…
…t' console. #47
- Loading branch information
Saldatsenka, Siarhei
committed
Aug 6, 2024
1 parent
4e91d46
commit 52bb47e
Showing
8 changed files
with
419 additions
and
7 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
55 changes: 55 additions & 0 deletions
55
...main/java/com/exadel/etoolbox/linkinspector/core/servlets/LinkTypesDataSourceServlet.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,55 @@ | ||
package com.exadel.etoolbox.linkinspector.core.servlets; | ||
|
||
import com.adobe.granite.ui.components.ds.DataSource; | ||
import com.adobe.granite.ui.components.ds.SimpleDataSource; | ||
import com.exadel.etoolbox.linkinspector.api.LinkResolver; | ||
import com.exadel.etoolbox.linkinspector.core.services.util.ServletUtil; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.SlingHttpServletResponse; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.servlets.HttpConstants; | ||
import org.apache.sling.api.servlets.SlingSafeMethodsServlet; | ||
import org.apache.sling.jcr.resource.api.JcrResourceConstants; | ||
import org.apache.sling.servlets.annotations.SlingServletResourceTypes; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
import javax.servlet.Servlet; | ||
import java.util.*; | ||
|
||
/** | ||
* @author Sergey Soldatenko | ||
*/ | ||
@Component(service = {Servlet.class}) | ||
@SlingServletResourceTypes( | ||
resourceTypes = "/bin/etoolbox/link-inspector/settings/linktypes", | ||
methods = HttpConstants.METHOD_GET) | ||
public class LinkTypesDataSourceServlet extends SlingSafeMethodsServlet { | ||
|
||
@Reference | ||
private transient List<LinkResolver> linkResolvers; | ||
|
||
@Override | ||
protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) { | ||
|
||
List<Resource> links = new ArrayList<>(); | ||
for (LinkResolver linkResolver : linkResolvers) { | ||
Map<String, Object> fieldProperties = new HashMap<>(); | ||
fieldProperties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, "granite/ui/components/coral/foundation/form/checkbox"); | ||
|
||
String serviceName = linkResolver.getClass().getName(); | ||
fieldProperties.put("name", "./" + serviceName + ".linkType"); | ||
fieldProperties.put("text", StringUtils.substringAfterLast(serviceName, ".")); | ||
fieldProperties.put("fieldDescription", StringUtils.substringAfterLast(serviceName, ".")); | ||
fieldProperties.put("uncheckedValue", Boolean.FALSE); | ||
fieldProperties.put("checked", Boolean.FALSE); | ||
fieldProperties.put("value", Boolean.TRUE); | ||
Resource checkBox = ServletUtil.createResource(request.getResourceResolver(), serviceName, fieldProperties, Collections.emptyList()); | ||
links.add(checkBox); | ||
} | ||
|
||
DataSource dataSource = new SimpleDataSource(links.iterator()); | ||
request.setAttribute(DataSource.class.getName(), dataSource); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
.../main/java/com/exadel/etoolbox/linkinspector/core/servlets/SettingsDataSourceServlet.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,114 @@ | ||
package com.exadel.etoolbox.linkinspector.core.servlets; | ||
|
||
/** | ||
* @author Sergey Soldatenko | ||
*/ | ||
|
||
import com.adobe.granite.ui.components.ds.DataSource; | ||
import com.adobe.granite.ui.components.ds.SimpleDataSource; | ||
import com.exadel.etoolbox.linkinspector.api.LinkResolver; | ||
import com.exadel.etoolbox.linkinspector.core.services.util.ServletUtil; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.SlingHttpServletResponse; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.servlets.HttpConstants; | ||
import org.apache.sling.api.servlets.SlingSafeMethodsServlet; | ||
import org.apache.sling.jcr.resource.api.JcrResourceConstants; | ||
import org.apache.sling.servlets.annotations.SlingServletResourceTypes; | ||
import org.osgi.framework.Bundle; | ||
import org.osgi.framework.FrameworkUtil; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.osgi.service.metatype.AttributeDefinition; | ||
import org.osgi.service.metatype.MetaTypeInformation; | ||
import org.osgi.service.metatype.MetaTypeService; | ||
import org.osgi.service.metatype.ObjectClassDefinition; | ||
|
||
import javax.servlet.Servlet; | ||
import java.util.*; | ||
|
||
@Component(service = {Servlet.class}) | ||
@SlingServletResourceTypes( | ||
resourceTypes = "/bin/etoolbox/link-inspector/settings", | ||
methods = HttpConstants.METHOD_GET) | ||
public class SettingsDataSourceServlet extends SlingSafeMethodsServlet { | ||
|
||
@Reference | ||
private transient MetaTypeService metaTypeService; | ||
|
||
@Reference | ||
private transient List<LinkResolver> linkResolvers; | ||
|
||
@Override | ||
protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) { | ||
|
||
String rootPath = request.getRequestPathInfo().getResourcePath(); | ||
Resource mainTab = request.getResourceResolver().getResource(rootPath + "/main"); | ||
Resource advancedTab = request.getResourceResolver().getResource(rootPath + "/advanced"); | ||
|
||
List<Resource> tabs = new ArrayList<>(); | ||
tabs.add(mainTab); | ||
tabs.add(advancedTab); | ||
|
||
Map<String, List<AttributeDefinition>> serviceSettings = getServiceSettings(); | ||
for (Map.Entry<String, List<AttributeDefinition>> stringListEntry : serviceSettings.entrySet()) { | ||
String serviceName = stringListEntry.getKey(); | ||
List<Resource> innerFields = new ArrayList<>(); | ||
for (AttributeDefinition attributeDefinition : stringListEntry.getValue()) { | ||
Map<String, Object> fieldProperties = new HashMap<>(); | ||
fieldProperties.put(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, getResourceTypeByType(attributeDefinition.getType())); | ||
|
||
fieldProperties.put("name", "./" + serviceName + "." + attributeDefinition.getID()); | ||
fieldProperties.put("fieldLabel", attributeDefinition.getName()); | ||
fieldProperties.put("fieldDescription", attributeDefinition.getDescription()); | ||
Resource textField = ServletUtil.createResource(request.getResourceResolver(), serviceName + "." + attributeDefinition.getID(), fieldProperties, Collections.emptyList()); | ||
innerFields.add(textField); | ||
} | ||
Resource tab = ServletUtil.createTab(request.getResourceResolver(), serviceName, splitCamelCase(StringUtils.substringAfterLast(serviceName, ".")), innerFields); | ||
tabs.add(tab); | ||
} | ||
|
||
DataSource dataSource = new SimpleDataSource(tabs.iterator()); | ||
request.setAttribute(DataSource.class.getName(), dataSource); | ||
} | ||
|
||
private String splitCamelCase(String s) { | ||
return s.replaceAll( | ||
String.format("%s|%s|%s", | ||
"(?<=[A-Z])(?=[A-Z][a-z])", | ||
"(?<=[^A-Z])(?=[A-Z])", | ||
"(?<=[A-Za-z])(?=[^A-Za-z])" | ||
), | ||
" " | ||
); | ||
} | ||
|
||
private String getResourceTypeByType(int type) { | ||
String result; | ||
switch (type) { | ||
case 3: | ||
result = "granite/ui/components/coral/foundation/form/numberfield"; | ||
break; | ||
default: | ||
result = "granite/ui/components/coral/foundation/form/textfield"; | ||
break; | ||
} | ||
return result; | ||
} | ||
|
||
private Map<String, List<AttributeDefinition>> getServiceSettings() { | ||
Map<String, List<AttributeDefinition>> result = new HashMap<>(); | ||
for (LinkResolver linkResolver : linkResolvers) { | ||
Bundle bundle = FrameworkUtil.getBundle(linkResolver.getClass()); | ||
MetaTypeInformation metaTypeInformation = metaTypeService.getMetaTypeInformation(bundle); | ||
ObjectClassDefinition objectClassDefinition = metaTypeInformation.getObjectClassDefinition(linkResolver.getClass().getName(), null); | ||
AttributeDefinition[] definitions = objectClassDefinition.getAttributeDefinitions(ObjectClassDefinition.ALL); | ||
|
||
result.put(linkResolver.getClass().getName(), Arrays.asList(definitions)); | ||
} | ||
return result; | ||
} | ||
|
||
|
||
} |
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
Oops, something went wrong.