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

LPD-30646 DSM URL Parameters resolver spike #26

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
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
@@ -0,0 +1,57 @@
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.commerce.frontend.constants;

/**
* @author Gianmarco Brunialti Masera
*/
public class CommerceFDSRestApplicationConstants {

public static final String ADMIN_ACCOUNT_V1 =
"/headless-commerce-admin-account/v1.0";

public static final String ADMIN_CATALOG_V1 =
"/headless-commerce-admin-catalog/v1.0";

public static final String ADMIN_CHANNEL_V1 =
"/headless-commerce-admin-channel/v1.0";

public static final String ADMIN_INVENTORY_V1 =
"/headless-commerce-admin-inventory/v1.0";

public static final String ADMIN_ORDER_V1 =
"/headless-commerce-admin-order/v1.0";

public static final String ADMIN_PAYMENT_V1 =
"/headless-commerce-admin-payment/v1.0";

public static final String ADMIN_PRICING_V1 =
"/headless-commerce-admin-pricing/v1.0";

public static final String ADMIN_PRICING_V2 =
"/headless-commerce-admin-pricing/v2.0";

public static final String ADMIN_SHIPMENT_V1 =
"/headless-commerce-admin-shipment/v1.0";

public static final String ADMIN_SITE_SETTING_V1 =
"/headless-commerce-admin-site-setting/v1.0";

public static final String DELIVERY_CART_V1 =
"/headless-commerce-delivery-cart/v1.0";

public static final String DELIVERY_CATALOG_V1 =
"/headless-commerce-delivery-catalog/v1.0";

public static final String DELIVERY_ORDER_V1 =
"/headless-commerce-delivery-order/v1.0";

public static final String MACHINE_LEARNING_V1 =
"/headless-commerce-machine-learning/v1.0";

public static final String PUNCHOUT_V1 = "/headless-commerce-punchout/v1.0";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.commerce.order.content.web.internal.frontend.data.set.resolver;

import com.liferay.account.model.AccountEntry;
import com.liferay.commerce.constants.CommerceWebKeys;
import com.liferay.commerce.context.CommerceContext;
import com.liferay.commerce.frontend.constants.CommerceFDSRestApplicationConstants;
import com.liferay.commerce.model.CommerceOrder;
import com.liferay.commerce.product.model.CommerceChannel;
import com.liferay.commerce.product.service.CommerceChannelLocalService;
import com.liferay.frontend.data.set.resolver.FDSAPIURLResolver;
import com.liferay.portal.kernel.exception.PortalException;

import javax.servlet.http.HttpServletRequest;

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

/**
* @author Gianmarco Brunialti Masera
*/
@Component(
property = "fds.rest.application.key=" + CommerceFDSRestApplicationConstants.DELIVERY_CART_V1 + "/Cart",
service = FDSAPIURLResolver.class
)
public class PendingCommerceOrderFDSAPIURLResolver
implements FDSAPIURLResolver {

@Override
public String getSchema() {
return "Cart";
}

@Override
public String resolve(String baseURL, HttpServletRequest httpServletRequest)
throws PortalException {

CommerceContext commerceContext =
(CommerceContext)httpServletRequest.getAttribute(
CommerceWebKeys.COMMERCE_CONTEXT);

AccountEntry accountEntry = commerceContext.getAccountEntry();
CommerceChannel commerceChannel =
_commerceChannelLocalService.getCommerceChannel(
commerceContext.getCommerceChannelId());
CommerceOrder commerceOrder = commerceContext.getCommerceOrder();

return baseURL.replaceAll(
"\\{accountExternalReferenceCode\\}",
accountEntry.getExternalReferenceCode()
).replaceAll(
"\\{accountId\\}", String.valueOf(accountEntry.getAccountEntryId())
).replaceAll(
"\\{cartId\\}", String.valueOf(commerceOrder.getCommerceOrderId())
).replaceAll(
"\\{channelExternalReferenceCode\\}",
String.valueOf(commerceChannel.getExternalReferenceCode())
).replaceAll(
"\\{channelId\\}",
String.valueOf(commerceContext.getCommerceChannelId())
).replaceAll(
"\\{externalReferenceCode\\}",
commerceOrder.getExternalReferenceCode()
);
}

@Reference
private CommerceChannelLocalService _commerceChannelLocalService;

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import com.liferay.client.extension.type.manager.CETManager;
import com.liferay.frontend.data.set.admin.web.internal.constants.FDSAdminPortletKeys;
import com.liferay.frontend.data.set.admin.web.internal.portlet.FDSAdminPortlet;
import com.liferay.frontend.data.set.resolver.FDSAPIURLResolver;
import com.liferay.frontend.data.set.resolver.FDSAPIURLResolverRegistry;
import com.liferay.object.model.ObjectDefinition;
import com.liferay.object.service.ObjectDefinitionLocalService;
import com.liferay.osgi.service.tracker.collections.list.ServiceTrackerList;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.feature.flag.FeatureFlagManagerUtil;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONUtil;
Expand Down Expand Up @@ -42,12 +45,14 @@ public class FDSAdminDisplayContext {

public FDSAdminDisplayContext(
CETManager cetManager,
FDSAPIURLResolverRegistry fdsAPIURLResolverRegistry,
ObjectDefinitionLocalService objectDefinitionLocalService,
RenderRequest renderRequest, RenderResponse renderResponse,
ServiceTrackerList<FDSAdminPortlet.CompanyScopedOpenAPIResource>
serviceTrackerList) {

_cetManager = cetManager;
_fdsAPIURLResolverRegistry = fdsAPIURLResolverRegistry;
_renderRequest = renderRequest;
_renderResponse = renderResponse;
_serviceTrackerList = serviceTrackerList;
Expand Down Expand Up @@ -198,6 +203,21 @@ public String getFDSViewURL() {
).buildString();
}

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

if (FeatureFlagManagerUtil.isEnabled("LPD-25230")) {
List<FDSAPIURLResolver> resolvers =
_fdsAPIURLResolverRegistry.getResolvers();

for (FDSAPIURLResolver resolver : resolvers) {
jsonArray.put(resolver.getSchema());
}
}

return jsonArray;
}

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

Expand Down Expand Up @@ -255,6 +275,7 @@ public String getSaveFDSSortURL() {
}

private final CETManager _cetManager;
private final FDSAPIURLResolverRegistry _fdsAPIURLResolverRegistry;
private final ObjectDefinition _fdsEntryObjectDefinition;
private final ObjectDefinition _fdsViewObjectDefinition;
private final RenderRequest _renderRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.liferay.fragment.renderer.FragmentRendererContext;
import com.liferay.fragment.util.configuration.FragmentEntryConfigurationParser;
import com.liferay.frontend.data.set.constants.FDSEntityFieldTypes;
import com.liferay.frontend.data.set.resolver.FDSAPIURLResolver;
import com.liferay.frontend.data.set.resolver.FDSAPIURLResolverRegistry;
import com.liferay.list.type.model.ListTypeDefinition;
import com.liferay.list.type.model.ListTypeEntry;
import com.liferay.list.type.service.ListTypeDefinitionLocalService;
Expand All @@ -28,6 +30,7 @@
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.feature.flag.FeatureFlagManagerUtil;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONException;
Expand Down Expand Up @@ -339,17 +342,21 @@ private String _getAPIURL(

Map<String, Object> properties = fdsEntryObjectEntry.getProperties();

String restApplication = String.valueOf(
properties.get("restApplication"));

StringBundler sb = new StringBundler(3);

sb.append("/o");
sb.append(
StringUtil.replaceLast(
String.valueOf(properties.get("restApplication")), "/v1.0",
StringPool.BLANK));
StringUtil.replaceLast(restApplication, "/v1.0", StringPool.BLANK));
sb.append(String.valueOf(properties.get("restEndpoint")));

return _interpolateURL(
_getNestedFields(sb.toString(), fdsFieldObjectEntries),
return _resolveParameters(
_interpolateURL(
_getNestedFields(sb.toString(), fdsFieldObjectEntries),
httpServletRequest),
restApplication, String.valueOf(properties.get("restSchema")),
httpServletRequest);
}

Expand Down Expand Up @@ -1074,12 +1081,40 @@ private String _interpolateURL(
return apiURL;
}

private String _resolveParameters(
String apiURL, String restApplication, String restSchema,
HttpServletRequest httpServletRequest) {

if (FeatureFlagManagerUtil.isEnabled("LPD-25230")) {
FDSAPIURLResolver fdsAPIURLResolver =
_fdsAPIURLResolverRegistry.getResolver(
restApplication, restSchema);

if (fdsAPIURLResolver != null) {
try {
return fdsAPIURLResolver.resolve(
apiURL, httpServletRequest);
}
catch (PortalException portalException) {
_log.error(portalException);

return apiURL;
}
}
}

return apiURL;
}

private static final Log _log = LogFactoryUtil.getLog(
FDSAdminFragmentRenderer.class);

@Reference
private CETManager _cetManager;

@Reference
private FDSAPIURLResolverRegistry _fdsAPIURLResolverRegistry;

@Reference
private FragmentEntryConfigurationParser _fragmentEntryConfigurationParser;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.liferay.frontend.data.set.admin.web.internal.constants.FDSAdminPortletKeys;
import com.liferay.frontend.data.set.admin.web.internal.constants.FDSAdminWebKeys;
import com.liferay.frontend.data.set.admin.web.internal.display.context.FDSAdminDisplayContext;
import com.liferay.frontend.data.set.resolver.FDSAPIURLResolverRegistry;
import com.liferay.object.constants.ObjectDefinitionConstants;
import com.liferay.object.constants.ObjectFieldConstants;
import com.liferay.object.constants.ObjectRelationshipConstants;
Expand Down Expand Up @@ -142,8 +143,9 @@ protected void doDispatch(
renderRequest.setAttribute(
FDSAdminWebKeys.FDS_ADMIN_DISPLAY_CONTEXT,
new FDSAdminDisplayContext(
_cetManager, _objectDefinitionLocalService, renderRequest,
renderResponse, _serviceTrackerList));
_cetManager, _fdsAPIURLResolverRegistry,
_objectDefinitionLocalService, renderRequest, renderResponse,
_serviceTrackerList));

super.doDispatch(renderRequest, renderResponse);
}
Expand Down Expand Up @@ -995,6 +997,9 @@ private synchronized void _generate(
@Reference
private CETManager _cetManager;

@Reference
private FDSAPIURLResolverRegistry _fdsAPIURLResolverRegistry;

@Reference
private Language _language;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ portletDisplay.setBeta(true);
"namespace", liferayPortletResponse.getNamespace()
).put(
"permissionsURL", fdsAdminDisplayContext.getFDSViewPermissionsURL()
).put(
"resolvedRESTSchemas", fdsAdminDisplayContext.getRESTApplicationResolvedSchemasJSONArray()
).put(
"restApplications", fdsAdminDisplayContext.getRESTApplicationsJSONArray()
).build()
Expand All @@ -38,6 +40,8 @@ portletDisplay.setBeta(true);
"namespace", liferayPortletResponse.getNamespace()
).put(
"permissionsURL", fdsAdminDisplayContext.getFDSEntryPermissionsURL()
).put(
"resolvedRESTSchemas", fdsAdminDisplayContext.getRESTApplicationResolvedSchemasJSONArray()
).put(
"restApplications", fdsAdminDisplayContext.getRESTApplicationsJSONArray()
).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ const NewDataSetModalContent = ({
closeModal,
loadData,
namespace,
resolvedRESTSchemas,
restApplications,
}: {
closeModal: Function;
loadData: Function;
namespace: string;
resolvedRESTSchemas?: Array<string>;
restApplications?: Array<string>;
}) => {
const [label, setLabel] = useState('');
Expand Down Expand Up @@ -188,7 +190,10 @@ const NewDataSetModalContent = ({
return true;
};

const getRESTSchemas = async (restApplication: string) => {
const getRESTSchemas = async (
restApplication: string,
resolvedRESTSchemas: Array<string> = []
) => {
if (!restApplication) {
return;
}
Expand All @@ -212,7 +217,10 @@ const NewDataSetModalContent = ({

schemaNames.forEach((schemaName) => {
paths.forEach((path: string) => {
if (!isPathValid(path, ALLOWED_ENDPOINTS_PARAMETERS)) {
if (
!isPathValid(path, ALLOWED_ENDPOINTS_PARAMETERS) &&
!resolvedRESTSchemas.includes(schemaName)
) {
return;
}

Expand Down Expand Up @@ -324,7 +332,7 @@ const NewDataSetModalContent = ({

setRequiredRESTApplicationValidationError(false);

getRESTSchemas(item);
getRESTSchemas(item, resolvedRESTSchemas);
}}
restApplications={restApplications!}
/>
Expand Down Expand Up @@ -529,11 +537,13 @@ const DataSets = ({
editDataSetURL,
namespace,
permissionsURL,
resolvedRESTSchemas,
restApplications,
}: {
editDataSetURL: string;
namespace: string;
permissionsURL: string;
resolvedRESTSchemas: Array<string>,
restApplications: Array<string>;
}) => {
const creationMenu = {
Expand All @@ -551,6 +561,7 @@ const DataSets = ({
closeModal={closeModal}
loadData={loadData}
namespace={namespace}
resolvedRESTSchemas={resolvedRESTSchemas}
restApplications={restApplications}
/>
),
Expand Down
Loading