Skip to content

Commit

Permalink
Forms-17238 Fix link component to support spaces in asset path (#1478)
Browse files Browse the repository at this point in the history
* Forms-17238 Fix link component to support spaces in asset path

* Forms-17238 Fix link component to support spaces in asset path

* Forms-17238 Fix link component to support spaces in asset path
  • Loading branch information
girotraapankaj authored Nov 19, 2024
1 parent aeed3f4 commit 78b9db7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public String getAssetPathWithQueryParams() {
return "#";
}
try {
URIBuilder uriBuilder = null;
uriBuilder = new URIBuilder(url);
URIBuilder uriBuilder = new URIBuilder(url);
Map<String, String> queryParams = getQueryParams();
if (queryParams != null && !uriBuilder.isPathEmpty()) {
for (String key : queryParams.keySet()) {
Expand All @@ -118,7 +117,7 @@ public String getAssetPathWithQueryParams() {
}
url = uriBuilder.build().toString();
} catch (URISyntaxException e) {
logger.error("[FORMS] Link Component Failed to parse assetPath {}", url, e);
logger.warn("The [Forms] link component failed to process the asset path {}. Parameters will not be added to the URL.", url, e);
}
return url;
}
Expand Down Expand Up @@ -199,16 +198,28 @@ public Boolean accepts(LinkImpl link) {
@Override
public String processLink(LinkImpl link, SlingHttpServletRequest request) {
String givenPath = link.getAssetPath();
String builtPath = givenPath + "/" + JcrConstants.JCR_CONTENT;
String encodedPath = encodePath(givenPath);
String builtPath = encodedPath + "/" + JcrConstants.JCR_CONTENT;
ResourceResolver resourceResolver = request.getResourceResolver();
if (resourceResolver.getResource(builtPath) != null) {
if (resourceResolver.getResource(givenPath + "/" + JcrConstants.JCR_CONTENT) != null) {
Map<String, String> params = link.getQueryParams();
if (AssetType.ADAPTIVE_FORM.equals(link.getAssetType()) && !params.containsKey(QP_AF_DEFAULT_MODE_KEY)) {
builtPath += "?" + QP_AF_DEFAULT_MODE_KEY + "=" + QP_AF_DEFAULT_MODE_VALUE;
}
givenPath = builtPath;
encodedPath = builtPath;
}
return encodedPath;
}

private String encodePath(String path) {
try {
// Encode path to allow space in asset names using URIBuilder#setPath method
URIBuilder uriBuilder = new URIBuilder().setPath(path);
return uriBuilder.build().toString();
} catch (URISyntaxException e) {
logger.warn("The [Forms] link component failed to process the asset path {} due to invalid path.", path, e);
return path;
}
return givenPath;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class LinkImplTest {
private static final String LINK1_PATH = ROOT_PAGE + "/linkcomponent-v1";
private static final String LINK1_PATH_WITH_INVALID_LINK = ROOT_PAGE + "/linkcomponent-v1-invalidref";

private static final String LINK2_PATH_WITH_SPACE_IN_ASSET_NAME = ROOT_PAGE + "/linkcomponent-v2-with-space-in-asset-name";

@BeforeEach
public void setUp() {
context.load().json(TEST_BASE + FormsCoreComponentTestContext.TEST_CONTENT_JSON, CONTENT_ROOT);
Expand Down Expand Up @@ -82,12 +84,22 @@ public void testLinkComponent() {
public void testLinkComponentWithInvalidPath() {
Link link = getLinkUnderTest(LINK1_PATH_WITH_INVALID_LINK);
Assertions.assertEquals("https://www.adobe.com/", link.getAssetPath());
Assertions.assertEquals("https://www.adobe.com/?hello", link.getAssetPathWithQueryParams());
Assertions.assertEquals("/https://www.adobe.com/?hello", link.getAssetPathWithQueryParams());
Assertions.assertEquals("Link Component", link.getTitle());
Assertions.assertEquals("Some Hover Tooltip Text", link.getTooltip());
Assertions.assertEquals(Link.AssetType.ADAPTIVE_FORM, link.getAssetType());
}

@Test
public void testLinkComponentWithSpaceInAssetPath() {
Link link = getLinkUnderTest(LINK2_PATH_WITH_SPACE_IN_ASSET_NAME);
Assertions.assertEquals("/content/dam/formsanddocuments/sample form", link.getAssetPath());
Assertions.assertEquals("/content/dam/formsanddocuments/sample%20form?hello=world", link.getAssetPathWithQueryParams());
Assertions.assertEquals("Link Component", link.getTitle());
Assertions.assertEquals("Some Hover Tooltip Text", link.getTooltip());
Assertions.assertEquals(Link.AssetType.PDF, link.getAssetType());
}

@Test
public void testMainInterface() {
Link linkMock = Mockito.mock(Link.class);
Expand Down
17 changes: 16 additions & 1 deletion bundles/core/src/test/resources/link/test-content.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@
"title" : "Link Component",
"sling:resourceType" : "core/fd/components/formsportal/link/v2/link",
"assetType" : "Others"
},
"linkcomponent-v2-with-space-in-asset-name":{
"tooltip" : "Some Hover Tooltip Text",
"pdfPath" : "/content/dam/formsanddocuments/sample form",
"title" : "Link Component",
"sling:resourceType" : "core/fd/components/formsportal/link/v2/link",
"assetType" : "PDF",
"queryParams":{
"jcr:primaryType" : "nt:unstructured",
"item0":{
"jcr:primaryType" : "nt:unstructured",
"key" : "hello",
"value" : "world"
}
}
}
},
"dam" : {
Expand All @@ -128,4 +143,4 @@
}
}
}
}
}

0 comments on commit 78b9db7

Please sign in to comment.