Skip to content

Commit

Permalink
(#918) After review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fanifieiev committed Nov 20, 2019
1 parent 58ce68a commit 5db4c9c
Show file tree
Hide file tree
Showing 31 changed files with 129 additions and 132 deletions.
60 changes: 23 additions & 37 deletions src/main/java/org/takes/facets/fallback/TkFallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
import org.takes.HttpException;
import org.takes.Request;
import org.takes.Response;
import org.takes.Scalar;
import org.takes.Take;
import org.takes.misc.Opt;
import org.takes.rq.RqHref;
import org.takes.rq.RqMethod;
import org.takes.rs.RsOf;
import org.takes.rs.ResponseOf;
import org.takes.tk.TkWrap;

/**
Expand All @@ -47,6 +46,8 @@
*
* @since 0.1
* @checkstyle IllegalCatchCheck (500 lines)
* @todo #918:30min {@link TkFallback} class is very complicated, hard to read.
* Please consider removing static methods and replace them with decorators.
*/
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
Expand Down Expand Up @@ -147,23 +148,31 @@ private static RqFallback.Fake fallback(final Request req, final long start,
* @param fbk Fallback
* @param req Request
* @return Response
* @checkstyle ExecutableStatementCount
* @checkstyle ExecutableStatementCountCheck (100 lines)
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private static Response wrap(final Response res, final Fallback fbk,
final Request req) {
return new RsOf(
return new ResponseOf(
() -> {
final long start = System.currentTimeMillis();
Iterable<String> head;
try {
head = res.head();
} catch (final HttpException ex) {
try {
head = fbk.route(
TkFallback.fallback(req, start, ex, ex.code())
).get().head();
} catch (final Exception exx) {
throw (IOException) new IOException(exx).initCause(ex);
}
} catch (final Throwable ex) {
try {
head = fbk.route(
TkFallback.fallback(
req, start, ex,
new TkFallback.HtErrCodeOf(ex).get()
HttpURLConnection.HTTP_INTERNAL_ERROR
)
).get().head();
} catch (final Exception exx) {
Expand All @@ -177,12 +186,20 @@ private static Response wrap(final Response res, final Fallback fbk,
InputStream body;
try {
body = res.body();
} catch (final HttpException ex) {
try {
body = fbk.route(
TkFallback.fallback(req, start, ex, ex.code())
).get().body();
} catch (final Exception exx) {
throw (IOException) new IOException(exx).initCause(ex);
}
} catch (final Throwable ex) {
try {
body = fbk.route(
TkFallback.fallback(
req, start, ex,
new TkFallback.HtErrCodeOf(ex).get()
HttpURLConnection.HTTP_INTERNAL_ERROR
)
).get().body();
} catch (final Exception exx) {
Expand Down Expand Up @@ -225,35 +242,4 @@ private static Throwable error(final Throwable exp, final Request req,
);
}

/**
* Scalar to retrieve Http Status Error Code from Exception.
* @since 2.0
*/
private static final class HtErrCodeOf implements Scalar<Integer> {

/**
* Throwable object.
*/
private final Throwable throwable;

/**
* Ctor.
* @param throwable Throwable param
*/
HtErrCodeOf(final Throwable throwable) {
this.throwable = throwable;
}

@Override
public Integer get() throws IOException {
final int code;
if (this.throwable instanceof HttpException) {
code = ((HttpException) throwable).code();
} else {
code = HttpURLConnection.HTTP_INTERNAL_ERROR;
}
return code;
}
}

}
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/fork/RsFork.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.takes.Request;
import org.takes.Response;
import org.takes.misc.Opt;
import org.takes.rs.RsOf;
import org.takes.rs.ResponseOf;
import org.takes.rs.RsWrap;

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ public RsFork(final Request req, final Fork... list) {
*/
public RsFork(final Request req, final Iterable<Fork> list) {
super(
new RsOf(
new ResponseOf(
() -> RsFork.pick(req, list).head(),
() -> RsFork.pick(req, list).body()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.io.IOException;
import java.io.InputStream;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Scalar;

Expand All @@ -36,8 +35,7 @@
* <p>The class is immutable and thread-safe.
* @since 2.0
*/
@EqualsAndHashCode(exclude = {"shead", "sbody"})
public final class RqOf implements Request {
public final class RequestOf implements Request {
/**
* Original head scalar.
*/
Expand All @@ -48,12 +46,25 @@ public final class RqOf implements Request {
*/
private final Scalar<InputStream> sbody;

/**
* Ctor.
* @param head Iterable head value
* @param body InputStream body value
*/
public RequestOf(final Iterable<String> head,
final InputStream body) {
this(
() -> head,
() -> body
);
}

/**
* Ctor.
* @param head Scalar to provide head value
* @param body Scalar to provide body value
*/
public RqOf(final Scalar<Iterable<String>> head,
public RequestOf(final Scalar<Iterable<String>> head,
final Scalar<InputStream> body) {
this.shead = head;
this.sbody = body;
Expand All @@ -68,4 +79,14 @@ public Iterable<String> head() throws IOException {
public InputStream body() throws IOException {
return this.sbody.get();
}

@Override
public boolean equals(final Object that) {
return this == that || RequestOf.class.equals(that.getClass());
}

@Override
public int hashCode() {
return 0;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqBuffered.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class RqBuffered extends RqWrap {
*/
public RqBuffered(final Request req) {
super(
new RqOf(
new RequestOf(
req::head,
() -> new BufferedInputStream(req.body())
)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public final class RqChunk extends RqWrap {
*/
public RqChunk(final Request req) {
super(
new RqOf(
new RequestOf(
req::head,
() -> RqChunk.cap(req)
)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public RqFake(final List<String> head, final byte[] body) {
*/
public RqFake(final List<String> head, final InputStream body) {
super(
new RqOf(
new RequestOf(
() -> Collections.unmodifiableList(head),
() -> body
)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqGreedy.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public RqGreedy(final Request req) throws IOException {
private static Request consume(final Request req) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
new RqPrint(req).printBody(baos);
return new RqOf(
return new RequestOf(
req::head,
() -> new ByteArrayInputStream(baos.toByteArray())
);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqLengthAware.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final class RqLengthAware extends RqWrap {
*/
public RqLengthAware(final Request req) {
super(
new RqOf(
new RequestOf(
req::head,
() -> RqLengthAware.cap(req)
)
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/takes/rq/RqLive.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ private static Request parse(final InputStream input) throws IOException {
if (eof) {
throw new IOException("empty request");
}
return new RqOf(
() -> head,
() -> input
);
return new RequestOf(head, input);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqOnce.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public RqOnce(final Request req) {
*/
private static Request wrap(final Request req) {
final AtomicBoolean seen = new AtomicBoolean(false);
return new RqOf(
return new RequestOf(
req::head,
() -> {
if (!seen.getAndSet(true)) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqSimple.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class RqSimple extends RqWrap {
*/
public RqSimple(final Iterable<String> head, final InputStream body) {
super(
new RqOf(
new RequestOf(
() -> head,
() -> body
)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqWithBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class RqWithBody extends RqWrap {
*/
public RqWithBody(final Request req, final CharSequence bdy) {
super(
new RqOf(
new RequestOf(
req::head,
() -> new ByteArrayInputStream(
new Utf8String(bdy.toString()).asBytes()
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqWithHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public RqWithHeaders(final Request req, final CharSequence... headers) {
public RqWithHeaders(final Request req,
final Iterable<? extends CharSequence> headers) {
super(
new RqOf(
new RequestOf(
() -> {
final List<String> head = new LinkedList<>();
for (final String hdr : req.head()) {
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/takes/rq/RqWithoutHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.cactoos.Text;
import org.cactoos.iterable.Filtered;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Lowered;
import org.takes.Request;
import org.takes.misc.EnglishLowerCase;

/**
* Request without a header (even if it was absent).
Expand All @@ -48,16 +48,14 @@ public final class RqWithoutHeader extends RqWrap {
public RqWithoutHeader(final Request req, final CharSequence name) {
super(
// @checkstyle AnonInnerLengthCheck (50 lines)
new RqOf(
new RequestOf(
() -> {
final Text prefix = new FormattedText(
"%s:",
new EnglishLowerCase(
name.toString()
).string()
new Lowered(name.toString()).asString()
);
return new Filtered<>(
header -> !new EnglishLowerCase(header).string()
header -> !new Lowered(header).asString()
.startsWith(prefix.asString()),
req.head()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import java.io.IOException;
import java.io.InputStream;
import lombok.EqualsAndHashCode;
import org.takes.Response;
import org.takes.Scalar;

Expand All @@ -36,8 +35,7 @@
*
* @since 2.0
*/
@EqualsAndHashCode(exclude = {"shead", "sbody"})
public class RsOf implements Response {
public final class ResponseOf implements Response {

/**
* Original head scalar.
Expand All @@ -49,24 +47,47 @@ public class RsOf implements Response {
*/
private final Scalar<InputStream> sbody;

/**
* Ctor.
* @param head Iterable head value
* @param body InputStream body value
*/
public ResponseOf(final Iterable<String> head,
final InputStream body) {
this(
() -> head,
() -> body
);
}

/**
* Ctor.
* @param head Scalar to provide head value
* @param body Scalar to provide body value
*/
public RsOf(final Scalar<Iterable<String>> head,
public ResponseOf(final Scalar<Iterable<String>> head,
final Scalar<InputStream> body) {
this.shead = head;
this.sbody = body;
}

@Override
public final Iterable<String> head() throws IOException {
public Iterable<String> head() throws IOException {
return this.shead.get();
}

@Override
public final InputStream body() throws IOException {
public InputStream body() throws IOException {
return this.sbody.get();
}

@Override
public boolean equals(final Object that) {
return this == that || ResponseOf.class.equals(that.getClass());
}

@Override
public int hashCode() {
return 0;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rs/RsBuffered.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class RsBuffered extends RsWrap {
*/
public RsBuffered(final Response res) {
super(
new RsOf(
new ResponseOf(
res::head,
() -> new BufferedInputStream(res.body())
)
Expand Down
Loading

0 comments on commit 5db4c9c

Please sign in to comment.