-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add link support to QuteWebTemplateBuildItem
- Loading branch information
Showing
9 changed files
with
199 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
deployment/src/main/java/io/quarkiverse/qute/web/deployment/QuteWebTemplateBuildItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.quarkiverse.qute.web.deployment; | ||
|
||
import static io.quarkiverse.qute.web.runtime.PathUtils.removeExtension; | ||
import static io.quarkiverse.qute.web.runtime.PathUtils.removeLeadingSlash; | ||
import static io.quarkiverse.qute.web.runtime.PathUtils.removeTrailingSlash; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
public final class QuteWebTemplateBuildItem extends MultiBuildItem { | ||
|
||
/** | ||
* templatePath is used also as path if link is null | ||
*/ | ||
private final String templatePath; | ||
|
||
/** | ||
* The link to use for this template | ||
*/ | ||
private final String link; | ||
|
||
public QuteWebTemplateBuildItem(String templatePath, String link) { | ||
this.templatePath = removeExtension(templatePath); | ||
this.link = normalizeLink(link); | ||
} | ||
|
||
public String templatePath() { | ||
return templatePath; | ||
} | ||
|
||
public String link() { | ||
return link; | ||
} | ||
|
||
public boolean hasLink() { | ||
return link != null; | ||
} | ||
|
||
private static String normalizeLink(String link) { | ||
if (link == null) { | ||
return null; | ||
} | ||
return removeTrailingSlash(removeLeadingSlash(link)); | ||
} | ||
|
||
} |
17 changes: 0 additions & 17 deletions
17
...oyment/src/main/java/io/quarkiverse/qute/web/deployment/QuteWebTemplatePathBuildItem.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
deployment/src/test/java/io/quarkiverse/qute/web/test/LinkedTemplateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.quarkiverse.qute.web.test; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.containsString; | ||
|
||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkiverse.qute.web.deployment.QuteWebTemplateBuildItem; | ||
import io.quarkus.builder.BuildContext; | ||
import io.quarkus.builder.BuildStep; | ||
import io.quarkus.builder.BuildStepBuilder; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class LinkedTemplateTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest().withApplicationRoot(root -> { | ||
root.addAsResource(new StringAsset( | ||
"Hello {name ?: 'world'}!"), | ||
"templates/pub/hello.txt"); | ||
}).addBuildChainCustomizer(buildChainBuilder -> { | ||
final BuildStepBuilder stepBuilder = buildChainBuilder.addBuildStep(new BuildStep() { | ||
@Override | ||
public void execute(BuildContext context) { | ||
context.produce(new QuteWebTemplateBuildItem("pub/hello.txt", "/foo/bar")); | ||
context.produce(new QuteWebTemplateBuildItem("pub/hello.txt", "/")); | ||
} | ||
}); | ||
stepBuilder.produces(QuteWebTemplateBuildItem.class).build(); | ||
}); | ||
|
||
@Test | ||
public void testFixedLink() { | ||
given() | ||
.when().get("/hello") | ||
.then() | ||
.statusCode(200) | ||
.body(containsString("Hello world!")); | ||
given() | ||
.when().get("/foo/bar") | ||
.then() | ||
.statusCode(200) | ||
.body(containsString("Hello world!")); | ||
given() | ||
.when().get("/") | ||
.then() | ||
.statusCode(200) | ||
.body(containsString("Hello world!")); | ||
|
||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
runtime/src/main/java/io/quarkiverse/qute/web/runtime/PathUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.quarkiverse.qute.web.runtime; | ||
|
||
public final class PathUtils { | ||
|
||
public static String toUnixPath(String path) { | ||
return path.replaceAll("\\\\", "/"); | ||
} | ||
|
||
public static String prefixWithSlash(String path) { | ||
return path.startsWith("/") ? path : "/" + path; | ||
} | ||
|
||
public static String surroundWithSlashes(String path) { | ||
return prefixWithSlash(addTrailingSlash(path)); | ||
} | ||
|
||
public static String addTrailingSlash(String path) { | ||
return path.endsWith("/") ? path : path + "/"; | ||
} | ||
|
||
public static String join(String path1, String path2) { | ||
return addTrailingSlash(path1) + removeLeadingSlash(path2); | ||
} | ||
|
||
public static String removeLeadingSlash(String path) { | ||
return path.startsWith("/") ? path.substring(1) : path; | ||
} | ||
|
||
public static String removeTrailingSlash(String path) { | ||
return path.endsWith("/") ? path.substring(0, path.length() - 1) : path; | ||
} | ||
|
||
public static String removeExtension(String path) { | ||
final int i = path.lastIndexOf("."); | ||
return i > 0 ? path.substring(0, i) : path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.