diff --git a/src/main/java/io/jare/dynamo/DyUser.java b/src/main/java/io/jare/dynamo/DyUser.java index a853d1f..2bc0e6b 100644 --- a/src/main/java/io/jare/dynamo/DyUser.java +++ b/src/main/java/io/jare/dynamo/DyUser.java @@ -34,7 +34,6 @@ import java.io.IOException; import java.util.Iterator; import java.util.Locale; -import java.util.regex.Pattern; import java.util.stream.Collectors; /** @@ -47,13 +46,6 @@ */ public final class DyUser implements User { - /** - * Pattern to match. - */ - private static final Pattern PTN = Pattern.compile( - "[a-zA-Z0-9\\.\\-]+" - ); - /** * The region to work with. */ @@ -93,15 +85,6 @@ public Iterable mine() { @Override public void add(final String name) throws IOException { - if (!DyUser.PTN.matcher(name).matches()) { - throw new IllegalArgumentException( - String.format( - // @checkstyle LineLength (1 line) - "invalid domain name \"%s\", must contain letters, dots and dashes", - name - ) - ); - } synchronized (this.region) { final Iterator before = new DyBase(this.region) .domain(name); diff --git a/src/main/java/io/jare/smarts/SafeUser.java b/src/main/java/io/jare/smarts/SafeUser.java new file mode 100644 index 0000000..77e88c2 --- /dev/null +++ b/src/main/java/io/jare/smarts/SafeUser.java @@ -0,0 +1,93 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2016 jare.io + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: the above copyright notice and this + * permission notice shall be included in all copies or substantial + * portions of the Software. The software is provided "as is", without + * warranty of any kind, express or implied, including but not limited to + * the warranties of merchantability, fitness for a particular purpose + * and non-infringement. In no event shall the authors or copyright + * holders be liable for any claim, damages or other liability, whether + * in an action of contract, tort or otherwise, arising from, out of or + * in connection with the software or the use or other dealings in the + * software. + */ +package io.jare.smarts; + +import io.jare.model.Domain; +import io.jare.model.User; +import java.io.IOException; +import java.util.regex.Pattern; + +/** + * Safe user. + * + * @author Yegor Bugayenko (yegor@teamed.io) + * @version $Id$ + * @since 1.0 + */ +public final class SafeUser implements User { + + /** + * Pattern to match. + */ + private static final Pattern PTN = Pattern.compile( + "(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(? mine() { + return this.origin.mine(); + } + + @Override + public void add(final String name) throws IOException { + if (!SafeUser.PTN.matcher(name).matches()) { + throw new SafeUser.InvalidNameException(name); + } + this.origin.add(name); + } + + /** + * When name is not valid. + */ + public static final class InvalidNameException extends IOException { + /** + * Serialization marker. + */ + private static final long serialVersionUID = -869776873934626730L; + /** + * Ctor. + * @param name Domain name + */ + public InvalidNameException(final String name) { + super( + String.format( + "domain name \"%s\" is not valid", + name + ) + ); + } + } +} diff --git a/src/main/java/io/jare/smarts/package-info.java b/src/main/java/io/jare/smarts/package-info.java new file mode 100644 index 0000000..09d86c9 --- /dev/null +++ b/src/main/java/io/jare/smarts/package-info.java @@ -0,0 +1,31 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2016 jare.io + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: the above copyright notice and this + * permission notice shall be included in all copies or substantial + * portions of the Software. The software is provided "as is", without + * warranty of any kind, express or implied, including but not limited to + * the warranties of merchantability, fitness for a particular purpose + * and non-infringement. In no event shall the authors or copyright + * holders be liable for any claim, damages or other liability, whether + * in an action of contract, tort or otherwise, arising from, out of or + * in connection with the software or the use or other dealings in the + * software. + */ + +/** + * Smarts. + * + * @author Yegor Bugayenko (yegor@teamed.io) + * @version $Id$ + * @since 1.0 + */ +package io.jare.smarts; diff --git a/src/main/java/io/jare/tk/TkAdd.java b/src/main/java/io/jare/tk/TkAdd.java index fab14f4..4998e1e 100644 --- a/src/main/java/io/jare/tk/TkAdd.java +++ b/src/main/java/io/jare/tk/TkAdd.java @@ -23,6 +23,7 @@ package io.jare.tk; import io.jare.model.Base; +import io.jare.smarts.SafeUser; import java.io.IOException; import org.takes.Request; import org.takes.Response; @@ -57,7 +58,7 @@ final class TkAdd implements Take { public Response act(final Request req) throws IOException { final String name = new RqForm.Base(req).param("name") .iterator().next().trim(); - this.base.user(new RqUser(req).name()).add(name); + new SafeUser(this.base.user(new RqUser(req).name())).add(name); return new RsForward( new RsFlash( String.format( diff --git a/src/test/java/io/jare/smarts/SafeUserTest.java b/src/test/java/io/jare/smarts/SafeUserTest.java new file mode 100644 index 0000000..dc12671 --- /dev/null +++ b/src/test/java/io/jare/smarts/SafeUserTest.java @@ -0,0 +1,87 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2016 jare.io + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: the above copyright notice and this + * permission notice shall be included in all copies or substantial + * portions of the Software. The software is provided "as is", without + * warranty of any kind, express or implied, including but not limited to + * the warranties of merchantability, fitness for a particular purpose + * and non-infringement. In no event shall the authors or copyright + * holders be liable for any claim, damages or other liability, whether + * in an action of contract, tort or otherwise, arising from, out of or + * in connection with the software or the use or other dealings in the + * software. + */ +package io.jare.smarts; + +import io.jare.fake.FkUser; +import io.jare.model.User; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; + +/** + * Test case for {@link User}. + * @author Yegor Bugayenko (yegor@teamed.io) + * @version $Id$ + * @since 0.5 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +public final class SafeUserTest { + + /** + * User.Safe can accept normal domain names. + * @throws Exception If some problem inside + */ + @Test + public void acceptsValidDomains() throws Exception { + final User user = new SafeUser(new FkUser()); + final String[] domains = { + "google.com", + "www.google.com", + "www-1.google.com", + "google.ua", + "www-8-9.google.ua", + }; + for (final String domain : domains) { + user.add(domain); + } + } + + /** + * User.Safe can reject invalid domain names. + * @throws Exception If some problem inside + */ + @Test + public void rejectsInvalidDomains() throws Exception { + final User user = new SafeUser(new FkUser()); + final String[] domains = { + "google-com", + "google", + "www-1 .google.com", + "google.УА", + "www-8=9.google.ua", + }; + for (final String domain : domains) { + try { + user.add(domain); + Assert.fail(String.format("exception expected: %s", domain)); + } catch (final SafeUser.InvalidNameException ex) { + MatcherAssert.assertThat( + ex.getLocalizedMessage(), + Matchers.containsString(domain) + ); + } + } + } + +} diff --git a/src/test/java/io/jare/smarts/package-info.java b/src/test/java/io/jare/smarts/package-info.java new file mode 100644 index 0000000..9ed66b4 --- /dev/null +++ b/src/test/java/io/jare/smarts/package-info.java @@ -0,0 +1,31 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2016 jare.io + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: the above copyright notice and this + * permission notice shall be included in all copies or substantial + * portions of the Software. The software is provided "as is", without + * warranty of any kind, express or implied, including but not limited to + * the warranties of merchantability, fitness for a particular purpose + * and non-infringement. In no event shall the authors or copyright + * holders be liable for any claim, damages or other liability, whether + * in an action of contract, tort or otherwise, arising from, out of or + * in connection with the software or the use or other dealings in the + * software. + */ + +/** + * Smarts, tests. + * + * @author Yegor Bugayenko (yegor@teamed.io) + * @version $Id$ + * @since 1.0 + */ +package io.jare.smarts;