-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from mcruzdev/issue-77
Add support for redirect with aliases
- Loading branch information
Showing
19 changed files
with
366 additions
and
48 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
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
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
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,63 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.quarkiverse.roq</groupId> | ||
<artifactId>quarkus-roq-plugin-aliases-parent</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>quarkus-roq-plugin-aliases-deployment</artifactId> | ||
<name>Quarkus Roq - Plugin - Aliases - Deployment</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.quarkiverse.roq</groupId> | ||
<artifactId>quarkus-roq-frontmatter-deployment</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkiverse.roq</groupId> | ||
<artifactId>quarkus-roq-plugin-aliases</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-junit5-internal</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-rest</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.rest-assured</groupId> | ||
<artifactId>rest-assured</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>${assertj.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-extension-processor</artifactId> | ||
<version>${quarkus.version}</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
99 changes: 99 additions & 0 deletions
99
...src/main/java/io/quarkiverse/roq/plugin/aliases/deployment/RoqPluginAliasesProcessor.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,99 @@ | ||
package io.quarkiverse.roq.plugin.aliases.deployment; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import io.quarkiverse.roq.frontmatter.deployment.data.RoqFrontMatterTemplateBuildItem; | ||
import io.quarkiverse.roq.frontmatter.runtime.model.RoqUrl; | ||
import io.quarkiverse.roq.generator.deployment.items.SelectedPathBuildItem; | ||
import io.quarkiverse.roq.plugin.aliases.deployment.items.RoqFrontMatterAliasesBuildItem; | ||
import io.quarkiverse.roq.plugin.aliases.runtime.RoqFrontMatterAliasesRecorder; | ||
import io.quarkiverse.roq.util.PathUtils; | ||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.annotations.ExecutionTime; | ||
import io.quarkus.deployment.annotations.Record; | ||
import io.quarkus.deployment.builditem.FeatureBuildItem; | ||
import io.quarkus.vertx.http.deployment.RouteBuildItem; | ||
import io.quarkus.vertx.http.deployment.devmode.NotFoundPageDisplayableEndpointBuildItem; | ||
import io.vertx.core.json.JsonArray; | ||
import io.vertx.core.json.JsonObject; | ||
|
||
public class RoqPluginAliasesProcessor { | ||
|
||
private static final String FEATURE = "roq-plugin-aliases"; | ||
private static final String ALIASES_KEY = "aliases"; | ||
|
||
@BuildStep | ||
FeatureBuildItem feature() { | ||
return new FeatureBuildItem(FEATURE); | ||
} | ||
|
||
@BuildStep | ||
public void consumeTemplates(List<RoqFrontMatterTemplateBuildItem> templates, | ||
BuildProducer<RoqFrontMatterAliasesBuildItem> aliasesProducer, | ||
BuildProducer<SelectedPathBuildItem> selectedPathsProducer, | ||
BuildProducer<NotFoundPageDisplayableEndpointBuildItem> notFoundPageDisplayableEndpointProducer) { | ||
|
||
if (templates.isEmpty()) { | ||
return; | ||
} | ||
|
||
HashMap<String, String> aliasMap = new HashMap<>(); | ||
for (RoqFrontMatterTemplateBuildItem item : templates) { | ||
|
||
if (!item.published()) { | ||
continue; | ||
} | ||
|
||
Set<String> aliasesName = getAliases(item.data()); | ||
if (aliasesName.isEmpty()) { | ||
continue; | ||
} | ||
RoqUrl url = item.url(); | ||
for (String alias : aliasesName) { | ||
aliasMap.put(alias, url.path()); | ||
} | ||
} | ||
|
||
for (Map.Entry<String, String> alias : aliasMap.entrySet()) { | ||
aliasesProducer.produce(new RoqFrontMatterAliasesBuildItem(alias.getKey(), alias.getValue())); | ||
selectedPathsProducer.produce(new SelectedPathBuildItem( | ||
alias.getKey(), null)); | ||
notFoundPageDisplayableEndpointProducer.produce( | ||
new NotFoundPageDisplayableEndpointBuildItem(alias.getKey(), | ||
"Roq URL alias for " + alias.getValue() + " URL.")); | ||
} | ||
} | ||
|
||
@BuildStep | ||
@Record(ExecutionTime.RUNTIME_INIT) | ||
public void createVertxRedirects(RoqFrontMatterAliasesRecorder recorder, | ||
BuildProducer<RouteBuildItem> routes, | ||
List<RoqFrontMatterAliasesBuildItem> aliases) { | ||
for (RoqFrontMatterAliasesBuildItem item : aliases) { | ||
routes.produce(RouteBuildItem.builder() | ||
.route(PathUtils.prefixWithSlash(item.alias())) | ||
.handler(recorder.addRedirect(item.target())) | ||
.build()); | ||
} | ||
} | ||
|
||
private Set<String> getAliases(JsonObject json) { | ||
JsonArray array = json.getJsonArray(ALIASES_KEY); | ||
if (array == null) { | ||
return Set.of(); | ||
} | ||
Set<String> aliases = new HashSet<>(); | ||
for (int i = 0; i < array.size(); i++) { | ||
String alias = array.getString(i); | ||
if (!alias.isBlank()) { | ||
aliases.add(alias); | ||
} | ||
} | ||
return aliases; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...va/io/quarkiverse/roq/plugin/aliases/deployment/items/RoqFrontMatterAliasesBuildItem.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,29 @@ | ||
package io.quarkiverse.roq.plugin.aliases.deployment.items; | ||
|
||
import io.quarkus.builder.item.MultiBuildItem; | ||
|
||
public final class RoqFrontMatterAliasesBuildItem extends MultiBuildItem { | ||
|
||
/** | ||
* Represents an alias of a determined link. | ||
*/ | ||
private final String alias; | ||
|
||
/** | ||
* The link where the {@code aliases} are pointing to. | ||
*/ | ||
private final String target; | ||
|
||
public RoqFrontMatterAliasesBuildItem(String alias, String target) { | ||
this.alias = alias; | ||
this.target = target; | ||
} | ||
|
||
public String alias() { | ||
return alias; | ||
} | ||
|
||
public String target() { | ||
return target; | ||
} | ||
} |
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,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>io.quarkiverse.roq</groupId> | ||
<artifactId>quarkus-roq-plugin-parent</artifactId> | ||
<version>999-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<artifactId>quarkus-roq-plugin-aliases-parent</artifactId> | ||
<packaging>pom</packaging> | ||
<name>Quarkus Roq - Plugin - Aliases</name> | ||
|
||
<modules> | ||
<module>deployment</module> | ||
<module>runtime</module> | ||
</modules> | ||
</project> |
Oops, something went wrong.