Skip to content

Commit

Permalink
#882 Un-exclude findbugs from qulice checks
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Dec 24, 2018
1 parent 7a4efc7 commit 49b612f
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 88 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Expand Up @@ -417,13 +417,6 @@ SOFTWARE.
<configuration>
<excludes combine.children="append">
<exclude>checkstyle:/src/site/resources/.*</exclude>
<!--
@todo #837:30min Un-exclude findbugs from qulice checks
and fix the bugs it detects for the build to pass.
Most of those are related to reliance on default encoding
and synchronization problems.
-->
<exclude>findbugs:.*</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/org/takes/facets/auth/PsToken.java
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.StringReader;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Base64;
import java.util.Iterator;
Expand Down Expand Up @@ -125,18 +126,26 @@ public Opt<Identity> enter(final Request req) throws IOException {
if (!head.isEmpty()) {
final String jwt = head.split(" ", 2)[1].trim();
final String[] parts = jwt.split(dot);
final byte[] jwtheader = parts[0].getBytes();
final byte[] jwtpayload = parts[1].getBytes();
final byte[] jwtsign = parts[2].getBytes();
final byte[] jwtheader = parts[0].getBytes(
Charset.defaultCharset()
);
final byte[] jwtpayload = parts[1].getBytes(
Charset.defaultCharset()
);
final byte[] jwtsign = parts[2].getBytes(Charset.defaultCharset());
final ByteBuffer tocheck = ByteBuffer.allocate(
jwtheader.length + jwtpayload.length + 1
);
tocheck.put(jwtheader).put(".".getBytes()).put(jwtpayload);
tocheck.put(jwtheader).put(".".getBytes(Charset.defaultCharset()))
.put(jwtpayload);
final byte[] checked = this.signature.sign(tocheck.array());
if (Arrays.equals(jwtsign, checked)) {
try (JsonReader rdr = Json.createReader(
new StringReader(
new String(Base64.getDecoder().decode(jwtpayload))
new String(
Base64.getDecoder().decode(jwtpayload),
Charset.defaultCharset()
)
)
)) {
user = new Opt.Single<>(
Expand All @@ -161,7 +170,7 @@ public Response exit(final Response res,
jwtheader.length + jwtpayload.length + 1
);
tosign.put(jwtheader);
tosign.put(".".getBytes());
tosign.put(".".getBytes(Charset.defaultCharset()));
tosign.put(jwtpayload);
final byte[] sign = this.signature.sign(tosign.array());
try (JsonReader reader = Json.createReader(res.body())) {
Expand All @@ -170,9 +179,9 @@ public Response exit(final Response res,
.add(
"jwt", String.format(
"%s.%s.%s",
new String(jwtheader),
new String(jwtpayload),
new String(sign)
new String(jwtheader, Charset.defaultCharset()),
new String(jwtpayload, Charset.defaultCharset()),
new String(sign, Charset.defaultCharset())
)
)
.build();
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/takes/facets/auth/Token.java
Expand Up @@ -24,6 +24,7 @@
package org.takes.facets.auth;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.Calendar;
import java.util.TimeZone;
Expand Down Expand Up @@ -92,7 +93,9 @@ public JsonObject json() {

@Override
public byte[] encoded() throws IOException {
return Base64.getEncoder().encode(this.joseo.toString().getBytes());
return Base64.getEncoder().encode(
this.joseo.toString().getBytes(Charset.defaultCharset())
);
}
}

Expand Down Expand Up @@ -163,7 +166,9 @@ public JsonObject json() {

@Override
public byte[] encoded() throws IOException {
return Base64.getEncoder().encode(this.jwto.toString().getBytes());
return Base64.getEncoder().encode(
this.jwto.toString().getBytes(Charset.defaultCharset())
);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/auth/signatures/SiHmac.java
Expand Up @@ -147,7 +147,7 @@ private byte[] encrypt(final byte[] bytes) throws IOException {
for (final byte byt : this.create().doFinal(bytes)) {
formatter.format("%02x", byt);
}
return formatter.toString().getBytes();
return formatter.toString().getBytes(Charset.defaultCharset());
}
}

Expand Down
39 changes: 17 additions & 22 deletions src/main/java/org/takes/facets/fork/FkHitRefresh.java
Expand Up @@ -30,6 +30,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import lombok.EqualsAndHashCode;
import org.takes.Request;
Expand Down Expand Up @@ -145,11 +146,6 @@ public Opt<Response> route(final Request req) throws IOException {
* A handle for serving hit-refresh feature.
*/
private static final class HitRefreshHandle {
/**
* A flag File, which is not yet instantiated.
*/
private static final File INEXISTENT_FLAG = new File("/inexistent");

/**
* Directory to watch.
*/
Expand All @@ -159,7 +155,7 @@ private static final class HitRefreshHandle {
* Internal state. Flag file touched on every exec run.
* Instantiated at first touch.
*/
private volatile File flag;
private final List<File> flag;

/**
* A lock for concurrent access to flag file.
Expand All @@ -186,7 +182,7 @@ private HitRefreshHandle(final File dir,
final ReentrantReadWriteLock lock) {
this.dir = dir;
this.lock = lock;
this.flag = HitRefreshHandle.INEXISTENT_FLAG;
this.flag = new CopyOnWriteArrayList<>();
}

/**
Expand All @@ -195,17 +191,15 @@ private HitRefreshHandle(final File dir,
* @throws IOException If fails
*/
public File touchedFile() throws IOException {
this.lock.readLock().lock();
final boolean cold = this.flag == HitRefreshHandle.INEXISTENT_FLAG;
this.lock.readLock().unlock();
if (cold) {
if (this.flag.isEmpty()) {
this.lock.writeLock().lock();
this.flag = File.createTempFile("take", ".txt");
this.flag.deleteOnExit();
final File file = File.createTempFile("take", ".txt");
file.deleteOnExit();
this.flag.add(file);
this.lock.writeLock().unlock();
this.touch();
}
return this.flag;
return this.flag.get(0);
}

/**
Expand All @@ -226,11 +220,8 @@ public void touch() throws IOException {
* @throws IOException If fails
*/
private boolean expired() throws IOException {
this.lock.readLock().lock();
final boolean cold = this.flag == HitRefreshHandle.INEXISTENT_FLAG;
this.lock.readLock().unlock();
boolean expired = false;
if (cold) {
final boolean expired;
if (this.flag.isEmpty()) {
expired = true;
} else {
expired = this.directoryUpdated();
Expand All @@ -246,9 +237,13 @@ private boolean expired() throws IOException {
* in README.md file and must be eliminated.
*/
private boolean directoryUpdated() {
this.lock.readLock().lock();
final long recent = this.flag.lastModified();
this.lock.readLock().unlock();
final long recent;
try {
this.lock.readLock().lock();
recent = this.flag.get(0).lastModified();
} finally {
this.lock.readLock().unlock();
}
final File[] files = this.dir.listFiles();
boolean expired = false;
if (files != null) {
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/org/takes/facets/forward/RsForward.java
Expand Up @@ -25,6 +25,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import lombok.EqualsAndHashCode;
import lombok.ToString;
Expand Down Expand Up @@ -60,7 +62,7 @@ public class RsForward extends HttpException implements Response {
/**
* Original response.
*/
private final transient Response origin;
private final Response origin;

/**
* Ctor.
Expand Down Expand Up @@ -171,4 +173,27 @@ public final Iterable<String> head() throws IOException {
public final InputStream body() throws IOException {
return this.origin.body();
}

/**
* Set default object to write serializated data.
* @param stream Stream to write
* @throws IOException if fails
*/
private static void writeObject(
final ObjectOutputStream stream
) throws IOException {
stream.defaultWriteObject();
}

/**
* Set default object to read serializated data.
* @param stream Stream to read
* @throws IOException if fails
* @throws ClassNotFoundException if class doesn't exists
*/
private static void readObject(
final ObjectInputStream stream
) throws IOException, ClassNotFoundException {
stream.defaultReadObject();
}
}
22 changes: 10 additions & 12 deletions src/main/java/org/takes/facets/forward/TkForward.java
Expand Up @@ -110,19 +110,17 @@ public InputStream body() throws IOException {
* @throws IOException If fails
*/
private Response load() throws IOException {
synchronized (this.saved) {
if (this.saved.isEmpty()) {
Iterable<String> head;
InputStream body;
try {
head = this.origin.head();
body = this.origin.body();
} catch (final RsForward ex) {
head = ex.head();
body = ex.body();
}
this.saved.add(new RsSimple(head, body));
if (this.saved.isEmpty()) {
Iterable<String> head;
InputStream body;
try {
head = this.origin.head();
body = this.origin.body();
} catch (final RsForward ex) {
head = ex.head();
body = ex.body();
}
this.saved.add(new RsSimple(head, body));
}
return this.saved.get(0);
}
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/takes/misc/Expires.java
Expand Up @@ -152,13 +152,10 @@ public Date(final String ptn, final Locale locale,
*/
public Date(final String ptn, final Locale locale,
final java.util.Date expires) {
this.format = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(ptn, locale);
}
};
this.expires = expires;
this.format = ThreadLocal.withInitial(
() -> new SimpleDateFormat(ptn, locale)
);
this.expires = new java.util.Date(expires.getTime());
}

@Override
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/takes/rq/form/RqFormBase.java
Expand Up @@ -124,12 +124,10 @@ private static String decode(final CharSequence txt) {
* @throws IOException If something fails reading or parsing body
*/
private Map<String, List<String>> map() throws IOException {
synchronized (this.saved) {
if (this.saved.isEmpty()) {
this.saved.add(this.freshMap());
}
return this.saved.get(0);
if (this.saved.isEmpty()) {
this.saved.add(this.freshMap());
}
return this.saved.get(0);
}

/**
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/org/takes/rs/RsGzip.java
Expand Up @@ -80,19 +80,17 @@ public InputStream body() throws IOException {
* @throws IOException If fails
*/
private Response make() throws IOException {
synchronized (this.zipped) {
if (this.zipped.isEmpty()) {
this.zipped.add(
new RsWithHeader(
new RsWithBody(
this.origin,
RsGzip.gzip(this.origin.body())
),
"Content-Encoding",
"gzip"
)
);
}
if (this.zipped.isEmpty()) {
this.zipped.add(
new RsWithHeader(
new RsWithBody(
this.origin,
RsGzip.gzip(this.origin.body())
),
"Content-Encoding",
"gzip"
)
);
}
return this.zipped.get(0);
}
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/org/takes/rs/RsPrettyJson.java
Expand Up @@ -85,15 +85,13 @@ public InputStream body() throws IOException {
* @throws IOException If fails
*/
private Response make() throws IOException {
synchronized (this.transformed) {
if (this.transformed.isEmpty()) {
this.transformed.add(
new RsWithBody(
this.origin,
RsPrettyJson.transform(this.origin.body())
)
);
}
if (this.transformed.isEmpty()) {
this.transformed.add(
new RsWithBody(
this.origin,
RsPrettyJson.transform(this.origin.body())
)
);
}
return this.transformed.get(0);
}
Expand Down

0 comments on commit 49b612f

Please sign in to comment.