Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#624 - Removing 'excludes' of FindBuging #647

Merged
8 changes: 2 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@
<excludes combine.children="append">
<exclude>checkstyle:/src/site/resources/.*</exclude>
<!--
@todo #574:30min Continue fixing FindBugs violations started in #574.
#574 turned on the FindBugs, temporarily excluding some classes.
@todo #624:30min Continue fixing FindBugs violations started in #624.
#624 turned on the FindBugs, temporarily excluding some classes.
Remove all excludes of FindBugs below and fix the issues.
-->
<exclude>findbugs:org.takes.facets.forward.TkForward$Safe</exclude>
Expand All @@ -330,11 +330,7 @@
<exclude>findbugs:org.takes.tk.TkReadAlways</exclude>
<exclude>findbugs:org.takes.HttpException</exclude>
<exclude>findbugs:org.takes.facets.forward.RsForward</exclude>
<exclude>findbugs:org.takes.facets.fork.FkRegex$2</exclude>
<exclude>findbugs:org.takes.http.FtCLI$3</exclude>
<exclude>findbugs:org.takes.http.MainRemote$1</exclude>
<exclude>findbugs:org.takes.http.MainRemote</exclude>
<exclude>findbugs:org.takes.rq.RqMultipart$Base</exclude>
<exclude>findbugs:org.takes.http.Options</exclude>
</excludes>
</configuration>
Expand Down
60 changes: 46 additions & 14 deletions src/main/java/org/takes/facets/fork/FkRegex.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,7 @@ public Opt<Response> route(final Request req) throws IOException {
if (matcher.matches()) {
resp = new Opt.Single<Response>(
this.target.act(
new RqRegex() {
@Override
public Matcher matcher() {
return matcher;
}
@Override
public Iterable<String> head() throws IOException {
return req.head();
}
@Override
public InputStream body() throws IOException {
return req.body();
}
}
new RqMatcher(matcher, req)
)
);
} else {
Expand All @@ -210,4 +197,49 @@ public InputStream body() throws IOException {
return resp;
}

/**
* Request with a matcher inside.
*
* @author Dali Freire (dalifreire@gmail.com)
* @version $Id$
* @since 0.32.5
*/
private static final class RqMatcher implements RqRegex {

/**
* Matcher.
*/
private final transient Matcher mtr;

/**
* Original request.
*/
private final transient Request req;

/**
* Ctor.
* @param matcher Matcher
* @param request Request
*/
RqMatcher(final Matcher matcher, final Request request) {
this.mtr = matcher;
this.req = request;
}

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

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

@Override
public Matcher matcher() {
return this.mtr;
}
}

}
42 changes: 36 additions & 6 deletions src/main/java/org/takes/http/FtCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,43 @@ private Exit exit(final Exit exit) {
final long max = this.options.lifetime();
return new Exit.Or(
exit,
new Exit() {
@Override
public boolean ready() {
return System.currentTimeMillis() - start > max;
}
}
new Lifetime(start, max)
);
}

/**
* Lifetime exceeded exit.
*
* @author Dali Freire (dalifreire@gmail.com)
* @version $Id$
* @since 0.32.5
*/
private static final class Lifetime implements Exit {

/**
* Start time.
*/
private final transient long start;

/**
* Max lifetime.
*/
private final transient long max;

/**
* Ctor.
* @param start Start time
* @param max Max lifetime
*/
Lifetime(final long start, final long max) {
this.start = start;
this.max = max;
}

@Override
public boolean ready() {
return System.currentTimeMillis() - this.start > this.max;
}
};

}
67 changes: 53 additions & 14 deletions src/main/java/org/takes/http/MainRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,7 @@ public void exec(final MainRemote.Script script) throws Exception {
for (int idx = 0; idx < this.args.length; ++idx) {
passed[idx + 1] = this.args[idx];
}
final Thread thread = new Thread(
new Runnable() {
@Override
public void run() {
try {
method.invoke(null, (Object) passed);
} catch (final InvocationTargetException ex) {
throw new IllegalStateException(ex);
} catch (final IllegalAccessException ex) {
throw new IllegalStateException(ex);
}
}
}
);
final Thread thread = new Thread(new MainMethod(method, passed));
thread.start();
try {
script.exec(
Expand Down Expand Up @@ -163,4 +150,56 @@ public interface Script {
void exec(URI home) throws IOException;
}

/**
* Runnable main method.
*
* @author Dali Freire (dalifreire@gmail.com)
* @version $Id$
* @since 0.32.5
*/
private static final class MainMethod implements Runnable {

/**
* Method.
*/
private final transient Method method;

/**
* Additional arguments.
*/
private final transient String[] passed;

/**
* Ctor.
* @param method Main method
* @param passed Additional arguments to be passed to the main method
*/
MainMethod(final Method method, final String... passed) {
this.method = method;
this.passed = Arrays.copyOf(passed, passed.length);
}

@Override
public void run() {
try {
this.method.invoke(null, (Object) this.passed);
} catch (final InvocationTargetException ex) {
throw new IllegalStateException(
String.format(
"The %s method has been invoked at an illegal time.",
this.method.getName()
),
ex
);
} catch (final IllegalAccessException ex) {
throw new IllegalStateException(
String.format(
"The visibility of the %s method do not allow access.",
this.method.getName()
),
ex
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dalifreire this indentation looks strange to me :) and a few lines above too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yegor256 Fixed, thanks! :)

}
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rq/RqMultipart.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private Map<String, List<Request>> requests(
);
}
final byte[] boundary = String.format(
"\r\n--%s", matcher.group(1)
"\r%n--%s", matcher.group(1)
).getBytes(StandardCharsets.UTF_8);
this.buffer.flip();
this.buffer.position(boundary.length - 2);
Expand Down