Skip to content

Commit

Permalink
refactoring for yegor256#473 according to @yegor256 CR
Browse files Browse the repository at this point in the history
  • Loading branch information
xupyprmv committed Jan 4, 2016
1 parent b66a214 commit a9d7c99
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 46 deletions.
3 changes: 1 addition & 2 deletions src/main/java/org/takes/rq/RqHref.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/takes/rq/RqMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
103 changes: 68 additions & 35 deletions src/main/java/org/takes/rq/RqRequestLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions src/test/java/org/takes/rq/RqRequestLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -106,7 +105,7 @@ public void extractsParams() throws IOException {
public void failsOnAbsentRequestLineToken() throws IOException {
new RqRequestLine.Base(
new RqSimple(Collections.<String>emptyList(), null)
).requestLineHeaderToken(Token.METHOD);
).method();
}

/**
Expand All @@ -125,7 +124,7 @@ public void failsOnIllegalRequestLineToken() throws IOException {
),
""
)
).requestLineHeaderToken(Token.METHOD);
).method();
}

/**
Expand All @@ -145,7 +144,7 @@ public void extractsFirstParam() throws IOException {
),
""
)
).requestLineHeaderToken(Token.METHOD),
).method(),
Matchers.equalToIgnoringCase("GET")
);
}
Expand All @@ -167,7 +166,7 @@ public void extractsSecondParam() throws IOException {
),
""
)
).requestLineHeaderToken(Token.URI),
).requestUri(),
Matchers.equalToIgnoringCase("/hello?since=3432")
);
}
Expand All @@ -189,7 +188,7 @@ public void extractsThirdParam() throws IOException {
),
""
)
).requestLineHeaderToken(Token.HTTPVERSION),
).httpVersion(),
Matchers.equalToIgnoringCase("HTTP/1.1")
);
}
Expand All @@ -211,7 +210,7 @@ public void extractsEmptyThirdParam() throws IOException {
),
""
)
).requestLineHeaderToken(Token.HTTPVERSION),
).httpVersion(),
Matchers.equalTo(null)
);
}
Expand Down

0 comments on commit a9d7c99

Please sign in to comment.