Skip to content

Commit

Permalink
fixed a problem with the Base64 encoder (L2 cached results contained …
Browse files Browse the repository at this point in the history
…the decoded content, attempted to re-decode a decoded message caused a problem)
  • Loading branch information
Amir Kibbar committed Dec 23, 2016
1 parent 3395907 commit 1802237
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 49 deletions.
8 changes: 5 additions & 3 deletions src/main/java/ajk/ghcache/services/CacheService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
import org.apache.http.HeaderElement;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.bouncycastle.util.encoders.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
Expand Down Expand Up @@ -44,6 +45,7 @@ public class CacheService {

private Pattern cacheMaxAge = Pattern.compile(".*max-age=(\\p{Digit}*).*", CASE_INSENSITIVE);

@Cacheable(value = "responses", unless = "#result == null || #root.args[1]")
public CachedResponse fetch(String path, boolean force) {
CachedResponse response;
if (force) {
Expand Down Expand Up @@ -130,7 +132,7 @@ private CachedResponse fetchFromRemote(String path) {
}

private String decompress(String src) {
ByteArrayInputStream in = new ByteArrayInputStream(Base64.decode(src.getBytes()));
ByteArrayInputStream in = new ByteArrayInputStream(Base64.getDecoder().decode(src.getBytes()));
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (GZIPInputStream gzip = new GZIPInputStream(in)) {
copy(gzip, out);
Expand All @@ -151,7 +153,7 @@ private String compress(String src) {
return "";
}

return new String(Base64.encode(out.toByteArray()));
return new String(Base64.getEncoder().encode(out.toByteArray()));
}

private String readNextLink(Header linkHeader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.commons.logging.Log;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.io.IOException;
Expand Down Expand Up @@ -36,21 +35,20 @@ public class ConsulResponseRepository implements ResponseRepository {
private MetricRegistry metrics;

public CachedResponse store(String path, CachedResponse result) {
log.info("caching " + path + ", with result " + result.getContent());
log.info("caching " + path);

try {
String storeUrl = props.getConsulUrl().toString() + "/v1/kv/" + props.getConsulKVRoot() + path;
HttpResponse response = newInstance().execute(Put(storeUrl)
.bodyString(mapper.writeValueAsString(result), TEXT_PLAIN)).returnResponse();
log.info(path + ": " + response.getStatusLine());
} catch (IOException e) {
log.warn("couldn't cache " + result + " in path " + path + ", " + e.getMessage(), e);
log.warn("couldn't cache path " + path + ", " + e.getMessage(), e);
}

return result;
}

@Cacheable(value = "responses", unless = "#result == null")
public CachedResponse fetch(String path) {
try {
// do some error validation - if the value doesn't exist in Consul - return null
Expand Down
Loading

0 comments on commit 1802237

Please sign in to comment.