You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Apache <ra...@dslextreme.com> on 2017/01/10 01:47:26 UTC

Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

This commit appears to be failing for me. The testInvalidIpAddress method is failing on the assert.

Ralph

> On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org wrote:
> 
> Repository: logging-log4j2
> Updated Branches:
>  refs/heads/master 367d26b09 -> 4254e2558
> 
> 
> [LOG4J2-1755]: Add converters and validators for hostnames/ports
> 
> 
> Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255
> Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255
> Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255
> 
> Branch: refs/heads/master
> Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
> Parents: 367d26b
> Author: Matt Sicker <ma...@spr.com>
> Authored: Fri Dec 30 15:00:17 2016 -0600
> Committer: Matt Sicker <ma...@spr.com>
> Committed: Fri Dec 30 15:01:26 2016 -0600
> 
> ----------------------------------------------------------------------
> .../config/plugins/convert/TypeConverters.java  | 13 +++-
> .../validation/constraints/ValidHost.java       | 41 +++++++++++
> .../validation/constraints/ValidPort.java       | 44 ++++++++++++
> .../validators/ValidHostValidator.java          | 58 +++++++++++++++
> .../validators/ValidPortValidator.java          | 53 ++++++++++++++
> .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
> .../validators/ValidHostValidatorTest.java      | 74 ++++++++++++++++++++
> .../validators/ValidPortValidatorTest.java      | 70 ++++++++++++++++++
> src/changes/changes.xml                         |  3 +
> 9 files changed, 401 insertions(+), 1 deletion(-)
> ----------------------------------------------------------------------
> 
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
> index 2895e52..421d711 100644
> --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
> @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.config.plugins.convert;
> import java.io.File;
> import java.math.BigDecimal;
> import java.math.BigInteger;
> +import java.net.InetAddress;
> import java.net.MalformedURLException;
> import java.net.URI;
> import java.net.URISyntaxException;
> @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
> import java.security.Provider;
> import java.security.Security;
> import java.util.regex.Pattern;
> -
> import javax.xml.bind.DatatypeConverter;
> 
> import org.apache.logging.log4j.Level;
> @@ -233,6 +233,17 @@ public final class TypeConverters {
>     }
> 
>     /**
> +     * Converts a {@link String} into an {@link InetAddress}.
> +     */
> +    @Plugin(name = "InetAddress", category = CATEGORY)
> +    public static class InetAddressConverter implements TypeConverter<InetAddress> {
> +        @Override
> +        public InetAddress convert(final String s) throws Exception {
> +            return InetAddress.getByName(s);
> +        }
> +    }
> +
> +    /**
>      * Converts a {@link String} into a {@link Integer}.
>      */
>     @Plugin(name = "Integer", category = CATEGORY)
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
> new file mode 100644
> index 0000000..c652d40
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
> @@ -0,0 +1,41 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements. See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to You under the Apache license, Version 2.0
> + * (the "License"); you may not use this file except in compliance with
> + * the License. You may obtain a copy of the License at
> + *
> + *      http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the license for the specific language governing permissions and
> + * limitations under the license.
> + */
> +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
> +
> +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
> +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidHostValidator;
> +
> +import java.lang.annotation.*;
> +import java.net.InetAddress;
> +
> +/**
> + * Indicates that a plugin attribute must be a valid host. This relies on the same validation rules as
> + * {@link InetAddress#getByName(String)}.
> + *
> + * @since 2.8
> + */
> +@Documented
> +@Retention(RetentionPolicy.RUNTIME)
> +@Target({ElementType.FIELD, ElementType.PARAMETER})
> +@Constraint(ValidHostValidator.class)
> +public @interface ValidHost {
> +
> +    /**
> +     * The message to be logged if this constraint is violated. This should normally be overridden.
> +     */
> +    String message() default "The hostname is invalid";
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
> new file mode 100644
> index 0000000..a7c68b1
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
> @@ -0,0 +1,44 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements. See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to You under the Apache license, Version 2.0
> + * (the "License"); you may not use this file except in compliance with
> + * the License. You may obtain a copy of the License at
> + *
> + *      http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the license for the specific language governing permissions and
> + * limitations under the license.
> + */
> +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
> +
> +import java.lang.annotation.Documented;
> +import java.lang.annotation.ElementType;
> +import java.lang.annotation.Retention;
> +import java.lang.annotation.RetentionPolicy;
> +import java.lang.annotation.Target;
> +
> +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
> +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidPortValidator;
> +
> +/**
> + * Indicates that a plugin attribute must be a valid port number. A valid port number is an integer between 0 and
> + * 65535.
> + *
> + * @since 2.8
> + */
> +@Documented
> +@Retention(RetentionPolicy.RUNTIME)
> +@Target({ElementType.FIELD, ElementType.PARAMETER})
> +@Constraint(ValidPortValidator.class)
> +public @interface ValidPort {
> +
> +    /**
> +     * The message to be logged if this constraint is violated. This should normally be overridden.
> +     */
> +    String message() default "The port number is invalid";
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
> new file mode 100644
> index 0000000..3669915
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
> @@ -0,0 +1,58 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements. See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to You under the Apache license, Version 2.0
> + * (the "License"); you may not use this file except in compliance with
> + * the License. You may obtain a copy of the License at
> + *
> + *      http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the license for the specific language governing permissions and
> + * limitations under the license.
> + */
> +package org.apache.logging.log4j.core.config.plugins.validation.validators;
> +
> +import org.apache.logging.log4j.Logger;
> +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
> +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
> +import org.apache.logging.log4j.status.StatusLogger;
> +
> +import java.net.InetAddress;
> +import java.net.UnknownHostException;
> +
> +/**
> + * Validator that checks an object to verify it is a valid hostname or IP address. Validation rules follow the same
> + * logic as in {@link InetAddress#getByName(String)}.
> + *
> + * @since 2.8
> + */
> +public class ValidHostValidator implements ConstraintValidator<ValidHost> {
> +
> +    private static final Logger LOGGER = StatusLogger.getLogger();
> +
> +    private ValidHost annotation;
> +
> +    @Override
> +    public void initialize(ValidHost annotation) {
> +        this.annotation = annotation;
> +    }
> +
> +    @Override
> +    public boolean isValid(String name, Object value) {
> +        if (value == null) {
> +            LOGGER.error(annotation.message());
> +            return false;
> +        }
> +        try {
> +            InetAddress.getByName(value.toString());
> +            return true;
> +        } catch (final UnknownHostException e) {
> +            LOGGER.error(annotation.message(), e);
> +            return false;
> +        }
> +    }
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
> new file mode 100644
> index 0000000..f18f8fc
> --- /dev/null
> +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
> @@ -0,0 +1,53 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements. See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to You under the Apache license, Version 2.0
> + * (the "License"); you may not use this file except in compliance with
> + * the License. You may obtain a copy of the License at
> + *
> + *      http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the license for the specific language governing permissions and
> + * limitations under the license.
> + */
> +package org.apache.logging.log4j.core.config.plugins.validation.validators;
> +
> +import org.apache.logging.log4j.Logger;
> +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
> +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
> +import org.apache.logging.log4j.status.StatusLogger;
> +
> +/**
> + * Validator that checks an object to verify it is a valid port number (an integer between 0 and 65535).
> + *
> + * @since 2.8
> + */
> +public class ValidPortValidator implements ConstraintValidator<ValidPort> {
> +
> +    private static final Logger LOGGER = StatusLogger.getLogger();
> +
> +    private ValidPort annotation;
> +
> +    @Override
> +    public void initialize(final ValidPort annotation) {
> +        this.annotation = annotation;
> +    }
> +
> +    @Override
> +    public boolean isValid(final String name, final Object value) {
> +        if (!Integer.class.isInstance(value)) {
> +            LOGGER.error(annotation.message());
> +            return false;
> +        }
> +        int port = (int) value;
> +        if (port < 0 || port > 65535) {
> +            LOGGER.error(annotation.message());
> +            return false;
> +        }
> +        return true;
> +    }
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
> new file mode 100644
> index 0000000..4f05d68
> --- /dev/null
> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
> @@ -0,0 +1,46 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements. See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to You under the Apache license, Version 2.0
> + * (the "License"); you may not use this file except in compliance with
> + * the License. You may obtain a copy of the License at
> + *
> + *      http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the license for the specific language governing permissions and
> + * limitations under the license.
> + */
> +package org.apache.logging.log4j.core.config.plugins.validation;
> +
> +import java.net.InetSocketAddress;
> +
> +import org.apache.logging.log4j.core.config.plugins.Plugin;
> +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
> +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
> +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
> +
> +@Plugin(name = "HostAndPort", category = "Test")
> +public class HostAndPort {
> +
> +    private final InetSocketAddress address;
> +
> +    private HostAndPort(final InetSocketAddress address) {
> +        this.address = address;
> +    }
> +
> +    public boolean isValid() {
> +        return !address.isUnresolved();
> +    }
> +
> +    @PluginFactory
> +    public static HostAndPort createPlugin(
> +        @ValidHost(message = "Unit test (host)") @PluginAttribute("host") final String host,
> +        @ValidPort(message = "Unit test (port)") @PluginAttribute("port") final int port) {
> +        return new HostAndPort(new InetSocketAddress(host, port));
> +    }
> +}
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
> new file mode 100644
> index 0000000..3b0480d
> --- /dev/null
> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
> @@ -0,0 +1,74 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements. See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to You under the Apache license, Version 2.0
> + * (the "License"); you may not use this file except in compliance with
> + * the License. You may obtain a copy of the License at
> + *
> + *      http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the license for the specific language governing permissions and
> + * limitations under the license.
> + */
> +package org.apache.logging.log4j.core.config.plugins.validation.validators;
> +
> +import java.util.UUID;
> +
> +import org.apache.logging.log4j.core.config.Node;
> +import org.apache.logging.log4j.core.config.NullConfiguration;
> +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
> +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
> +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
> +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
> +import org.junit.Before;
> +import org.junit.Test;
> +
> +import static org.junit.Assert.*;
> +
> +public class ValidHostValidatorTest {
> +
> +    private PluginType<HostAndPort> plugin;
> +    private Node node;
> +
> +    @SuppressWarnings("unchecked")
> +    @Before
> +    public void setUp() throws Exception {
> +        final PluginManager manager = new PluginManager("Test");
> +        manager.collectPlugins();
> +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
> +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
> +        node = new Node(null, "HostAndPort", plugin);
> +    }
> +
> +    @Test
> +    public void testNullHost() throws Exception {
> +        assertNull(buildPlugin());
> +    }
> +
> +    @Test
> +    public void testInvalidIpAddress() throws Exception {
> +        node.getAttributes().put("host", UUID.randomUUID().toString());
> +        node.getAttributes().put("port", "1");
> +        assertNull(buildPlugin());
> +    }
> +
> +    @Test
> +    public void testLocalhost() throws Exception {
> +        node.getAttributes().put("host", "localhost");
> +        node.getAttributes().put("port", "1");
> +        final HostAndPort hostAndPort = buildPlugin();
> +        assertNotNull(hostAndPort);
> +        assertTrue(hostAndPort.isValid());
> +    }
> +
> +    private HostAndPort buildPlugin() {
> +        return (HostAndPort) new PluginBuilder(plugin)
> +            .withConfiguration(new NullConfiguration())
> +            .withConfigurationNode(node)
> +            .build();
> +    }
> +}
> \ No newline at end of file
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
> ----------------------------------------------------------------------
> diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
> new file mode 100644
> index 0000000..3aab08d
> --- /dev/null
> +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
> @@ -0,0 +1,70 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements. See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to You under the Apache license, Version 2.0
> + * (the "License"); you may not use this file except in compliance with
> + * the License. You may obtain a copy of the License at
> + *
> + *      http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the license for the specific language governing permissions and
> + * limitations under the license.
> + */
> +package org.apache.logging.log4j.core.config.plugins.validation.validators;
> +
> +import org.apache.logging.log4j.core.config.Node;
> +import org.apache.logging.log4j.core.config.NullConfiguration;
> +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
> +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
> +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
> +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
> +import org.junit.Before;
> +import org.junit.Test;
> +
> +import static org.junit.Assert.*;
> +
> +public class ValidPortValidatorTest {
> +    private PluginType<HostAndPort> plugin;
> +    private Node node;
> +
> +    @SuppressWarnings("unchecked")
> +    @Before
> +    public void setUp() throws Exception {
> +        final PluginManager manager = new PluginManager("Test");
> +        manager.collectPlugins();
> +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
> +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
> +        node = new Node(null, "HostAndPort", plugin);
> +        node.getAttributes().put("host", "localhost");
> +    }
> +
> +    @Test
> +    public void testNegativePort() throws Exception {
> +        node.getAttributes().put("port", "-1");
> +        assertNull(buildPlugin());
> +    }
> +
> +    @Test
> +    public void testValidPort() throws Exception {
> +        node.getAttributes().put("port", "10");
> +        assertNotNull(buildPlugin());
> +    }
> +
> +    @Test
> +    public void testInvalidPort() throws Exception {
> +        node.getAttributes().put("port", "1234567890");
> +        assertNull(buildPlugin());
> +    }
> +
> +    private HostAndPort buildPlugin() {
> +        return (HostAndPort) new PluginBuilder(plugin)
> +            .withConfiguration(new NullConfiguration())
> +            .withConfigurationNode(node)
> +            .build();
> +    }
> +
> +}
> \ No newline at end of file
> 
> http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml
> ----------------------------------------------------------------------
> diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> index c05de09..bc08dbb 100644
> --- a/src/changes/changes.xml
> +++ b/src/changes/changes.xml
> @@ -213,6 +213,9 @@
>       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>         The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2.
>       </action>
> +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
> +        Add converters and validators related to hostnames and ports.
> +      </action>
>       <action issue="LOG4J2-969" dev="ggregory" type="add">
>         Refactor SyslogAppender so that Layout is a Plugin element.
>       </action>
> 
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Matt Sicker <bo...@gmail.com>.
Oh, I totally forgot about OpenDNS. I thought they did redirects, too.

On 9 January 2017 at 21:37, Apache <ra...@dslextreme.com> wrote:

> OK. I called Cox and they gave me the ip addresses that don’t redirect but
> I was still getting their ipv6 DNS that does. I had to set that to go to
> OpenDNS instead (see https://www.opendns.com/about/innovations/ipv6/).
> Now the test passes.
>
> Ralph
>
> On Jan 9, 2017, at 7:43 PM, Matt Sicker <bo...@gmail.com> wrote:
>
> I'm trying an invalid IP address instead. If that still doesn't work, then
> I'll add an Assume.assumeThat() to the test so it's ignored with weird DNS
> servers.
>
> A lot of people like 8.8.8.8 (Google) and 4.2.2.2 (Level3) for DNS.
> There's also 75.75.75.75 which is Comcast (or 2001:558:feed::1 which is
> their IPv6 version apparently).
>
> On 9 January 2017 at 20:39, Apache <ra...@dslextreme.com> wrote:
>
>> It appears I need to use a different DNS server for it to stop
>> redirecting me. I need to figure out which one to use.
>>
>> Ralph
>>
>> On Jan 9, 2017, at 7:34 PM, Apache <ra...@dslextreme.com> wrote:
>>
>>
>> Didn’t run the test. Don’t really have to.
>>
>> nslookup "#$%^&*(*&^%$"
>> Server: 2001:578:3f::30
>> Address: 2001:578:3f::30#53
>>
>> Non-authoritative answer:
>> Name: #\$%^&*\(*&^%\$
>> Address: 92.242.140.2
>>
>>
>> Ralph
>>
>>
>>
>> On Jan 9, 2017, at 7:31 PM, Matt Sicker <bo...@gmail.com> wrote:
>>
>> Let me know if my updated test still causes issues.
>>
>> On 9 January 2017 at 20:30, Apache <ra...@dslextreme.com> wrote:
>>
>>> I suspect this is my ISP at work.  When I enter an invalid domain I get
>>> a web site from cox.net saying it can’t find the domain and it offers
>>> suggestions for other sites.
>>>
>>> I think this test has to be removed.
>>>
>>> Ralph
>>>
>>> On Jan 9, 2017, at 7:28 PM, Matt Sicker <bo...@gmail.com> wrote:
>>>
>>> Well technically a UUID can be a valid hostname itself besides being
>>> interpreted as a hex-encoded IP address (if the rest of it gets chopped
>>> off). I've updated the test to use a jumble of invalid hostname characters
>>> instead.
>>>
>>> On 9 January 2017 at 20:26, Apache <ra...@dslextreme.com> wrote:
>>>
>>>> nslookup dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>>> Server: 2001:578:3f::30
>>>> Address: 2001:578:3f::30#53
>>>>
>>>> Non-authoritative answer:
>>>> Name: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>>> Address: 92.242.140.2
>>>>
>>>>
>>>>
>>>> On Jan 9, 2017, at 7:24 PM, Matt Sicker <bo...@gmail.com> wrote:
>>>>
>>>> I suppose technically the first part of a UUID can be an IPv4 address
>>>> encoded in hex. I'll make a better invalid value.
>>>>
>>>> On 9 January 2017 at 20:20, Apache <ra...@dslextreme.com> wrote:
>>>>
>>>>> Debugging this and InetAddress.getByName is returning an InetAddress
>>>>> object. The value is
>>>>> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>>>> Address: 92.242.140.2
>>>>>
>>>>> Ralph
>>>>>
>>>>> On Jan 9, 2017, at 7:12 PM, Matt Sicker <bo...@gmail.com> wrote:
>>>>>
>>>>> Not sure how that's possible, but I added a better assert message. Let
>>>>> me know if you're still having an issue with it. Is it a failure from
>>>>> IntelliJ or Maven?
>>>>>
>>>>> On 9 January 2017 at 19:47, Apache <ra...@dslextreme.com> wrote:
>>>>>
>>>>>> This commit appears to be failing for me. The testInvalidIpAddress
>>>>>> method is failing on the assert.
>>>>>>
>>>>>> Ralph
>>>>>>
>>>>>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org wrote:
>>>>>> >
>>>>>> > Repository: logging-log4j2
>>>>>> > Updated Branches:
>>>>>> >  refs/heads/master 367d26b09 -> 4254e2558
>>>>>> >
>>>>>> >
>>>>>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>>>>>> >
>>>>>> >
>>>>>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>>>>>> > Commit: http://git-wip-us.apache.org/r
>>>>>> epos/asf/logging-log4j2/commit/4254e255
>>>>>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4
>>>>>> 254e255
>>>>>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4
>>>>>> 254e255
>>>>>> >
>>>>>> > Branch: refs/heads/master
>>>>>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>>>>>> > Parents: 367d26b
>>>>>> > Author: Matt Sicker <ma...@spr.com>
>>>>>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>>>>>> > Committer: Matt Sicker <ma...@spr.com>
>>>>>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>>>>>> >
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>>>>>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>>>>>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>>>>>> > .../validators/ValidHostValidator.java          | 58
>>>>>> +++++++++++++++
>>>>>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>>>>>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>>>>>> > .../validators/ValidHostValidatorTest.java      | 74
>>>>>> ++++++++++++++++++++
>>>>>> > .../validators/ValidPortValidatorTest.java      | 70
>>>>>> ++++++++++++++++++
>>>>>> > src/changes/changes.xml                         |  3 +
>>>>>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> >
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>>>> re/config/plugins/convert/TypeConverters.java
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> > diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/convert/TypeConverters.java
>>>>>> > index 2895e52..421d711 100644
>>>>>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/convert/TypeConverters.java
>>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/convert/TypeConverters.java
>>>>>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.
>>>>>> config.plugins.convert;
>>>>>> > import java.io.File;
>>>>>> > import java.math.BigDecimal;
>>>>>> > import java.math.BigInteger;
>>>>>> > +import java.net.InetAddress;
>>>>>> > import java.net.MalformedURLException;
>>>>>> > import java.net.URI;
>>>>>> > import java.net.URISyntaxException;
>>>>>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>>>>>> > import java.security.Provider;
>>>>>> > import java.security.Security;
>>>>>> > import java.util.regex.Pattern;
>>>>>> > -
>>>>>> > import javax.xml.bind.DatatypeConverter;
>>>>>> >
>>>>>> > import org.apache.logging.log4j.Level;
>>>>>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>>>>>> >     }
>>>>>> >
>>>>>> >     /**
>>>>>> > +     * Converts a {@link String} into an {@link InetAddress}.
>>>>>> > +     */
>>>>>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>>>>>> > +    public static class InetAddressConverter implements
>>>>>> TypeConverter<InetAddress> {
>>>>>> > +        @Override
>>>>>> > +        public InetAddress convert(final String s) throws
>>>>>> Exception {
>>>>>> > +            return InetAddress.getByName(s);
>>>>>> > +        }
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    /**
>>>>>> >      * Converts a {@link String} into a {@link Integer}.
>>>>>> >      */
>>>>>> >     @Plugin(name = "Integer", category = CATEGORY)
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>>>> re/config/plugins/validation/constraints/ValidHost.java
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> > diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/constraints/ValidHost.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..c652d40
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/constraints/ValidHost.java
>>>>>> > @@ -0,0 +1,41 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>>> more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>>> with
>>>>>> > + * this work for additional information regarding copyright
>>>>>> ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>>> Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>>> with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>>> software
>>>>>> > + * distributed under the License is distributed on an "AS IS"
>>>>>> BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>>> implied.
>>>>>> > + * See the license for the specific language governing permissions
>>>>>> and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.constraints;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.Constraint;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.validators.ValidHostValidator;
>>>>>> > +
>>>>>> > +import java.lang.annotation.*;
>>>>>> > +import java.net.InetAddress;
>>>>>> > +
>>>>>> > +/**
>>>>>> > + * Indicates that a plugin attribute must be a valid host. This
>>>>>> relies on the same validation rules as
>>>>>> > + * {@link InetAddress#getByName(String)}.
>>>>>> > + *
>>>>>> > + * @since 2.8
>>>>>> > + */
>>>>>> > +@Documented
>>>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>>>> > +@Constraint(ValidHostValidator.class)
>>>>>> > +public @interface ValidHost {
>>>>>> > +
>>>>>> > +    /**
>>>>>> > +     * The message to be logged if this constraint is violated.
>>>>>> This should normally be overridden.
>>>>>> > +     */
>>>>>> > +    String message() default "The hostname is invalid";
>>>>>> > +}
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>>>> re/config/plugins/validation/constraints/ValidPort.java
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> > diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/constraints/ValidPort.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..a7c68b1
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/constraints/ValidPort.java
>>>>>> > @@ -0,0 +1,44 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>>> more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>>> with
>>>>>> > + * this work for additional information regarding copyright
>>>>>> ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>>> Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>>> with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>>> software
>>>>>> > + * distributed under the License is distributed on an "AS IS"
>>>>>> BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>>> implied.
>>>>>> > + * See the license for the specific language governing permissions
>>>>>> and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.constraints;
>>>>>> > +
>>>>>> > +import java.lang.annotation.Documented;
>>>>>> > +import java.lang.annotation.ElementType;
>>>>>> > +import java.lang.annotation.Retention;
>>>>>> > +import java.lang.annotation.RetentionPolicy;
>>>>>> > +import java.lang.annotation.Target;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.Constraint;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.validators.ValidPortValidator;
>>>>>> > +
>>>>>> > +/**
>>>>>> > + * Indicates that a plugin attribute must be a valid port number.
>>>>>> A valid port number is an integer between 0 and
>>>>>> > + * 65535.
>>>>>> > + *
>>>>>> > + * @since 2.8
>>>>>> > + */
>>>>>> > +@Documented
>>>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>>>> > +@Constraint(ValidPortValidator.class)
>>>>>> > +public @interface ValidPort {
>>>>>> > +
>>>>>> > +    /**
>>>>>> > +     * The message to be logged if this constraint is violated.
>>>>>> This should normally be overridden.
>>>>>> > +     */
>>>>>> > +    String message() default "The port number is invalid";
>>>>>> > +}
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>>>> re/config/plugins/validation/validators/ValidHostValidator.java
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> > diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/validators/ValidHostValidator.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..3669915
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/validators/ValidHostValidator.java
>>>>>> > @@ -0,0 +1,58 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>>> more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>>> with
>>>>>> > + * this work for additional information regarding copyright
>>>>>> ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>>> Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>>> with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>>> software
>>>>>> > + * distributed under the License is distributed on an "AS IS"
>>>>>> BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>>> implied.
>>>>>> > + * See the license for the specific language governing permissions
>>>>>> and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.validators;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.Logger;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.ConstraintValidator;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.constraints.ValidHost;
>>>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>>>> > +
>>>>>> > +import java.net.InetAddress;
>>>>>> > +import java.net.UnknownHostException;
>>>>>> > +
>>>>>> > +/**
>>>>>> > + * Validator that checks an object to verify it is a valid
>>>>>> hostname or IP address. Validation rules follow the same
>>>>>> > + * logic as in {@link InetAddress#getByName(String)}.
>>>>>> > + *
>>>>>> > + * @since 2.8
>>>>>> > + */
>>>>>> > +public class ValidHostValidator implements
>>>>>> ConstraintValidator<ValidHost> {
>>>>>> > +
>>>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>>>> > +
>>>>>> > +    private ValidHost annotation;
>>>>>> > +
>>>>>> > +    @Override
>>>>>> > +    public void initialize(ValidHost annotation) {
>>>>>> > +        this.annotation = annotation;
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Override
>>>>>> > +    public boolean isValid(String name, Object value) {
>>>>>> > +        if (value == null) {
>>>>>> > +            LOGGER.error(annotation.message());
>>>>>> > +            return false;
>>>>>> > +        }
>>>>>> > +        try {
>>>>>> > +            InetAddress.getByName(value.toString());
>>>>>> > +            return true;
>>>>>> > +        } catch (final UnknownHostException e) {
>>>>>> > +            LOGGER.error(annotation.message(), e);
>>>>>> > +            return false;
>>>>>> > +        }
>>>>>> > +    }
>>>>>> > +}
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>>>> re/config/plugins/validation/validators/ValidPortValidator.java
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> > diff --git a/log4j-core/src/main/java/org
>>>>>> /apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/validators/ValidPortValidator.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..f18f8fc
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/validators/ValidPortValidator.java
>>>>>> > @@ -0,0 +1,53 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>>> more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>>> with
>>>>>> > + * this work for additional information regarding copyright
>>>>>> ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>>> Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>>> with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>>> software
>>>>>> > + * distributed under the License is distributed on an "AS IS"
>>>>>> BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>>> implied.
>>>>>> > + * See the license for the specific language governing permissions
>>>>>> and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.validators;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.Logger;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.ConstraintValidator;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.constraints.ValidPort;
>>>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>>>> > +
>>>>>> > +/**
>>>>>> > + * Validator that checks an object to verify it is a valid port
>>>>>> number (an integer between 0 and 65535).
>>>>>> > + *
>>>>>> > + * @since 2.8
>>>>>> > + */
>>>>>> > +public class ValidPortValidator implements
>>>>>> ConstraintValidator<ValidPort> {
>>>>>> > +
>>>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>>>> > +
>>>>>> > +    private ValidPort annotation;
>>>>>> > +
>>>>>> > +    @Override
>>>>>> > +    public void initialize(final ValidPort annotation) {
>>>>>> > +        this.annotation = annotation;
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Override
>>>>>> > +    public boolean isValid(final String name, final Object value) {
>>>>>> > +        if (!Integer.class.isInstance(value)) {
>>>>>> > +            LOGGER.error(annotation.message());
>>>>>> > +            return false;
>>>>>> > +        }
>>>>>> > +        int port = (int) value;
>>>>>> > +        if (port < 0 || port > 65535) {
>>>>>> > +            LOGGER.error(annotation.message());
>>>>>> > +            return false;
>>>>>> > +        }
>>>>>> > +        return true;
>>>>>> > +    }
>>>>>> > +}
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>>>>> re/config/plugins/validation/HostAndPort.java
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> > diff --git a/log4j-core/src/test/java/org
>>>>>> /apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>>>>> b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/HostAndPort.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..4f05d68
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/HostAndPort.java
>>>>>> > @@ -0,0 +1,46 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>>> more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>>> with
>>>>>> > + * this work for additional information regarding copyright
>>>>>> ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>>> Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>>> with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>>> software
>>>>>> > + * distributed under the License is distributed on an "AS IS"
>>>>>> BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>>> implied.
>>>>>> > + * See the license for the specific language governing permissions
>>>>>> and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>>>>>> > +
>>>>>> > +import java.net.InetSocketAddress;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.PluginAttribute;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.constraints.ValidHost;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.constraints.ValidPort;
>>>>>> > +
>>>>>> > +@Plugin(name = "HostAndPort", category = "Test")
>>>>>> > +public class HostAndPort {
>>>>>> > +
>>>>>> > +    private final InetSocketAddress address;
>>>>>> > +
>>>>>> > +    private HostAndPort(final InetSocketAddress address) {
>>>>>> > +        this.address = address;
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    public boolean isValid() {
>>>>>> > +        return !address.isUnresolved();
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @PluginFactory
>>>>>> > +    public static HostAndPort createPlugin(
>>>>>> > +        @ValidHost(message = "Unit test (host)")
>>>>>> @PluginAttribute("host") final String host,
>>>>>> > +        @ValidPort(message = "Unit test (port)")
>>>>>> @PluginAttribute("port") final int port) {
>>>>>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>>>>>> > +    }
>>>>>> > +}
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>>>>> re/config/plugins/validation/validators/ValidHostValidatorTest.java
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> > diff --git a/log4j-core/src/test/java/org
>>>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>>>> ors/ValidHostValidatorTest.java b/log4j-core/src/test/java/org
>>>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>>>> ors/ValidHostValidatorTest.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..3b0480d
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/validators/ValidHostValidatorTest.java
>>>>>> > @@ -0,0 +1,74 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>>> more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>>> with
>>>>>> > + * this work for additional information regarding copyright
>>>>>> ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>>> Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>>> with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>>> software
>>>>>> > + * distributed under the License is distributed on an "AS IS"
>>>>>> BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>>> implied.
>>>>>> > + * See the license for the specific language governing permissions
>>>>>> and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.validators;
>>>>>> > +
>>>>>> > +import java.util.UUID;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.util.PluginBuilder;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.util.PluginManager;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.util.PluginType;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.HostAndPort;
>>>>>> > +import org.junit.Before;
>>>>>> > +import org.junit.Test;
>>>>>> > +
>>>>>> > +import static org.junit.Assert.*;
>>>>>> > +
>>>>>> > +public class ValidHostValidatorTest {
>>>>>> > +
>>>>>> > +    private PluginType<HostAndPort> plugin;
>>>>>> > +    private Node node;
>>>>>> > +
>>>>>> > +    @SuppressWarnings("unchecked")
>>>>>> > +    @Before
>>>>>> > +    public void setUp() throws Exception {
>>>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>>>> > +        manager.collectPlugins();
>>>>>> > +        plugin = (PluginType<HostAndPort>)
>>>>>> manager.getPluginType("HostAndPort");
>>>>>> > +        assertNotNull("Rebuild this module to ensure annotation
>>>>>> processing has been done.", plugin);
>>>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testNullHost() throws Exception {
>>>>>> > +        assertNull(buildPlugin());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testInvalidIpAddress() throws Exception {
>>>>>> > +        node.getAttributes().put("host",
>>>>>> UUID.randomUUID().toString());
>>>>>> > +        node.getAttributes().put("port", "1");
>>>>>> > +        assertNull(buildPlugin());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testLocalhost() throws Exception {
>>>>>> > +        node.getAttributes().put("host", "localhost");
>>>>>> > +        node.getAttributes().put("port", "1");
>>>>>> > +        final HostAndPort hostAndPort = buildPlugin();
>>>>>> > +        assertNotNull(hostAndPort);
>>>>>> > +        assertTrue(hostAndPort.isValid());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    private HostAndPort buildPlugin() {
>>>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>>>> > +            .withConfiguration(new NullConfiguration())
>>>>>> > +            .withConfigurationNode(node)
>>>>>> > +            .build();
>>>>>> > +    }
>>>>>> > +}
>>>>>> > \ No newline at end of file
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>>>>> re/config/plugins/validation/validators/ValidPortValidatorTest.java
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> > diff --git a/log4j-core/src/test/java/org
>>>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>>>> ors/ValidPortValidatorTest.java b/log4j-core/src/test/java/org
>>>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>>>> ors/ValidPortValidatorTest.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..3aab08d
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>>>> fig/plugins/validation/validators/ValidPortValidatorTest.java
>>>>>> > @@ -0,0 +1,70 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>>> more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>>> with
>>>>>> > + * this work for additional information regarding copyright
>>>>>> ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>>> Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>>> with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>>> software
>>>>>> > + * distributed under the License is distributed on an "AS IS"
>>>>>> BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>>> implied.
>>>>>> > + * See the license for the specific language governing permissions
>>>>>> and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.validators;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.util.PluginBuilder;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.util.PluginManager;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.util.PluginType;
>>>>>> > +import org.apache.logging.log4j.core.
>>>>>> config.plugins.validation.HostAndPort;
>>>>>> > +import org.junit.Before;
>>>>>> > +import org.junit.Test;
>>>>>> > +
>>>>>> > +import static org.junit.Assert.*;
>>>>>> > +
>>>>>> > +public class ValidPortValidatorTest {
>>>>>> > +    private PluginType<HostAndPort> plugin;
>>>>>> > +    private Node node;
>>>>>> > +
>>>>>> > +    @SuppressWarnings("unchecked")
>>>>>> > +    @Before
>>>>>> > +    public void setUp() throws Exception {
>>>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>>>> > +        manager.collectPlugins();
>>>>>> > +        plugin = (PluginType<HostAndPort>)
>>>>>> manager.getPluginType("HostAndPort");
>>>>>> > +        assertNotNull("Rebuild this module to ensure annotation
>>>>>> processing has been done.", plugin);
>>>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>>>> > +        node.getAttributes().put("host", "localhost");
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testNegativePort() throws Exception {
>>>>>> > +        node.getAttributes().put("port", "-1");
>>>>>> > +        assertNull(buildPlugin());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testValidPort() throws Exception {
>>>>>> > +        node.getAttributes().put("port", "10");
>>>>>> > +        assertNotNull(buildPlugin());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testInvalidPort() throws Exception {
>>>>>> > +        node.getAttributes().put("port", "1234567890");
>>>>>> > +        assertNull(buildPlugin());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    private HostAndPort buildPlugin() {
>>>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>>>> > +            .withConfiguration(new NullConfiguration())
>>>>>> > +            .withConfigurationNode(node)
>>>>>> > +            .build();
>>>>>> > +    }
>>>>>> > +
>>>>>> > +}
>>>>>> > \ No newline at end of file
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>>> 254e255/src/changes/changes.xml
>>>>>> > ------------------------------------------------------------
>>>>>> ----------
>>>>>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>>>> > index c05de09..bc08dbb 100644
>>>>>> > --- a/src/changes/changes.xml
>>>>>> > +++ b/src/changes/changes.xml
>>>>>> > @@ -213,6 +213,9 @@
>>>>>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>>>>>> >         The log4j-slf4j-impl module now declares a runtime
>>>>>> dependency on log4j-core. While not technically required, this makes the
>>>>>> log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates
>>>>>> migration to Log4j 2.
>>>>>> >       </action>
>>>>>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>>>>>> > +        Add converters and validators related to hostnames and
>>>>>> ports.
>>>>>> > +      </action>
>>>>>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>>>>>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>>>>>> >       </action>
>>>>>> >
>>>>>> >
>>>>>>
>>>>>>
>>>>>>
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
>>>>>> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Matt Sicker <bo...@gmail.com>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Matt Sicker <bo...@gmail.com>
>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> Matt Sicker <bo...@gmail.com>
>>>
>>>
>>>
>>
>>
>> --
>> Matt Sicker <bo...@gmail.com>
>>
>>
>>
>>
>
>
> --
> Matt Sicker <bo...@gmail.com>
>
>
>


-- 
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Apache <ra...@dslextreme.com>.
OK. I called Cox and they gave me the ip addresses that don’t redirect but I was still getting their ipv6 DNS that does. I had to set that to go to OpenDNS instead (see https://www.opendns.com/about/innovations/ipv6/ <https://www.opendns.com/about/innovations/ipv6/>). Now the test passes.

Ralph

> On Jan 9, 2017, at 7:43 PM, Matt Sicker <bo...@gmail.com> wrote:
> 
> I'm trying an invalid IP address instead. If that still doesn't work, then I'll add an Assume.assumeThat() to the test so it's ignored with weird DNS servers.
> 
> A lot of people like 8.8.8.8 (Google) and 4.2.2.2 (Level3) for DNS. There's also 75.75.75.75 which is Comcast (or 2001:558:feed::1 which is their IPv6 version apparently).
> 
> On 9 January 2017 at 20:39, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
> It appears I need to use a different DNS server for it to stop redirecting me. I need to figure out which one to use.
> 
> Ralph
> 
>> On Jan 9, 2017, at 7:34 PM, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>> 
>> 
>> Didn’t run the test. Don’t really have to.
>> 
>> nslookup "#$%^&*(*&^%$"
>> Server:		2001:578:3f::30
>> Address:	2001:578:3f::30#53
>> 
>> Non-authoritative answer:
>> Name:	#\$%^&*\(*&^%\$
>> Address: 92.242.140.2
>> 
>> 
>> Ralph
>> 
>> 
>> 
>>> On Jan 9, 2017, at 7:31 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>>> 
>>> Let me know if my updated test still causes issues.
>>> 
>>> On 9 January 2017 at 20:30, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>>> I suspect this is my ISP at work.  When I enter an invalid domain I get a web site from cox.net <http://cox.net/> saying it can’t find the domain and it offers suggestions for other sites.
>>> 
>>> I think this test has to be removed.
>>> 
>>> Ralph
>>> 
>>>> On Jan 9, 2017, at 7:28 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>>>> 
>>>> Well technically a UUID can be a valid hostname itself besides being interpreted as a hex-encoded IP address (if the rest of it gets chopped off). I've updated the test to use a jumble of invalid hostname characters instead.
>>>> 
>>>> On 9 January 2017 at 20:26, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>>>> nslookup dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>>> Server:		2001:578:3f::30
>>>> Address:	2001:578:3f::30#53
>>>> 
>>>> Non-authoritative answer:
>>>> Name:	dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>>> Address: 92.242.140.2
>>>> 
>>>> 
>>>> 
>>>>> On Jan 9, 2017, at 7:24 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>>>>> 
>>>>> I suppose technically the first part of a UUID can be an IPv4 address encoded in hex. I'll make a better invalid value.
>>>>> 
>>>>> On 9 January 2017 at 20:20, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>>>>> Debugging this and InetAddress.getByName is returning an InetAddress object. The value is
>>>>> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>>>> Address: 92.242.140.2
>>>>> 
>>>>> Ralph
>>>>> 
>>>>>> On Jan 9, 2017, at 7:12 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>>>>>> 
>>>>>> Not sure how that's possible, but I added a better assert message. Let me know if you're still having an issue with it. Is it a failure from IntelliJ or Maven?
>>>>>> 
>>>>>> On 9 January 2017 at 19:47, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>>>>>> This commit appears to be failing for me. The testInvalidIpAddress method is failing on the assert.
>>>>>> 
>>>>>> Ralph
>>>>>> 
>>>>>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org <ma...@apache.org> wrote:
>>>>>> >
>>>>>> > Repository: logging-log4j2
>>>>>> > Updated Branches:
>>>>>> >  refs/heads/master 367d26b09 -> 4254e2558
>>>>>> >
>>>>>> >
>>>>>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>>>>>> >
>>>>>> >
>>>>>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo <http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo>
>>>>>> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255>
>>>>>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255>
>>>>>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255>
>>>>>> >
>>>>>> > Branch: refs/heads/master
>>>>>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>>>>>> > Parents: 367d26b
>>>>>> > Author: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>>>>>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>>>>>> > Committer: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>>>>>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>>>>>> >
>>>>>> > ----------------------------------------------------------------------
>>>>>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>>>>>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>>>>>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>>>>>> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
>>>>>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>>>>>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>>>>>> > .../validators/ValidHostValidatorTest.java      | 74 ++++++++++++++++++++
>>>>>> > .../validators/ValidPortValidatorTest.java      | 70 ++++++++++++++++++
>>>>>> > src/changes/changes.xml                         |  3 +
>>>>>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>>>>>> > ----------------------------------------------------------------------
>>>>>> >
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java>
>>>>>> > ----------------------------------------------------------------------
>>>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>>>> > index 2895e52..421d711 100644
>>>>>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>>>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.config.plugins.convert;
>>>>>> > import java.io.File;
>>>>>> > import java.math.BigDecimal;
>>>>>> > import java.math.BigInteger;
>>>>>> > +import java.net.InetAddress;
>>>>>> > import java.net <http://java.net/>.MalformedURLException;
>>>>>> > import java.net.URI;
>>>>>> > import java.net.URISyntaxException;
>>>>>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>>>>>> > import java.security.Provider;
>>>>>> > import java.security.Security;
>>>>>> > import java.util.regex.Pattern;
>>>>>> > -
>>>>>> > import javax.xml.bind.DatatypeConverter;
>>>>>> >
>>>>>> > import org.apache.logging.log4j.Level;
>>>>>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>>>>>> >     }
>>>>>> >
>>>>>> >     /**
>>>>>> > +     * Converts a {@link String} into an {@link InetAddress}.
>>>>>> > +     */
>>>>>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>>>>>> > +    public static class InetAddressConverter implements TypeConverter<InetAddress> {
>>>>>> > +        @Override
>>>>>> > +        public InetAddress convert(final String s) throws Exception {
>>>>>> > +            return InetAddress.getByName(s);
>>>>>> > +        }
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    /**
>>>>>> >      * Converts a {@link String} into a {@link Integer}.
>>>>>> >      */
>>>>>> >     @Plugin(name = "Integer", category = CATEGORY)
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java>
>>>>>> > ----------------------------------------------------------------------
>>>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..c652d40
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>>>>> > @@ -0,0 +1,41 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>>> > + * this work for additional information regarding copyright ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>>> > + * See the license for the specific language governing permissions and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidHostValidator;
>>>>>> > +
>>>>>> > +import java.lang.annotation.*;
>>>>>> > +import java.net.InetAddress;
>>>>>> > +
>>>>>> > +/**
>>>>>> > + * Indicates that a plugin attribute must be a valid host. This relies on the same validation rules as
>>>>>> > + * {@link InetAddress#getByName(String)}.
>>>>>> > + *
>>>>>> > + * @since 2.8
>>>>>> > + */
>>>>>> > +@Documented
>>>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>>>> > +@Constraint(ValidHostValidator.class)
>>>>>> > +public @interface ValidHost {
>>>>>> > +
>>>>>> > +    /**
>>>>>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>>>>>> > +     */
>>>>>> > +    String message() default "The hostname is invalid";
>>>>>> > +}
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java>
>>>>>> > ----------------------------------------------------------------------
>>>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..a7c68b1
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>>>>> > @@ -0,0 +1,44 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>>> > + * this work for additional information regarding copyright ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>>> > + * See the license for the specific language governing permissions and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>>>>>> > +
>>>>>> > +import java.lang.annotation.Documented;
>>>>>> > +import java.lang.annotation.ElementType;
>>>>>> > +import java.lang.annotation.Retention;
>>>>>> > +import java.lang.annotation.RetentionPolicy;
>>>>>> > +import java.lang.annotation.Target;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidPortValidator;
>>>>>> > +
>>>>>> > +/**
>>>>>> > + * Indicates that a plugin attribute must be a valid port number. A valid port number is an integer between 0 and
>>>>>> > + * 65535.
>>>>>> > + *
>>>>>> > + * @since 2.8
>>>>>> > + */
>>>>>> > +@Documented
>>>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>>>> > +@Constraint(ValidPortValidator.class)
>>>>>> > +public @interface ValidPort {
>>>>>> > +
>>>>>> > +    /**
>>>>>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>>>>>> > +     */
>>>>>> > +    String message() default "The port number is invalid";
>>>>>> > +}
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java>
>>>>>> > ----------------------------------------------------------------------
>>>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..3669915
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>>>>> > @@ -0,0 +1,58 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>>> > + * this work for additional information regarding copyright ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>>> > + * See the license for the specific language governing permissions and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.Logger;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>>>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>>>> > +
>>>>>> > +import java.net.InetAddress;
>>>>>> > +import java.net.UnknownHostException;
>>>>>> > +
>>>>>> > +/**
>>>>>> > + * Validator that checks an object to verify it is a valid hostname or IP address. Validation rules follow the same
>>>>>> > + * logic as in {@link InetAddress#getByName(String)}.
>>>>>> > + *
>>>>>> > + * @since 2.8
>>>>>> > + */
>>>>>> > +public class ValidHostValidator implements ConstraintValidator<ValidHost> {
>>>>>> > +
>>>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>>>> > +
>>>>>> > +    private ValidHost annotation;
>>>>>> > +
>>>>>> > +    @Override
>>>>>> > +    public void initialize(ValidHost annotation) {
>>>>>> > +        this.annotation = annotation;
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Override
>>>>>> > +    public boolean isValid(String name, Object value) {
>>>>>> > +        if (value == null) {
>>>>>> > +            LOGGER.error(annotation.message());
>>>>>> > +            return false;
>>>>>> > +        }
>>>>>> > +        try {
>>>>>> > +            InetAddress.getByName(value.to <http://value.to/>String());
>>>>>> > +            return true;
>>>>>> > +        } catch (final UnknownHostException e) {
>>>>>> > +            LOGGER.error(annotation.message(), e);
>>>>>> > +            return false;
>>>>>> > +        }
>>>>>> > +    }
>>>>>> > +}
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java>
>>>>>> > ----------------------------------------------------------------------
>>>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..f18f8fc
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>>>>> > @@ -0,0 +1,53 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>>> > + * this work for additional information regarding copyright ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>>> > + * See the license for the specific language governing permissions and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.Logger;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>>>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>>>> > +
>>>>>> > +/**
>>>>>> > + * Validator that checks an object to verify it is a valid port number (an integer between 0 and 65535).
>>>>>> > + *
>>>>>> > + * @since 2.8
>>>>>> > + */
>>>>>> > +public class ValidPortValidator implements ConstraintValidator<ValidPort> {
>>>>>> > +
>>>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>>>> > +
>>>>>> > +    private ValidPort annotation;
>>>>>> > +
>>>>>> > +    @Override
>>>>>> > +    public void initialize(final ValidPort annotation) {
>>>>>> > +        this.annotation = annotation;
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Override
>>>>>> > +    public boolean isValid(final String name, final Object value) {
>>>>>> > +        if (!Integer.class.isInstance(value)) {
>>>>>> > +            LOGGER.error(annotation.message());
>>>>>> > +            return false;
>>>>>> > +        }
>>>>>> > +        int port = (int) value;
>>>>>> > +        if (port < 0 || port > 65535) {
>>>>>> > +            LOGGER.error(annotation.message());
>>>>>> > +            return false;
>>>>>> > +        }
>>>>>> > +        return true;
>>>>>> > +    }
>>>>>> > +}
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java>
>>>>>> > ----------------------------------------------------------------------
>>>>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..4f05d68
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>>>>> > @@ -0,0 +1,46 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>>> > + * this work for additional information regarding copyright ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>>> > + * See the license for the specific language governing permissions and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>>>>>> > +
>>>>>> > +import java.net.InetSocketAddress;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>>>>>> > +
>>>>>> > +@Plugin(name = "HostAndPort", category = "Test")
>>>>>> > +public class HostAndPort {
>>>>>> > +
>>>>>> > +    private final InetSocketAddress address;
>>>>>> > +
>>>>>> > +    private HostAndPort(final InetSocketAddress address) {
>>>>>> > +        this.address = address;
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    public boolean isValid() {
>>>>>> > +        return !address.isUnresolved();
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @PluginFactory
>>>>>> > +    public static HostAndPort createPlugin(
>>>>>> > +        @ValidHost(message = "Unit test (host)") @PluginAttribute("host") final String host,
>>>>>> > +        @ValidPort(message = "Unit test (port)") @PluginAttribute("port") final int port) {
>>>>>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>>>>>> > +    }
>>>>>> > +}
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java>
>>>>>> > ----------------------------------------------------------------------
>>>>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..3b0480d
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>>>>>> > @@ -0,0 +1,74 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>>> > + * this work for additional information regarding copyright ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>>> > + * See the license for the specific language governing permissions and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>>>> > +
>>>>>> > +import java.util.UUID;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>>>>>> > +import org.junit.Before;
>>>>>> > +import org.junit.Test;
>>>>>> > +
>>>>>> > +import static org.junit.Assert.*;
>>>>>> > +
>>>>>> > +public class ValidHostValidatorTest {
>>>>>> > +
>>>>>> > +    private PluginType<HostAndPort> plugin;
>>>>>> > +    private Node node;
>>>>>> > +
>>>>>> > +    @SuppressWarnings("unchecked")
>>>>>> > +    @Before
>>>>>> > +    public void setUp() throws Exception {
>>>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>>>> > +        manager.collectPlugins();
>>>>>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>>>>>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>>>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testNullHost() throws Exception {
>>>>>> > +        assertNull(buildPlugin());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testInvalidIpAddress() throws Exception {
>>>>>> > +        node.getAttributes().put("host", UUID.randomUUID().toString());
>>>>>> > +        node.getAttributes().put("port", "1");
>>>>>> > +        assertNull(buildPlugin());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testLocalhost() throws Exception {
>>>>>> > +        node.getAttributes().put("host", "localhost");
>>>>>> > +        node.getAttributes().put("port", "1");
>>>>>> > +        final HostAndPort hostAndPort = buildPlugin();
>>>>>> > +        assertNotNull(hostAndPort);
>>>>>> > +        assertTrue(hostAndPort.isValid());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    private HostAndPort buildPlugin() {
>>>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>>>> > +            .withConfiguration(new NullConfiguration())
>>>>>> > +            .withConfigurationNode(node)
>>>>>> > +            .build();
>>>>>> > +    }
>>>>>> > +}
>>>>>> > \ No newline at end of file
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java>
>>>>>> > ----------------------------------------------------------------------
>>>>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>>>>>> > new file mode 100644
>>>>>> > index 0000000..3aab08d
>>>>>> > --- /dev/null
>>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>>>>>> > @@ -0,0 +1,70 @@
>>>>>> > +/*
>>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>>> > + * this work for additional information regarding copyright ownership.
>>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>>> > + * the License. You may obtain a copy of the License at
>>>>>> > + *
>>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>>> > + *
>>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>>> > + * See the license for the specific language governing permissions and
>>>>>> > + * limitations under the license.
>>>>>> > + */
>>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>>>> > +
>>>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>>>>>> > +import org.junit.Before;
>>>>>> > +import org.junit.Test;
>>>>>> > +
>>>>>> > +import static org.junit.Assert.*;
>>>>>> > +
>>>>>> > +public class ValidPortValidatorTest {
>>>>>> > +    private PluginType<HostAndPort> plugin;
>>>>>> > +    private Node node;
>>>>>> > +
>>>>>> > +    @SuppressWarnings("unchecked")
>>>>>> > +    @Before
>>>>>> > +    public void setUp() throws Exception {
>>>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>>>> > +        manager.collectPlugins();
>>>>>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>>>>>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>>>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>>>> > +        node.getAttributes().put("host", "localhost");
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testNegativePort() throws Exception {
>>>>>> > +        node.getAttributes().put("port", "-1");
>>>>>> > +        assertNull(buildPlugin());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testValidPort() throws Exception {
>>>>>> > +        node.getAttributes().put("port", "10");
>>>>>> > +        assertNotNull(buildPlugin());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    @Test
>>>>>> > +    public void testInvalidPort() throws Exception {
>>>>>> > +        node.getAttributes().put("port", "1234567890");
>>>>>> > +        assertNull(buildPlugin());
>>>>>> > +    }
>>>>>> > +
>>>>>> > +    private HostAndPort buildPlugin() {
>>>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>>>> > +            .withConfiguration(new NullConfiguration())
>>>>>> > +            .withConfigurationNode(node)
>>>>>> > +            .build();
>>>>>> > +    }
>>>>>> > +
>>>>>> > +}
>>>>>> > \ No newline at end of file
>>>>>> >
>>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml>
>>>>>> > ----------------------------------------------------------------------
>>>>>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>>>> > index c05de09..bc08dbb 100644
>>>>>> > --- a/src/changes/changes.xml
>>>>>> > +++ b/src/changes/changes.xml
>>>>>> > @@ -213,6 +213,9 @@
>>>>>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>>>>>> >         The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2.
>>>>>> >       </action>
>>>>>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>>>>>> > +        Add converters and validators related to hostnames and ports.
>>>>>> > +      </action>
>>>>>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>>>>>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>>>>>> >       </action>
>>>>>> >
>>>>>> >
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org <ma...@logging.apache.org>
>>>>>> For additional commands, e-mail: log4j-dev-help@logging.apache.org <ma...@logging.apache.org>
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> -- 
>>>>>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> -- 
>>>>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
>>>> 
>>>> 
>>>> 
>>>> 
>>>> -- 
>>>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
>>> 
>>> 
>>> 
>>> 
>>> -- 
>>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
>> 
> 
> 
> 
> 
> -- 
> Matt Sicker <boards@gmail.com <ma...@gmail.com>>


Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Matt Sicker <bo...@gmail.com>.
I'm trying an invalid IP address instead. If that still doesn't work, then
I'll add an Assume.assumeThat() to the test so it's ignored with weird DNS
servers.

A lot of people like 8.8.8.8 (Google) and 4.2.2.2 (Level3) for DNS. There's
also 75.75.75.75 which is Comcast (or 2001:558:feed::1 which is their IPv6
version apparently).

On 9 January 2017 at 20:39, Apache <ra...@dslextreme.com> wrote:

> It appears I need to use a different DNS server for it to stop redirecting
> me. I need to figure out which one to use.
>
> Ralph
>
> On Jan 9, 2017, at 7:34 PM, Apache <ra...@dslextreme.com> wrote:
>
>
> Didn’t run the test. Don’t really have to.
>
> nslookup "#$%^&*(*&^%$"
> Server: 2001:578:3f::30
> Address: 2001:578:3f::30#53
>
> Non-authoritative answer:
> Name: #\$%^&*\(*&^%\$
> Address: 92.242.140.2
>
>
> Ralph
>
>
>
> On Jan 9, 2017, at 7:31 PM, Matt Sicker <bo...@gmail.com> wrote:
>
> Let me know if my updated test still causes issues.
>
> On 9 January 2017 at 20:30, Apache <ra...@dslextreme.com> wrote:
>
>> I suspect this is my ISP at work.  When I enter an invalid domain I get a
>> web site from cox.net saying it can’t find the domain and it offers
>> suggestions for other sites.
>>
>> I think this test has to be removed.
>>
>> Ralph
>>
>> On Jan 9, 2017, at 7:28 PM, Matt Sicker <bo...@gmail.com> wrote:
>>
>> Well technically a UUID can be a valid hostname itself besides being
>> interpreted as a hex-encoded IP address (if the rest of it gets chopped
>> off). I've updated the test to use a jumble of invalid hostname characters
>> instead.
>>
>> On 9 January 2017 at 20:26, Apache <ra...@dslextreme.com> wrote:
>>
>>> nslookup dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>> Server: 2001:578:3f::30
>>> Address: 2001:578:3f::30#53
>>>
>>> Non-authoritative answer:
>>> Name: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>> Address: 92.242.140.2
>>>
>>>
>>>
>>> On Jan 9, 2017, at 7:24 PM, Matt Sicker <bo...@gmail.com> wrote:
>>>
>>> I suppose technically the first part of a UUID can be an IPv4 address
>>> encoded in hex. I'll make a better invalid value.
>>>
>>> On 9 January 2017 at 20:20, Apache <ra...@dslextreme.com> wrote:
>>>
>>>> Debugging this and InetAddress.getByName is returning an InetAddress
>>>> object. The value is
>>>> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>>> Address: 92.242.140.2
>>>>
>>>> Ralph
>>>>
>>>> On Jan 9, 2017, at 7:12 PM, Matt Sicker <bo...@gmail.com> wrote:
>>>>
>>>> Not sure how that's possible, but I added a better assert message. Let
>>>> me know if you're still having an issue with it. Is it a failure from
>>>> IntelliJ or Maven?
>>>>
>>>> On 9 January 2017 at 19:47, Apache <ra...@dslextreme.com> wrote:
>>>>
>>>>> This commit appears to be failing for me. The testInvalidIpAddress
>>>>> method is failing on the assert.
>>>>>
>>>>> Ralph
>>>>>
>>>>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org wrote:
>>>>> >
>>>>> > Repository: logging-log4j2
>>>>> > Updated Branches:
>>>>> >  refs/heads/master 367d26b09 -> 4254e2558
>>>>> >
>>>>> >
>>>>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>>>>> >
>>>>> >
>>>>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>>>>> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit
>>>>> /4254e255
>>>>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4
>>>>> 254e255
>>>>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4
>>>>> 254e255
>>>>> >
>>>>> > Branch: refs/heads/master
>>>>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>>>>> > Parents: 367d26b
>>>>> > Author: Matt Sicker <ma...@spr.com>
>>>>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>>>>> > Committer: Matt Sicker <ma...@spr.com>
>>>>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>>>>> >
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>>>>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>>>>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>>>>> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
>>>>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>>>>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>>>>> > .../validators/ValidHostValidatorTest.java      | 74
>>>>> ++++++++++++++++++++
>>>>> > .../validators/ValidPortValidatorTest.java      | 70
>>>>> ++++++++++++++++++
>>>>> > src/changes/changes.xml                         |  3 +
>>>>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> >
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>>> re/config/plugins/convert/TypeConverters.java
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> > diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/convert/TypeConverters.java
>>>>> > index 2895e52..421d711 100644
>>>>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/convert/TypeConverters.java
>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/convert/TypeConverters.java
>>>>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.
>>>>> config.plugins.convert;
>>>>> > import java.io.File;
>>>>> > import java.math.BigDecimal;
>>>>> > import java.math.BigInteger;
>>>>> > +import java.net.InetAddress;
>>>>> > import java.net.MalformedURLException;
>>>>> > import java.net.URI;
>>>>> > import java.net.URISyntaxException;
>>>>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>>>>> > import java.security.Provider;
>>>>> > import java.security.Security;
>>>>> > import java.util.regex.Pattern;
>>>>> > -
>>>>> > import javax.xml.bind.DatatypeConverter;
>>>>> >
>>>>> > import org.apache.logging.log4j.Level;
>>>>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>>>>> >     }
>>>>> >
>>>>> >     /**
>>>>> > +     * Converts a {@link String} into an {@link InetAddress}.
>>>>> > +     */
>>>>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>>>>> > +    public static class InetAddressConverter implements
>>>>> TypeConverter<InetAddress> {
>>>>> > +        @Override
>>>>> > +        public InetAddress convert(final String s) throws Exception
>>>>> {
>>>>> > +            return InetAddress.getByName(s);
>>>>> > +        }
>>>>> > +    }
>>>>> > +
>>>>> > +    /**
>>>>> >      * Converts a {@link String} into a {@link Integer}.
>>>>> >      */
>>>>> >     @Plugin(name = "Integer", category = CATEGORY)
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>>> re/config/plugins/validation/constraints/ValidHost.java
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> > diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/constraints/ValidHost.java
>>>>> > new file mode 100644
>>>>> > index 0000000..c652d40
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/constraints/ValidHost.java
>>>>> > @@ -0,0 +1,41 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>> more
>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>> with
>>>>> > + * this work for additional information regarding copyright
>>>>> ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>> Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>> with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>> software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>> implied.
>>>>> > + * See the license for the specific language governing permissions
>>>>> and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.
>>>>> config.plugins.validation.constraints;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>>>> traint;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.vali
>>>>> dators.ValidHostValidator;
>>>>> > +
>>>>> > +import java.lang.annotation.*;
>>>>> > +import java.net.InetAddress;
>>>>> > +
>>>>> > +/**
>>>>> > + * Indicates that a plugin attribute must be a valid host. This
>>>>> relies on the same validation rules as
>>>>> > + * {@link InetAddress#getByName(String)}.
>>>>> > + *
>>>>> > + * @since 2.8
>>>>> > + */
>>>>> > +@Documented
>>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>>> > +@Constraint(ValidHostValidator.class)
>>>>> > +public @interface ValidHost {
>>>>> > +
>>>>> > +    /**
>>>>> > +     * The message to be logged if this constraint is violated.
>>>>> This should normally be overridden.
>>>>> > +     */
>>>>> > +    String message() default "The hostname is invalid";
>>>>> > +}
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>>> re/config/plugins/validation/constraints/ValidPort.java
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> > diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/constraints/ValidPort.java
>>>>> > new file mode 100644
>>>>> > index 0000000..a7c68b1
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/constraints/ValidPort.java
>>>>> > @@ -0,0 +1,44 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>> more
>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>> with
>>>>> > + * this work for additional information regarding copyright
>>>>> ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>> Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>> with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>> software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>> implied.
>>>>> > + * See the license for the specific language governing permissions
>>>>> and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.
>>>>> config.plugins.validation.constraints;
>>>>> > +
>>>>> > +import java.lang.annotation.Documented;
>>>>> > +import java.lang.annotation.ElementType;
>>>>> > +import java.lang.annotation.Retention;
>>>>> > +import java.lang.annotation.RetentionPolicy;
>>>>> > +import java.lang.annotation.Target;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>>>> traint;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.vali
>>>>> dators.ValidPortValidator;
>>>>> > +
>>>>> > +/**
>>>>> > + * Indicates that a plugin attribute must be a valid port number. A
>>>>> valid port number is an integer between 0 and
>>>>> > + * 65535.
>>>>> > + *
>>>>> > + * @since 2.8
>>>>> > + */
>>>>> > +@Documented
>>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>>> > +@Constraint(ValidPortValidator.class)
>>>>> > +public @interface ValidPort {
>>>>> > +
>>>>> > +    /**
>>>>> > +     * The message to be logged if this constraint is violated.
>>>>> This should normally be overridden.
>>>>> > +     */
>>>>> > +    String message() default "The port number is invalid";
>>>>> > +}
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>>> re/config/plugins/validation/validators/ValidHostValidator.java
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> > diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/validators/ValidHostValidator.java
>>>>> > new file mode 100644
>>>>> > index 0000000..3669915
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/validators/ValidHostValidator.java
>>>>> > @@ -0,0 +1,58 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>> more
>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>> with
>>>>> > + * this work for additional information regarding copyright
>>>>> ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>> Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>> with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>> software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>> implied.
>>>>> > + * See the license for the specific language governing permissions
>>>>> and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.
>>>>> config.plugins.validation.validators;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.Logger;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>>>> traintValidator;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>>>> traints.ValidHost;
>>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>>> > +
>>>>> > +import java.net.InetAddress;
>>>>> > +import java.net.UnknownHostException;
>>>>> > +
>>>>> > +/**
>>>>> > + * Validator that checks an object to verify it is a valid hostname
>>>>> or IP address. Validation rules follow the same
>>>>> > + * logic as in {@link InetAddress#getByName(String)}.
>>>>> > + *
>>>>> > + * @since 2.8
>>>>> > + */
>>>>> > +public class ValidHostValidator implements
>>>>> ConstraintValidator<ValidHost> {
>>>>> > +
>>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>>> > +
>>>>> > +    private ValidHost annotation;
>>>>> > +
>>>>> > +    @Override
>>>>> > +    public void initialize(ValidHost annotation) {
>>>>> > +        this.annotation = annotation;
>>>>> > +    }
>>>>> > +
>>>>> > +    @Override
>>>>> > +    public boolean isValid(String name, Object value) {
>>>>> > +        if (value == null) {
>>>>> > +            LOGGER.error(annotation.message());
>>>>> > +            return false;
>>>>> > +        }
>>>>> > +        try {
>>>>> > +            InetAddress.getByName(value.toString());
>>>>> > +            return true;
>>>>> > +        } catch (final UnknownHostException e) {
>>>>> > +            LOGGER.error(annotation.message(), e);
>>>>> > +            return false;
>>>>> > +        }
>>>>> > +    }
>>>>> > +}
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>>> re/config/plugins/validation/validators/ValidPortValidator.java
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> > diff --git a/log4j-core/src/main/java/org
>>>>> /apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/validators/ValidPortValidator.java
>>>>> > new file mode 100644
>>>>> > index 0000000..f18f8fc
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/validators/ValidPortValidator.java
>>>>> > @@ -0,0 +1,53 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>> more
>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>> with
>>>>> > + * this work for additional information regarding copyright
>>>>> ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>> Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>> with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>> software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>> implied.
>>>>> > + * See the license for the specific language governing permissions
>>>>> and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.
>>>>> config.plugins.validation.validators;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.Logger;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>>>> traintValidator;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>>>> traints.ValidPort;
>>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>>> > +
>>>>> > +/**
>>>>> > + * Validator that checks an object to verify it is a valid port
>>>>> number (an integer between 0 and 65535).
>>>>> > + *
>>>>> > + * @since 2.8
>>>>> > + */
>>>>> > +public class ValidPortValidator implements
>>>>> ConstraintValidator<ValidPort> {
>>>>> > +
>>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>>> > +
>>>>> > +    private ValidPort annotation;
>>>>> > +
>>>>> > +    @Override
>>>>> > +    public void initialize(final ValidPort annotation) {
>>>>> > +        this.annotation = annotation;
>>>>> > +    }
>>>>> > +
>>>>> > +    @Override
>>>>> > +    public boolean isValid(final String name, final Object value) {
>>>>> > +        if (!Integer.class.isInstance(value)) {
>>>>> > +            LOGGER.error(annotation.message());
>>>>> > +            return false;
>>>>> > +        }
>>>>> > +        int port = (int) value;
>>>>> > +        if (port < 0 || port > 65535) {
>>>>> > +            LOGGER.error(annotation.message());
>>>>> > +            return false;
>>>>> > +        }
>>>>> > +        return true;
>>>>> > +    }
>>>>> > +}
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>>>> re/config/plugins/validation/HostAndPort.java
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> > diff --git a/log4j-core/src/test/java/org
>>>>> /apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>>>> b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/HostAndPort.java
>>>>> > new file mode 100644
>>>>> > index 0000000..4f05d68
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/HostAndPort.java
>>>>> > @@ -0,0 +1,46 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>> more
>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>> with
>>>>> > + * this work for additional information regarding copyright
>>>>> ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>> Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>> with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>> software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>> implied.
>>>>> > + * See the license for the specific language governing permissions
>>>>> and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>>>>> > +
>>>>> > +import java.net.InetSocketAddress;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute
>>>>> ;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>>>> traints.ValidHost;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>>>> traints.ValidPort;
>>>>> > +
>>>>> > +@Plugin(name = "HostAndPort", category = "Test")
>>>>> > +public class HostAndPort {
>>>>> > +
>>>>> > +    private final InetSocketAddress address;
>>>>> > +
>>>>> > +    private HostAndPort(final InetSocketAddress address) {
>>>>> > +        this.address = address;
>>>>> > +    }
>>>>> > +
>>>>> > +    public boolean isValid() {
>>>>> > +        return !address.isUnresolved();
>>>>> > +    }
>>>>> > +
>>>>> > +    @PluginFactory
>>>>> > +    public static HostAndPort createPlugin(
>>>>> > +        @ValidHost(message = "Unit test (host)")
>>>>> @PluginAttribute("host") final String host,
>>>>> > +        @ValidPort(message = "Unit test (port)")
>>>>> @PluginAttribute("port") final int port) {
>>>>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>>>>> > +    }
>>>>> > +}
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>>>> re/config/plugins/validation/validators/ValidHostValidatorTest.java
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> > diff --git a/log4j-core/src/test/java/org
>>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>>> ors/ValidHostValidatorTest.java b/log4j-core/src/test/java/org
>>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>>> ors/ValidHostValidatorTest.java
>>>>> > new file mode 100644
>>>>> > index 0000000..3b0480d
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/validators/ValidHostValidatorTest.java
>>>>> > @@ -0,0 +1,74 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>> more
>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>> with
>>>>> > + * this work for additional information regarding copyright
>>>>> ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>> Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>> with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>> software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>> implied.
>>>>> > + * See the license for the specific language governing permissions
>>>>> and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.
>>>>> config.plugins.validation.validators;
>>>>> > +
>>>>> > +import java.util.UUID;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuil
>>>>> der;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginMana
>>>>> ger;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType
>>>>> ;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Host
>>>>> AndPort;
>>>>> > +import org.junit.Before;
>>>>> > +import org.junit.Test;
>>>>> > +
>>>>> > +import static org.junit.Assert.*;
>>>>> > +
>>>>> > +public class ValidHostValidatorTest {
>>>>> > +
>>>>> > +    private PluginType<HostAndPort> plugin;
>>>>> > +    private Node node;
>>>>> > +
>>>>> > +    @SuppressWarnings("unchecked")
>>>>> > +    @Before
>>>>> > +    public void setUp() throws Exception {
>>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>>> > +        manager.collectPlugins();
>>>>> > +        plugin = (PluginType<HostAndPort>)
>>>>> manager.getPluginType("HostAndPort");
>>>>> > +        assertNotNull("Rebuild this module to ensure annotation
>>>>> processing has been done.", plugin);
>>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testNullHost() throws Exception {
>>>>> > +        assertNull(buildPlugin());
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testInvalidIpAddress() throws Exception {
>>>>> > +        node.getAttributes().put("host",
>>>>> UUID.randomUUID().toString());
>>>>> > +        node.getAttributes().put("port", "1");
>>>>> > +        assertNull(buildPlugin());
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testLocalhost() throws Exception {
>>>>> > +        node.getAttributes().put("host", "localhost");
>>>>> > +        node.getAttributes().put("port", "1");
>>>>> > +        final HostAndPort hostAndPort = buildPlugin();
>>>>> > +        assertNotNull(hostAndPort);
>>>>> > +        assertTrue(hostAndPort.isValid());
>>>>> > +    }
>>>>> > +
>>>>> > +    private HostAndPort buildPlugin() {
>>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>>> > +            .withConfiguration(new NullConfiguration())
>>>>> > +            .withConfigurationNode(node)
>>>>> > +            .build();
>>>>> > +    }
>>>>> > +}
>>>>> > \ No newline at end of file
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>>>> re/config/plugins/validation/validators/ValidPortValidatorTest.java
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> > diff --git a/log4j-core/src/test/java/org
>>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>>> ors/ValidPortValidatorTest.java b/log4j-core/src/test/java/org
>>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>>> ors/ValidPortValidatorTest.java
>>>>> > new file mode 100644
>>>>> > index 0000000..3aab08d
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>>> fig/plugins/validation/validators/ValidPortValidatorTest.java
>>>>> > @@ -0,0 +1,70 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or
>>>>> more
>>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>>> with
>>>>> > + * this work for additional information regarding copyright
>>>>> ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license,
>>>>> Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance
>>>>> with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing,
>>>>> software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>>> implied.
>>>>> > + * See the license for the specific language governing permissions
>>>>> and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.
>>>>> config.plugins.validation.validators;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuil
>>>>> der;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginMana
>>>>> ger;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType
>>>>> ;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Host
>>>>> AndPort;
>>>>> > +import org.junit.Before;
>>>>> > +import org.junit.Test;
>>>>> > +
>>>>> > +import static org.junit.Assert.*;
>>>>> > +
>>>>> > +public class ValidPortValidatorTest {
>>>>> > +    private PluginType<HostAndPort> plugin;
>>>>> > +    private Node node;
>>>>> > +
>>>>> > +    @SuppressWarnings("unchecked")
>>>>> > +    @Before
>>>>> > +    public void setUp() throws Exception {
>>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>>> > +        manager.collectPlugins();
>>>>> > +        plugin = (PluginType<HostAndPort>)
>>>>> manager.getPluginType("HostAndPort");
>>>>> > +        assertNotNull("Rebuild this module to ensure annotation
>>>>> processing has been done.", plugin);
>>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>>> > +        node.getAttributes().put("host", "localhost");
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testNegativePort() throws Exception {
>>>>> > +        node.getAttributes().put("port", "-1");
>>>>> > +        assertNull(buildPlugin());
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testValidPort() throws Exception {
>>>>> > +        node.getAttributes().put("port", "10");
>>>>> > +        assertNotNull(buildPlugin());
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testInvalidPort() throws Exception {
>>>>> > +        node.getAttributes().put("port", "1234567890");
>>>>> > +        assertNull(buildPlugin());
>>>>> > +    }
>>>>> > +
>>>>> > +    private HostAndPort buildPlugin() {
>>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>>> > +            .withConfiguration(new NullConfiguration())
>>>>> > +            .withConfigurationNode(node)
>>>>> > +            .build();
>>>>> > +    }
>>>>> > +
>>>>> > +}
>>>>> > \ No newline at end of file
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>>> 254e255/src/changes/changes.xml
>>>>> > ------------------------------------------------------------
>>>>> ----------
>>>>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>>> > index c05de09..bc08dbb 100644
>>>>> > --- a/src/changes/changes.xml
>>>>> > +++ b/src/changes/changes.xml
>>>>> > @@ -213,6 +213,9 @@
>>>>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>>>>> >         The log4j-slf4j-impl module now declares a runtime
>>>>> dependency on log4j-core. While not technically required, this makes the
>>>>> log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates
>>>>> migration to Log4j 2.
>>>>> >       </action>
>>>>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>>>>> > +        Add converters and validators related to hostnames and
>>>>> ports.
>>>>> > +      </action>
>>>>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>>>>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>>>>> >       </action>
>>>>> >
>>>>> >
>>>>>
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
>>>>> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> Matt Sicker <bo...@gmail.com>
>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> Matt Sicker <bo...@gmail.com>
>>>
>>>
>>>
>>
>>
>> --
>> Matt Sicker <bo...@gmail.com>
>>
>>
>>
>
>
> --
> Matt Sicker <bo...@gmail.com>
>
>
>
>


-- 
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Apache <ra...@dslextreme.com>.
It appears I need to use a different DNS server for it to stop redirecting me. I need to figure out which one to use.

Ralph

> On Jan 9, 2017, at 7:34 PM, Apache <ra...@dslextreme.com> wrote:
> 
> 
> Didn’t run the test. Don’t really have to.
> 
> nslookup "#$%^&*(*&^%$"
> Server:		2001:578:3f::30
> Address:	2001:578:3f::30#53
> 
> Non-authoritative answer:
> Name:	#\$%^&*\(*&^%\$
> Address: 92.242.140.2
> 
> 
> Ralph
> 
> 
> 
>> On Jan 9, 2017, at 7:31 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>> 
>> Let me know if my updated test still causes issues.
>> 
>> On 9 January 2017 at 20:30, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>> I suspect this is my ISP at work.  When I enter an invalid domain I get a web site from cox.net <http://cox.net/> saying it can’t find the domain and it offers suggestions for other sites.
>> 
>> I think this test has to be removed.
>> 
>> Ralph
>> 
>>> On Jan 9, 2017, at 7:28 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>>> 
>>> Well technically a UUID can be a valid hostname itself besides being interpreted as a hex-encoded IP address (if the rest of it gets chopped off). I've updated the test to use a jumble of invalid hostname characters instead.
>>> 
>>> On 9 January 2017 at 20:26, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>>> nslookup dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>> Server:		2001:578:3f::30
>>> Address:	2001:578:3f::30#53
>>> 
>>> Non-authoritative answer:
>>> Name:	dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>> Address: 92.242.140.2
>>> 
>>> 
>>> 
>>>> On Jan 9, 2017, at 7:24 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>>>> 
>>>> I suppose technically the first part of a UUID can be an IPv4 address encoded in hex. I'll make a better invalid value.
>>>> 
>>>> On 9 January 2017 at 20:20, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>>>> Debugging this and InetAddress.getByName is returning an InetAddress object. The value is
>>>> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>>> Address: 92.242.140.2
>>>> 
>>>> Ralph
>>>> 
>>>>> On Jan 9, 2017, at 7:12 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>>>>> 
>>>>> Not sure how that's possible, but I added a better assert message. Let me know if you're still having an issue with it. Is it a failure from IntelliJ or Maven?
>>>>> 
>>>>> On 9 January 2017 at 19:47, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>>>>> This commit appears to be failing for me. The testInvalidIpAddress method is failing on the assert.
>>>>> 
>>>>> Ralph
>>>>> 
>>>>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org <ma...@apache.org> wrote:
>>>>> >
>>>>> > Repository: logging-log4j2
>>>>> > Updated Branches:
>>>>> >  refs/heads/master 367d26b09 -> 4254e2558
>>>>> >
>>>>> >
>>>>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>>>>> >
>>>>> >
>>>>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo <http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo>
>>>>> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255>
>>>>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255>
>>>>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255>
>>>>> >
>>>>> > Branch: refs/heads/master
>>>>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>>>>> > Parents: 367d26b
>>>>> > Author: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>>>>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>>>>> > Committer: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>>>>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>>>>> >
>>>>> > ----------------------------------------------------------------------
>>>>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>>>>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>>>>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>>>>> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
>>>>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>>>>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>>>>> > .../validators/ValidHostValidatorTest.java      | 74 ++++++++++++++++++++
>>>>> > .../validators/ValidPortValidatorTest.java      | 70 ++++++++++++++++++
>>>>> > src/changes/changes.xml                         |  3 +
>>>>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>>>>> > ----------------------------------------------------------------------
>>>>> >
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java>
>>>>> > ----------------------------------------------------------------------
>>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>>> > index 2895e52..421d711 100644
>>>>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.config.plugins.convert;
>>>>> > import java.io.File;
>>>>> > import java.math.BigDecimal;
>>>>> > import java.math.BigInteger;
>>>>> > +import java.net.InetAddress;
>>>>> > import java.net <http://java.net/>.MalformedURLException;
>>>>> > import java.net.URI;
>>>>> > import java.net.URISyntaxException;
>>>>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>>>>> > import java.security.Provider;
>>>>> > import java.security.Security;
>>>>> > import java.util.regex.Pattern;
>>>>> > -
>>>>> > import javax.xml.bind.DatatypeConverter;
>>>>> >
>>>>> > import org.apache.logging.log4j.Level;
>>>>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>>>>> >     }
>>>>> >
>>>>> >     /**
>>>>> > +     * Converts a {@link String} into an {@link InetAddress}.
>>>>> > +     */
>>>>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>>>>> > +    public static class InetAddressConverter implements TypeConverter<InetAddress> {
>>>>> > +        @Override
>>>>> > +        public InetAddress convert(final String s) throws Exception {
>>>>> > +            return InetAddress.getByName(s);
>>>>> > +        }
>>>>> > +    }
>>>>> > +
>>>>> > +    /**
>>>>> >      * Converts a {@link String} into a {@link Integer}.
>>>>> >      */
>>>>> >     @Plugin(name = "Integer", category = CATEGORY)
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java>
>>>>> > ----------------------------------------------------------------------
>>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>>>> > new file mode 100644
>>>>> > index 0000000..c652d40
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>>>> > @@ -0,0 +1,41 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>> > + * this work for additional information regarding copyright ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>> > + * See the license for the specific language governing permissions and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidHostValidator;
>>>>> > +
>>>>> > +import java.lang.annotation.*;
>>>>> > +import java.net.InetAddress;
>>>>> > +
>>>>> > +/**
>>>>> > + * Indicates that a plugin attribute must be a valid host. This relies on the same validation rules as
>>>>> > + * {@link InetAddress#getByName(String)}.
>>>>> > + *
>>>>> > + * @since 2.8
>>>>> > + */
>>>>> > +@Documented
>>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>>> > +@Constraint(ValidHostValidator.class)
>>>>> > +public @interface ValidHost {
>>>>> > +
>>>>> > +    /**
>>>>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>>>>> > +     */
>>>>> > +    String message() default "The hostname is invalid";
>>>>> > +}
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java>
>>>>> > ----------------------------------------------------------------------
>>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>>>> > new file mode 100644
>>>>> > index 0000000..a7c68b1
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>>>> > @@ -0,0 +1,44 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>> > + * this work for additional information regarding copyright ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>> > + * See the license for the specific language governing permissions and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>>>>> > +
>>>>> > +import java.lang.annotation.Documented;
>>>>> > +import java.lang.annotation.ElementType;
>>>>> > +import java.lang.annotation.Retention;
>>>>> > +import java.lang.annotation.RetentionPolicy;
>>>>> > +import java.lang.annotation.Target;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidPortValidator;
>>>>> > +
>>>>> > +/**
>>>>> > + * Indicates that a plugin attribute must be a valid port number. A valid port number is an integer between 0 and
>>>>> > + * 65535.
>>>>> > + *
>>>>> > + * @since 2.8
>>>>> > + */
>>>>> > +@Documented
>>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>>> > +@Constraint(ValidPortValidator.class)
>>>>> > +public @interface ValidPort {
>>>>> > +
>>>>> > +    /**
>>>>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>>>>> > +     */
>>>>> > +    String message() default "The port number is invalid";
>>>>> > +}
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java>
>>>>> > ----------------------------------------------------------------------
>>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>>>> > new file mode 100644
>>>>> > index 0000000..3669915
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>>>> > @@ -0,0 +1,58 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>> > + * this work for additional information regarding copyright ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>> > + * See the license for the specific language governing permissions and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.Logger;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>>> > +
>>>>> > +import java.net.InetAddress;
>>>>> > +import java.net.UnknownHostException;
>>>>> > +
>>>>> > +/**
>>>>> > + * Validator that checks an object to verify it is a valid hostname or IP address. Validation rules follow the same
>>>>> > + * logic as in {@link InetAddress#getByName(String)}.
>>>>> > + *
>>>>> > + * @since 2.8
>>>>> > + */
>>>>> > +public class ValidHostValidator implements ConstraintValidator<ValidHost> {
>>>>> > +
>>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>>> > +
>>>>> > +    private ValidHost annotation;
>>>>> > +
>>>>> > +    @Override
>>>>> > +    public void initialize(ValidHost annotation) {
>>>>> > +        this.annotation = annotation;
>>>>> > +    }
>>>>> > +
>>>>> > +    @Override
>>>>> > +    public boolean isValid(String name, Object value) {
>>>>> > +        if (value == null) {
>>>>> > +            LOGGER.error(annotation.message());
>>>>> > +            return false;
>>>>> > +        }
>>>>> > +        try {
>>>>> > +            InetAddress.getByName(value.to <http://value.to/>String());
>>>>> > +            return true;
>>>>> > +        } catch (final UnknownHostException e) {
>>>>> > +            LOGGER.error(annotation.message(), e);
>>>>> > +            return false;
>>>>> > +        }
>>>>> > +    }
>>>>> > +}
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java>
>>>>> > ----------------------------------------------------------------------
>>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>>>> > new file mode 100644
>>>>> > index 0000000..f18f8fc
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>>>> > @@ -0,0 +1,53 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>> > + * this work for additional information regarding copyright ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>> > + * See the license for the specific language governing permissions and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.Logger;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>>> > +
>>>>> > +/**
>>>>> > + * Validator that checks an object to verify it is a valid port number (an integer between 0 and 65535).
>>>>> > + *
>>>>> > + * @since 2.8
>>>>> > + */
>>>>> > +public class ValidPortValidator implements ConstraintValidator<ValidPort> {
>>>>> > +
>>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>>> > +
>>>>> > +    private ValidPort annotation;
>>>>> > +
>>>>> > +    @Override
>>>>> > +    public void initialize(final ValidPort annotation) {
>>>>> > +        this.annotation = annotation;
>>>>> > +    }
>>>>> > +
>>>>> > +    @Override
>>>>> > +    public boolean isValid(final String name, final Object value) {
>>>>> > +        if (!Integer.class.isInstance(value)) {
>>>>> > +            LOGGER.error(annotation.message());
>>>>> > +            return false;
>>>>> > +        }
>>>>> > +        int port = (int) value;
>>>>> > +        if (port < 0 || port > 65535) {
>>>>> > +            LOGGER.error(annotation.message());
>>>>> > +            return false;
>>>>> > +        }
>>>>> > +        return true;
>>>>> > +    }
>>>>> > +}
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java>
>>>>> > ----------------------------------------------------------------------
>>>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>>>> > new file mode 100644
>>>>> > index 0000000..4f05d68
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>>>> > @@ -0,0 +1,46 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>> > + * this work for additional information regarding copyright ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>> > + * See the license for the specific language governing permissions and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>>>>> > +
>>>>> > +import java.net.InetSocketAddress;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>>>>> > +
>>>>> > +@Plugin(name = "HostAndPort", category = "Test")
>>>>> > +public class HostAndPort {
>>>>> > +
>>>>> > +    private final InetSocketAddress address;
>>>>> > +
>>>>> > +    private HostAndPort(final InetSocketAddress address) {
>>>>> > +        this.address = address;
>>>>> > +    }
>>>>> > +
>>>>> > +    public boolean isValid() {
>>>>> > +        return !address.isUnresolved();
>>>>> > +    }
>>>>> > +
>>>>> > +    @PluginFactory
>>>>> > +    public static HostAndPort createPlugin(
>>>>> > +        @ValidHost(message = "Unit test (host)") @PluginAttribute("host") final String host,
>>>>> > +        @ValidPort(message = "Unit test (port)") @PluginAttribute("port") final int port) {
>>>>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>>>>> > +    }
>>>>> > +}
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java>
>>>>> > ----------------------------------------------------------------------
>>>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>>>>> > new file mode 100644
>>>>> > index 0000000..3b0480d
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>>>>> > @@ -0,0 +1,74 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>> > + * this work for additional information regarding copyright ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>> > + * See the license for the specific language governing permissions and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>>> > +
>>>>> > +import java.util.UUID;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>>>>> > +import org.junit.Before;
>>>>> > +import org.junit.Test;
>>>>> > +
>>>>> > +import static org.junit.Assert.*;
>>>>> > +
>>>>> > +public class ValidHostValidatorTest {
>>>>> > +
>>>>> > +    private PluginType<HostAndPort> plugin;
>>>>> > +    private Node node;
>>>>> > +
>>>>> > +    @SuppressWarnings("unchecked")
>>>>> > +    @Before
>>>>> > +    public void setUp() throws Exception {
>>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>>> > +        manager.collectPlugins();
>>>>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>>>>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testNullHost() throws Exception {
>>>>> > +        assertNull(buildPlugin());
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testInvalidIpAddress() throws Exception {
>>>>> > +        node.getAttributes().put("host", UUID.randomUUID().toString());
>>>>> > +        node.getAttributes().put("port", "1");
>>>>> > +        assertNull(buildPlugin());
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testLocalhost() throws Exception {
>>>>> > +        node.getAttributes().put("host", "localhost");
>>>>> > +        node.getAttributes().put("port", "1");
>>>>> > +        final HostAndPort hostAndPort = buildPlugin();
>>>>> > +        assertNotNull(hostAndPort);
>>>>> > +        assertTrue(hostAndPort.isValid());
>>>>> > +    }
>>>>> > +
>>>>> > +    private HostAndPort buildPlugin() {
>>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>>> > +            .withConfiguration(new NullConfiguration())
>>>>> > +            .withConfigurationNode(node)
>>>>> > +            .build();
>>>>> > +    }
>>>>> > +}
>>>>> > \ No newline at end of file
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java>
>>>>> > ----------------------------------------------------------------------
>>>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>>>>> > new file mode 100644
>>>>> > index 0000000..3aab08d
>>>>> > --- /dev/null
>>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>>>>> > @@ -0,0 +1,70 @@
>>>>> > +/*
>>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>>> > + * this work for additional information regarding copyright ownership.
>>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>>> > + * (the "License"); you may not use this file except in compliance with
>>>>> > + * the License. You may obtain a copy of the License at
>>>>> > + *
>>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>>> > + *
>>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>>> > + * See the license for the specific language governing permissions and
>>>>> > + * limitations under the license.
>>>>> > + */
>>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>>> > +
>>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>>>>> > +import org.junit.Before;
>>>>> > +import org.junit.Test;
>>>>> > +
>>>>> > +import static org.junit.Assert.*;
>>>>> > +
>>>>> > +public class ValidPortValidatorTest {
>>>>> > +    private PluginType<HostAndPort> plugin;
>>>>> > +    private Node node;
>>>>> > +
>>>>> > +    @SuppressWarnings("unchecked")
>>>>> > +    @Before
>>>>> > +    public void setUp() throws Exception {
>>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>>> > +        manager.collectPlugins();
>>>>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>>>>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>>> > +        node.getAttributes().put("host", "localhost");
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testNegativePort() throws Exception {
>>>>> > +        node.getAttributes().put("port", "-1");
>>>>> > +        assertNull(buildPlugin());
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testValidPort() throws Exception {
>>>>> > +        node.getAttributes().put("port", "10");
>>>>> > +        assertNotNull(buildPlugin());
>>>>> > +    }
>>>>> > +
>>>>> > +    @Test
>>>>> > +    public void testInvalidPort() throws Exception {
>>>>> > +        node.getAttributes().put("port", "1234567890");
>>>>> > +        assertNull(buildPlugin());
>>>>> > +    }
>>>>> > +
>>>>> > +    private HostAndPort buildPlugin() {
>>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>>> > +            .withConfiguration(new NullConfiguration())
>>>>> > +            .withConfigurationNode(node)
>>>>> > +            .build();
>>>>> > +    }
>>>>> > +
>>>>> > +}
>>>>> > \ No newline at end of file
>>>>> >
>>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml>
>>>>> > ----------------------------------------------------------------------
>>>>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>>> > index c05de09..bc08dbb 100644
>>>>> > --- a/src/changes/changes.xml
>>>>> > +++ b/src/changes/changes.xml
>>>>> > @@ -213,6 +213,9 @@
>>>>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>>>>> >         The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2.
>>>>> >       </action>
>>>>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>>>>> > +        Add converters and validators related to hostnames and ports.
>>>>> > +      </action>
>>>>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>>>>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>>>>> >       </action>
>>>>> >
>>>>> >
>>>>> 
>>>>> 
>>>>> 
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org <ma...@logging.apache.org>
>>>>> For additional commands, e-mail: log4j-dev-help@logging.apache.org <ma...@logging.apache.org>
>>>>> 
>>>>> 
>>>>> 
>>>>> 
>>>>> -- 
>>>>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
>>>> 
>>>> 
>>>> 
>>>> 
>>>> -- 
>>>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
>>> 
>>> 
>>> 
>>> 
>>> -- 
>>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
>> 
>> 
>> 
>> 
>> -- 
>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
> 


Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Apache <ra...@dslextreme.com>.
Didn’t run the test. Don’t really have to.

nslookup "#$%^&*(*&^%$"
Server:		2001:578:3f::30
Address:	2001:578:3f::30#53

Non-authoritative answer:
Name:	#\$%^&*\(*&^%\$
Address: 92.242.140.2


Ralph



> On Jan 9, 2017, at 7:31 PM, Matt Sicker <bo...@gmail.com> wrote:
> 
> Let me know if my updated test still causes issues.
> 
> On 9 January 2017 at 20:30, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
> I suspect this is my ISP at work.  When I enter an invalid domain I get a web site from cox.net <http://cox.net/> saying it can’t find the domain and it offers suggestions for other sites.
> 
> I think this test has to be removed.
> 
> Ralph
> 
>> On Jan 9, 2017, at 7:28 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>> 
>> Well technically a UUID can be a valid hostname itself besides being interpreted as a hex-encoded IP address (if the rest of it gets chopped off). I've updated the test to use a jumble of invalid hostname characters instead.
>> 
>> On 9 January 2017 at 20:26, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>> nslookup dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>> Server:		2001:578:3f::30
>> Address:	2001:578:3f::30#53
>> 
>> Non-authoritative answer:
>> Name:	dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>> Address: 92.242.140.2
>> 
>> 
>> 
>>> On Jan 9, 2017, at 7:24 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>>> 
>>> I suppose technically the first part of a UUID can be an IPv4 address encoded in hex. I'll make a better invalid value.
>>> 
>>> On 9 January 2017 at 20:20, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>>> Debugging this and InetAddress.getByName is returning an InetAddress object. The value is
>>> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>> Address: 92.242.140.2
>>> 
>>> Ralph
>>> 
>>>> On Jan 9, 2017, at 7:12 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>>>> 
>>>> Not sure how that's possible, but I added a better assert message. Let me know if you're still having an issue with it. Is it a failure from IntelliJ or Maven?
>>>> 
>>>> On 9 January 2017 at 19:47, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>>>> This commit appears to be failing for me. The testInvalidIpAddress method is failing on the assert.
>>>> 
>>>> Ralph
>>>> 
>>>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org <ma...@apache.org> wrote:
>>>> >
>>>> > Repository: logging-log4j2
>>>> > Updated Branches:
>>>> >  refs/heads/master 367d26b09 -> 4254e2558
>>>> >
>>>> >
>>>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>>>> >
>>>> >
>>>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo <http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo>
>>>> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255>
>>>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255>
>>>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255>
>>>> >
>>>> > Branch: refs/heads/master
>>>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>>>> > Parents: 367d26b
>>>> > Author: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>>>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>>>> > Committer: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>>>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>>>> >
>>>> > ----------------------------------------------------------------------
>>>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>>>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>>>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>>>> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
>>>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>>>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>>>> > .../validators/ValidHostValidatorTest.java      | 74 ++++++++++++++++++++
>>>> > .../validators/ValidPortValidatorTest.java      | 70 ++++++++++++++++++
>>>> > src/changes/changes.xml                         |  3 +
>>>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>>>> > ----------------------------------------------------------------------
>>>> >
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java>
>>>> > ----------------------------------------------------------------------
>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>> > index 2895e52..421d711 100644
>>>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.config.plugins.convert;
>>>> > import java.io.File;
>>>> > import java.math.BigDecimal;
>>>> > import java.math.BigInteger;
>>>> > +import java.net.InetAddress;
>>>> > import java.net <http://java.net/>.MalformedURLException;
>>>> > import java.net.URI;
>>>> > import java.net.URISyntaxException;
>>>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>>>> > import java.security.Provider;
>>>> > import java.security.Security;
>>>> > import java.util.regex.Pattern;
>>>> > -
>>>> > import javax.xml.bind.DatatypeConverter;
>>>> >
>>>> > import org.apache.logging.log4j.Level;
>>>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>>>> >     }
>>>> >
>>>> >     /**
>>>> > +     * Converts a {@link String} into an {@link InetAddress}.
>>>> > +     */
>>>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>>>> > +    public static class InetAddressConverter implements TypeConverter<InetAddress> {
>>>> > +        @Override
>>>> > +        public InetAddress convert(final String s) throws Exception {
>>>> > +            return InetAddress.getByName(s);
>>>> > +        }
>>>> > +    }
>>>> > +
>>>> > +    /**
>>>> >      * Converts a {@link String} into a {@link Integer}.
>>>> >      */
>>>> >     @Plugin(name = "Integer", category = CATEGORY)
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java>
>>>> > ----------------------------------------------------------------------
>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>>> > new file mode 100644
>>>> > index 0000000..c652d40
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>>> > @@ -0,0 +1,41 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>> > + * this work for additional information regarding copyright ownership.
>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>> > + * See the license for the specific language governing permissions and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>>>> > +
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidHostValidator;
>>>> > +
>>>> > +import java.lang.annotation.*;
>>>> > +import java.net.InetAddress;
>>>> > +
>>>> > +/**
>>>> > + * Indicates that a plugin attribute must be a valid host. This relies on the same validation rules as
>>>> > + * {@link InetAddress#getByName(String)}.
>>>> > + *
>>>> > + * @since 2.8
>>>> > + */
>>>> > +@Documented
>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>> > +@Constraint(ValidHostValidator.class)
>>>> > +public @interface ValidHost {
>>>> > +
>>>> > +    /**
>>>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>>>> > +     */
>>>> > +    String message() default "The hostname is invalid";
>>>> > +}
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java>
>>>> > ----------------------------------------------------------------------
>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>>> > new file mode 100644
>>>> > index 0000000..a7c68b1
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>>> > @@ -0,0 +1,44 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>> > + * this work for additional information regarding copyright ownership.
>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>> > + * See the license for the specific language governing permissions and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>>>> > +
>>>> > +import java.lang.annotation.Documented;
>>>> > +import java.lang.annotation.ElementType;
>>>> > +import java.lang.annotation.Retention;
>>>> > +import java.lang.annotation.RetentionPolicy;
>>>> > +import java.lang.annotation.Target;
>>>> > +
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidPortValidator;
>>>> > +
>>>> > +/**
>>>> > + * Indicates that a plugin attribute must be a valid port number. A valid port number is an integer between 0 and
>>>> > + * 65535.
>>>> > + *
>>>> > + * @since 2.8
>>>> > + */
>>>> > +@Documented
>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>> > +@Constraint(ValidPortValidator.class)
>>>> > +public @interface ValidPort {
>>>> > +
>>>> > +    /**
>>>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>>>> > +     */
>>>> > +    String message() default "The port number is invalid";
>>>> > +}
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java>
>>>> > ----------------------------------------------------------------------
>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>>> > new file mode 100644
>>>> > index 0000000..3669915
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>>> > @@ -0,0 +1,58 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>> > + * this work for additional information regarding copyright ownership.
>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>> > + * See the license for the specific language governing permissions and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>> > +
>>>> > +import org.apache.logging.log4j.Logger;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>> > +
>>>> > +import java.net.InetAddress;
>>>> > +import java.net.UnknownHostException;
>>>> > +
>>>> > +/**
>>>> > + * Validator that checks an object to verify it is a valid hostname or IP address. Validation rules follow the same
>>>> > + * logic as in {@link InetAddress#getByName(String)}.
>>>> > + *
>>>> > + * @since 2.8
>>>> > + */
>>>> > +public class ValidHostValidator implements ConstraintValidator<ValidHost> {
>>>> > +
>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>> > +
>>>> > +    private ValidHost annotation;
>>>> > +
>>>> > +    @Override
>>>> > +    public void initialize(ValidHost annotation) {
>>>> > +        this.annotation = annotation;
>>>> > +    }
>>>> > +
>>>> > +    @Override
>>>> > +    public boolean isValid(String name, Object value) {
>>>> > +        if (value == null) {
>>>> > +            LOGGER.error(annotation.message());
>>>> > +            return false;
>>>> > +        }
>>>> > +        try {
>>>> > +            InetAddress.getByName(value.to <http://value.to/>String());
>>>> > +            return true;
>>>> > +        } catch (final UnknownHostException e) {
>>>> > +            LOGGER.error(annotation.message(), e);
>>>> > +            return false;
>>>> > +        }
>>>> > +    }
>>>> > +}
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java>
>>>> > ----------------------------------------------------------------------
>>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>>> > new file mode 100644
>>>> > index 0000000..f18f8fc
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>>> > @@ -0,0 +1,53 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>> > + * this work for additional information regarding copyright ownership.
>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>> > + * See the license for the specific language governing permissions and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>> > +
>>>> > +import org.apache.logging.log4j.Logger;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>> > +
>>>> > +/**
>>>> > + * Validator that checks an object to verify it is a valid port number (an integer between 0 and 65535).
>>>> > + *
>>>> > + * @since 2.8
>>>> > + */
>>>> > +public class ValidPortValidator implements ConstraintValidator<ValidPort> {
>>>> > +
>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>> > +
>>>> > +    private ValidPort annotation;
>>>> > +
>>>> > +    @Override
>>>> > +    public void initialize(final ValidPort annotation) {
>>>> > +        this.annotation = annotation;
>>>> > +    }
>>>> > +
>>>> > +    @Override
>>>> > +    public boolean isValid(final String name, final Object value) {
>>>> > +        if (!Integer.class.isInstance(value)) {
>>>> > +            LOGGER.error(annotation.message());
>>>> > +            return false;
>>>> > +        }
>>>> > +        int port = (int) value;
>>>> > +        if (port < 0 || port > 65535) {
>>>> > +            LOGGER.error(annotation.message());
>>>> > +            return false;
>>>> > +        }
>>>> > +        return true;
>>>> > +    }
>>>> > +}
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java>
>>>> > ----------------------------------------------------------------------
>>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>>> > new file mode 100644
>>>> > index 0000000..4f05d68
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>>> > @@ -0,0 +1,46 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>> > + * this work for additional information regarding copyright ownership.
>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>> > + * See the license for the specific language governing permissions and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>>>> > +
>>>> > +import java.net.InetSocketAddress;
>>>> > +
>>>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>>>> > +
>>>> > +@Plugin(name = "HostAndPort", category = "Test")
>>>> > +public class HostAndPort {
>>>> > +
>>>> > +    private final InetSocketAddress address;
>>>> > +
>>>> > +    private HostAndPort(final InetSocketAddress address) {
>>>> > +        this.address = address;
>>>> > +    }
>>>> > +
>>>> > +    public boolean isValid() {
>>>> > +        return !address.isUnresolved();
>>>> > +    }
>>>> > +
>>>> > +    @PluginFactory
>>>> > +    public static HostAndPort createPlugin(
>>>> > +        @ValidHost(message = "Unit test (host)") @PluginAttribute("host") final String host,
>>>> > +        @ValidPort(message = "Unit test (port)") @PluginAttribute("port") final int port) {
>>>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>>>> > +    }
>>>> > +}
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java>
>>>> > ----------------------------------------------------------------------
>>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>>>> > new file mode 100644
>>>> > index 0000000..3b0480d
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>>>> > @@ -0,0 +1,74 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>> > + * this work for additional information regarding copyright ownership.
>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>> > + * See the license for the specific language governing permissions and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>> > +
>>>> > +import java.util.UUID;
>>>> > +
>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>>>> > +import org.junit.Before;
>>>> > +import org.junit.Test;
>>>> > +
>>>> > +import static org.junit.Assert.*;
>>>> > +
>>>> > +public class ValidHostValidatorTest {
>>>> > +
>>>> > +    private PluginType<HostAndPort> plugin;
>>>> > +    private Node node;
>>>> > +
>>>> > +    @SuppressWarnings("unchecked")
>>>> > +    @Before
>>>> > +    public void setUp() throws Exception {
>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>> > +        manager.collectPlugins();
>>>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>>>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testNullHost() throws Exception {
>>>> > +        assertNull(buildPlugin());
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testInvalidIpAddress() throws Exception {
>>>> > +        node.getAttributes().put("host", UUID.randomUUID().toString());
>>>> > +        node.getAttributes().put("port", "1");
>>>> > +        assertNull(buildPlugin());
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testLocalhost() throws Exception {
>>>> > +        node.getAttributes().put("host", "localhost");
>>>> > +        node.getAttributes().put("port", "1");
>>>> > +        final HostAndPort hostAndPort = buildPlugin();
>>>> > +        assertNotNull(hostAndPort);
>>>> > +        assertTrue(hostAndPort.isValid());
>>>> > +    }
>>>> > +
>>>> > +    private HostAndPort buildPlugin() {
>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>> > +            .withConfiguration(new NullConfiguration())
>>>> > +            .withConfigurationNode(node)
>>>> > +            .build();
>>>> > +    }
>>>> > +}
>>>> > \ No newline at end of file
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java>
>>>> > ----------------------------------------------------------------------
>>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>>>> > new file mode 100644
>>>> > index 0000000..3aab08d
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>>>> > @@ -0,0 +1,70 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed with
>>>> > + * this work for additional information regarding copyright ownership.
>>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing, software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>>> > + * See the license for the specific language governing permissions and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>>> > +
>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>>>> > +import org.junit.Before;
>>>> > +import org.junit.Test;
>>>> > +
>>>> > +import static org.junit.Assert.*;
>>>> > +
>>>> > +public class ValidPortValidatorTest {
>>>> > +    private PluginType<HostAndPort> plugin;
>>>> > +    private Node node;
>>>> > +
>>>> > +    @SuppressWarnings("unchecked")
>>>> > +    @Before
>>>> > +    public void setUp() throws Exception {
>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>> > +        manager.collectPlugins();
>>>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>>>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>> > +        node.getAttributes().put("host", "localhost");
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testNegativePort() throws Exception {
>>>> > +        node.getAttributes().put("port", "-1");
>>>> > +        assertNull(buildPlugin());
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testValidPort() throws Exception {
>>>> > +        node.getAttributes().put("port", "10");
>>>> > +        assertNotNull(buildPlugin());
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testInvalidPort() throws Exception {
>>>> > +        node.getAttributes().put("port", "1234567890");
>>>> > +        assertNull(buildPlugin());
>>>> > +    }
>>>> > +
>>>> > +    private HostAndPort buildPlugin() {
>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>> > +            .withConfiguration(new NullConfiguration())
>>>> > +            .withConfigurationNode(node)
>>>> > +            .build();
>>>> > +    }
>>>> > +
>>>> > +}
>>>> > \ No newline at end of file
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml>
>>>> > ----------------------------------------------------------------------
>>>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>> > index c05de09..bc08dbb 100644
>>>> > --- a/src/changes/changes.xml
>>>> > +++ b/src/changes/changes.xml
>>>> > @@ -213,6 +213,9 @@
>>>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>>>> >         The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2.
>>>> >       </action>
>>>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>>>> > +        Add converters and validators related to hostnames and ports.
>>>> > +      </action>
>>>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>>>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>>>> >       </action>
>>>> >
>>>> >
>>>> 
>>>> 
>>>> 
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org <ma...@logging.apache.org>
>>>> For additional commands, e-mail: log4j-dev-help@logging.apache.org <ma...@logging.apache.org>
>>>> 
>>>> 
>>>> 
>>>> 
>>>> -- 
>>>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
>>> 
>>> 
>>> 
>>> 
>>> -- 
>>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
>> 
>> 
>> 
>> 
>> -- 
>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
> 
> 
> 
> 
> -- 
> Matt Sicker <boards@gmail.com <ma...@gmail.com>>


Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Matt Sicker <bo...@gmail.com>.
Let me know if my updated test still causes issues.

On 9 January 2017 at 20:30, Apache <ra...@dslextreme.com> wrote:

> I suspect this is my ISP at work.  When I enter an invalid domain I get a
> web site from cox.net saying it can’t find the domain and it offers
> suggestions for other sites.
>
> I think this test has to be removed.
>
> Ralph
>
> On Jan 9, 2017, at 7:28 PM, Matt Sicker <bo...@gmail.com> wrote:
>
> Well technically a UUID can be a valid hostname itself besides being
> interpreted as a hex-encoded IP address (if the rest of it gets chopped
> off). I've updated the test to use a jumble of invalid hostname characters
> instead.
>
> On 9 January 2017 at 20:26, Apache <ra...@dslextreme.com> wrote:
>
>> nslookup dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>> Server: 2001:578:3f::30
>> Address: 2001:578:3f::30#53
>>
>> Non-authoritative answer:
>> Name: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>> Address: 92.242.140.2
>>
>>
>>
>> On Jan 9, 2017, at 7:24 PM, Matt Sicker <bo...@gmail.com> wrote:
>>
>> I suppose technically the first part of a UUID can be an IPv4 address
>> encoded in hex. I'll make a better invalid value.
>>
>> On 9 January 2017 at 20:20, Apache <ra...@dslextreme.com> wrote:
>>
>>> Debugging this and InetAddress.getByName is returning an InetAddress
>>> object. The value is
>>> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>>> Address: 92.242.140.2
>>>
>>> Ralph
>>>
>>> On Jan 9, 2017, at 7:12 PM, Matt Sicker <bo...@gmail.com> wrote:
>>>
>>> Not sure how that's possible, but I added a better assert message. Let
>>> me know if you're still having an issue with it. Is it a failure from
>>> IntelliJ or Maven?
>>>
>>> On 9 January 2017 at 19:47, Apache <ra...@dslextreme.com> wrote:
>>>
>>>> This commit appears to be failing for me. The testInvalidIpAddress
>>>> method is failing on the assert.
>>>>
>>>> Ralph
>>>>
>>>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org wrote:
>>>> >
>>>> > Repository: logging-log4j2
>>>> > Updated Branches:
>>>> >  refs/heads/master 367d26b09 -> 4254e2558
>>>> >
>>>> >
>>>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>>>> >
>>>> >
>>>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>>>> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit
>>>> /4254e255
>>>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4
>>>> 254e255
>>>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4
>>>> 254e255
>>>> >
>>>> > Branch: refs/heads/master
>>>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>>>> > Parents: 367d26b
>>>> > Author: Matt Sicker <ma...@spr.com>
>>>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>>>> > Committer: Matt Sicker <ma...@spr.com>
>>>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>>>> >
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>>>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>>>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>>>> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
>>>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>>>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>>>> > .../validators/ValidHostValidatorTest.java      | 74
>>>> ++++++++++++++++++++
>>>> > .../validators/ValidPortValidatorTest.java      | 70
>>>> ++++++++++++++++++
>>>> > src/changes/changes.xml                         |  3 +
>>>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> >
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>> re/config/plugins/convert/TypeConverters.java
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> > diff --git a/log4j-core/src/main/java/org
>>>> /apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/convert/TypeConverters.java
>>>> > index 2895e52..421d711 100644
>>>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/convert/TypeConverters.java
>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/convert/TypeConverters.java
>>>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.
>>>> config.plugins.convert;
>>>> > import java.io.File;
>>>> > import java.math.BigDecimal;
>>>> > import java.math.BigInteger;
>>>> > +import java.net.InetAddress;
>>>> > import java.net.MalformedURLException;
>>>> > import java.net.URI;
>>>> > import java.net.URISyntaxException;
>>>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>>>> > import java.security.Provider;
>>>> > import java.security.Security;
>>>> > import java.util.regex.Pattern;
>>>> > -
>>>> > import javax.xml.bind.DatatypeConverter;
>>>> >
>>>> > import org.apache.logging.log4j.Level;
>>>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>>>> >     }
>>>> >
>>>> >     /**
>>>> > +     * Converts a {@link String} into an {@link InetAddress}.
>>>> > +     */
>>>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>>>> > +    public static class InetAddressConverter implements
>>>> TypeConverter<InetAddress> {
>>>> > +        @Override
>>>> > +        public InetAddress convert(final String s) throws Exception {
>>>> > +            return InetAddress.getByName(s);
>>>> > +        }
>>>> > +    }
>>>> > +
>>>> > +    /**
>>>> >      * Converts a {@link String} into a {@link Integer}.
>>>> >      */
>>>> >     @Plugin(name = "Integer", category = CATEGORY)
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>> re/config/plugins/validation/constraints/ValidHost.java
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> > diff --git a/log4j-core/src/main/java/org
>>>> /apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/constraints/ValidHost.java
>>>> > new file mode 100644
>>>> > index 0000000..c652d40
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/constraints/ValidHost.java
>>>> > @@ -0,0 +1,41 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>> with
>>>> > + * this work for additional information regarding copyright
>>>> ownership.
>>>> > + * The ASF licenses this file to You under the Apache license,
>>>> Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance
>>>> with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing,
>>>> software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>> implied.
>>>> > + * See the license for the specific language governing permissions
>>>> and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.cons
>>>> traints;
>>>> > +
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>>> traint;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.vali
>>>> dators.ValidHostValidator;
>>>> > +
>>>> > +import java.lang.annotation.*;
>>>> > +import java.net.InetAddress;
>>>> > +
>>>> > +/**
>>>> > + * Indicates that a plugin attribute must be a valid host. This
>>>> relies on the same validation rules as
>>>> > + * {@link InetAddress#getByName(String)}.
>>>> > + *
>>>> > + * @since 2.8
>>>> > + */
>>>> > +@Documented
>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>> > +@Constraint(ValidHostValidator.class)
>>>> > +public @interface ValidHost {
>>>> > +
>>>> > +    /**
>>>> > +     * The message to be logged if this constraint is violated. This
>>>> should normally be overridden.
>>>> > +     */
>>>> > +    String message() default "The hostname is invalid";
>>>> > +}
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>> re/config/plugins/validation/constraints/ValidPort.java
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> > diff --git a/log4j-core/src/main/java/org
>>>> /apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/constraints/ValidPort.java
>>>> > new file mode 100644
>>>> > index 0000000..a7c68b1
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/constraints/ValidPort.java
>>>> > @@ -0,0 +1,44 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>> with
>>>> > + * this work for additional information regarding copyright
>>>> ownership.
>>>> > + * The ASF licenses this file to You under the Apache license,
>>>> Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance
>>>> with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing,
>>>> software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>> implied.
>>>> > + * See the license for the specific language governing permissions
>>>> and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.cons
>>>> traints;
>>>> > +
>>>> > +import java.lang.annotation.Documented;
>>>> > +import java.lang.annotation.ElementType;
>>>> > +import java.lang.annotation.Retention;
>>>> > +import java.lang.annotation.RetentionPolicy;
>>>> > +import java.lang.annotation.Target;
>>>> > +
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>>> traint;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.vali
>>>> dators.ValidPortValidator;
>>>> > +
>>>> > +/**
>>>> > + * Indicates that a plugin attribute must be a valid port number. A
>>>> valid port number is an integer between 0 and
>>>> > + * 65535.
>>>> > + *
>>>> > + * @since 2.8
>>>> > + */
>>>> > +@Documented
>>>> > +@Retention(RetentionPolicy.RUNTIME)
>>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>>> > +@Constraint(ValidPortValidator.class)
>>>> > +public @interface ValidPort {
>>>> > +
>>>> > +    /**
>>>> > +     * The message to be logged if this constraint is violated. This
>>>> should normally be overridden.
>>>> > +     */
>>>> > +    String message() default "The port number is invalid";
>>>> > +}
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>> re/config/plugins/validation/validators/ValidHostValidator.java
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> > diff --git a/log4j-core/src/main/java/org
>>>> /apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/validators/ValidHostValidator.java
>>>> > new file mode 100644
>>>> > index 0000000..3669915
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/validators/ValidHostValidator.java
>>>> > @@ -0,0 +1,58 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>> with
>>>> > + * this work for additional information regarding copyright
>>>> ownership.
>>>> > + * The ASF licenses this file to You under the Apache license,
>>>> Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance
>>>> with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing,
>>>> software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>> implied.
>>>> > + * See the license for the specific language governing permissions
>>>> and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>>>> dators;
>>>> > +
>>>> > +import org.apache.logging.log4j.Logger;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>>> traintValidator;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>>> traints.ValidHost;
>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>> > +
>>>> > +import java.net.InetAddress;
>>>> > +import java.net.UnknownHostException;
>>>> > +
>>>> > +/**
>>>> > + * Validator that checks an object to verify it is a valid hostname
>>>> or IP address. Validation rules follow the same
>>>> > + * logic as in {@link InetAddress#getByName(String)}.
>>>> > + *
>>>> > + * @since 2.8
>>>> > + */
>>>> > +public class ValidHostValidator implements
>>>> ConstraintValidator<ValidHost> {
>>>> > +
>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>> > +
>>>> > +    private ValidHost annotation;
>>>> > +
>>>> > +    @Override
>>>> > +    public void initialize(ValidHost annotation) {
>>>> > +        this.annotation = annotation;
>>>> > +    }
>>>> > +
>>>> > +    @Override
>>>> > +    public boolean isValid(String name, Object value) {
>>>> > +        if (value == null) {
>>>> > +            LOGGER.error(annotation.message());
>>>> > +            return false;
>>>> > +        }
>>>> > +        try {
>>>> > +            InetAddress.getByName(value.toString());
>>>> > +            return true;
>>>> > +        } catch (final UnknownHostException e) {
>>>> > +            LOGGER.error(annotation.message(), e);
>>>> > +            return false;
>>>> > +        }
>>>> > +    }
>>>> > +}
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>>> re/config/plugins/validation/validators/ValidPortValidator.java
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> > diff --git a/log4j-core/src/main/java/org
>>>> /apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/validators/ValidPortValidator.java
>>>> > new file mode 100644
>>>> > index 0000000..f18f8fc
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/validators/ValidPortValidator.java
>>>> > @@ -0,0 +1,53 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>> with
>>>> > + * this work for additional information regarding copyright
>>>> ownership.
>>>> > + * The ASF licenses this file to You under the Apache license,
>>>> Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance
>>>> with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing,
>>>> software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>> implied.
>>>> > + * See the license for the specific language governing permissions
>>>> and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>>>> dators;
>>>> > +
>>>> > +import org.apache.logging.log4j.Logger;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>>> traintValidator;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>>> traints.ValidPort;
>>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>>> > +
>>>> > +/**
>>>> > + * Validator that checks an object to verify it is a valid port
>>>> number (an integer between 0 and 65535).
>>>> > + *
>>>> > + * @since 2.8
>>>> > + */
>>>> > +public class ValidPortValidator implements
>>>> ConstraintValidator<ValidPort> {
>>>> > +
>>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>>> > +
>>>> > +    private ValidPort annotation;
>>>> > +
>>>> > +    @Override
>>>> > +    public void initialize(final ValidPort annotation) {
>>>> > +        this.annotation = annotation;
>>>> > +    }
>>>> > +
>>>> > +    @Override
>>>> > +    public boolean isValid(final String name, final Object value) {
>>>> > +        if (!Integer.class.isInstance(value)) {
>>>> > +            LOGGER.error(annotation.message());
>>>> > +            return false;
>>>> > +        }
>>>> > +        int port = (int) value;
>>>> > +        if (port < 0 || port > 65535) {
>>>> > +            LOGGER.error(annotation.message());
>>>> > +            return false;
>>>> > +        }
>>>> > +        return true;
>>>> > +    }
>>>> > +}
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>>> re/config/plugins/validation/HostAndPort.java
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> > diff --git a/log4j-core/src/test/java/org
>>>> /apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>>> b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/HostAndPort.java
>>>> > new file mode 100644
>>>> > index 0000000..4f05d68
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/HostAndPort.java
>>>> > @@ -0,0 +1,46 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>> with
>>>> > + * this work for additional information regarding copyright
>>>> ownership.
>>>> > + * The ASF licenses this file to You under the Apache license,
>>>> Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance
>>>> with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing,
>>>> software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>> implied.
>>>> > + * See the license for the specific language governing permissions
>>>> and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>>>> > +
>>>> > +import java.net.InetSocketAddress;
>>>> > +
>>>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
>>>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>>> traints.ValidHost;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>>> traints.ValidPort;
>>>> > +
>>>> > +@Plugin(name = "HostAndPort", category = "Test")
>>>> > +public class HostAndPort {
>>>> > +
>>>> > +    private final InetSocketAddress address;
>>>> > +
>>>> > +    private HostAndPort(final InetSocketAddress address) {
>>>> > +        this.address = address;
>>>> > +    }
>>>> > +
>>>> > +    public boolean isValid() {
>>>> > +        return !address.isUnresolved();
>>>> > +    }
>>>> > +
>>>> > +    @PluginFactory
>>>> > +    public static HostAndPort createPlugin(
>>>> > +        @ValidHost(message = "Unit test (host)")
>>>> @PluginAttribute("host") final String host,
>>>> > +        @ValidPort(message = "Unit test (port)")
>>>> @PluginAttribute("port") final int port) {
>>>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>>>> > +    }
>>>> > +}
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>>> re/config/plugins/validation/validators/ValidHostValidatorTest.java
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> > diff --git a/log4j-core/src/test/java/org
>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>> ors/ValidHostValidatorTest.java b/log4j-core/src/test/java/org
>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>> ors/ValidHostValidatorTest.java
>>>> > new file mode 100644
>>>> > index 0000000..3b0480d
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/validators/ValidHostValidatorTest.java
>>>> > @@ -0,0 +1,74 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>> with
>>>> > + * this work for additional information regarding copyright
>>>> ownership.
>>>> > + * The ASF licenses this file to You under the Apache license,
>>>> Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance
>>>> with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing,
>>>> software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>> implied.
>>>> > + * See the license for the specific language governing permissions
>>>> and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>>>> dators;
>>>> > +
>>>> > +import java.util.UUID;
>>>> > +
>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuil
>>>> der;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginMana
>>>> ger;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Host
>>>> AndPort;
>>>> > +import org.junit.Before;
>>>> > +import org.junit.Test;
>>>> > +
>>>> > +import static org.junit.Assert.*;
>>>> > +
>>>> > +public class ValidHostValidatorTest {
>>>> > +
>>>> > +    private PluginType<HostAndPort> plugin;
>>>> > +    private Node node;
>>>> > +
>>>> > +    @SuppressWarnings("unchecked")
>>>> > +    @Before
>>>> > +    public void setUp() throws Exception {
>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>> > +        manager.collectPlugins();
>>>> > +        plugin = (PluginType<HostAndPort>)
>>>> manager.getPluginType("HostAndPort");
>>>> > +        assertNotNull("Rebuild this module to ensure annotation
>>>> processing has been done.", plugin);
>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testNullHost() throws Exception {
>>>> > +        assertNull(buildPlugin());
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testInvalidIpAddress() throws Exception {
>>>> > +        node.getAttributes().put("host",
>>>> UUID.randomUUID().toString());
>>>> > +        node.getAttributes().put("port", "1");
>>>> > +        assertNull(buildPlugin());
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testLocalhost() throws Exception {
>>>> > +        node.getAttributes().put("host", "localhost");
>>>> > +        node.getAttributes().put("port", "1");
>>>> > +        final HostAndPort hostAndPort = buildPlugin();
>>>> > +        assertNotNull(hostAndPort);
>>>> > +        assertTrue(hostAndPort.isValid());
>>>> > +    }
>>>> > +
>>>> > +    private HostAndPort buildPlugin() {
>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>> > +            .withConfiguration(new NullConfiguration())
>>>> > +            .withConfigurationNode(node)
>>>> > +            .build();
>>>> > +    }
>>>> > +}
>>>> > \ No newline at end of file
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>>> re/config/plugins/validation/validators/ValidPortValidatorTest.java
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> > diff --git a/log4j-core/src/test/java/org
>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>> ors/ValidPortValidatorTest.java b/log4j-core/src/test/java/org
>>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>>> ors/ValidPortValidatorTest.java
>>>> > new file mode 100644
>>>> > index 0000000..3aab08d
>>>> > --- /dev/null
>>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>>> fig/plugins/validation/validators/ValidPortValidatorTest.java
>>>> > @@ -0,0 +1,70 @@
>>>> > +/*
>>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>>> > + * contributor license agreements. See the NOTICE file distributed
>>>> with
>>>> > + * this work for additional information regarding copyright
>>>> ownership.
>>>> > + * The ASF licenses this file to You under the Apache license,
>>>> Version 2.0
>>>> > + * (the "License"); you may not use this file except in compliance
>>>> with
>>>> > + * the License. You may obtain a copy of the License at
>>>> > + *
>>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>>> > + *
>>>> > + * Unless required by applicable law or agreed to in writing,
>>>> software
>>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>>> implied.
>>>> > + * See the license for the specific language governing permissions
>>>> and
>>>> > + * limitations under the license.
>>>> > + */
>>>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>>>> dators;
>>>> > +
>>>> > +import org.apache.logging.log4j.core.config.Node;
>>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuil
>>>> der;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginMana
>>>> ger;
>>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Host
>>>> AndPort;
>>>> > +import org.junit.Before;
>>>> > +import org.junit.Test;
>>>> > +
>>>> > +import static org.junit.Assert.*;
>>>> > +
>>>> > +public class ValidPortValidatorTest {
>>>> > +    private PluginType<HostAndPort> plugin;
>>>> > +    private Node node;
>>>> > +
>>>> > +    @SuppressWarnings("unchecked")
>>>> > +    @Before
>>>> > +    public void setUp() throws Exception {
>>>> > +        final PluginManager manager = new PluginManager("Test");
>>>> > +        manager.collectPlugins();
>>>> > +        plugin = (PluginType<HostAndPort>)
>>>> manager.getPluginType("HostAndPort");
>>>> > +        assertNotNull("Rebuild this module to ensure annotation
>>>> processing has been done.", plugin);
>>>> > +        node = new Node(null, "HostAndPort", plugin);
>>>> > +        node.getAttributes().put("host", "localhost");
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testNegativePort() throws Exception {
>>>> > +        node.getAttributes().put("port", "-1");
>>>> > +        assertNull(buildPlugin());
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testValidPort() throws Exception {
>>>> > +        node.getAttributes().put("port", "10");
>>>> > +        assertNotNull(buildPlugin());
>>>> > +    }
>>>> > +
>>>> > +    @Test
>>>> > +    public void testInvalidPort() throws Exception {
>>>> > +        node.getAttributes().put("port", "1234567890");
>>>> > +        assertNull(buildPlugin());
>>>> > +    }
>>>> > +
>>>> > +    private HostAndPort buildPlugin() {
>>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>>> > +            .withConfiguration(new NullConfiguration())
>>>> > +            .withConfigurationNode(node)
>>>> > +            .build();
>>>> > +    }
>>>> > +
>>>> > +}
>>>> > \ No newline at end of file
>>>> >
>>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>>> 254e255/src/changes/changes.xml
>>>> > ------------------------------------------------------------
>>>> ----------
>>>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>>> > index c05de09..bc08dbb 100644
>>>> > --- a/src/changes/changes.xml
>>>> > +++ b/src/changes/changes.xml
>>>> > @@ -213,6 +213,9 @@
>>>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>>>> >         The log4j-slf4j-impl module now declares a runtime dependency
>>>> on log4j-core. While not technically required, this makes the
>>>> log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates
>>>> migration to Log4j 2.
>>>> >       </action>
>>>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>>>> > +        Add converters and validators related to hostnames and ports.
>>>> > +      </action>
>>>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>>>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>>>> >       </action>
>>>> >
>>>> >
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
>>>> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>>>>
>>>>
>>>
>>>
>>> --
>>> Matt Sicker <bo...@gmail.com>
>>>
>>>
>>>
>>
>>
>> --
>> Matt Sicker <bo...@gmail.com>
>>
>>
>>
>
>
> --
> Matt Sicker <bo...@gmail.com>
>
>
>


-- 
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Apache <ra...@dslextreme.com>.
I suspect this is my ISP at work.  When I enter an invalid domain I get a web site from cox.net <http://cox.net/> saying it can’t find the domain and it offers suggestions for other sites.

I think this test has to be removed.

Ralph

> On Jan 9, 2017, at 7:28 PM, Matt Sicker <bo...@gmail.com> wrote:
> 
> Well technically a UUID can be a valid hostname itself besides being interpreted as a hex-encoded IP address (if the rest of it gets chopped off). I've updated the test to use a jumble of invalid hostname characters instead.
> 
> On 9 January 2017 at 20:26, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
> nslookup dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
> Server:		2001:578:3f::30
> Address:	2001:578:3f::30#53
> 
> Non-authoritative answer:
> Name:	dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
> Address: 92.242.140.2
> 
> 
> 
>> On Jan 9, 2017, at 7:24 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>> 
>> I suppose technically the first part of a UUID can be an IPv4 address encoded in hex. I'll make a better invalid value.
>> 
>> On 9 January 2017 at 20:20, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>> Debugging this and InetAddress.getByName is returning an InetAddress object. The value is
>> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>> Address: 92.242.140.2
>> 
>> Ralph
>> 
>>> On Jan 9, 2017, at 7:12 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>>> 
>>> Not sure how that's possible, but I added a better assert message. Let me know if you're still having an issue with it. Is it a failure from IntelliJ or Maven?
>>> 
>>> On 9 January 2017 at 19:47, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>>> This commit appears to be failing for me. The testInvalidIpAddress method is failing on the assert.
>>> 
>>> Ralph
>>> 
>>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org <ma...@apache.org> wrote:
>>> >
>>> > Repository: logging-log4j2
>>> > Updated Branches:
>>> >  refs/heads/master 367d26b09 -> 4254e2558
>>> >
>>> >
>>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>>> >
>>> >
>>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo <http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo>
>>> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255>
>>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255>
>>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255>
>>> >
>>> > Branch: refs/heads/master
>>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>>> > Parents: 367d26b
>>> > Author: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>>> > Committer: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>>> >
>>> > ----------------------------------------------------------------------
>>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>>> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
>>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>>> > .../validators/ValidHostValidatorTest.java      | 74 ++++++++++++++++++++
>>> > .../validators/ValidPortValidatorTest.java      | 70 ++++++++++++++++++
>>> > src/changes/changes.xml                         |  3 +
>>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>>> > ----------------------------------------------------------------------
>>> >
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java>
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>> > index 2895e52..421d711 100644
>>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.config.plugins.convert;
>>> > import java.io.File;
>>> > import java.math.BigDecimal;
>>> > import java.math.BigInteger;
>>> > +import java.net.InetAddress;
>>> > import java.net <http://java.net/>.MalformedURLException;
>>> > import java.net.URI;
>>> > import java.net.URISyntaxException;
>>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>>> > import java.security.Provider;
>>> > import java.security.Security;
>>> > import java.util.regex.Pattern;
>>> > -
>>> > import javax.xml.bind.DatatypeConverter;
>>> >
>>> > import org.apache.logging.log4j.Level;
>>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>>> >     }
>>> >
>>> >     /**
>>> > +     * Converts a {@link String} into an {@link InetAddress}.
>>> > +     */
>>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>>> > +    public static class InetAddressConverter implements TypeConverter<InetAddress> {
>>> > +        @Override
>>> > +        public InetAddress convert(final String s) throws Exception {
>>> > +            return InetAddress.getByName(s);
>>> > +        }
>>> > +    }
>>> > +
>>> > +    /**
>>> >      * Converts a {@link String} into a {@link Integer}.
>>> >      */
>>> >     @Plugin(name = "Integer", category = CATEGORY)
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java>
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>> > new file mode 100644
>>> > index 0000000..c652d40
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>> > @@ -0,0 +1,41 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>>> > +
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidHostValidator;
>>> > +
>>> > +import java.lang.annotation.*;
>>> > +import java.net.InetAddress;
>>> > +
>>> > +/**
>>> > + * Indicates that a plugin attribute must be a valid host. This relies on the same validation rules as
>>> > + * {@link InetAddress#getByName(String)}.
>>> > + *
>>> > + * @since 2.8
>>> > + */
>>> > +@Documented
>>> > +@Retention(RetentionPolicy.RUNTIME)
>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>> > +@Constraint(ValidHostValidator.class)
>>> > +public @interface ValidHost {
>>> > +
>>> > +    /**
>>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>>> > +     */
>>> > +    String message() default "The hostname is invalid";
>>> > +}
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java>
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>> > new file mode 100644
>>> > index 0000000..a7c68b1
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>> > @@ -0,0 +1,44 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>>> > +
>>> > +import java.lang.annotation.Documented;
>>> > +import java.lang.annotation.ElementType;
>>> > +import java.lang.annotation.Retention;
>>> > +import java.lang.annotation.RetentionPolicy;
>>> > +import java.lang.annotation.Target;
>>> > +
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidPortValidator;
>>> > +
>>> > +/**
>>> > + * Indicates that a plugin attribute must be a valid port number. A valid port number is an integer between 0 and
>>> > + * 65535.
>>> > + *
>>> > + * @since 2.8
>>> > + */
>>> > +@Documented
>>> > +@Retention(RetentionPolicy.RUNTIME)
>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>> > +@Constraint(ValidPortValidator.class)
>>> > +public @interface ValidPort {
>>> > +
>>> > +    /**
>>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>>> > +     */
>>> > +    String message() default "The port number is invalid";
>>> > +}
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java>
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>> > new file mode 100644
>>> > index 0000000..3669915
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>> > @@ -0,0 +1,58 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>> > +
>>> > +import org.apache.logging.log4j.Logger;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>> > +
>>> > +import java.net.InetAddress;
>>> > +import java.net.UnknownHostException;
>>> > +
>>> > +/**
>>> > + * Validator that checks an object to verify it is a valid hostname or IP address. Validation rules follow the same
>>> > + * logic as in {@link InetAddress#getByName(String)}.
>>> > + *
>>> > + * @since 2.8
>>> > + */
>>> > +public class ValidHostValidator implements ConstraintValidator<ValidHost> {
>>> > +
>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>> > +
>>> > +    private ValidHost annotation;
>>> > +
>>> > +    @Override
>>> > +    public void initialize(ValidHost annotation) {
>>> > +        this.annotation = annotation;
>>> > +    }
>>> > +
>>> > +    @Override
>>> > +    public boolean isValid(String name, Object value) {
>>> > +        if (value == null) {
>>> > +            LOGGER.error(annotation.message());
>>> > +            return false;
>>> > +        }
>>> > +        try {
>>> > +            InetAddress.getByName(value.to <http://value.to/>String());
>>> > +            return true;
>>> > +        } catch (final UnknownHostException e) {
>>> > +            LOGGER.error(annotation.message(), e);
>>> > +            return false;
>>> > +        }
>>> > +    }
>>> > +}
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java>
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>> > new file mode 100644
>>> > index 0000000..f18f8fc
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>> > @@ -0,0 +1,53 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>> > +
>>> > +import org.apache.logging.log4j.Logger;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>> > +
>>> > +/**
>>> > + * Validator that checks an object to verify it is a valid port number (an integer between 0 and 65535).
>>> > + *
>>> > + * @since 2.8
>>> > + */
>>> > +public class ValidPortValidator implements ConstraintValidator<ValidPort> {
>>> > +
>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>> > +
>>> > +    private ValidPort annotation;
>>> > +
>>> > +    @Override
>>> > +    public void initialize(final ValidPort annotation) {
>>> > +        this.annotation = annotation;
>>> > +    }
>>> > +
>>> > +    @Override
>>> > +    public boolean isValid(final String name, final Object value) {
>>> > +        if (!Integer.class.isInstance(value)) {
>>> > +            LOGGER.error(annotation.message());
>>> > +            return false;
>>> > +        }
>>> > +        int port = (int) value;
>>> > +        if (port < 0 || port > 65535) {
>>> > +            LOGGER.error(annotation.message());
>>> > +            return false;
>>> > +        }
>>> > +        return true;
>>> > +    }
>>> > +}
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java>
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>> > new file mode 100644
>>> > index 0000000..4f05d68
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>> > @@ -0,0 +1,46 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>>> > +
>>> > +import java.net.InetSocketAddress;
>>> > +
>>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>>> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
>>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>>> > +
>>> > +@Plugin(name = "HostAndPort", category = "Test")
>>> > +public class HostAndPort {
>>> > +
>>> > +    private final InetSocketAddress address;
>>> > +
>>> > +    private HostAndPort(final InetSocketAddress address) {
>>> > +        this.address = address;
>>> > +    }
>>> > +
>>> > +    public boolean isValid() {
>>> > +        return !address.isUnresolved();
>>> > +    }
>>> > +
>>> > +    @PluginFactory
>>> > +    public static HostAndPort createPlugin(
>>> > +        @ValidHost(message = "Unit test (host)") @PluginAttribute("host") final String host,
>>> > +        @ValidPort(message = "Unit test (port)") @PluginAttribute("port") final int port) {
>>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>>> > +    }
>>> > +}
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java>
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>>> > new file mode 100644
>>> > index 0000000..3b0480d
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>>> > @@ -0,0 +1,74 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>> > +
>>> > +import java.util.UUID;
>>> > +
>>> > +import org.apache.logging.log4j.core.config.Node;
>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>>> > +import org.junit.Before;
>>> > +import org.junit.Test;
>>> > +
>>> > +import static org.junit.Assert.*;
>>> > +
>>> > +public class ValidHostValidatorTest {
>>> > +
>>> > +    private PluginType<HostAndPort> plugin;
>>> > +    private Node node;
>>> > +
>>> > +    @SuppressWarnings("unchecked")
>>> > +    @Before
>>> > +    public void setUp() throws Exception {
>>> > +        final PluginManager manager = new PluginManager("Test");
>>> > +        manager.collectPlugins();
>>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>>> > +        node = new Node(null, "HostAndPort", plugin);
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testNullHost() throws Exception {
>>> > +        assertNull(buildPlugin());
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testInvalidIpAddress() throws Exception {
>>> > +        node.getAttributes().put("host", UUID.randomUUID().toString());
>>> > +        node.getAttributes().put("port", "1");
>>> > +        assertNull(buildPlugin());
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testLocalhost() throws Exception {
>>> > +        node.getAttributes().put("host", "localhost");
>>> > +        node.getAttributes().put("port", "1");
>>> > +        final HostAndPort hostAndPort = buildPlugin();
>>> > +        assertNotNull(hostAndPort);
>>> > +        assertTrue(hostAndPort.isValid());
>>> > +    }
>>> > +
>>> > +    private HostAndPort buildPlugin() {
>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>> > +            .withConfiguration(new NullConfiguration())
>>> > +            .withConfigurationNode(node)
>>> > +            .build();
>>> > +    }
>>> > +}
>>> > \ No newline at end of file
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java>
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>>> > new file mode 100644
>>> > index 0000000..3aab08d
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>>> > @@ -0,0 +1,70 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>>> > +
>>> > +import org.apache.logging.log4j.core.config.Node;
>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>>> > +import org.junit.Before;
>>> > +import org.junit.Test;
>>> > +
>>> > +import static org.junit.Assert.*;
>>> > +
>>> > +public class ValidPortValidatorTest {
>>> > +    private PluginType<HostAndPort> plugin;
>>> > +    private Node node;
>>> > +
>>> > +    @SuppressWarnings("unchecked")
>>> > +    @Before
>>> > +    public void setUp() throws Exception {
>>> > +        final PluginManager manager = new PluginManager("Test");
>>> > +        manager.collectPlugins();
>>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>>> > +        node = new Node(null, "HostAndPort", plugin);
>>> > +        node.getAttributes().put("host", "localhost");
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testNegativePort() throws Exception {
>>> > +        node.getAttributes().put("port", "-1");
>>> > +        assertNull(buildPlugin());
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testValidPort() throws Exception {
>>> > +        node.getAttributes().put("port", "10");
>>> > +        assertNotNull(buildPlugin());
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testInvalidPort() throws Exception {
>>> > +        node.getAttributes().put("port", "1234567890");
>>> > +        assertNull(buildPlugin());
>>> > +    }
>>> > +
>>> > +    private HostAndPort buildPlugin() {
>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>> > +            .withConfiguration(new NullConfiguration())
>>> > +            .withConfigurationNode(node)
>>> > +            .build();
>>> > +    }
>>> > +
>>> > +}
>>> > \ No newline at end of file
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml>
>>> > ----------------------------------------------------------------------
>>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>> > index c05de09..bc08dbb 100644
>>> > --- a/src/changes/changes.xml
>>> > +++ b/src/changes/changes.xml
>>> > @@ -213,6 +213,9 @@
>>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>>> >         The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2.
>>> >       </action>
>>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>>> > +        Add converters and validators related to hostnames and ports.
>>> > +      </action>
>>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>>> >       </action>
>>> >
>>> >
>>> 
>>> 
>>> 
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org <ma...@logging.apache.org>
>>> For additional commands, e-mail: log4j-dev-help@logging.apache.org <ma...@logging.apache.org>
>>> 
>>> 
>>> 
>>> 
>>> -- 
>>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
>> 
>> 
>> 
>> 
>> -- 
>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
> 
> 
> 
> 
> -- 
> Matt Sicker <boards@gmail.com <ma...@gmail.com>>


Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Matt Sicker <bo...@gmail.com>.
Well technically a UUID can be a valid hostname itself besides being
interpreted as a hex-encoded IP address (if the rest of it gets chopped
off). I've updated the test to use a jumble of invalid hostname characters
instead.

On 9 January 2017 at 20:26, Apache <ra...@dslextreme.com> wrote:

> nslookup dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
> Server: 2001:578:3f::30
> Address: 2001:578:3f::30#53
>
> Non-authoritative answer:
> Name: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
> Address: 92.242.140.2
>
>
>
> On Jan 9, 2017, at 7:24 PM, Matt Sicker <bo...@gmail.com> wrote:
>
> I suppose technically the first part of a UUID can be an IPv4 address
> encoded in hex. I'll make a better invalid value.
>
> On 9 January 2017 at 20:20, Apache <ra...@dslextreme.com> wrote:
>
>> Debugging this and InetAddress.getByName is returning an InetAddress
>> object. The value is
>> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
>> Address: 92.242.140.2
>>
>> Ralph
>>
>> On Jan 9, 2017, at 7:12 PM, Matt Sicker <bo...@gmail.com> wrote:
>>
>> Not sure how that's possible, but I added a better assert message. Let me
>> know if you're still having an issue with it. Is it a failure from IntelliJ
>> or Maven?
>>
>> On 9 January 2017 at 19:47, Apache <ra...@dslextreme.com> wrote:
>>
>>> This commit appears to be failing for me. The testInvalidIpAddress
>>> method is failing on the assert.
>>>
>>> Ralph
>>>
>>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org wrote:
>>> >
>>> > Repository: logging-log4j2
>>> > Updated Branches:
>>> >  refs/heads/master 367d26b09 -> 4254e2558
>>> >
>>> >
>>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>>> >
>>> >
>>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>>> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit
>>> /4254e255
>>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4
>>> 254e255
>>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4
>>> 254e255
>>> >
>>> > Branch: refs/heads/master
>>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>>> > Parents: 367d26b
>>> > Author: Matt Sicker <ma...@spr.com>
>>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>>> > Committer: Matt Sicker <ma...@spr.com>
>>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>>> >
>>> > ----------------------------------------------------------------------
>>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>>> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
>>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>>> > .../validators/ValidHostValidatorTest.java      | 74
>>> ++++++++++++++++++++
>>> > .../validators/ValidPortValidatorTest.java      | 70
>>> ++++++++++++++++++
>>> > src/changes/changes.xml                         |  3 +
>>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>>> > ----------------------------------------------------------------------
>>> >
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>> re/config/plugins/convert/TypeConverters.java
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/main/java/org
>>> /apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/convert/TypeConverters.java
>>> > index 2895e52..421d711 100644
>>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/convert/TypeConverters.java
>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/convert/TypeConverters.java
>>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.
>>> config.plugins.convert;
>>> > import java.io.File;
>>> > import java.math.BigDecimal;
>>> > import java.math.BigInteger;
>>> > +import java.net.InetAddress;
>>> > import java.net.MalformedURLException;
>>> > import java.net.URI;
>>> > import java.net.URISyntaxException;
>>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>>> > import java.security.Provider;
>>> > import java.security.Security;
>>> > import java.util.regex.Pattern;
>>> > -
>>> > import javax.xml.bind.DatatypeConverter;
>>> >
>>> > import org.apache.logging.log4j.Level;
>>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>>> >     }
>>> >
>>> >     /**
>>> > +     * Converts a {@link String} into an {@link InetAddress}.
>>> > +     */
>>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>>> > +    public static class InetAddressConverter implements
>>> TypeConverter<InetAddress> {
>>> > +        @Override
>>> > +        public InetAddress convert(final String s) throws Exception {
>>> > +            return InetAddress.getByName(s);
>>> > +        }
>>> > +    }
>>> > +
>>> > +    /**
>>> >      * Converts a {@link String} into a {@link Integer}.
>>> >      */
>>> >     @Plugin(name = "Integer", category = CATEGORY)
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>> re/config/plugins/validation/constraints/ValidHost.java
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/main/java/org
>>> /apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/constraints/ValidHost.java
>>> > new file mode 100644
>>> > index 0000000..c652d40
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/constraints/ValidHost.java
>>> > @@ -0,0 +1,41 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed
>>> with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license,
>>> Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance
>>> with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>> implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.cons
>>> traints;
>>> > +
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>> traint;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.vali
>>> dators.ValidHostValidator;
>>> > +
>>> > +import java.lang.annotation.*;
>>> > +import java.net.InetAddress;
>>> > +
>>> > +/**
>>> > + * Indicates that a plugin attribute must be a valid host. This
>>> relies on the same validation rules as
>>> > + * {@link InetAddress#getByName(String)}.
>>> > + *
>>> > + * @since 2.8
>>> > + */
>>> > +@Documented
>>> > +@Retention(RetentionPolicy.RUNTIME)
>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>> > +@Constraint(ValidHostValidator.class)
>>> > +public @interface ValidHost {
>>> > +
>>> > +    /**
>>> > +     * The message to be logged if this constraint is violated. This
>>> should normally be overridden.
>>> > +     */
>>> > +    String message() default "The hostname is invalid";
>>> > +}
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>> re/config/plugins/validation/constraints/ValidPort.java
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/main/java/org
>>> /apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/constraints/ValidPort.java
>>> > new file mode 100644
>>> > index 0000000..a7c68b1
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/constraints/ValidPort.java
>>> > @@ -0,0 +1,44 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed
>>> with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license,
>>> Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance
>>> with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>> implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.cons
>>> traints;
>>> > +
>>> > +import java.lang.annotation.Documented;
>>> > +import java.lang.annotation.ElementType;
>>> > +import java.lang.annotation.Retention;
>>> > +import java.lang.annotation.RetentionPolicy;
>>> > +import java.lang.annotation.Target;
>>> > +
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>> traint;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.vali
>>> dators.ValidPortValidator;
>>> > +
>>> > +/**
>>> > + * Indicates that a plugin attribute must be a valid port number. A
>>> valid port number is an integer between 0 and
>>> > + * 65535.
>>> > + *
>>> > + * @since 2.8
>>> > + */
>>> > +@Documented
>>> > +@Retention(RetentionPolicy.RUNTIME)
>>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>>> > +@Constraint(ValidPortValidator.class)
>>> > +public @interface ValidPort {
>>> > +
>>> > +    /**
>>> > +     * The message to be logged if this constraint is violated. This
>>> should normally be overridden.
>>> > +     */
>>> > +    String message() default "The port number is invalid";
>>> > +}
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>> re/config/plugins/validation/validators/ValidHostValidator.java
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/main/java/org
>>> /apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/validators/ValidHostValidator.java
>>> > new file mode 100644
>>> > index 0000000..3669915
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/validators/ValidHostValidator.java
>>> > @@ -0,0 +1,58 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed
>>> with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license,
>>> Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance
>>> with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>> implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>>> dators;
>>> > +
>>> > +import org.apache.logging.log4j.Logger;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>> traintValidator;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>> traints.ValidHost;
>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>> > +
>>> > +import java.net.InetAddress;
>>> > +import java.net.UnknownHostException;
>>> > +
>>> > +/**
>>> > + * Validator that checks an object to verify it is a valid hostname
>>> or IP address. Validation rules follow the same
>>> > + * logic as in {@link InetAddress#getByName(String)}.
>>> > + *
>>> > + * @since 2.8
>>> > + */
>>> > +public class ValidHostValidator implements
>>> ConstraintValidator<ValidHost> {
>>> > +
>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>> > +
>>> > +    private ValidHost annotation;
>>> > +
>>> > +    @Override
>>> > +    public void initialize(ValidHost annotation) {
>>> > +        this.annotation = annotation;
>>> > +    }
>>> > +
>>> > +    @Override
>>> > +    public boolean isValid(String name, Object value) {
>>> > +        if (value == null) {
>>> > +            LOGGER.error(annotation.message());
>>> > +            return false;
>>> > +        }
>>> > +        try {
>>> > +            InetAddress.getByName(value.toString());
>>> > +            return true;
>>> > +        } catch (final UnknownHostException e) {
>>> > +            LOGGER.error(annotation.message(), e);
>>> > +            return false;
>>> > +        }
>>> > +    }
>>> > +}
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>>> re/config/plugins/validation/validators/ValidPortValidator.java
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/main/java/org
>>> /apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/validators/ValidPortValidator.java
>>> > new file mode 100644
>>> > index 0000000..f18f8fc
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/validators/ValidPortValidator.java
>>> > @@ -0,0 +1,53 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed
>>> with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license,
>>> Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance
>>> with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>> implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>>> dators;
>>> > +
>>> > +import org.apache.logging.log4j.Logger;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>>> traintValidator;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>> traints.ValidPort;
>>> > +import org.apache.logging.log4j.status.StatusLogger;
>>> > +
>>> > +/**
>>> > + * Validator that checks an object to verify it is a valid port
>>> number (an integer between 0 and 65535).
>>> > + *
>>> > + * @since 2.8
>>> > + */
>>> > +public class ValidPortValidator implements
>>> ConstraintValidator<ValidPort> {
>>> > +
>>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>>> > +
>>> > +    private ValidPort annotation;
>>> > +
>>> > +    @Override
>>> > +    public void initialize(final ValidPort annotation) {
>>> > +        this.annotation = annotation;
>>> > +    }
>>> > +
>>> > +    @Override
>>> > +    public boolean isValid(final String name, final Object value) {
>>> > +        if (!Integer.class.isInstance(value)) {
>>> > +            LOGGER.error(annotation.message());
>>> > +            return false;
>>> > +        }
>>> > +        int port = (int) value;
>>> > +        if (port < 0 || port > 65535) {
>>> > +            LOGGER.error(annotation.message());
>>> > +            return false;
>>> > +        }
>>> > +        return true;
>>> > +    }
>>> > +}
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>> re/config/plugins/validation/HostAndPort.java
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/test/java/org
>>> /apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>>> b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/HostAndPort.java
>>> > new file mode 100644
>>> > index 0000000..4f05d68
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/HostAndPort.java
>>> > @@ -0,0 +1,46 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed
>>> with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license,
>>> Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance
>>> with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>> implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>>> > +
>>> > +import java.net.InetSocketAddress;
>>> > +
>>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>>> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
>>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>> traints.ValidHost;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>>> traints.ValidPort;
>>> > +
>>> > +@Plugin(name = "HostAndPort", category = "Test")
>>> > +public class HostAndPort {
>>> > +
>>> > +    private final InetSocketAddress address;
>>> > +
>>> > +    private HostAndPort(final InetSocketAddress address) {
>>> > +        this.address = address;
>>> > +    }
>>> > +
>>> > +    public boolean isValid() {
>>> > +        return !address.isUnresolved();
>>> > +    }
>>> > +
>>> > +    @PluginFactory
>>> > +    public static HostAndPort createPlugin(
>>> > +        @ValidHost(message = "Unit test (host)")
>>> @PluginAttribute("host") final String host,
>>> > +        @ValidPort(message = "Unit test (port)")
>>> @PluginAttribute("port") final int port) {
>>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>>> > +    }
>>> > +}
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>> re/config/plugins/validation/validators/ValidHostValidatorTest.java
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/test/java/org
>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>> ors/ValidHostValidatorTest.java b/log4j-core/src/test/java/org
>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>> ors/ValidHostValidatorTest.java
>>> > new file mode 100644
>>> > index 0000000..3b0480d
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/validators/ValidHostValidatorTest.java
>>> > @@ -0,0 +1,74 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed
>>> with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license,
>>> Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance
>>> with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>> implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>>> dators;
>>> > +
>>> > +import java.util.UUID;
>>> > +
>>> > +import org.apache.logging.log4j.core.config.Node;
>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuil
>>> der;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginMana
>>> ger;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Host
>>> AndPort;
>>> > +import org.junit.Before;
>>> > +import org.junit.Test;
>>> > +
>>> > +import static org.junit.Assert.*;
>>> > +
>>> > +public class ValidHostValidatorTest {
>>> > +
>>> > +    private PluginType<HostAndPort> plugin;
>>> > +    private Node node;
>>> > +
>>> > +    @SuppressWarnings("unchecked")
>>> > +    @Before
>>> > +    public void setUp() throws Exception {
>>> > +        final PluginManager manager = new PluginManager("Test");
>>> > +        manager.collectPlugins();
>>> > +        plugin = (PluginType<HostAndPort>)
>>> manager.getPluginType("HostAndPort");
>>> > +        assertNotNull("Rebuild this module to ensure annotation
>>> processing has been done.", plugin);
>>> > +        node = new Node(null, "HostAndPort", plugin);
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testNullHost() throws Exception {
>>> > +        assertNull(buildPlugin());
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testInvalidIpAddress() throws Exception {
>>> > +        node.getAttributes().put("host",
>>> UUID.randomUUID().toString());
>>> > +        node.getAttributes().put("port", "1");
>>> > +        assertNull(buildPlugin());
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testLocalhost() throws Exception {
>>> > +        node.getAttributes().put("host", "localhost");
>>> > +        node.getAttributes().put("port", "1");
>>> > +        final HostAndPort hostAndPort = buildPlugin();
>>> > +        assertNotNull(hostAndPort);
>>> > +        assertTrue(hostAndPort.isValid());
>>> > +    }
>>> > +
>>> > +    private HostAndPort buildPlugin() {
>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>> > +            .withConfiguration(new NullConfiguration())
>>> > +            .withConfigurationNode(node)
>>> > +            .build();
>>> > +    }
>>> > +}
>>> > \ No newline at end of file
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>>> re/config/plugins/validation/validators/ValidPortValidatorTest.java
>>> > ----------------------------------------------------------------------
>>> > diff --git a/log4j-core/src/test/java/org
>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>> ors/ValidPortValidatorTest.java b/log4j-core/src/test/java/org
>>> /apache/logging/log4j/core/config/plugins/validation/validat
>>> ors/ValidPortValidatorTest.java
>>> > new file mode 100644
>>> > index 0000000..3aab08d
>>> > --- /dev/null
>>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>>> fig/plugins/validation/validators/ValidPortValidatorTest.java
>>> > @@ -0,0 +1,70 @@
>>> > +/*
>>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>>> > + * contributor license agreements. See the NOTICE file distributed
>>> with
>>> > + * this work for additional information regarding copyright ownership.
>>> > + * The ASF licenses this file to You under the Apache license,
>>> Version 2.0
>>> > + * (the "License"); you may not use this file except in compliance
>>> with
>>> > + * the License. You may obtain a copy of the License at
>>> > + *
>>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>>> > + *
>>> > + * Unless required by applicable law or agreed to in writing, software
>>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>>> implied.
>>> > + * See the license for the specific language governing permissions and
>>> > + * limitations under the license.
>>> > + */
>>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>>> dators;
>>> > +
>>> > +import org.apache.logging.log4j.core.config.Node;
>>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuil
>>> der;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginMana
>>> ger;
>>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>>> > +import org.apache.logging.log4j.core.config.plugins.validation.Host
>>> AndPort;
>>> > +import org.junit.Before;
>>> > +import org.junit.Test;
>>> > +
>>> > +import static org.junit.Assert.*;
>>> > +
>>> > +public class ValidPortValidatorTest {
>>> > +    private PluginType<HostAndPort> plugin;
>>> > +    private Node node;
>>> > +
>>> > +    @SuppressWarnings("unchecked")
>>> > +    @Before
>>> > +    public void setUp() throws Exception {
>>> > +        final PluginManager manager = new PluginManager("Test");
>>> > +        manager.collectPlugins();
>>> > +        plugin = (PluginType<HostAndPort>)
>>> manager.getPluginType("HostAndPort");
>>> > +        assertNotNull("Rebuild this module to ensure annotation
>>> processing has been done.", plugin);
>>> > +        node = new Node(null, "HostAndPort", plugin);
>>> > +        node.getAttributes().put("host", "localhost");
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testNegativePort() throws Exception {
>>> > +        node.getAttributes().put("port", "-1");
>>> > +        assertNull(buildPlugin());
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testValidPort() throws Exception {
>>> > +        node.getAttributes().put("port", "10");
>>> > +        assertNotNull(buildPlugin());
>>> > +    }
>>> > +
>>> > +    @Test
>>> > +    public void testInvalidPort() throws Exception {
>>> > +        node.getAttributes().put("port", "1234567890");
>>> > +        assertNull(buildPlugin());
>>> > +    }
>>> > +
>>> > +    private HostAndPort buildPlugin() {
>>> > +        return (HostAndPort) new PluginBuilder(plugin)
>>> > +            .withConfiguration(new NullConfiguration())
>>> > +            .withConfigurationNode(node)
>>> > +            .build();
>>> > +    }
>>> > +
>>> > +}
>>> > \ No newline at end of file
>>> >
>>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>>> 254e255/src/changes/changes.xml
>>> > ----------------------------------------------------------------------
>>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>>> > index c05de09..bc08dbb 100644
>>> > --- a/src/changes/changes.xml
>>> > +++ b/src/changes/changes.xml
>>> > @@ -213,6 +213,9 @@
>>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>>> >         The log4j-slf4j-impl module now declares a runtime dependency
>>> on log4j-core. While not technically required, this makes the
>>> log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates
>>> migration to Log4j 2.
>>> >       </action>
>>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>>> > +        Add converters and validators related to hostnames and ports.
>>> > +      </action>
>>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>>> >       </action>
>>> >
>>> >
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
>>> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>>>
>>>
>>
>>
>> --
>> Matt Sicker <bo...@gmail.com>
>>
>>
>>
>
>
> --
> Matt Sicker <bo...@gmail.com>
>
>
>


-- 
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Apache <ra...@dslextreme.com>.
nslookup dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
Server:		2001:578:3f::30
Address:	2001:578:3f::30#53

Non-authoritative answer:
Name:	dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
Address: 92.242.140.2



> On Jan 9, 2017, at 7:24 PM, Matt Sicker <bo...@gmail.com> wrote:
> 
> I suppose technically the first part of a UUID can be an IPv4 address encoded in hex. I'll make a better invalid value.
> 
> On 9 January 2017 at 20:20, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
> Debugging this and InetAddress.getByName is returning an InetAddress object. The value is
> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
> Address: 92.242.140.2
> 
> Ralph
> 
>> On Jan 9, 2017, at 7:12 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>> 
>> Not sure how that's possible, but I added a better assert message. Let me know if you're still having an issue with it. Is it a failure from IntelliJ or Maven?
>> 
>> On 9 January 2017 at 19:47, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>> This commit appears to be failing for me. The testInvalidIpAddress method is failing on the assert.
>> 
>> Ralph
>> 
>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org <ma...@apache.org> wrote:
>> >
>> > Repository: logging-log4j2
>> > Updated Branches:
>> >  refs/heads/master 367d26b09 -> 4254e2558
>> >
>> >
>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>> >
>> >
>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo <http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo>
>> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255>
>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255>
>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255>
>> >
>> > Branch: refs/heads/master
>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>> > Parents: 367d26b
>> > Author: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>> > Committer: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>> >
>> > ----------------------------------------------------------------------
>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>> > .../validators/ValidHostValidatorTest.java      | 74 ++++++++++++++++++++
>> > .../validators/ValidPortValidatorTest.java      | 70 ++++++++++++++++++
>> > src/changes/changes.xml                         |  3 +
>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>> > ----------------------------------------------------------------------
>> >
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>> > index 2895e52..421d711 100644
>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.config.plugins.convert;
>> > import java.io.File;
>> > import java.math.BigDecimal;
>> > import java.math.BigInteger;
>> > +import java.net.InetAddress;
>> > import java.net <http://java.net/>.MalformedURLException;
>> > import java.net.URI;
>> > import java.net.URISyntaxException;
>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>> > import java.security.Provider;
>> > import java.security.Security;
>> > import java.util.regex.Pattern;
>> > -
>> > import javax.xml.bind.DatatypeConverter;
>> >
>> > import org.apache.logging.log4j.Level;
>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>> >     }
>> >
>> >     /**
>> > +     * Converts a {@link String} into an {@link InetAddress}.
>> > +     */
>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>> > +    public static class InetAddressConverter implements TypeConverter<InetAddress> {
>> > +        @Override
>> > +        public InetAddress convert(final String s) throws Exception {
>> > +            return InetAddress.getByName(s);
>> > +        }
>> > +    }
>> > +
>> > +    /**
>> >      * Converts a {@link String} into a {@link Integer}.
>> >      */
>> >     @Plugin(name = "Integer", category = CATEGORY)
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>> > new file mode 100644
>> > index 0000000..c652d40
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>> > @@ -0,0 +1,41 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>> > +
>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidHostValidator;
>> > +
>> > +import java.lang.annotation.*;
>> > +import java.net.InetAddress;
>> > +
>> > +/**
>> > + * Indicates that a plugin attribute must be a valid host. This relies on the same validation rules as
>> > + * {@link InetAddress#getByName(String)}.
>> > + *
>> > + * @since 2.8
>> > + */
>> > +@Documented
>> > +@Retention(RetentionPolicy.RUNTIME)
>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>> > +@Constraint(ValidHostValidator.class)
>> > +public @interface ValidHost {
>> > +
>> > +    /**
>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>> > +     */
>> > +    String message() default "The hostname is invalid";
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>> > new file mode 100644
>> > index 0000000..a7c68b1
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>> > @@ -0,0 +1,44 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>> > +
>> > +import java.lang.annotation.Documented;
>> > +import java.lang.annotation.ElementType;
>> > +import java.lang.annotation.Retention;
>> > +import java.lang.annotation.RetentionPolicy;
>> > +import java.lang.annotation.Target;
>> > +
>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidPortValidator;
>> > +
>> > +/**
>> > + * Indicates that a plugin attribute must be a valid port number. A valid port number is an integer between 0 and
>> > + * 65535.
>> > + *
>> > + * @since 2.8
>> > + */
>> > +@Documented
>> > +@Retention(RetentionPolicy.RUNTIME)
>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>> > +@Constraint(ValidPortValidator.class)
>> > +public @interface ValidPort {
>> > +
>> > +    /**
>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>> > +     */
>> > +    String message() default "The port number is invalid";
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>> > new file mode 100644
>> > index 0000000..3669915
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>> > @@ -0,0 +1,58 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>> > +
>> > +import org.apache.logging.log4j.Logger;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>> > +import org.apache.logging.log4j.status.StatusLogger;
>> > +
>> > +import java.net.InetAddress;
>> > +import java.net.UnknownHostException;
>> > +
>> > +/**
>> > + * Validator that checks an object to verify it is a valid hostname or IP address. Validation rules follow the same
>> > + * logic as in {@link InetAddress#getByName(String)}.
>> > + *
>> > + * @since 2.8
>> > + */
>> > +public class ValidHostValidator implements ConstraintValidator<ValidHost> {
>> > +
>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>> > +
>> > +    private ValidHost annotation;
>> > +
>> > +    @Override
>> > +    public void initialize(ValidHost annotation) {
>> > +        this.annotation = annotation;
>> > +    }
>> > +
>> > +    @Override
>> > +    public boolean isValid(String name, Object value) {
>> > +        if (value == null) {
>> > +            LOGGER.error(annotation.message());
>> > +            return false;
>> > +        }
>> > +        try {
>> > +            InetAddress.getByName(value.to <http://value.to/>String());
>> > +            return true;
>> > +        } catch (final UnknownHostException e) {
>> > +            LOGGER.error(annotation.message(), e);
>> > +            return false;
>> > +        }
>> > +    }
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>> > new file mode 100644
>> > index 0000000..f18f8fc
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>> > @@ -0,0 +1,53 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>> > +
>> > +import org.apache.logging.log4j.Logger;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>> > +import org.apache.logging.log4j.status.StatusLogger;
>> > +
>> > +/**
>> > + * Validator that checks an object to verify it is a valid port number (an integer between 0 and 65535).
>> > + *
>> > + * @since 2.8
>> > + */
>> > +public class ValidPortValidator implements ConstraintValidator<ValidPort> {
>> > +
>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>> > +
>> > +    private ValidPort annotation;
>> > +
>> > +    @Override
>> > +    public void initialize(final ValidPort annotation) {
>> > +        this.annotation = annotation;
>> > +    }
>> > +
>> > +    @Override
>> > +    public boolean isValid(final String name, final Object value) {
>> > +        if (!Integer.class.isInstance(value)) {
>> > +            LOGGER.error(annotation.message());
>> > +            return false;
>> > +        }
>> > +        int port = (int) value;
>> > +        if (port < 0 || port > 65535) {
>> > +            LOGGER.error(annotation.message());
>> > +            return false;
>> > +        }
>> > +        return true;
>> > +    }
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>> > new file mode 100644
>> > index 0000000..4f05d68
>> > --- /dev/null
>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>> > @@ -0,0 +1,46 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>> > +
>> > +import java.net.InetSocketAddress;
>> > +
>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>> > +
>> > +@Plugin(name = "HostAndPort", category = "Test")
>> > +public class HostAndPort {
>> > +
>> > +    private final InetSocketAddress address;
>> > +
>> > +    private HostAndPort(final InetSocketAddress address) {
>> > +        this.address = address;
>> > +    }
>> > +
>> > +    public boolean isValid() {
>> > +        return !address.isUnresolved();
>> > +    }
>> > +
>> > +    @PluginFactory
>> > +    public static HostAndPort createPlugin(
>> > +        @ValidHost(message = "Unit test (host)") @PluginAttribute("host") final String host,
>> > +        @ValidPort(message = "Unit test (port)") @PluginAttribute("port") final int port) {
>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>> > +    }
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>> > new file mode 100644
>> > index 0000000..3b0480d
>> > --- /dev/null
>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>> > @@ -0,0 +1,74 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>> > +
>> > +import java.util.UUID;
>> > +
>> > +import org.apache.logging.log4j.core.config.Node;
>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>> > +import org.junit.Before;
>> > +import org.junit.Test;
>> > +
>> > +import static org.junit.Assert.*;
>> > +
>> > +public class ValidHostValidatorTest {
>> > +
>> > +    private PluginType<HostAndPort> plugin;
>> > +    private Node node;
>> > +
>> > +    @SuppressWarnings("unchecked")
>> > +    @Before
>> > +    public void setUp() throws Exception {
>> > +        final PluginManager manager = new PluginManager("Test");
>> > +        manager.collectPlugins();
>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>> > +        node = new Node(null, "HostAndPort", plugin);
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testNullHost() throws Exception {
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testInvalidIpAddress() throws Exception {
>> > +        node.getAttributes().put("host", UUID.randomUUID().toString());
>> > +        node.getAttributes().put("port", "1");
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testLocalhost() throws Exception {
>> > +        node.getAttributes().put("host", "localhost");
>> > +        node.getAttributes().put("port", "1");
>> > +        final HostAndPort hostAndPort = buildPlugin();
>> > +        assertNotNull(hostAndPort);
>> > +        assertTrue(hostAndPort.isValid());
>> > +    }
>> > +
>> > +    private HostAndPort buildPlugin() {
>> > +        return (HostAndPort) new PluginBuilder(plugin)
>> > +            .withConfiguration(new NullConfiguration())
>> > +            .withConfigurationNode(node)
>> > +            .build();
>> > +    }
>> > +}
>> > \ No newline at end of file
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>> > new file mode 100644
>> > index 0000000..3aab08d
>> > --- /dev/null
>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>> > @@ -0,0 +1,70 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>> > +
>> > +import org.apache.logging.log4j.core.config.Node;
>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>> > +import org.junit.Before;
>> > +import org.junit.Test;
>> > +
>> > +import static org.junit.Assert.*;
>> > +
>> > +public class ValidPortValidatorTest {
>> > +    private PluginType<HostAndPort> plugin;
>> > +    private Node node;
>> > +
>> > +    @SuppressWarnings("unchecked")
>> > +    @Before
>> > +    public void setUp() throws Exception {
>> > +        final PluginManager manager = new PluginManager("Test");
>> > +        manager.collectPlugins();
>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>> > +        node = new Node(null, "HostAndPort", plugin);
>> > +        node.getAttributes().put("host", "localhost");
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testNegativePort() throws Exception {
>> > +        node.getAttributes().put("port", "-1");
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testValidPort() throws Exception {
>> > +        node.getAttributes().put("port", "10");
>> > +        assertNotNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testInvalidPort() throws Exception {
>> > +        node.getAttributes().put("port", "1234567890");
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    private HostAndPort buildPlugin() {
>> > +        return (HostAndPort) new PluginBuilder(plugin)
>> > +            .withConfiguration(new NullConfiguration())
>> > +            .withConfigurationNode(node)
>> > +            .build();
>> > +    }
>> > +
>> > +}
>> > \ No newline at end of file
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml>
>> > ----------------------------------------------------------------------
>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>> > index c05de09..bc08dbb 100644
>> > --- a/src/changes/changes.xml
>> > +++ b/src/changes/changes.xml
>> > @@ -213,6 +213,9 @@
>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>> >         The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2.
>> >       </action>
>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>> > +        Add converters and validators related to hostnames and ports.
>> > +      </action>
>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>> >       </action>
>> >
>> >
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org <ma...@logging.apache.org>
>> For additional commands, e-mail: log4j-dev-help@logging.apache.org <ma...@logging.apache.org>
>> 
>> 
>> 
>> 
>> -- 
>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
> 
> 
> 
> 
> -- 
> Matt Sicker <boards@gmail.com <ma...@gmail.com>>


Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Matt Sicker <bo...@gmail.com>.
I suppose technically the first part of a UUID can be an IPv4 address
encoded in hex. I'll make a better invalid value.

On 9 January 2017 at 20:20, Apache <ra...@dslextreme.com> wrote:

> Debugging this and InetAddress.getByName is returning an InetAddress
> object. The value is
> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
> Address: 92.242.140.2
>
> Ralph
>
> On Jan 9, 2017, at 7:12 PM, Matt Sicker <bo...@gmail.com> wrote:
>
> Not sure how that's possible, but I added a better assert message. Let me
> know if you're still having an issue with it. Is it a failure from IntelliJ
> or Maven?
>
> On 9 January 2017 at 19:47, Apache <ra...@dslextreme.com> wrote:
>
>> This commit appears to be failing for me. The testInvalidIpAddress method
>> is failing on the assert.
>>
>> Ralph
>>
>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org wrote:
>> >
>> > Repository: logging-log4j2
>> > Updated Branches:
>> >  refs/heads/master 367d26b09 -> 4254e2558
>> >
>> >
>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>> >
>> >
>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
>> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit
>> /4254e255
>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4
>> 254e255
>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4
>> 254e255
>> >
>> > Branch: refs/heads/master
>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>> > Parents: 367d26b
>> > Author: Matt Sicker <ma...@spr.com>
>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>> > Committer: Matt Sicker <ma...@spr.com>
>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>> >
>> > ----------------------------------------------------------------------
>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>> > .../validators/ValidHostValidatorTest.java      | 74
>> ++++++++++++++++++++
>> > .../validators/ValidPortValidatorTest.java      | 70 ++++++++++++++++++
>> > src/changes/changes.xml                         |  3 +
>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>> > ----------------------------------------------------------------------
>> >
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>> re/config/plugins/convert/TypeConverters.java
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/org
>> /apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>> > index 2895e52..421d711 100644
>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/convert/TypeConverters.java
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/convert/TypeConverters.java
>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.
>> config.plugins.convert;
>> > import java.io.File;
>> > import java.math.BigDecimal;
>> > import java.math.BigInteger;
>> > +import java.net.InetAddress;
>> > import java.net.MalformedURLException;
>> > import java.net.URI;
>> > import java.net.URISyntaxException;
>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>> > import java.security.Provider;
>> > import java.security.Security;
>> > import java.util.regex.Pattern;
>> > -
>> > import javax.xml.bind.DatatypeConverter;
>> >
>> > import org.apache.logging.log4j.Level;
>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>> >     }
>> >
>> >     /**
>> > +     * Converts a {@link String} into an {@link InetAddress}.
>> > +     */
>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>> > +    public static class InetAddressConverter implements
>> TypeConverter<InetAddress> {
>> > +        @Override
>> > +        public InetAddress convert(final String s) throws Exception {
>> > +            return InetAddress.getByName(s);
>> > +        }
>> > +    }
>> > +
>> > +    /**
>> >      * Converts a {@link String} into a {@link Integer}.
>> >      */
>> >     @Plugin(name = "Integer", category = CATEGORY)
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>> re/config/plugins/validation/constraints/ValidHost.java
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/constraints/ValidHost.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/constraints/ValidHost.java
>> > new file mode 100644
>> > index 0000000..c652d40
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/constraints/ValidHost.java
>> > @@ -0,0 +1,41 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version
>> 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>> implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.cons
>> traints;
>> > +
>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>> traint;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.vali
>> dators.ValidHostValidator;
>> > +
>> > +import java.lang.annotation.*;
>> > +import java.net.InetAddress;
>> > +
>> > +/**
>> > + * Indicates that a plugin attribute must be a valid host. This relies
>> on the same validation rules as
>> > + * {@link InetAddress#getByName(String)}.
>> > + *
>> > + * @since 2.8
>> > + */
>> > +@Documented
>> > +@Retention(RetentionPolicy.RUNTIME)
>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>> > +@Constraint(ValidHostValidator.class)
>> > +public @interface ValidHost {
>> > +
>> > +    /**
>> > +     * The message to be logged if this constraint is violated. This
>> should normally be overridden.
>> > +     */
>> > +    String message() default "The hostname is invalid";
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>> re/config/plugins/validation/constraints/ValidPort.java
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/constraints/ValidPort.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/constraints/ValidPort.java
>> > new file mode 100644
>> > index 0000000..a7c68b1
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/constraints/ValidPort.java
>> > @@ -0,0 +1,44 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version
>> 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>> implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.cons
>> traints;
>> > +
>> > +import java.lang.annotation.Documented;
>> > +import java.lang.annotation.ElementType;
>> > +import java.lang.annotation.Retention;
>> > +import java.lang.annotation.RetentionPolicy;
>> > +import java.lang.annotation.Target;
>> > +
>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>> traint;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.vali
>> dators.ValidPortValidator;
>> > +
>> > +/**
>> > + * Indicates that a plugin attribute must be a valid port number. A
>> valid port number is an integer between 0 and
>> > + * 65535.
>> > + *
>> > + * @since 2.8
>> > + */
>> > +@Documented
>> > +@Retention(RetentionPolicy.RUNTIME)
>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>> > +@Constraint(ValidPortValidator.class)
>> > +public @interface ValidPort {
>> > +
>> > +    /**
>> > +     * The message to be logged if this constraint is violated. This
>> should normally be overridden.
>> > +     */
>> > +    String message() default "The port number is invalid";
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>> re/config/plugins/validation/validators/ValidHostValidator.java
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidHostValidator.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidHostValidator.java
>> > new file mode 100644
>> > index 0000000..3669915
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidHostValidator.java
>> > @@ -0,0 +1,58 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version
>> 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>> implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>> dators;
>> > +
>> > +import org.apache.logging.log4j.Logger;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>> traintValidator;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>> traints.ValidHost;
>> > +import org.apache.logging.log4j.status.StatusLogger;
>> > +
>> > +import java.net.InetAddress;
>> > +import java.net.UnknownHostException;
>> > +
>> > +/**
>> > + * Validator that checks an object to verify it is a valid hostname or
>> IP address. Validation rules follow the same
>> > + * logic as in {@link InetAddress#getByName(String)}.
>> > + *
>> > + * @since 2.8
>> > + */
>> > +public class ValidHostValidator implements
>> ConstraintValidator<ValidHost> {
>> > +
>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>> > +
>> > +    private ValidHost annotation;
>> > +
>> > +    @Override
>> > +    public void initialize(ValidHost annotation) {
>> > +        this.annotation = annotation;
>> > +    }
>> > +
>> > +    @Override
>> > +    public boolean isValid(String name, Object value) {
>> > +        if (value == null) {
>> > +            LOGGER.error(annotation.message());
>> > +            return false;
>> > +        }
>> > +        try {
>> > +            InetAddress.getByName(value.toString());
>> > +            return true;
>> > +        } catch (final UnknownHostException e) {
>> > +            LOGGER.error(annotation.message(), e);
>> > +            return false;
>> > +        }
>> > +    }
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>> 254e255/log4j-core/src/main/java/org/apache/logging/log4j/co
>> re/config/plugins/validation/validators/ValidPortValidator.java
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidPortValidator.java
>> b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidPortValidator.java
>> > new file mode 100644
>> > index 0000000..f18f8fc
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidPortValidator.java
>> > @@ -0,0 +1,53 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version
>> 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>> implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>> dators;
>> > +
>> > +import org.apache.logging.log4j.Logger;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.Cons
>> traintValidator;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>> traints.ValidPort;
>> > +import org.apache.logging.log4j.status.StatusLogger;
>> > +
>> > +/**
>> > + * Validator that checks an object to verify it is a valid port number
>> (an integer between 0 and 65535).
>> > + *
>> > + * @since 2.8
>> > + */
>> > +public class ValidPortValidator implements
>> ConstraintValidator<ValidPort> {
>> > +
>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>> > +
>> > +    private ValidPort annotation;
>> > +
>> > +    @Override
>> > +    public void initialize(final ValidPort annotation) {
>> > +        this.annotation = annotation;
>> > +    }
>> > +
>> > +    @Override
>> > +    public boolean isValid(final String name, final Object value) {
>> > +        if (!Integer.class.isInstance(value)) {
>> > +            LOGGER.error(annotation.message());
>> > +            return false;
>> > +        }
>> > +        int port = (int) value;
>> > +        if (port < 0 || port > 65535) {
>> > +            LOGGER.error(annotation.message());
>> > +            return false;
>> > +        }
>> > +        return true;
>> > +    }
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>> re/config/plugins/validation/HostAndPort.java
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/HostAndPort.java b/log4j-core/src/test/java/org
>> /apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>> > new file mode 100644
>> > index 0000000..4f05d68
>> > --- /dev/null
>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/HostAndPort.java
>> > @@ -0,0 +1,46 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version
>> 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>> implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>> > +
>> > +import java.net.InetSocketAddress;
>> > +
>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>> traints.ValidHost;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.cons
>> traints.ValidPort;
>> > +
>> > +@Plugin(name = "HostAndPort", category = "Test")
>> > +public class HostAndPort {
>> > +
>> > +    private final InetSocketAddress address;
>> > +
>> > +    private HostAndPort(final InetSocketAddress address) {
>> > +        this.address = address;
>> > +    }
>> > +
>> > +    public boolean isValid() {
>> > +        return !address.isUnresolved();
>> > +    }
>> > +
>> > +    @PluginFactory
>> > +    public static HostAndPort createPlugin(
>> > +        @ValidHost(message = "Unit test (host)")
>> @PluginAttribute("host") final String host,
>> > +        @ValidPort(message = "Unit test (port)")
>> @PluginAttribute("port") final int port) {
>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>> > +    }
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>> re/config/plugins/validation/validators/ValidHostValidatorTest.java
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidHostValidatorTest.java
>> b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidHostValidatorTest.java
>> > new file mode 100644
>> > index 0000000..3b0480d
>> > --- /dev/null
>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidHostValidatorTest.java
>> > @@ -0,0 +1,74 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version
>> 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>> implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>> dators;
>> > +
>> > +import java.util.UUID;
>> > +
>> > +import org.apache.logging.log4j.core.config.Node;
>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuil
>> der;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginMana
>> ger;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.Host
>> AndPort;
>> > +import org.junit.Before;
>> > +import org.junit.Test;
>> > +
>> > +import static org.junit.Assert.*;
>> > +
>> > +public class ValidHostValidatorTest {
>> > +
>> > +    private PluginType<HostAndPort> plugin;
>> > +    private Node node;
>> > +
>> > +    @SuppressWarnings("unchecked")
>> > +    @Before
>> > +    public void setUp() throws Exception {
>> > +        final PluginManager manager = new PluginManager("Test");
>> > +        manager.collectPlugins();
>> > +        plugin = (PluginType<HostAndPort>)
>> manager.getPluginType("HostAndPort");
>> > +        assertNotNull("Rebuild this module to ensure annotation
>> processing has been done.", plugin);
>> > +        node = new Node(null, "HostAndPort", plugin);
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testNullHost() throws Exception {
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testInvalidIpAddress() throws Exception {
>> > +        node.getAttributes().put("host",
>> UUID.randomUUID().toString());
>> > +        node.getAttributes().put("port", "1");
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testLocalhost() throws Exception {
>> > +        node.getAttributes().put("host", "localhost");
>> > +        node.getAttributes().put("port", "1");
>> > +        final HostAndPort hostAndPort = buildPlugin();
>> > +        assertNotNull(hostAndPort);
>> > +        assertTrue(hostAndPort.isValid());
>> > +    }
>> > +
>> > +    private HostAndPort buildPlugin() {
>> > +        return (HostAndPort) new PluginBuilder(plugin)
>> > +            .withConfiguration(new NullConfiguration())
>> > +            .withConfigurationNode(node)
>> > +            .build();
>> > +    }
>> > +}
>> > \ No newline at end of file
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>> 254e255/log4j-core/src/test/java/org/apache/logging/log4j/co
>> re/config/plugins/validation/validators/ValidPortValidatorTest.java
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidPortValidatorTest.java
>> b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidPortValidatorTest.java
>> > new file mode 100644
>> > index 0000000..3aab08d
>> > --- /dev/null
>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/con
>> fig/plugins/validation/validators/ValidPortValidatorTest.java
>> > @@ -0,0 +1,70 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version
>> 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
>> implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.vali
>> dators;
>> > +
>> > +import org.apache.logging.log4j.core.config.Node;
>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuil
>> der;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginMana
>> ger;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.Host
>> AndPort;
>> > +import org.junit.Before;
>> > +import org.junit.Test;
>> > +
>> > +import static org.junit.Assert.*;
>> > +
>> > +public class ValidPortValidatorTest {
>> > +    private PluginType<HostAndPort> plugin;
>> > +    private Node node;
>> > +
>> > +    @SuppressWarnings("unchecked")
>> > +    @Before
>> > +    public void setUp() throws Exception {
>> > +        final PluginManager manager = new PluginManager("Test");
>> > +        manager.collectPlugins();
>> > +        plugin = (PluginType<HostAndPort>)
>> manager.getPluginType("HostAndPort");
>> > +        assertNotNull("Rebuild this module to ensure annotation
>> processing has been done.", plugin);
>> > +        node = new Node(null, "HostAndPort", plugin);
>> > +        node.getAttributes().put("host", "localhost");
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testNegativePort() throws Exception {
>> > +        node.getAttributes().put("port", "-1");
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testValidPort() throws Exception {
>> > +        node.getAttributes().put("port", "10");
>> > +        assertNotNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testInvalidPort() throws Exception {
>> > +        node.getAttributes().put("port", "1234567890");
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    private HostAndPort buildPlugin() {
>> > +        return (HostAndPort) new PluginBuilder(plugin)
>> > +            .withConfiguration(new NullConfiguration())
>> > +            .withConfigurationNode(node)
>> > +            .build();
>> > +    }
>> > +
>> > +}
>> > \ No newline at end of file
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4
>> 254e255/src/changes/changes.xml
>> > ----------------------------------------------------------------------
>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>> > index c05de09..bc08dbb 100644
>> > --- a/src/changes/changes.xml
>> > +++ b/src/changes/changes.xml
>> > @@ -213,6 +213,9 @@
>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>> >         The log4j-slf4j-impl module now declares a runtime dependency
>> on log4j-core. While not technically required, this makes the
>> log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates
>> migration to Log4j 2.
>> >       </action>
>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>> > +        Add converters and validators related to hostnames and ports.
>> > +      </action>
>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>> >       </action>
>> >
>> >
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
>> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>>
>>
>
>
> --
> Matt Sicker <bo...@gmail.com>
>
>
>


-- 
Matt Sicker <bo...@gmail.com>

Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Apache <ra...@dslextreme.com>.
Running it again I obviously get a different host address but the same IP address is returned.

Ralph

> On Jan 9, 2017, at 7:20 PM, Apache <ra...@dslextreme.com> wrote:
> 
> Debugging this and InetAddress.getByName is returning an InetAddress object. The value is
> Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
> Address: 92.242.140.2
> 
> Ralph
> 
>> On Jan 9, 2017, at 7:12 PM, Matt Sicker <boards@gmail.com <ma...@gmail.com>> wrote:
>> 
>> Not sure how that's possible, but I added a better assert message. Let me know if you're still having an issue with it. Is it a failure from IntelliJ or Maven?
>> 
>> On 9 January 2017 at 19:47, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
>> This commit appears to be failing for me. The testInvalidIpAddress method is failing on the assert.
>> 
>> Ralph
>> 
>> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org <ma...@apache.org> wrote:
>> >
>> > Repository: logging-log4j2
>> > Updated Branches:
>> >  refs/heads/master 367d26b09 -> 4254e2558
>> >
>> >
>> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
>> >
>> >
>> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo <http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo>
>> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255>
>> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255>
>> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255>
>> >
>> > Branch: refs/heads/master
>> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
>> > Parents: 367d26b
>> > Author: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>> > Authored: Fri Dec 30 15:00:17 2016 -0600
>> > Committer: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
>> > Committed: Fri Dec 30 15:01:26 2016 -0600
>> >
>> > ----------------------------------------------------------------------
>> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
>> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
>> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
>> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
>> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
>> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
>> > .../validators/ValidHostValidatorTest.java      | 74 ++++++++++++++++++++
>> > .../validators/ValidPortValidatorTest.java      | 70 ++++++++++++++++++
>> > src/changes/changes.xml                         |  3 +
>> > 9 files changed, 401 insertions(+), 1 deletion(-)
>> > ----------------------------------------------------------------------
>> >
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>> > index 2895e52..421d711 100644
>> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
>> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.config.plugins.convert;
>> > import java.io.File;
>> > import java.math.BigDecimal;
>> > import java.math.BigInteger;
>> > +import java.net.InetAddress;
>> > import java.net <http://java.net/>.MalformedURLException;
>> > import java.net.URI;
>> > import java.net.URISyntaxException;
>> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
>> > import java.security.Provider;
>> > import java.security.Security;
>> > import java.util.regex.Pattern;
>> > -
>> > import javax.xml.bind.DatatypeConverter;
>> >
>> > import org.apache.logging.log4j.Level;
>> > @@ -233,6 +233,17 @@ public final class TypeConverters {
>> >     }
>> >
>> >     /**
>> > +     * Converts a {@link String} into an {@link InetAddress}.
>> > +     */
>> > +    @Plugin(name = "InetAddress", category = CATEGORY)
>> > +    public static class InetAddressConverter implements TypeConverter<InetAddress> {
>> > +        @Override
>> > +        public InetAddress convert(final String s) throws Exception {
>> > +            return InetAddress.getByName(s);
>> > +        }
>> > +    }
>> > +
>> > +    /**
>> >      * Converts a {@link String} into a {@link Integer}.
>> >      */
>> >     @Plugin(name = "Integer", category = CATEGORY)
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>> > new file mode 100644
>> > index 0000000..c652d40
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
>> > @@ -0,0 +1,41 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>> > +
>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidHostValidator;
>> > +
>> > +import java.lang.annotation.*;
>> > +import java.net.InetAddress;
>> > +
>> > +/**
>> > + * Indicates that a plugin attribute must be a valid host. This relies on the same validation rules as
>> > + * {@link InetAddress#getByName(String)}.
>> > + *
>> > + * @since 2.8
>> > + */
>> > +@Documented
>> > +@Retention(RetentionPolicy.RUNTIME)
>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>> > +@Constraint(ValidHostValidator.class)
>> > +public @interface ValidHost {
>> > +
>> > +    /**
>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>> > +     */
>> > +    String message() default "The hostname is invalid";
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>> > new file mode 100644
>> > index 0000000..a7c68b1
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
>> > @@ -0,0 +1,44 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
>> > +
>> > +import java.lang.annotation.Documented;
>> > +import java.lang.annotation.ElementType;
>> > +import java.lang.annotation.Retention;
>> > +import java.lang.annotation.RetentionPolicy;
>> > +import java.lang.annotation.Target;
>> > +
>> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidPortValidator;
>> > +
>> > +/**
>> > + * Indicates that a plugin attribute must be a valid port number. A valid port number is an integer between 0 and
>> > + * 65535.
>> > + *
>> > + * @since 2.8
>> > + */
>> > +@Documented
>> > +@Retention(RetentionPolicy.RUNTIME)
>> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
>> > +@Constraint(ValidPortValidator.class)
>> > +public @interface ValidPort {
>> > +
>> > +    /**
>> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
>> > +     */
>> > +    String message() default "The port number is invalid";
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>> > new file mode 100644
>> > index 0000000..3669915
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
>> > @@ -0,0 +1,58 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>> > +
>> > +import org.apache.logging.log4j.Logger;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>> > +import org.apache.logging.log4j.status.StatusLogger;
>> > +
>> > +import java.net.InetAddress;
>> > +import java.net.UnknownHostException;
>> > +
>> > +/**
>> > + * Validator that checks an object to verify it is a valid hostname or IP address. Validation rules follow the same
>> > + * logic as in {@link InetAddress#getByName(String)}.
>> > + *
>> > + * @since 2.8
>> > + */
>> > +public class ValidHostValidator implements ConstraintValidator<ValidHost> {
>> > +
>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>> > +
>> > +    private ValidHost annotation;
>> > +
>> > +    @Override
>> > +    public void initialize(ValidHost annotation) {
>> > +        this.annotation = annotation;
>> > +    }
>> > +
>> > +    @Override
>> > +    public boolean isValid(String name, Object value) {
>> > +        if (value == null) {
>> > +            LOGGER.error(annotation.message());
>> > +            return false;
>> > +        }
>> > +        try {
>> > +            InetAddress.getByName(value.toString());
>> > +            return true;
>> > +        } catch (final UnknownHostException e) {
>> > +            LOGGER.error(annotation.message(), e);
>> > +            return false;
>> > +        }
>> > +    }
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>> > new file mode 100644
>> > index 0000000..f18f8fc
>> > --- /dev/null
>> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
>> > @@ -0,0 +1,53 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>> > +
>> > +import org.apache.logging.log4j.Logger;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>> > +import org.apache.logging.log4j.status.StatusLogger;
>> > +
>> > +/**
>> > + * Validator that checks an object to verify it is a valid port number (an integer between 0 and 65535).
>> > + *
>> > + * @since 2.8
>> > + */
>> > +public class ValidPortValidator implements ConstraintValidator<ValidPort> {
>> > +
>> > +    private static final Logger LOGGER = StatusLogger.getLogger();
>> > +
>> > +    private ValidPort annotation;
>> > +
>> > +    @Override
>> > +    public void initialize(final ValidPort annotation) {
>> > +        this.annotation = annotation;
>> > +    }
>> > +
>> > +    @Override
>> > +    public boolean isValid(final String name, final Object value) {
>> > +        if (!Integer.class.isInstance(value)) {
>> > +            LOGGER.error(annotation.message());
>> > +            return false;
>> > +        }
>> > +        int port = (int) value;
>> > +        if (port < 0 || port > 65535) {
>> > +            LOGGER.error(annotation.message());
>> > +            return false;
>> > +        }
>> > +        return true;
>> > +    }
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>> > new file mode 100644
>> > index 0000000..4f05d68
>> > --- /dev/null
>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
>> > @@ -0,0 +1,46 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation;
>> > +
>> > +import java.net.InetSocketAddress;
>> > +
>> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
>> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
>> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
>> > +
>> > +@Plugin(name = "HostAndPort", category = "Test")
>> > +public class HostAndPort {
>> > +
>> > +    private final InetSocketAddress address;
>> > +
>> > +    private HostAndPort(final InetSocketAddress address) {
>> > +        this.address = address;
>> > +    }
>> > +
>> > +    public boolean isValid() {
>> > +        return !address.isUnresolved();
>> > +    }
>> > +
>> > +    @PluginFactory
>> > +    public static HostAndPort createPlugin(
>> > +        @ValidHost(message = "Unit test (host)") @PluginAttribute("host") final String host,
>> > +        @ValidPort(message = "Unit test (port)") @PluginAttribute("port") final int port) {
>> > +        return new HostAndPort(new InetSocketAddress(host, port));
>> > +    }
>> > +}
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>> > new file mode 100644
>> > index 0000000..3b0480d
>> > --- /dev/null
>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
>> > @@ -0,0 +1,74 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>> > +
>> > +import java.util.UUID;
>> > +
>> > +import org.apache.logging.log4j.core.config.Node;
>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>> > +import org.junit.Before;
>> > +import org.junit.Test;
>> > +
>> > +import static org.junit.Assert.*;
>> > +
>> > +public class ValidHostValidatorTest {
>> > +
>> > +    private PluginType<HostAndPort> plugin;
>> > +    private Node node;
>> > +
>> > +    @SuppressWarnings("unchecked")
>> > +    @Before
>> > +    public void setUp() throws Exception {
>> > +        final PluginManager manager = new PluginManager("Test");
>> > +        manager.collectPlugins();
>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>> > +        node = new Node(null, "HostAndPort", plugin);
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testNullHost() throws Exception {
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testInvalidIpAddress() throws Exception {
>> > +        node.getAttributes().put("host", UUID.randomUUID().toString());
>> > +        node.getAttributes().put("port", "1");
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testLocalhost() throws Exception {
>> > +        node.getAttributes().put("host", "localhost");
>> > +        node.getAttributes().put("port", "1");
>> > +        final HostAndPort hostAndPort = buildPlugin();
>> > +        assertNotNull(hostAndPort);
>> > +        assertTrue(hostAndPort.isValid());
>> > +    }
>> > +
>> > +    private HostAndPort buildPlugin() {
>> > +        return (HostAndPort) new PluginBuilder(plugin)
>> > +            .withConfiguration(new NullConfiguration())
>> > +            .withConfigurationNode(node)
>> > +            .build();
>> > +    }
>> > +}
>> > \ No newline at end of file
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java>
>> > ----------------------------------------------------------------------
>> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>> > new file mode 100644
>> > index 0000000..3aab08d
>> > --- /dev/null
>> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
>> > @@ -0,0 +1,70 @@
>> > +/*
>> > + * Licensed to the Apache Software Foundation (ASF) under one or more
>> > + * contributor license agreements. See the NOTICE file distributed with
>> > + * this work for additional information regarding copyright ownership.
>> > + * The ASF licenses this file to You under the Apache license, Version 2.0
>> > + * (the "License"); you may not use this file except in compliance with
>> > + * the License. You may obtain a copy of the License at
>> > + *
>> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
>> > + *
>> > + * Unless required by applicable law or agreed to in writing, software
>> > + * distributed under the License is distributed on an "AS IS" BASIS,
>> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> > + * See the license for the specific language governing permissions and
>> > + * limitations under the license.
>> > + */
>> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
>> > +
>> > +import org.apache.logging.log4j.core.config.Node;
>> > +import org.apache.logging.log4j.core.config.NullConfiguration;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
>> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
>> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
>> > +import org.junit.Before;
>> > +import org.junit.Test;
>> > +
>> > +import static org.junit.Assert.*;
>> > +
>> > +public class ValidPortValidatorTest {
>> > +    private PluginType<HostAndPort> plugin;
>> > +    private Node node;
>> > +
>> > +    @SuppressWarnings("unchecked")
>> > +    @Before
>> > +    public void setUp() throws Exception {
>> > +        final PluginManager manager = new PluginManager("Test");
>> > +        manager.collectPlugins();
>> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
>> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
>> > +        node = new Node(null, "HostAndPort", plugin);
>> > +        node.getAttributes().put("host", "localhost");
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testNegativePort() throws Exception {
>> > +        node.getAttributes().put("port", "-1");
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testValidPort() throws Exception {
>> > +        node.getAttributes().put("port", "10");
>> > +        assertNotNull(buildPlugin());
>> > +    }
>> > +
>> > +    @Test
>> > +    public void testInvalidPort() throws Exception {
>> > +        node.getAttributes().put("port", "1234567890");
>> > +        assertNull(buildPlugin());
>> > +    }
>> > +
>> > +    private HostAndPort buildPlugin() {
>> > +        return (HostAndPort) new PluginBuilder(plugin)
>> > +            .withConfiguration(new NullConfiguration())
>> > +            .withConfigurationNode(node)
>> > +            .build();
>> > +    }
>> > +
>> > +}
>> > \ No newline at end of file
>> >
>> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml>
>> > ----------------------------------------------------------------------
>> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
>> > index c05de09..bc08dbb 100644
>> > --- a/src/changes/changes.xml
>> > +++ b/src/changes/changes.xml
>> > @@ -213,6 +213,9 @@
>> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
>> >         The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2.
>> >       </action>
>> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
>> > +        Add converters and validators related to hostnames and ports.
>> > +      </action>
>> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
>> >         Refactor SyslogAppender so that Layout is a Plugin element.
>> >       </action>
>> >
>> >
>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org <ma...@logging.apache.org>
>> For additional commands, e-mail: log4j-dev-help@logging.apache.org <ma...@logging.apache.org>
>> 
>> 
>> 
>> 
>> -- 
>> Matt Sicker <boards@gmail.com <ma...@gmail.com>>
> 


Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Apache <ra...@dslextreme.com>.
Debugging this and InetAddress.getByName is returning an InetAddress object. The value is
Host: dcc071ab-bc05-46ed-bfc0-be5fe876f6ea
Address: 92.242.140.2

Ralph

> On Jan 9, 2017, at 7:12 PM, Matt Sicker <bo...@gmail.com> wrote:
> 
> Not sure how that's possible, but I added a better assert message. Let me know if you're still having an issue with it. Is it a failure from IntelliJ or Maven?
> 
> On 9 January 2017 at 19:47, Apache <ralph.goers@dslextreme.com <ma...@dslextreme.com>> wrote:
> This commit appears to be failing for me. The testInvalidIpAddress method is failing on the assert.
> 
> Ralph
> 
> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org <ma...@apache.org> wrote:
> >
> > Repository: logging-log4j2
> > Updated Branches:
> >  refs/heads/master 367d26b09 -> 4254e2558
> >
> >
> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
> >
> >
> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo <http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo>
> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4254e255>
> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4254e255>
> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255 <http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4254e255>
> >
> > Branch: refs/heads/master
> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
> > Parents: 367d26b
> > Author: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
> > Authored: Fri Dec 30 15:00:17 2016 -0600
> > Committer: Matt Sicker <matt.sicker@spr.com <ma...@spr.com>>
> > Committed: Fri Dec 30 15:01:26 2016 -0600
> >
> > ----------------------------------------------------------------------
> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
> > .../validators/ValidHostValidatorTest.java      | 74 ++++++++++++++++++++
> > .../validators/ValidPortValidatorTest.java      | 70 ++++++++++++++++++
> > src/changes/changes.xml                         |  3 +
> > 9 files changed, 401 insertions(+), 1 deletion(-)
> > ----------------------------------------------------------------------
> >
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java>
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
> > index 2895e52..421d711 100644
> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.config.plugins.convert;
> > import java.io.File;
> > import java.math.BigDecimal;
> > import java.math.BigInteger;
> > +import java.net.InetAddress;
> > import java.net <http://java.net/>.MalformedURLException;
> > import java.net.URI;
> > import java.net.URISyntaxException;
> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
> > import java.security.Provider;
> > import java.security.Security;
> > import java.util.regex.Pattern;
> > -
> > import javax.xml.bind.DatatypeConverter;
> >
> > import org.apache.logging.log4j.Level;
> > @@ -233,6 +233,17 @@ public final class TypeConverters {
> >     }
> >
> >     /**
> > +     * Converts a {@link String} into an {@link InetAddress}.
> > +     */
> > +    @Plugin(name = "InetAddress", category = CATEGORY)
> > +    public static class InetAddressConverter implements TypeConverter<InetAddress> {
> > +        @Override
> > +        public InetAddress convert(final String s) throws Exception {
> > +            return InetAddress.getByName(s);
> > +        }
> > +    }
> > +
> > +    /**
> >      * Converts a {@link String} into a {@link Integer}.
> >      */
> >     @Plugin(name = "Integer", category = CATEGORY)
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java>
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
> > new file mode 100644
> > index 0000000..c652d40
> > --- /dev/null
> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidHost.java
> > @@ -0,0 +1,41 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
> > +
> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidHostValidator;
> > +
> > +import java.lang.annotation.*;
> > +import java.net.InetAddress;
> > +
> > +/**
> > + * Indicates that a plugin attribute must be a valid host. This relies on the same validation rules as
> > + * {@link InetAddress#getByName(String)}.
> > + *
> > + * @since 2.8
> > + */
> > +@Documented
> > +@Retention(RetentionPolicy.RUNTIME)
> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
> > +@Constraint(ValidHostValidator.class)
> > +public @interface ValidHost {
> > +
> > +    /**
> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
> > +     */
> > +    String message() default "The hostname is invalid";
> > +}
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java>
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
> > new file mode 100644
> > index 0000000..a7c68b1
> > --- /dev/null
> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/constraints/ValidPort.java
> > @@ -0,0 +1,44 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.constraints;
> > +
> > +import java.lang.annotation.Documented;
> > +import java.lang.annotation.ElementType;
> > +import java.lang.annotation.Retention;
> > +import java.lang.annotation.RetentionPolicy;
> > +import java.lang.annotation.Target;
> > +
> > +import org.apache.logging.log4j.core.config.plugins.validation.Constraint;
> > +import org.apache.logging.log4j.core.config.plugins.validation.validators.ValidPortValidator;
> > +
> > +/**
> > + * Indicates that a plugin attribute must be a valid port number. A valid port number is an integer between 0 and
> > + * 65535.
> > + *
> > + * @since 2.8
> > + */
> > +@Documented
> > +@Retention(RetentionPolicy.RUNTIME)
> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
> > +@Constraint(ValidPortValidator.class)
> > +public @interface ValidPort {
> > +
> > +    /**
> > +     * The message to be logged if this constraint is violated. This should normally be overridden.
> > +     */
> > +    String message() default "The port number is invalid";
> > +}
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java>
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
> > new file mode 100644
> > index 0000000..3669915
> > --- /dev/null
> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidator.java
> > @@ -0,0 +1,58 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
> > +
> > +import org.apache.logging.log4j.Logger;
> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
> > +import org.apache.logging.log4j.status.StatusLogger;
> > +
> > +import java.net.InetAddress;
> > +import java.net.UnknownHostException;
> > +
> > +/**
> > + * Validator that checks an object to verify it is a valid hostname or IP address. Validation rules follow the same
> > + * logic as in {@link InetAddress#getByName(String)}.
> > + *
> > + * @since 2.8
> > + */
> > +public class ValidHostValidator implements ConstraintValidator<ValidHost> {
> > +
> > +    private static final Logger LOGGER = StatusLogger.getLogger();
> > +
> > +    private ValidHost annotation;
> > +
> > +    @Override
> > +    public void initialize(ValidHost annotation) {
> > +        this.annotation = annotation;
> > +    }
> > +
> > +    @Override
> > +    public boolean isValid(String name, Object value) {
> > +        if (value == null) {
> > +            LOGGER.error(annotation.message());
> > +            return false;
> > +        }
> > +        try {
> > +            InetAddress.getByName(value.toString());
> > +            return true;
> > +        } catch (final UnknownHostException e) {
> > +            LOGGER.error(annotation.message(), e);
> > +            return false;
> > +        }
> > +    }
> > +}
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java>
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
> > new file mode 100644
> > index 0000000..f18f8fc
> > --- /dev/null
> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidator.java
> > @@ -0,0 +1,53 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
> > +
> > +import org.apache.logging.log4j.Logger;
> > +import org.apache.logging.log4j.core.config.plugins.validation.ConstraintValidator;
> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
> > +import org.apache.logging.log4j.status.StatusLogger;
> > +
> > +/**
> > + * Validator that checks an object to verify it is a valid port number (an integer between 0 and 65535).
> > + *
> > + * @since 2.8
> > + */
> > +public class ValidPortValidator implements ConstraintValidator<ValidPort> {
> > +
> > +    private static final Logger LOGGER = StatusLogger.getLogger();
> > +
> > +    private ValidPort annotation;
> > +
> > +    @Override
> > +    public void initialize(final ValidPort annotation) {
> > +        this.annotation = annotation;
> > +    }
> > +
> > +    @Override
> > +    public boolean isValid(final String name, final Object value) {
> > +        if (!Integer.class.isInstance(value)) {
> > +            LOGGER.error(annotation.message());
> > +            return false;
> > +        }
> > +        int port = (int) value;
> > +        if (port < 0 || port > 65535) {
> > +            LOGGER.error(annotation.message());
> > +            return false;
> > +        }
> > +        return true;
> > +    }
> > +}
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java>
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
> > new file mode 100644
> > index 0000000..4f05d68
> > --- /dev/null
> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
> > @@ -0,0 +1,46 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation;
> > +
> > +import java.net.InetSocketAddress;
> > +
> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost;
> > +import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort;
> > +
> > +@Plugin(name = "HostAndPort", category = "Test")
> > +public class HostAndPort {
> > +
> > +    private final InetSocketAddress address;
> > +
> > +    private HostAndPort(final InetSocketAddress address) {
> > +        this.address = address;
> > +    }
> > +
> > +    public boolean isValid() {
> > +        return !address.isUnresolved();
> > +    }
> > +
> > +    @PluginFactory
> > +    public static HostAndPort createPlugin(
> > +        @ValidHost(message = "Unit test (host)") @PluginAttribute("host") final String host,
> > +        @ValidPort(message = "Unit test (port)") @PluginAttribute("port") final int port) {
> > +        return new HostAndPort(new InetSocketAddress(host, port));
> > +    }
> > +}
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java>
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
> > new file mode 100644
> > index 0000000..3b0480d
> > --- /dev/null
> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidHostValidatorTest.java
> > @@ -0,0 +1,74 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
> > +
> > +import java.util.UUID;
> > +
> > +import org.apache.logging.log4j.core.config.Node;
> > +import org.apache.logging.log4j.core.config.NullConfiguration;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
> > +import org.junit.Before;
> > +import org.junit.Test;
> > +
> > +import static org.junit.Assert.*;
> > +
> > +public class ValidHostValidatorTest {
> > +
> > +    private PluginType<HostAndPort> plugin;
> > +    private Node node;
> > +
> > +    @SuppressWarnings("unchecked")
> > +    @Before
> > +    public void setUp() throws Exception {
> > +        final PluginManager manager = new PluginManager("Test");
> > +        manager.collectPlugins();
> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
> > +        node = new Node(null, "HostAndPort", plugin);
> > +    }
> > +
> > +    @Test
> > +    public void testNullHost() throws Exception {
> > +        assertNull(buildPlugin());
> > +    }
> > +
> > +    @Test
> > +    public void testInvalidIpAddress() throws Exception {
> > +        node.getAttributes().put("host", UUID.randomUUID().toString());
> > +        node.getAttributes().put("port", "1");
> > +        assertNull(buildPlugin());
> > +    }
> > +
> > +    @Test
> > +    public void testLocalhost() throws Exception {
> > +        node.getAttributes().put("host", "localhost");
> > +        node.getAttributes().put("port", "1");
> > +        final HostAndPort hostAndPort = buildPlugin();
> > +        assertNotNull(hostAndPort);
> > +        assertTrue(hostAndPort.isValid());
> > +    }
> > +
> > +    private HostAndPort buildPlugin() {
> > +        return (HostAndPort) new PluginBuilder(plugin)
> > +            .withConfiguration(new NullConfiguration())
> > +            .withConfigurationNode(node)
> > +            .build();
> > +    }
> > +}
> > \ No newline at end of file
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java>
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
> > new file mode 100644
> > index 0000000..3aab08d
> > --- /dev/null
> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/validation/validators/ValidPortValidatorTest.java
> > @@ -0,0 +1,70 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0 <http://www.apache.org/licenses/LICENSE-2.0>
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.validators;
> > +
> > +import org.apache.logging.log4j.core.config.Node;
> > +import org.apache.logging.log4j.core.config.NullConfiguration;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
> > +import org.apache.logging.log4j.core.config.plugins.validation.HostAndPort;
> > +import org.junit.Before;
> > +import org.junit.Test;
> > +
> > +import static org.junit.Assert.*;
> > +
> > +public class ValidPortValidatorTest {
> > +    private PluginType<HostAndPort> plugin;
> > +    private Node node;
> > +
> > +    @SuppressWarnings("unchecked")
> > +    @Before
> > +    public void setUp() throws Exception {
> > +        final PluginManager manager = new PluginManager("Test");
> > +        manager.collectPlugins();
> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("HostAndPort");
> > +        assertNotNull("Rebuild this module to ensure annotation processing has been done.", plugin);
> > +        node = new Node(null, "HostAndPort", plugin);
> > +        node.getAttributes().put("host", "localhost");
> > +    }
> > +
> > +    @Test
> > +    public void testNegativePort() throws Exception {
> > +        node.getAttributes().put("port", "-1");
> > +        assertNull(buildPlugin());
> > +    }
> > +
> > +    @Test
> > +    public void testValidPort() throws Exception {
> > +        node.getAttributes().put("port", "10");
> > +        assertNotNull(buildPlugin());
> > +    }
> > +
> > +    @Test
> > +    public void testInvalidPort() throws Exception {
> > +        node.getAttributes().put("port", "1234567890");
> > +        assertNull(buildPlugin());
> > +    }
> > +
> > +    private HostAndPort buildPlugin() {
> > +        return (HostAndPort) new PluginBuilder(plugin)
> > +            .withConfiguration(new NullConfiguration())
> > +            .withConfigurationNode(node)
> > +            .build();
> > +    }
> > +
> > +}
> > \ No newline at end of file
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml <http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4254e255/src/changes/changes.xml>
> > ----------------------------------------------------------------------
> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> > index c05de09..bc08dbb 100644
> > --- a/src/changes/changes.xml
> > +++ b/src/changes/changes.xml
> > @@ -213,6 +213,9 @@
> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
> >         The log4j-slf4j-impl module now declares a runtime dependency on log4j-core. While not technically required, this makes the log4j-slf4j-impl module behave similarly to slf4j-log4j12, and facilitates migration to Log4j 2.
> >       </action>
> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
> > +        Add converters and validators related to hostnames and ports.
> > +      </action>
> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
> >         Refactor SyslogAppender so that Layout is a Plugin element.
> >       </action>
> >
> >
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org <ma...@logging.apache.org>
> For additional commands, e-mail: log4j-dev-help@logging.apache.org <ma...@logging.apache.org>
> 
> 
> 
> 
> -- 
> Matt Sicker <boards@gmail.com <ma...@gmail.com>>


Re: logging-log4j2 git commit: [LOG4J2-1755]: Add converters and validators for hostnames/ports

Posted by Matt Sicker <bo...@gmail.com>.
Not sure how that's possible, but I added a better assert message. Let me
know if you're still having an issue with it. Is it a failure from IntelliJ
or Maven?

On 9 January 2017 at 19:47, Apache <ra...@dslextreme.com> wrote:

> This commit appears to be failing for me. The testInvalidIpAddress method
> is failing on the assert.
>
> Ralph
>
> > On Dec 30, 2016, at 2:01 PM, mattsicker@apache.org wrote:
> >
> > Repository: logging-log4j2
> > Updated Branches:
> >  refs/heads/master 367d26b09 -> 4254e2558
> >
> >
> > [LOG4J2-1755]: Add converters and validators for hostnames/ports
> >
> >
> > Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
> > Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/
> commit/4254e255
> > Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/
> 4254e255
> > Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/
> 4254e255
> >
> > Branch: refs/heads/master
> > Commit: 4254e2558d27351774c4bf2ad24471ca05e00018
> > Parents: 367d26b
> > Author: Matt Sicker <ma...@spr.com>
> > Authored: Fri Dec 30 15:00:17 2016 -0600
> > Committer: Matt Sicker <ma...@spr.com>
> > Committed: Fri Dec 30 15:01:26 2016 -0600
> >
> > ----------------------------------------------------------------------
> > .../config/plugins/convert/TypeConverters.java  | 13 +++-
> > .../validation/constraints/ValidHost.java       | 41 +++++++++++
> > .../validation/constraints/ValidPort.java       | 44 ++++++++++++
> > .../validators/ValidHostValidator.java          | 58 +++++++++++++++
> > .../validators/ValidPortValidator.java          | 53 ++++++++++++++
> > .../config/plugins/validation/HostAndPort.java  | 46 ++++++++++++
> > .../validators/ValidHostValidatorTest.java      | 74
> ++++++++++++++++++++
> > .../validators/ValidPortValidatorTest.java      | 70 ++++++++++++++++++
> > src/changes/changes.xml                         |  3 +
> > 9 files changed, 401 insertions(+), 1 deletion(-)
> > ----------------------------------------------------------------------
> >
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 4254e255/log4j-core/src/main/java/org/apache/logging/log4j/
> core/config/plugins/convert/TypeConverters.java
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/convert/TypeConverters.java b/log4j-core/src/main/java/
> org/apache/logging/log4j/core/config/plugins/convert/TypeConverters.java
> > index 2895e52..421d711 100644
> > --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/convert/TypeConverters.java
> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/convert/TypeConverters.java
> > @@ -20,6 +20,7 @@ package org.apache.logging.log4j.core.
> config.plugins.convert;
> > import java.io.File;
> > import java.math.BigDecimal;
> > import java.math.BigInteger;
> > +import java.net.InetAddress;
> > import java.net.MalformedURLException;
> > import java.net.URI;
> > import java.net.URISyntaxException;
> > @@ -28,7 +29,6 @@ import java.nio.charset.Charset;
> > import java.security.Provider;
> > import java.security.Security;
> > import java.util.regex.Pattern;
> > -
> > import javax.xml.bind.DatatypeConverter;
> >
> > import org.apache.logging.log4j.Level;
> > @@ -233,6 +233,17 @@ public final class TypeConverters {
> >     }
> >
> >     /**
> > +     * Converts a {@link String} into an {@link InetAddress}.
> > +     */
> > +    @Plugin(name = "InetAddress", category = CATEGORY)
> > +    public static class InetAddressConverter implements
> TypeConverter<InetAddress> {
> > +        @Override
> > +        public InetAddress convert(final String s) throws Exception {
> > +            return InetAddress.getByName(s);
> > +        }
> > +    }
> > +
> > +    /**
> >      * Converts a {@link String} into a {@link Integer}.
> >      */
> >     @Plugin(name = "Integer", category = CATEGORY)
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 4254e255/log4j-core/src/main/java/org/apache/logging/log4j/
> core/config/plugins/validation/constraints/ValidHost.java
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/constraints/ValidHost.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/constraints/ValidHost.java
> > new file mode 100644
> > index 0000000..c652d40
> > --- /dev/null
> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/constraints/ValidHost.java
> > @@ -0,0 +1,41 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version
> 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.
> constraints;
> > +
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> Constraint;
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> validators.ValidHostValidator;
> > +
> > +import java.lang.annotation.*;
> > +import java.net.InetAddress;
> > +
> > +/**
> > + * Indicates that a plugin attribute must be a valid host. This relies
> on the same validation rules as
> > + * {@link InetAddress#getByName(String)}.
> > + *
> > + * @since 2.8
> > + */
> > +@Documented
> > +@Retention(RetentionPolicy.RUNTIME)
> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
> > +@Constraint(ValidHostValidator.class)
> > +public @interface ValidHost {
> > +
> > +    /**
> > +     * The message to be logged if this constraint is violated. This
> should normally be overridden.
> > +     */
> > +    String message() default "The hostname is invalid";
> > +}
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 4254e255/log4j-core/src/main/java/org/apache/logging/log4j/
> core/config/plugins/validation/constraints/ValidPort.java
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/constraints/ValidPort.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/constraints/ValidPort.java
> > new file mode 100644
> > index 0000000..a7c68b1
> > --- /dev/null
> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/constraints/ValidPort.java
> > @@ -0,0 +1,44 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version
> 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.
> constraints;
> > +
> > +import java.lang.annotation.Documented;
> > +import java.lang.annotation.ElementType;
> > +import java.lang.annotation.Retention;
> > +import java.lang.annotation.RetentionPolicy;
> > +import java.lang.annotation.Target;
> > +
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> Constraint;
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> validators.ValidPortValidator;
> > +
> > +/**
> > + * Indicates that a plugin attribute must be a valid port number. A
> valid port number is an integer between 0 and
> > + * 65535.
> > + *
> > + * @since 2.8
> > + */
> > +@Documented
> > +@Retention(RetentionPolicy.RUNTIME)
> > +@Target({ElementType.FIELD, ElementType.PARAMETER})
> > +@Constraint(ValidPortValidator.class)
> > +public @interface ValidPort {
> > +
> > +    /**
> > +     * The message to be logged if this constraint is violated. This
> should normally be overridden.
> > +     */
> > +    String message() default "The port number is invalid";
> > +}
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 4254e255/log4j-core/src/main/java/org/apache/logging/log4j/
> core/config/plugins/validation/validators/ValidHostValidator.java
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidHostValidator.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidHostValidator.java
> > new file mode 100644
> > index 0000000..3669915
> > --- /dev/null
> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidHostValidator.java
> > @@ -0,0 +1,58 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version
> 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.
> validators;
> > +
> > +import org.apache.logging.log4j.Logger;
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> ConstraintValidator;
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> constraints.ValidHost;
> > +import org.apache.logging.log4j.status.StatusLogger;
> > +
> > +import java.net.InetAddress;
> > +import java.net.UnknownHostException;
> > +
> > +/**
> > + * Validator that checks an object to verify it is a valid hostname or
> IP address. Validation rules follow the same
> > + * logic as in {@link InetAddress#getByName(String)}.
> > + *
> > + * @since 2.8
> > + */
> > +public class ValidHostValidator implements
> ConstraintValidator<ValidHost> {
> > +
> > +    private static final Logger LOGGER = StatusLogger.getLogger();
> > +
> > +    private ValidHost annotation;
> > +
> > +    @Override
> > +    public void initialize(ValidHost annotation) {
> > +        this.annotation = annotation;
> > +    }
> > +
> > +    @Override
> > +    public boolean isValid(String name, Object value) {
> > +        if (value == null) {
> > +            LOGGER.error(annotation.message());
> > +            return false;
> > +        }
> > +        try {
> > +            InetAddress.getByName(value.toString());
> > +            return true;
> > +        } catch (final UnknownHostException e) {
> > +            LOGGER.error(annotation.message(), e);
> > +            return false;
> > +        }
> > +    }
> > +}
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 4254e255/log4j-core/src/main/java/org/apache/logging/log4j/
> core/config/plugins/validation/validators/ValidPortValidator.java
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidPortValidator.java
> b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidPortValidator.java
> > new file mode 100644
> > index 0000000..f18f8fc
> > --- /dev/null
> > +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidPortValidator.java
> > @@ -0,0 +1,53 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version
> 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.
> validators;
> > +
> > +import org.apache.logging.log4j.Logger;
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> ConstraintValidator;
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> constraints.ValidPort;
> > +import org.apache.logging.log4j.status.StatusLogger;
> > +
> > +/**
> > + * Validator that checks an object to verify it is a valid port number
> (an integer between 0 and 65535).
> > + *
> > + * @since 2.8
> > + */
> > +public class ValidPortValidator implements
> ConstraintValidator<ValidPort> {
> > +
> > +    private static final Logger LOGGER = StatusLogger.getLogger();
> > +
> > +    private ValidPort annotation;
> > +
> > +    @Override
> > +    public void initialize(final ValidPort annotation) {
> > +        this.annotation = annotation;
> > +    }
> > +
> > +    @Override
> > +    public boolean isValid(final String name, final Object value) {
> > +        if (!Integer.class.isInstance(value)) {
> > +            LOGGER.error(annotation.message());
> > +            return false;
> > +        }
> > +        int port = (int) value;
> > +        if (port < 0 || port > 65535) {
> > +            LOGGER.error(annotation.message());
> > +            return false;
> > +        }
> > +        return true;
> > +    }
> > +}
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 4254e255/log4j-core/src/test/java/org/apache/logging/log4j/
> core/config/plugins/validation/HostAndPort.java
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
> config/plugins/validation/HostAndPort.java b/log4j-core/src/test/java/
> org/apache/logging/log4j/core/config/plugins/validation/HostAndPort.java
> > new file mode 100644
> > index 0000000..4f05d68
> > --- /dev/null
> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> config/plugins/validation/HostAndPort.java
> > @@ -0,0 +1,46 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version
> 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation;
> > +
> > +import java.net.InetSocketAddress;
> > +
> > +import org.apache.logging.log4j.core.config.plugins.Plugin;
> > +import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
> > +import org.apache.logging.log4j.core.config.plugins.PluginFactory;
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> constraints.ValidHost;
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> constraints.ValidPort;
> > +
> > +@Plugin(name = "HostAndPort", category = "Test")
> > +public class HostAndPort {
> > +
> > +    private final InetSocketAddress address;
> > +
> > +    private HostAndPort(final InetSocketAddress address) {
> > +        this.address = address;
> > +    }
> > +
> > +    public boolean isValid() {
> > +        return !address.isUnresolved();
> > +    }
> > +
> > +    @PluginFactory
> > +    public static HostAndPort createPlugin(
> > +        @ValidHost(message = "Unit test (host)")
> @PluginAttribute("host") final String host,
> > +        @ValidPort(message = "Unit test (port)")
> @PluginAttribute("port") final int port) {
> > +        return new HostAndPort(new InetSocketAddress(host, port));
> > +    }
> > +}
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 4254e255/log4j-core/src/test/java/org/apache/logging/log4j/
> core/config/plugins/validation/validators/ValidHostValidatorTest.java
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidHostValidatorTest.java
> b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidHostValidatorTest.java
> > new file mode 100644
> > index 0000000..3b0480d
> > --- /dev/null
> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidHostValidatorTest.java
> > @@ -0,0 +1,74 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version
> 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.
> validators;
> > +
> > +import java.util.UUID;
> > +
> > +import org.apache.logging.log4j.core.config.Node;
> > +import org.apache.logging.log4j.core.config.NullConfiguration;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> HostAndPort;
> > +import org.junit.Before;
> > +import org.junit.Test;
> > +
> > +import static org.junit.Assert.*;
> > +
> > +public class ValidHostValidatorTest {
> > +
> > +    private PluginType<HostAndPort> plugin;
> > +    private Node node;
> > +
> > +    @SuppressWarnings("unchecked")
> > +    @Before
> > +    public void setUp() throws Exception {
> > +        final PluginManager manager = new PluginManager("Test");
> > +        manager.collectPlugins();
> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("
> HostAndPort");
> > +        assertNotNull("Rebuild this module to ensure annotation
> processing has been done.", plugin);
> > +        node = new Node(null, "HostAndPort", plugin);
> > +    }
> > +
> > +    @Test
> > +    public void testNullHost() throws Exception {
> > +        assertNull(buildPlugin());
> > +    }
> > +
> > +    @Test
> > +    public void testInvalidIpAddress() throws Exception {
> > +        node.getAttributes().put("host", UUID.randomUUID().toString());
> > +        node.getAttributes().put("port", "1");
> > +        assertNull(buildPlugin());
> > +    }
> > +
> > +    @Test
> > +    public void testLocalhost() throws Exception {
> > +        node.getAttributes().put("host", "localhost");
> > +        node.getAttributes().put("port", "1");
> > +        final HostAndPort hostAndPort = buildPlugin();
> > +        assertNotNull(hostAndPort);
> > +        assertTrue(hostAndPort.isValid());
> > +    }
> > +
> > +    private HostAndPort buildPlugin() {
> > +        return (HostAndPort) new PluginBuilder(plugin)
> > +            .withConfiguration(new NullConfiguration())
> > +            .withConfigurationNode(node)
> > +            .build();
> > +    }
> > +}
> > \ No newline at end of file
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 4254e255/log4j-core/src/test/java/org/apache/logging/log4j/
> core/config/plugins/validation/validators/ValidPortValidatorTest.java
> > ----------------------------------------------------------------------
> > diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidPortValidatorTest.java
> b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidPortValidatorTest.java
> > new file mode 100644
> > index 0000000..3aab08d
> > --- /dev/null
> > +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/
> config/plugins/validation/validators/ValidPortValidatorTest.java
> > @@ -0,0 +1,70 @@
> > +/*
> > + * Licensed to the Apache Software Foundation (ASF) under one or more
> > + * contributor license agreements. See the NOTICE file distributed with
> > + * this work for additional information regarding copyright ownership.
> > + * The ASF licenses this file to You under the Apache license, Version
> 2.0
> > + * (the "License"); you may not use this file except in compliance with
> > + * the License. You may obtain a copy of the License at
> > + *
> > + *      http://www.apache.org/licenses/LICENSE-2.0
> > + *
> > + * Unless required by applicable law or agreed to in writing, software
> > + * distributed under the License is distributed on an "AS IS" BASIS,
> > + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
> implied.
> > + * See the license for the specific language governing permissions and
> > + * limitations under the license.
> > + */
> > +package org.apache.logging.log4j.core.config.plugins.validation.
> validators;
> > +
> > +import org.apache.logging.log4j.core.config.Node;
> > +import org.apache.logging.log4j.core.config.NullConfiguration;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginBuilder;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
> > +import org.apache.logging.log4j.core.config.plugins.util.PluginType;
> > +import org.apache.logging.log4j.core.config.plugins.validation.
> HostAndPort;
> > +import org.junit.Before;
> > +import org.junit.Test;
> > +
> > +import static org.junit.Assert.*;
> > +
> > +public class ValidPortValidatorTest {
> > +    private PluginType<HostAndPort> plugin;
> > +    private Node node;
> > +
> > +    @SuppressWarnings("unchecked")
> > +    @Before
> > +    public void setUp() throws Exception {
> > +        final PluginManager manager = new PluginManager("Test");
> > +        manager.collectPlugins();
> > +        plugin = (PluginType<HostAndPort>) manager.getPluginType("
> HostAndPort");
> > +        assertNotNull("Rebuild this module to ensure annotation
> processing has been done.", plugin);
> > +        node = new Node(null, "HostAndPort", plugin);
> > +        node.getAttributes().put("host", "localhost");
> > +    }
> > +
> > +    @Test
> > +    public void testNegativePort() throws Exception {
> > +        node.getAttributes().put("port", "-1");
> > +        assertNull(buildPlugin());
> > +    }
> > +
> > +    @Test
> > +    public void testValidPort() throws Exception {
> > +        node.getAttributes().put("port", "10");
> > +        assertNotNull(buildPlugin());
> > +    }
> > +
> > +    @Test
> > +    public void testInvalidPort() throws Exception {
> > +        node.getAttributes().put("port", "1234567890");
> > +        assertNull(buildPlugin());
> > +    }
> > +
> > +    private HostAndPort buildPlugin() {
> > +        return (HostAndPort) new PluginBuilder(plugin)
> > +            .withConfiguration(new NullConfiguration())
> > +            .withConfigurationNode(node)
> > +            .build();
> > +    }
> > +
> > +}
> > \ No newline at end of file
> >
> > http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/
> 4254e255/src/changes/changes.xml
> > ----------------------------------------------------------------------
> > diff --git a/src/changes/changes.xml b/src/changes/changes.xml
> > index c05de09..bc08dbb 100644
> > --- a/src/changes/changes.xml
> > +++ b/src/changes/changes.xml
> > @@ -213,6 +213,9 @@
> >       <action issue="LOG4J2-1302" dev="rpopma" type="update">
> >         The log4j-slf4j-impl module now declares a runtime dependency on
> log4j-core. While not technically required, this makes the log4j-slf4j-impl
> module behave similarly to slf4j-log4j12, and facilitates migration to
> Log4j 2.
> >       </action>
> > +      <action issue="LOG4J2-1755" dev="mattsicker" type="add">
> > +        Add converters and validators related to hostnames and ports.
> > +      </action>
> >       <action issue="LOG4J2-969" dev="ggregory" type="add">
> >         Refactor SyslogAppender so that Layout is a Plugin element.
> >       </action>
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-dev-help@logging.apache.org
>
>


-- 
Matt Sicker <bo...@gmail.com>