@Test public void simpleReplace() { File tmpDirLocation = Files.createTempDir(); try { String tmpDir = tmpDirLocation.toString(); ValueFilterHttpGet filter = CodecJSON.decodeString(new ValueFilterHttpGet(), "{cacheSize:5,cacheAge:10000," + "persist:true,persistDir:'" + tmpDir + "',template:'http: for (int i = 0; i < 5; i++) { String search = filter.filter("hello+world+" + i); System.out.println("#" + i + " >> " + (search != null ? search.length() : "null")); } for (int i = 0; i < 10; i++) { String search = filter.filter("hello+world+" + i); System.out.println("#" + i + " >> " + (search != null ? search.length() : "null")); } } catch (Exception e) { e.printStackTrace(); } finally { if (tmpDirLocation != null) { LessFiles.deleteDir(tmpDirLocation); } } }
@Override public String filter(String sv) { if (sv == null) { return null; } CacheObject cached = cacheGet(sv); if (cached == null || (cacheAge > 0 && System.currentTimeMillis() - cached.time > cacheAge)) { if (log.isDebugEnabled() && cached != null && cacheAge > 0 && System.currentTimeMillis() - cached.time > cacheAge) { log.debug("aging out, replacing " + cached.hash + " or " + cached.key); } int retries = retry; while (retries-- > 0) { try { String replacement = template.replace("{{}}", sv); byte[] val = httpGet(replacement, null, null, timeout, trace); if (val != null && (emptyOk || val.length > 0)) { cached = cachePut(sv, LessBytes.toString(val)); break; } else if (trace) { log.error("{} returned {} retries left = {}", replacement, (val != null ? val.length : -1), retries); } } catch (IOException e) { e.printStackTrace(); } try { Thread.sleep(retryTimeout); } catch (InterruptedException e) { e.printStackTrace(); } } if (cached == null && missValue != null) { cachePut(sv, missValue); } } return cached != null ? cached.data : null; }
ValueFilterHttpGet extends StringFilter implements SuperCodable { @Override public String filter(String sv) { if (sv == null) { return null; } CacheObject cached = cacheGet(sv); if (cached == null || (cacheAge > 0 && System.currentTimeMillis() - cached.time > cacheAge)) { if (log.isDebugEnabled() && cached != null && cacheAge > 0 && System.currentTimeMillis() - cached.time > cacheAge) { log.debug("aging out, replacing " + cached.hash + " or " + cached.key); } int retries = retry; while (retries-- > 0) { try { String replacement = template.replace("{{}}", sv); byte[] val = httpGet(replacement, null, null, timeout, trace); if (val != null && (emptyOk || val.length > 0)) { cached = cachePut(sv, LessBytes.toString(val)); break; } else if (trace) { log.error("{} returned {} retries left = {}", replacement, (val != null ? val.length : -1), retries); } } catch (IOException e) { e.printStackTrace(); } try { Thread.sleep(retryTimeout); } catch (InterruptedException e) { e.printStackTrace(); } } if (cached == null && missValue != null) { cachePut(sv, missValue); } } return cached != null ? cached.data : null; } }
ValueFilterHttpGet extends StringFilter implements SuperCodable { @Override public String filter(String sv) { if (sv == null) { return null; } CacheObject cached = cacheGet(sv); if (cached == null || (cacheAge > 0 && System.currentTimeMillis() - cached.time > cacheAge)) { if (log.isDebugEnabled() && cached != null && cacheAge > 0 && System.currentTimeMillis() - cached.time > cacheAge) { log.debug("aging out, replacing " + cached.hash + " or " + cached.key); } int retries = retry; while (retries-- > 0) { try { String replacement = template.replace("{{}}", sv); byte[] val = httpGet(replacement, null, null, timeout, trace); if (val != null && (emptyOk || val.length > 0)) { cached = cachePut(sv, LessBytes.toString(val)); break; } else if (trace) { log.error("{} returned {} retries left = {}", replacement, (val != null ? val.length : -1), retries); } } catch (IOException e) { e.printStackTrace(); } try { Thread.sleep(retryTimeout); } catch (InterruptedException e) { e.printStackTrace(); } } if (cached == null && missValue != null) { cachePut(sv, missValue); } } return cached != null ? cached.data : null; } @VisibleForTesting  ValueFilterHttpGet(); }
ValueFilterHttpGet extends StringFilter implements SuperCodable { @Override public String filter(String sv) { if (sv == null) { return null; } CacheObject cached = cacheGet(sv); if (cached == null || (cacheAge > 0 && System.currentTimeMillis() - cached.time > cacheAge)) { if (log.isDebugEnabled() && cached != null && cacheAge > 0 && System.currentTimeMillis() - cached.time > cacheAge) { log.debug("aging out, replacing " + cached.hash + " or " + cached.key); } int retries = retry; while (retries-- > 0) { try { String replacement = template.replace("{{}}", sv); byte[] val = httpGet(replacement, null, null, timeout, trace); if (val != null && (emptyOk || val.length > 0)) { cached = cachePut(sv, LessBytes.toString(val)); break; } else if (trace) { log.error("{} returned {} retries left = {}", replacement, (val != null ? val.length : -1), retries); } } catch (IOException e) { e.printStackTrace(); } try { Thread.sleep(retryTimeout); } catch (InterruptedException e) { e.printStackTrace(); } } if (cached == null && missValue != null) { cachePut(sv, missValue); } } return cached != null ? cached.data : null; } @VisibleForTesting  ValueFilterHttpGet(); @Override void postDecode(); @Override void preEncode(); @Override String filter(String sv); static byte[] httpGet(String url, Map<String, String> requestHeaders,
            Map<String, String> responseHeaders, int timeoutms,
            boolean traceError); }
ValueFilterHttpGet extends StringFilter implements SuperCodable { @Override public String filter(String sv) { if (sv == null) { return null; } CacheObject cached = cacheGet(sv); if (cached == null || (cacheAge > 0 && System.currentTimeMillis() - cached.time > cacheAge)) { if (log.isDebugEnabled() && cached != null && cacheAge > 0 && System.currentTimeMillis() - cached.time > cacheAge) { log.debug("aging out, replacing " + cached.hash + " or " + cached.key); } int retries = retry; while (retries-- > 0) { try { String replacement = template.replace("{{}}", sv); byte[] val = httpGet(replacement, null, null, timeout, trace); if (val != null && (emptyOk || val.length > 0)) { cached = cachePut(sv, LessBytes.toString(val)); break; } else if (trace) { log.error("{} returned {} retries left = {}", replacement, (val != null ? val.length : -1), retries); } } catch (IOException e) { e.printStackTrace(); } try { Thread.sleep(retryTimeout); } catch (InterruptedException e) { e.printStackTrace(); } } if (cached == null && missValue != null) { cachePut(sv, missValue); } } return cached != null ? cached.data : null; } @VisibleForTesting  ValueFilterHttpGet(); @Override void postDecode(); @Override void preEncode(); @Override String filter(String sv); static byte[] httpGet(String url, Map<String, String> requestHeaders,
            Map<String, String> responseHeaders, int timeoutms,
            boolean traceError);  }