From a9d7c999222c6fd3eeeb902fa9ba18c8063862a0 Mon Sep 17 00:00:00 2001 From: Maksimenko Vladimir Date: Mon, 4 Jan 2016 10:29:59 +0300 Subject: [PATCH] refactoring for #473 according to @yegor256 CR --- src/main/java/org/takes/rq/RqHref.java | 3 +- src/main/java/org/takes/rq/RqMethod.java | 3 +- src/main/java/org/takes/rq/RqRequestLine.java | 103 ++++++++++++------ .../java/org/takes/rq/RqRequestLineTest.java | 13 +-- 4 files changed, 76 insertions(+), 46 deletions(-) diff --git a/src/main/java/org/takes/rq/RqHref.java b/src/main/java/org/takes/rq/RqHref.java index 1963ed829..39d4f82a0 100644 --- a/src/main/java/org/takes/rq/RqHref.java +++ b/src/main/java/org/takes/rq/RqHref.java @@ -31,7 +31,6 @@ import org.takes.HttpException; import org.takes.Request; import org.takes.misc.Href; -import org.takes.rq.RqRequestLine.Token; /** * HTTP URI query parsing. @@ -73,7 +72,7 @@ public Base(final Request req) { @Override public Href href() throws IOException { final String uri = new RqRequestLine.Base(this) - .requestLineHeaderToken(Token.URI); + .requestUri(); return new Href( String.format( "http://%s%s", diff --git a/src/main/java/org/takes/rq/RqMethod.java b/src/main/java/org/takes/rq/RqMethod.java index 45f7502c9..db447d5f6 100644 --- a/src/main/java/org/takes/rq/RqMethod.java +++ b/src/main/java/org/takes/rq/RqMethod.java @@ -28,7 +28,6 @@ import java.util.regex.Pattern; import lombok.EqualsAndHashCode; import org.takes.Request; -import org.takes.rq.RqRequestLine.Token; /** * HTTP method parsing. @@ -123,7 +122,7 @@ public Base(final Request req) { @Override public String method() throws IOException { final String method = new RqRequestLine.Base(this) - .requestLineHeaderToken(Token.METHOD); + .method(); if (SEPARATORS.matcher(method).find()) { throw new IOException( String.format("Invalid HTTP method: %s", method) diff --git a/src/main/java/org/takes/rq/RqRequestLine.java b/src/main/java/org/takes/rq/RqRequestLine.java index eea64cb7f..a315d7083 100644 --- a/src/main/java/org/takes/rq/RqRequestLine.java +++ b/src/main/java/org/takes/rq/RqRequestLine.java @@ -42,36 +42,6 @@ */ public interface RqRequestLine extends Request { - public static enum Token { - /** - * METHOD token. - */ - METHOD(1), - - /** - * URI token. - */ - URI(2), - - /** - * HTTPVERSION token. - */ - HTTPVERSION(3); - - /** - * Value. - */ - private final int value; - - /** - * Ctor. - * @param val Value - */ - private Token(final int val) { - this.value = val; - } - } - /** * Get Request-Line header. * @return HTTP Request-Line header @@ -80,12 +50,25 @@ private Token(final int val) { String requestLineHeader() throws IOException; /** - * Get Request-Line header token. - * @param token Token - * @return HTTP Request-Line header token + * Get Request-Line method token. + * @return HTTP Request-Line method token + * @throws IOException If fails + */ + String method() throws IOException; + + /** + * Get Request-Line Request-URI token. + * @return HTTP Request-Line method token * @throws IOException If fails */ - String requestLineHeaderToken(Token token) throws IOException; + String requestUri() throws IOException; + + /** + * Get Request-Line HTTP-Version token. + * @return HTTP Request-Line method token + * @throws IOException If fails + */ + String httpVersion() throws IOException; /** * Request decorator for Request-Line header validation @@ -107,6 +90,36 @@ final class Base extends RqWrap implements RqRequestLine { "([!-~]+) ([^ ]+)( [^ ]+)?" ); + private static enum Token { + /** + * METHOD token. + */ + METHOD(1), + + /** + * URI token. + */ + URI(2), + + /** + * HTTPVERSION token. + */ + HTTPVERSION(3); + + /** + * Value. + */ + private final int value; + + /** + * Ctor. + * @param val Value + */ + private Token(final int val) { + this.value = val; + } + } + /** * Ctor. * @param req Original request @@ -123,7 +136,27 @@ public String requestLineHeader() throws IOException { } @Override - public String requestLineHeaderToken(final Token token) + public String method() throws IOException { + return this.requestLineHeaderToken(Token.METHOD); + } + + @Override + public String requestUri() throws IOException { + return this.requestLineHeaderToken(Token.URI); + } + + @Override + public String httpVersion() throws IOException { + return this.requestLineHeaderToken(Token.HTTPVERSION); + } + + /** + * Get Request-Line header token. + * @param token Token + * @return HTTP Request-Line header token + * @throws IOException If fails + */ + private String requestLineHeaderToken(final Token token) throws IOException { final String requestLine = this.getRequestLineHeader(); final Matcher matcher = this.validateRequestLine(requestLine); diff --git a/src/test/java/org/takes/rq/RqRequestLineTest.java b/src/test/java/org/takes/rq/RqRequestLineTest.java index 4ea3ce2e4..f300d72e4 100644 --- a/src/test/java/org/takes/rq/RqRequestLineTest.java +++ b/src/test/java/org/takes/rq/RqRequestLineTest.java @@ -30,7 +30,6 @@ import org.hamcrest.Matchers; import org.junit.Test; import org.takes.HttpException; -import org.takes.rq.RqRequestLine.Token; /** * Test case for {@link RqRequestLine.Base}. @@ -106,7 +105,7 @@ public void extractsParams() throws IOException { public void failsOnAbsentRequestLineToken() throws IOException { new RqRequestLine.Base( new RqSimple(Collections.emptyList(), null) - ).requestLineHeaderToken(Token.METHOD); + ).method(); } /** @@ -125,7 +124,7 @@ public void failsOnIllegalRequestLineToken() throws IOException { ), "" ) - ).requestLineHeaderToken(Token.METHOD); + ).method(); } /** @@ -145,7 +144,7 @@ public void extractsFirstParam() throws IOException { ), "" ) - ).requestLineHeaderToken(Token.METHOD), + ).method(), Matchers.equalToIgnoringCase("GET") ); } @@ -167,7 +166,7 @@ public void extractsSecondParam() throws IOException { ), "" ) - ).requestLineHeaderToken(Token.URI), + ).requestUri(), Matchers.equalToIgnoringCase("/hello?since=3432") ); } @@ -189,7 +188,7 @@ public void extractsThirdParam() throws IOException { ), "" ) - ).requestLineHeaderToken(Token.HTTPVERSION), + ).httpVersion(), Matchers.equalToIgnoringCase("HTTP/1.1") ); } @@ -211,7 +210,7 @@ public void extractsEmptyThirdParam() throws IOException { ), "" ) - ).requestLineHeaderToken(Token.HTTPVERSION), + ).httpVersion(), Matchers.equalTo(null) ); }