Skip to content

Commit

Permalink
#1134 fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jul 16, 2022
1 parent d09c484 commit 797db1c
Show file tree
Hide file tree
Showing 37 changed files with 331 additions and 228 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/takes/Body.java
Expand Up @@ -28,6 +28,7 @@

/**
* Body abstraction for {@link Request} and {@link Response}.
*
* @since 2.0
*/
public interface Body {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/takes/facets/auth/codecs/CcXor.java
Expand Up @@ -58,9 +58,8 @@ public final class CcXor implements Codec {
public CcXor(final Codec codec, final String key) {
this(
codec,
new UncheckedBytes(
new BytesOf(key)
).asBytes());
new UncheckedBytes(new BytesOf(key)).asBytes()
);
}

/**
Expand Down
28 changes: 9 additions & 19 deletions src/main/java/org/takes/http/BkBasic.java
Expand Up @@ -31,7 +31,6 @@
import java.net.Socket;
import lombok.EqualsAndHashCode;
import org.cactoos.bytes.BytesOf;
import org.cactoos.bytes.UncheckedBytes;
import org.cactoos.io.InputStreamOf;
import org.takes.HttpException;
import org.takes.Request;
Expand Down Expand Up @@ -123,27 +122,17 @@ public void accept(final Socket socket) throws IOException {
private void print(final Request req, final OutputStream output)
throws IOException {
try {
output.write(
new RsPrint(this.take.act(req)).asBytes()
);
new RsPrint(this.take.act(req)).print(output);
} catch (final HttpException ex) {
output.write(
new UncheckedBytes(
new RsPrint(BkBasic.failure(ex, ex.code()))
).asBytes()
);
new RsPrint(BkBasic.failure(ex, ex.code())).print(output);
// @checkstyle IllegalCatchCheck (10 lines)
} catch (final Throwable ex) {
output.write(
new UncheckedBytes(
new RsPrint(
BkBasic.failure(
ex,
HttpURLConnection.HTTP_INTERNAL_ERROR
)
)
).asBytes()
);
new RsPrint(
BkBasic.failure(
ex,
HttpURLConnection.HTTP_INTERNAL_ERROR
)
).print(output);
}
}

Expand Down Expand Up @@ -189,4 +178,5 @@ private static Request addSocketHeaders(final Request req,
String.format("%s: %d", BkBasic.REMOTEPORT, socket.getPort())
);
}

}
Expand Up @@ -39,7 +39,7 @@
*
* @since 0.32
*/
interface Body extends Input {
interface RsBody extends Input {
/**
* Gives an {@code InputStream} corresponding to the content of
* the body.
Expand All @@ -61,7 +61,7 @@ interface Body extends Input {
* Content of a body based on an {@link java.net.URL}.
* @since 0.32
*/
final class Url implements Body {
final class Url implements RsBody {

/**
* The {@link java.net.URL} of the content.
Expand Down Expand Up @@ -93,7 +93,7 @@ public int length() throws IOException {
* Content of a body based on a byte array.
* @since 0.32
*/
final class ByteArray implements Body {
final class ByteArray implements RsBody {

/**
* The content of the body in a byte array.
Expand Down Expand Up @@ -123,7 +123,7 @@ public int length() {
* The content of the body based on an {@link InputStream}.
* @since 0.32
*/
final class Stream implements Body {
final class Stream implements RsBody {

/**
* The content of the body in an InputStream.
Expand Down Expand Up @@ -173,12 +173,12 @@ private void estimate() throws IOException {
*
* <p><b>The content of the Body will be stored into a temporary
* file to be able to read it as many times as we want so use it only
* for large content, for small content use {@link Body.ByteArray}
* for large content, for small content use {@link RsBody.ByteArray}
* instead.</b>
*
* @since 0.32
*/
final class TempFile implements Body {
final class TempFile implements RsBody {

/**
* The temporary file that contains the content of the body.
Expand All @@ -188,20 +188,20 @@ final class TempFile implements Body {
/**
* The underlying body.
*/
private final Body body;
private final RsBody body;

/**
* Constructs a {@code TempFile} with the specified {@link Body}.
* @param body The content of the body to store into a temporary file.
* Constructs a {@code TempFile} with the specified {@link RsBody}.
* @param content The content of the body to store into a temporary file.
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
TempFile(final Body body) {
this.body = body;
TempFile(final RsBody content) {
this.body = content;
this.file = new File(
System.getProperty("java.io.tmpdir"),
String.format(
"%s-%s.tmp",
Body.TempFile.class.getName(),
RsBody.TempFile.class.getName(),
UUID.randomUUID().toString()
)
);
Expand Down
Expand Up @@ -23,22 +23,23 @@
*/
package org.takes.rs;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.cactoos.Text;
import org.cactoos.text.TextOf;
import org.takes.Response;

/**
* Response body decorator that can print an entire body response in HTTP
* format.
* Response body decorator that can print an entire textual (!)
* body response in HTTP format.
*
* <p>This class is mostly used for testing. Don't use it for
* production code, since it will break the binary content of your
* HTTP response. It's only suitable for texts in HTTP responses.</p>
*
* <p>The class is immutable and thread-safe.
*
* @since 2.0
*/
public final class BodyPrint implements Body, Text {
public final class RsBodyPrint implements Text {

/**
* The HTTP Response.
Expand All @@ -50,37 +51,12 @@ public final class BodyPrint implements Body, Text {
*
* @param res Original response
*/
public BodyPrint(final Response res) {
public RsBodyPrint(final Response res) {
this.response = res;
}

@Override
public String asString() throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
// @checkstyle MagicNumber (1 line)
final byte[] buf = new byte[4096];
final InputStream body = this.response.body();
try {
while (true) {
final int bts = body.read(buf);
if (bts < 0) {
break;
}
baos.write(buf, 0, bts);
}
} finally {
baos.flush();
}
return new TextOf(baos.toByteArray()).toString();
}

@Override
public InputStream stream() throws IOException {
return this.response.body();
}

@Override
public int length() throws IOException {
return this.asString().length();
return new RsPrint(this.response).printBody();
}
}
Expand Up @@ -25,9 +25,7 @@

import java.io.IOException;
import org.cactoos.Text;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.takes.Head;
import org.takes.Response;

/**
* Response head decorator that can print an entire head response in HTTP
Expand All @@ -37,42 +35,24 @@
*
* @since 2.0
*/
public final class HeadPrint implements Head, Text {

/**
* HTTP End Of Line.
*/
private static final String EOL = "\r\n";
public final class RsHeadPrint implements Text {

/**
* The HTTP Response.
*/
private final Head origin;
private final Response origin;

/**
* Ctor.
* @param head Original head
*/
public HeadPrint(final Head head) {
public RsHeadPrint(final Response head) {
this.origin = head;
}

@Override
public String asString() throws IOException {
return new FormattedText(
"%s%s%s",
new Joined(
HeadPrint.EOL,
this.head()
),
HeadPrint.EOL,
HeadPrint.EOL
).toString();
}

@Override
public Iterable<String> head() throws IOException {
return this.origin.head();
return new RsPrint(this.origin).printHead();
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rs/RsHtml.java
Expand Up @@ -106,7 +106,7 @@ public RsHtml(final Response res, final byte[] body) {
* @param body HTML body
*/
public RsHtml(final Response res, final InputStream body) {
this(new RsWithBody(res, new Body.TempFile(new Body.Stream(body))));
this(new RsWithBody(res, new RsBody.TempFile(new RsBody.Stream(body))));
}

/**
Expand Down

0 comments on commit 797db1c

Please sign in to comment.