From 9f970b19b12dbb54990bef1061e02d3a2d7550a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Thu, 3 Mar 2016 19:50:45 -0300 Subject: [PATCH 1/4] For issue #594 Solving puzzle 558-afc1c081 in src/main/java/org/takes/rq/RqMultipart.java:131-135 --- src/main/java/org/takes/rq/RqMultipart.java | 44 ++++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/takes/rq/RqMultipart.java b/src/main/java/org/takes/rq/RqMultipart.java index 8b4059d45..ec08f61f3 100644 --- a/src/main/java/org/takes/rq/RqMultipart.java +++ b/src/main/java/org/takes/rq/RqMultipart.java @@ -128,26 +128,14 @@ final class Base extends RqWrap implements RqMultipart { * @param req Original request * @throws IOException If fails * @checkstyle ExecutableStatementCountCheck (2 lines) - * @todo #558:30min Base ctor. According to new qulice version, - * constructor must contain only variables initialization and other - * constructor calls. Refactor code according to that rule and - * remove `ConstructorOnlyInitializesOrCallOtherConstructors` - * warning suppression. */ - @SuppressWarnings - ( - "PMD.ConstructorOnlyInitializesOrCallOtherConstructors" - ) public Base(final Request req) throws IOException { super(req); - final InputStream stream = new RqLengthAware(req).body(); - this.body = Channels.newChannel(stream); - this.buffer = ByteBuffer.allocate( - // @checkstyle MagicNumberCheck (1 line) - Math.min(8192, stream.available()) - ); + this.body = Base.body(req); + this.buffer = Base.buffer(req); this.map = this.requests(req); } + @Override public Iterable part(final CharSequence name) { final List values = this.map @@ -176,6 +164,32 @@ public Iterable part(final CharSequence name) { public Iterable names() { return this.map.keySet(); } + + /** + * Returns the ReadableByteChannel based on request body. + * @param req Original request + * @return The ReadableByteChannel based on request body. + * @throws IOException If fails + */ + private static ReadableByteChannel body(final Request req) + throws IOException { + return Channels.newChannel(new RqLengthAware(req).body()); + } + + /** + * Returns the ByteBuffer for the request body. + * @param req Original request + * @return The ByteBuffer for the request body. + * @throws IOException If fails + */ + private static ByteBuffer buffer(final Request req) + throws IOException { + return ByteBuffer.allocate( + // @checkstyle MagicNumberCheck (1 line) + Math.min(8192, new RqLengthAware(req).body().available()) + ); + } + /** * Build a request for each part of the origin request. * @param req Origin request From ac3c4eb9e87d13348a221d0e0be3a8b5ed2f623d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Sat, 5 Mar 2016 17:00:52 -0300 Subject: [PATCH 2/4] Review fixes. --- src/main/java/org/takes/rq/RqMultipart.java | 55 +++++++-------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/takes/rq/RqMultipart.java b/src/main/java/org/takes/rq/RqMultipart.java index ec08f61f3..59a7f39bf 100644 --- a/src/main/java/org/takes/rq/RqMultipart.java +++ b/src/main/java/org/takes/rq/RqMultipart.java @@ -120,9 +120,9 @@ final class Base extends RqWrap implements RqMultipart { */ private final transient ByteBuffer buffer; /** - * Origin request body. + * InputStream based on request body. */ - private final transient ReadableByteChannel body; + private final transient InputStream stream; /** * Ctor. * @param req Original request @@ -131,11 +131,13 @@ final class Base extends RqWrap implements RqMultipart { */ public Base(final Request req) throws IOException { super(req); - this.body = Base.body(req); - this.buffer = Base.buffer(req); + this.stream = new RqLengthAware(req).body(); + this.buffer = ByteBuffer.allocate( + // @checkstyle MagicNumberCheck (1 line) + Math.min(8192, this.stream.available()) + ); this.map = this.requests(req); } - @Override public Iterable part(final CharSequence name) { final List values = this.map @@ -164,32 +166,6 @@ public Iterable part(final CharSequence name) { public Iterable names() { return this.map.keySet(); } - - /** - * Returns the ReadableByteChannel based on request body. - * @param req Original request - * @return The ReadableByteChannel based on request body. - * @throws IOException If fails - */ - private static ReadableByteChannel body(final Request req) - throws IOException { - return Channels.newChannel(new RqLengthAware(req).body()); - } - - /** - * Returns the ByteBuffer for the request body. - * @param req Original request - * @return The ByteBuffer for the request body. - * @throws IOException If fails - */ - private static ByteBuffer buffer(final Request req) - throws IOException { - return ByteBuffer.allocate( - // @checkstyle MagicNumberCheck (1 line) - Math.min(8192, new RqLengthAware(req).body().available()) - ); - } - /** * Build a request for each part of the origin request. * @param req Origin request @@ -224,7 +200,8 @@ private Map> requests( ) ); } - if (this.body.read(this.buffer) < 0) { + final ReadableByteChannel body = Channels.newChannel(this.stream); + if (body.read(this.buffer) < 0) { throw new HttpException( HttpURLConnection.HTTP_BAD_REQUEST, "failed to read the request body" @@ -242,7 +219,7 @@ private Map> requests( break; } this.buffer.position(this.buffer.position() + 1); - requests.add(this.make(boundary)); + requests.add(this.make(boundary, body)); } return RqMultipart.Base.asMap(requests); } @@ -251,6 +228,7 @@ private Map> requests( * Scans the origin request until the boundary reached. Caches * the content into a temporary file and returns it as a new request. * @param boundary Boundary + * @param body Origin request body * @return Request * @throws IOException If fails * @todo #254:30min in order to delete temporary files InputStream @@ -258,7 +236,8 @@ private Map> requests( * requests that means that body of all parts should be closed once * they are not needed anymore. */ - private Request make(final byte[] boundary) throws IOException { + private Request make(final byte[] boundary, + final ReadableByteChannel body) throws IOException { final File file = File.createTempFile( RqMultipart.class.getName(), ".tmp" ); @@ -279,7 +258,7 @@ private Request make(final byte[] boundary) throws IOException { RqMultipart.Fake.CRLF.getBytes(StandardCharsets.UTF_8) ) ); - this.copy(channel, boundary); + this.copy(channel, boundary, body); } finally { channel.close(); } @@ -298,11 +277,13 @@ private Request make(final byte[] boundary) throws IOException { * Copy until boundary reached. * @param target Output file channel * @param boundary Boundary + * @param body Origin request body * @throws IOException If fails * @checkstyle ExecutableStatementCountCheck (2 lines) */ private void copy(final WritableByteChannel target, - final byte[] boundary) throws IOException { + final byte[] boundary, + final ReadableByteChannel body) throws IOException { int match = 0; boolean cont = true; while (cont) { @@ -312,7 +293,7 @@ private void copy(final WritableByteChannel target, this.buffer.put(boundary[idx]); } match = 0; - if (this.body.read(this.buffer) == -1) { + if (body.read(this.buffer) == -1) { break; } this.buffer.flip(); From a5bbae1b3403eca631dc95ec503a592ebbdc440b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Sat, 5 Mar 2016 18:13:03 -0300 Subject: [PATCH 3/4] Indentation --- src/main/java/org/takes/rq/RqMultipart.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/takes/rq/RqMultipart.java b/src/main/java/org/takes/rq/RqMultipart.java index 59a7f39bf..f1b8205d0 100644 --- a/src/main/java/org/takes/rq/RqMultipart.java +++ b/src/main/java/org/takes/rq/RqMultipart.java @@ -282,8 +282,8 @@ private Request make(final byte[] boundary, * @checkstyle ExecutableStatementCountCheck (2 lines) */ private void copy(final WritableByteChannel target, - final byte[] boundary, - final ReadableByteChannel body) throws IOException { + final byte[] boundary, final ReadableByteChannel body) + throws IOException { int match = 0; boolean cont = true; while (cont) { From d1bec6a5fff2fc245327d919a78207597fbd46e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dal=C3=AD=20Freire?= Date: Sat, 5 Mar 2016 18:30:40 -0300 Subject: [PATCH 4/4] Indentation --- src/main/java/org/takes/rq/RqMultipart.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/rq/RqMultipart.java b/src/main/java/org/takes/rq/RqMultipart.java index f1b8205d0..d2edc2398 100644 --- a/src/main/java/org/takes/rq/RqMultipart.java +++ b/src/main/java/org/takes/rq/RqMultipart.java @@ -283,7 +283,7 @@ private Request make(final byte[] boundary, */ private void copy(final WritableByteChannel target, final byte[] boundary, final ReadableByteChannel body) - throws IOException { + throws IOException { int match = 0; boolean cont = true; while (cont) {