Skip to content

Commit

Permalink
Make virtual hosts case-insensitive (fixes #115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Aug 26, 2022
1 parent bdac395 commit 4ab6567
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies {

tasks {
runServer {
minecraftVersion("1.19")
minecraftVersion("1.19.2")
}
shadowJar {
commonRelocation("org.slf4j")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@
*/
package xyz.jpenilla.minimotd.common.config;

import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationOptions;
Expand All @@ -34,6 +39,7 @@
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializerCollection;

@DefaultQualifier(NonNull.class)
public final class ConfigLoader<C> {
private static final TypeSerializerCollection SERIALIZERS;

Expand All @@ -45,12 +51,14 @@ public final class ConfigLoader<C> {

private final HoconConfigurationLoader loader;
private final ObjectMapper<C> mapper;
private final List<Consumer<C>> postProcess;

public ConfigLoader(
final @NonNull Class<C> configClass,
final @NonNull Path configPath,
final @NonNull UnaryOperator<ConfigurationOptions> optionsModifier
final Class<C> configClass,
final Path configPath,
final UnaryOperator<ConfigurationOptions> optionsModifier
) {
this.postProcess = this.findPostProcessors(configClass);
this.loader = HoconConfigurationLoader.builder()
.path(configPath)
.defaultOptions(options ->
Expand All @@ -67,6 +75,24 @@ public ConfigLoader(
}
}

private List<Consumer<C>> findPostProcessors(final Class<C> configClass) {
final List<Consumer<C>> ret = new ArrayList<>();
for (final Method method : configClass.getDeclaredMethods()) {
if (method.getAnnotation(PostProcessor.class) == null) {
continue;
}
ret.add(config -> {
try {
method.setAccessible(true);
method.invoke(config);
} catch (final ReflectiveOperationException ex) {
throw new RuntimeException("Failed to invoke post processor method", ex);
}
});
}
return ret;
}

public ConfigLoader(
final @NonNull Class<C> configClass,
final @NonNull Path configPath
Expand All @@ -76,7 +102,11 @@ public ConfigLoader(

public @NonNull C load() throws ConfigurateException {
final CommentedConfigurationNode node = this.loader.load();
return this.mapper.load(node);
final C config = this.mapper.load(node);
for (final Consumer<C> processor : this.postProcess) {
processor.accept(config);
}
return config;
}

public void save(final @NonNull C config) throws ConfigurateException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package xyz.jpenilla.minimotd.common.config;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand Down Expand Up @@ -64,7 +65,7 @@ public boolean virtualHostTestMode() {
}

public @Nullable String findConfigStringForHost(final @NonNull String host) {
return this.virtualHostConfigs.get(host);
return this.virtualHostConfigs.get(host.toLowerCase(Locale.ENGLISH));
}
}

Expand All @@ -76,4 +77,10 @@ public boolean updateChecker() {
return this.updateChecker;
}

@PostProcessor
private void lowercaseVirtualHosts() {
final Map<String, String> virtualHosts = new HashMap<>(this.proxySettings.virtualHostConfigs);
this.proxySettings.virtualHostConfigs.clear();
virtualHosts.forEach((host, config) -> this.proxySettings.virtualHostConfigs.put(host.toLowerCase(Locale.ENGLISH), config));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This file is part of MiniMOTD, licensed under the MIT License.
*
* Copyright (c) 2020-2022 Jason Penilla
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package xyz.jpenilla.minimotd.common.config;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PostProcessor {
}

0 comments on commit 4ab6567

Please sign in to comment.