Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Sep 2, 2022
2 parents 94d7c95 + edbca0b commit 269c7ff
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
9 changes: 8 additions & 1 deletion changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@
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="1.8.0" date="not released">
<release version="1.9.0" date="2022-09-02">
<action type="add" dev="sseifert"><![CDATA[
Add UrlHandler.applySiteUrlAutoDetection method which allows to apply the Site URL auto-detection via <code>&lt;auto&gt;</code> placeholder also
for custom code that reads the Site URLs from Site Config directly.
]]></action>
</release>

<release version="1.8.0" date="2022-08-25">
<action type="add" dev="sseifert"><![CDATA[
Placeholder <code>&lt;auto&gt;</code> can be used in the author/publish URL properties in Site Config to enable auto-detection of these URLs based on the requested URL.
The placeholder can be combined with a fallback URL - if auto-detection is not possible (because the link is build outside request context) the configured URL is used.
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>io.wcm</groupId>
<artifactId>io.wcm.handler.url</artifactId>
<version>1.8.0</version>
<version>1.9.0</version>
<packaging>jar</packaging>

<name>URL Handler</name>
Expand All @@ -49,7 +49,7 @@
<site.url.module.prefix>handler/url</site.url.module.prefix>

<!-- Enable reproducible builds -->
<project.build.outputTimestamp>2022-08-25T08:09:38Z</project.build.outputTimestamp>
<project.build.outputTimestamp>2022-09-02T07:59:22Z</project.build.outputTimestamp>
</properties>

<dependencies>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/wcm/handler/url/UrlHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,21 @@ public interface UrlHandler {
*/
boolean isExternalized(@NotNull String url);

/**
* Applies auto-detection of Site URL (author or publish instance) for given Site URL that is
* configured in {@link SiteConfig}.
* <p>
* If this Site URL contains an <code>&lt;auto&gt;</code> placeholder the Site URL detection is enabled
* and the Site URL is replaced with the current hostname (if possible). Otherwise the remaining part of the
* Site URL string is returned as fallback.
* </p>
* <p>
* Site URL auto-detection does only work in context of a request - outside request context the placeholders
* is removed and the remaining string returned as fallback.
* </p>
* @param siteUrl Site URL (author or publish) {@link SiteConfig}.
* @return Automatic detected Site URL or fallback
*/
String applySiteUrlAutoDetection(@Nullable String siteUrl);

}
6 changes: 6 additions & 0 deletions src/main/java/io/wcm/handler/url/impl/UrlHandlerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import com.day.cq.wcm.api.Page;

Expand Down Expand Up @@ -346,4 +347,9 @@ String setFragment(String url, String fragment) {
return urlBuilder.toString();
}

@Override
public String applySiteUrlAutoDetection(@Nullable String siteUrl) {
return UrlPrefix.applyAutoDetection(siteUrl, self);
}

}
2 changes: 1 addition & 1 deletion src/main/java/io/wcm/handler/url/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
/**
* URL Handler API.
*/
@org.osgi.annotation.versioning.Version("1.2.1")
@org.osgi.annotation.versioning.Version("1.3.0")
package io.wcm.handler.url;
8 changes: 8 additions & 0 deletions src/test/java/io/wcm/handler/url/impl/UrlPrefixTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import io.wcm.handler.url.UrlHandler;
import io.wcm.handler.url.testcontext.AppAemContext;
import io.wcm.sling.commons.adapter.AdaptTo;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;

Expand Down Expand Up @@ -121,6 +123,12 @@ void testRequestUrlPlaceholder() {
assertEquals("http://servername:8080", applyAutoDetection("<auto>https://myhost", context.request()));
}

@Test
void testRequestUrlPlaceholder_URLHandler() {
UrlHandler urlHandler = AdaptTo.notNull(context.request(), UrlHandler.class);
assertEquals("http://servername:8080", urlHandler.applySiteUrlAutoDetection("<auto>https://myhost"));
}

@Test
void testRequestPlaceholder_AEMaaCS_author() {
context.request().setHeader(HTTP_HEADER_X_FORWARDED_HOST, "aemaacs-author");
Expand Down

0 comments on commit 269c7ff

Please sign in to comment.