Skip to content

Commit 8b29fe1

Browse files
fix(dotcli): Fix error when pulling folder with parentheses (#32258)
### **Problem** The code was getting the folder as an asset when it had parentheses or a special character on the name, because it was trying to do a replace on the name with the `replaceFirst` function, which handles the first argument as a regex, so as the name has a special character (in this scenario parentheses), it was recognizing it as a regex which led to be treated as an asset, and finally throwing `Asset not found error` ### **Fix** `Pattern.quote` was added to handle the string as literal
1 parent 53783fb commit 8b29fe1

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

dotCMS/src/main/java/com/dotcms/rest/api/v1/asset/AssetPathResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.net.URLDecoder;
2323
import java.nio.charset.StandardCharsets;
2424
import java.util.Optional;
25+
import java.util.regex.Pattern;
2526

2627
/**
2728
* This class is responsible for resolving a path to an asset. The path can be a folder or an asset.
@@ -237,7 +238,7 @@ Optional<String> asset(final Folder resolvedFolder, final String rawResource) {
237238
if(folderPath.endsWith(FORWARD_SLASH)){
238239
folderPath = folderPath.replaceAll(".$","");
239240
}
240-
resource = resource.replaceFirst(folderPath, BLANK);
241+
resource = resource.replaceFirst(Pattern.quote(folderPath), BLANK);
241242
resource = resource.replace(FORWARD_SLASH, BLANK);
242243
}
243244
return UtilMethods.isNotSet(resource) ? Optional.empty() : Optional.of(resource);

dotcms-integration/src/test/java/com/dotcms/rest/api/v1/asset/AssetPathResolverImplIntegrationTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,5 +331,52 @@ public void Test_Parse_Asset_Path_Create_Missing_Folder() throws DotDataExceptio
331331
assertNotNull(folderByPathAfter.getIdentifier());
332332
}
333333

334+
/**
335+
* Given scenario: A folder with parentheses in the name
336+
* Expected: Should resolve the resource without any exceptions and return the folder as folder and not as an asset
337+
*
338+
* @throws DotDataException
339+
*/
340+
@Test
341+
public void Test_Resolve_Folder_With_Parenthesis() throws DotDataException, DotSecurityException {
342+
Folder folder = null;
343+
try {
344+
folder = new FolderDataGen().site(host).name("(testFolder)").nextPersisted();
345+
final String url = String.format("http://%s/(testFolder)/", host.getHostname());
346+
347+
final ResolvedAssetAndPath parse = AssetPathResolver.newInstance()
348+
.resolve(url, APILocator.systemUser());
349+
assertEquals("/(testFolder)/", parse.path());
350+
assertNull(parse.asset());
351+
} finally {
352+
FolderDataGen.remove(folder);
353+
}
354+
355+
356+
}
357+
358+
/**
359+
* Given scenario: A folder with parentheses in the name and an asset
360+
* Expected: Should resolve the resource without any exceptions and return the folder as folder and the asset as an asset
361+
*
362+
* @throws DotDataException
363+
*/
364+
@Test
365+
public void Test_Resolve_Asset_In_Folder_With_Parenthesis() throws DotDataException, DotSecurityException {
366+
Folder folder = null;
367+
try {
368+
folder = new FolderDataGen().site(host).name("(testFolder)").nextPersisted();
369+
final String url = String.format("http://%s/(testFolder)/example.txt", host.getHostname());
370+
371+
final ResolvedAssetAndPath parse = AssetPathResolver.newInstance()
372+
.resolve(url, APILocator.systemUser());
373+
assertEquals("/(testFolder)/example.txt", parse.path());
374+
assertNotNull(parse.asset());
375+
assertEquals("example.txt", parse.asset());
376+
} finally {
377+
FolderDataGen.remove(folder);
378+
}
379+
}
380+
334381

335382
}

0 commit comments

Comments
 (0)