Skip to content

Commit

Permalink
resolves #12: Add option to build links with vanity paths included (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: Roy Teeuwen <[email protected]>
Co-authored-by: Stefan Seifert <[email protected]>
  • Loading branch information
3 people authored Mar 18, 2024
1 parent 3ad8aa0 commit e663454
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 2 deletions.
6 changes: 6 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd">
<body>

<release version="2.1.0" date="not released">
<action type="add" dev="royteeuwen" issue="13">
Add optional support for using vanity paths when building link URLs to AEM pages.
</action>
</release>

<release version="2.0.0" date="2024-01-26">
<action type="update" dev="sseifert"><![CDATA[
Version 2.0.0 contains minor breaking API changes, see <a href=https://wcm-io.atlassian.net/wiki/x/AYBxsw">Migrate from wcm.io Handler 1.x to 2.x</a> for details.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.handler.url</artifactId>
<version>2.0.0</version>
<version>2.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/wcm/handler/link/LinkArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.Map;

import io.wcm.handler.url.VanityMode;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
Expand All @@ -43,6 +44,7 @@
public final class LinkArgs implements Cloneable {

private UrlMode urlMode;
private VanityMode vanityMode;
private boolean dummyLink;
private String dummyLinkUrl;
private String selectors;
Expand All @@ -64,6 +66,13 @@ public final class LinkArgs implements Cloneable {
return this.urlMode;
}

/**
* @return Vanity mode for building the URL
*/
public @Nullable VanityMode getVanityMode() {
return this.vanityMode;
}

/**
* @param value URL mode for externalizing the URL
* @return this
Expand All @@ -73,6 +82,15 @@ public final class LinkArgs implements Cloneable {
return this;
}

/**
* @param value Vanity mode for building the URL
* @return this
*/
public @NotNull LinkArgs vanityMode(@Nullable VanityMode value) {
this.vanityMode = value;
return this;
}

/**
* @return If set to true, link handler returns a dummy link in edit mode when link is invalid.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/wcm/handler/link/LinkBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package io.wcm.handler.link;

import io.wcm.handler.url.VanityMode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ProviderType;
Expand Down Expand Up @@ -108,6 +109,15 @@ public interface LinkBuilder {
@NotNull
LinkBuilder urlMode(@Nullable UrlMode urlMode);


/**
* Set Vanity mode for building the URL
* @param vanityMode Vanity mode. If null, default Vanity mode is used.
* @return Link builder
*/
@NotNull
LinkBuilder vanityMode(@Nullable VanityMode vanityMode);

/**
* @param value If set to true, link handler returns a dummy link in edit mode when link is invalid.
* @return this
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/wcm/handler/link/impl/LinkBuilderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.wcm.handler.link.LinkNameConstants;
import io.wcm.handler.link.LinkRequest;
import io.wcm.handler.url.UrlMode;
import io.wcm.handler.url.VanityMode;
import io.wcm.wcm.commons.component.ComponentPropertyResolverFactory;

/**
Expand Down Expand Up @@ -145,6 +146,12 @@ private static String getWindowTargetFromResource(@NotNull Resource resource, @N
return this;
}

@Override
public @NotNull LinkBuilder vanityMode(@Nullable VanityMode value) {
this.linkArgs.vanityMode(value);
return this;
}

@Override
public @NotNull LinkBuilder dummyLink(boolean value) {
this.linkArgs.dummyLink(value);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/wcm/handler/link/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
/**
* Link Handler API.
*/
@org.osgi.annotation.versioning.Version("2.0.0")
@org.osgi.annotation.versioning.Version("2.1.0")
package io.wcm.handler.link;
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public boolean acceptPage(@Nullable Page page, @NotNull InternalLinkResolverOpti
.queryString(queryString)
.fragment(fragment)
.urlMode(linkArgs.getUrlMode())
.vanityMode(linkArgs.getVanityMode())
.disableSuffixSelector(linkArgs.isDisableSuffixSelector())
.buildExternalLinkUrl(targetPage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@
import com.day.cq.wcm.api.Page;

import io.wcm.handler.link.Link;
import io.wcm.handler.link.LinkBuilder;
import io.wcm.handler.link.LinkHandler;
import io.wcm.handler.link.LinkNameConstants;
import io.wcm.handler.link.SyntheticLinkResource;
import io.wcm.handler.link.testcontext.AppAemContext;
import io.wcm.handler.link.testcontext.DummyAppTemplate;
import io.wcm.handler.url.VanityMode;
import io.wcm.sling.commons.adapter.AdaptTo;
import io.wcm.sling.commons.resource.ImmutableValueMap;
import io.wcm.testing.mock.aem.junit5.AemContext;
Expand Down Expand Up @@ -122,4 +124,24 @@ void testGetSyntheticLinkResource() {
assertEquals(expected, ImmutableValueMap.copyOf(resource.getValueMap()));
}

@Test
void testVanityMode() {
String vanityPath = "/content/unittest/de_test/brand/de/vanity-path";
targetPage = context.create().page("/content/unittest/de_test/brand/de/section/content-vanity",
DummyAppTemplate.CONTENT.getTemplatePath(),
"sling:vanityPath", vanityPath);

SyntheticLinkResource linkResource = new SyntheticLinkResource(context.resourceResolver(),
"/content/dummy-path",
ImmutableValueMap.builder()
.put(LinkNameConstants.PN_LINK_TYPE, InternalCrossContextLinkType.ID)
.put(LinkNameConstants.PN_LINK_CROSSCONTEXT_CONTENT_REF, targetPage.getPath())
.build());

LinkHandler linkHandler = AdaptTo.notNull(adaptable(), LinkHandler.class);
LinkBuilder linkBuilder = linkHandler.get(linkResource)
.vanityMode(VanityMode.ALWAYS);
assertEquals("http://www.dummysite.org/content/unittest/de_test/brand/de/vanity-path.html", linkBuilder.buildUrl());
}

}
22 changes: 22 additions & 0 deletions src/test/java/io/wcm/handler/link/type/InternalLinkTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@
import com.day.cq.wcm.api.WCMMode;

import io.wcm.handler.link.Link;
import io.wcm.handler.link.LinkBuilder;
import io.wcm.handler.link.LinkHandler;
import io.wcm.handler.link.LinkNameConstants;
import io.wcm.handler.link.SyntheticLinkResource;
import io.wcm.handler.link.testcontext.AppAemContext;
import io.wcm.handler.link.testcontext.DummyAppTemplate;
import io.wcm.handler.url.UrlModes;
import io.wcm.handler.url.VanityMode;
import io.wcm.handler.url.integrator.IntegratorModes;
import io.wcm.handler.url.integrator.IntegratorNameConstants;
import io.wcm.handler.url.integrator.IntegratorProtocol;
Expand Down Expand Up @@ -552,4 +554,24 @@ void testReferenceAutoDetection() {
assertNotNull(link.getAnchor(), "anchor");
}

@Test
void testVanityMode() {
String vanityPath = "/content/unittest/de_test/brand/de/vanity-path";
targetPage = context.create().page("/content/unittest/de_test/brand/de/section/content-vanity",
DummyAppTemplate.CONTENT.getTemplatePath(),
"sling:vanityPath", vanityPath);

SyntheticLinkResource linkResource = new SyntheticLinkResource(context.resourceResolver(),
"/content/dummy-path",
ImmutableValueMap.builder()
.put(LinkNameConstants.PN_LINK_TYPE, InternalLinkType.ID)
.put(LinkNameConstants.PN_LINK_CONTENT_REF, targetPage.getPath())
.build());

LinkHandler linkHandler = AdaptTo.notNull(adaptable(), LinkHandler.class);
LinkBuilder linkBuilder = linkHandler.get(linkResource)
.vanityMode(VanityMode.ALWAYS);
assertEquals("http://www.dummysite.org/content/unittest/de_test/brand/de/vanity-path.html", linkBuilder.buildUrl());
}

}

0 comments on commit e663454

Please sign in to comment.