Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS-173907 Display list of available headless resources in FDS Views Manager #129996

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ dependencies {
compileOnly group: "javax.portlet", name: "portlet-api", version: "3.0.1"
compileOnly group: "org.apache.felix", name: "org.apache.felix.http.servlet-api", version: "1.1.2"
compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.4.0"
compileOnly group: "org.osgi", name: "osgi.core", version: "6.0.0"
compileOnly project(":apps:application-list:application-list-api")
compileOnly project(":apps:frontend-taglib:frontend-taglib-react")
compileOnly project(":core:petra:petra-function")
compileOnly project(":core:petra:petra-lang")
compileOnly project(":core:petra:petra-string")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"dependencies": {
"@liferay/frontend-data-set-web": "*",
"frontend-js-web": "*"
},
"name": "@liferay/frontend-data-set-views-web",
"private": true,
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<%--
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
Expand All @@ -12,6 +11,15 @@
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
--%>

<%@ include file="/init.jsp" %>
package com.liferay.frontend.data.set.views.web.internal.constants;

/**
* @author Marko Cikos
*/
public class FDSViewsWebKeys {

public static final String FDS_VIEWS_DISPLAY_CONTEXT =
"FDS_VIEWS_DISPLAY_CONTEXT";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.frontend.data.set.views.web.internal.display.context;

import com.liferay.frontend.data.set.views.web.internal.constants.FDSViewsPortletKeys;
import com.liferay.frontend.data.set.views.web.internal.resource.FDSHeadlessResource;
import com.liferay.frontend.data.set.views.web.internal.resource.FDSHeadlessResourcesUtil;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONUtil;
import com.liferay.portal.kernel.portlet.PortletURLFactoryUtil;
import com.liferay.portal.kernel.portlet.url.builder.PortletURLBuilder;

import java.util.Comparator;
import java.util.List;

import javax.portlet.PortletRequest;

/**
* @author Marko Cikos
*/
public class FDSViewsDisplayContext {

public FDSViewsDisplayContext(PortletRequest portletRequest) {
_portletRequest = portletRequest;
}

public String getFDSViewsURL() {
return PortletURLBuilder.create(
PortletURLFactoryUtil.create(
_portletRequest, FDSViewsPortletKeys.FDS_VIEWS,
PortletRequest.RENDER_PHASE)
).setMVCPath(
"/fds_views.jsp"
).buildString();
}

public JSONArray getHeadlessResourcesJSONArray() {
JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

List<FDSHeadlessResource> fdsHeadlessResources =
FDSHeadlessResourcesUtil.getFDSHeadlessResources();

fdsHeadlessResources.sort(
Comparator.comparing(FDSHeadlessResource::getBundleLabel));

fdsHeadlessResources.sort(
Comparator.comparing(FDSHeadlessResource::getName));

for (FDSHeadlessResource fdsHeadlessResource : fdsHeadlessResources) {
jsonArray.put(
JSONUtil.put(
"bundleLabel", fdsHeadlessResource.getBundleLabel()
).put(
"entityClassName", fdsHeadlessResource.getEntityClassName()
).put(
"name", fdsHeadlessResource.getName()
).put(
"version", fdsHeadlessResource.getVersion()
));
}

return jsonArray;
}

private final PortletRequest _portletRequest;

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
package com.liferay.frontend.data.set.views.web.internal.portlet;

import com.liferay.frontend.data.set.views.web.internal.constants.FDSViewsPortletKeys;
import com.liferay.frontend.data.set.views.web.internal.constants.FDSViewsWebKeys;
import com.liferay.frontend.data.set.views.web.internal.display.context.FDSViewsDisplayContext;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;

import java.io.IOException;

import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Component;

Expand All @@ -29,7 +36,7 @@
"com.liferay.portlet.display-category=category.hidden",
"com.liferay.portlet.layout-cacheable=true",
"javax.portlet.expiration-cache=0",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.init-param.view-template=/fds_views.jsp",
"javax.portlet.name=" + FDSViewsPortletKeys.FDS_VIEWS,
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=administrator,power-user,user",
Expand All @@ -38,4 +45,17 @@
service = Portlet.class
)
public class FDSViewsPortlet extends MVCPortlet {

@Override
protected void doDispatch(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {

renderRequest.setAttribute(
FDSViewsWebKeys.FDS_VIEWS_DISPLAY_CONTEXT,
new FDSViewsDisplayContext(renderRequest));

super.doDispatch(renderRequest, renderResponse);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.frontend.data.set.views.web.internal.resource;

/**
* @author Marko Cikos
*/
public class FDSHeadlessResource {

public FDSHeadlessResource(
String bundleLabel, String entityClassName, String name,
String version) {

_bundleLabel = bundleLabel;
_entityClassName = entityClassName;
_name = name;
_version = version;
}

public String getBundleLabel() {
return _bundleLabel;
}

public String getEntityClassName() {
return _entityClassName;
}

public String getName() {
return _name;
}

public String getVersion() {
return _version;
}

private final String _bundleLabel;
private final String _entityClassName;
private final String _name;
private final String _version;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.frontend.data.set.views.web.internal.resource;

import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.StringUtil;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**
* @author Marko Cikos
*/
@Component(service = {})
public class FDSHeadlessResourcesUtil {

public static List<FDSHeadlessResource> getFDSHeadlessResources() {
return new ArrayList<>(_fdsHeadlessResourcesMap.values());
}

@Activate
protected void activate(BundleContext bundleContext)
throws InvalidSyntaxException {

Filter filter = bundleContext.createFilter(
"(osgi.jaxrs.resource=true)");

_serviceTracker = new ServiceTracker<>(
bundleContext, filter,
new FDSHeadlessResourceServiceTrackerCustomizer(bundleContext));

_serviceTracker.open();
}

@Deactivate
protected void deactivate() {
_serviceTracker.close();
}

private static final Map<String, FDSHeadlessResource>
_fdsHeadlessResourcesMap = new ConcurrentHashMap<>();
private static ServiceTracker<Object, Object> _serviceTracker;

private class FDSHeadlessResourceServiceTrackerCustomizer
implements ServiceTrackerCustomizer<Object, Object> {

@Override
public Object addingService(ServiceReference<Object> serviceReference) {
String entityClassName = (String)serviceReference.getProperty(
"entity.class.name");

if (entityClassName != null) {
String[] entityClassNameParts = StringUtil.split(
entityClassName, StringPool.PERIOD);

_fdsHeadlessResourcesMap.put(
entityClassName,
new FDSHeadlessResource(
_getFDSHeadlessResourceBundleLabel(serviceReference),
entityClassName,
entityClassNameParts[entityClassNameParts.length - 1],
entityClassNameParts[entityClassNameParts.length - 2].
replaceAll(
StringPool.UNDERLINE, StringPool.PERIOD)));
}

return _bundleContext.getService(serviceReference);
}

@Override
public void modifiedService(
ServiceReference<Object> serviceReference, Object object) {

removedService(serviceReference, object);

addingService(serviceReference);
}

@Override
public void removedService(
ServiceReference<Object> serviceReference, Object object) {

String entityClassName = (String)serviceReference.getProperty(
"entity.class.name");

if (entityClassName != null) {
_fdsHeadlessResourcesMap.remove(entityClassName);
}

_bundleContext.ungetService(serviceReference);
}

private FDSHeadlessResourceServiceTrackerCustomizer(
BundleContext bundleContext) {

_bundleContext = bundleContext;
}

private String _getFDSHeadlessResourceBundleLabel(
ServiceReference<Object> serviceReference) {

Object object = _bundleContext.getService(serviceReference);

Bundle bundle = FrameworkUtil.getBundle(object.getClass());

Dictionary<String, String> headers = bundle.getHeaders(
StringPool.BLANK);

String bundleName = GetterUtil.getString(
headers.get(Constants.BUNDLE_NAME));

return bundleName.substring(
0, bundleName.lastIndexOf(StringPool.SPACE));
}

private final BundleContext _bundleContext;

}

}
Loading