Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 2 files changed
  • 0 comments
  • 1 contributor
106 org.touge.restclient/src/main/java/org/touge/restclient/RestClient.java
@@ -97,7 +97,7 @@
97 97 /**
98 98 * @return input stream of entry
99 99 */
100   - InputStream getInputStream();
  100 + byte [] getContent();
101 101 /**
102 102 * @return headers of entry
103 103 */
@@ -365,6 +365,62 @@ public String getMimeType() {
365 365 }
366 366
367 367 /**
  368 + * Simple cache implementation using a HashMap backend.
  369 + */
  370 + public static class HashMapCache implements RestClient.HttpGETCache {
  371 + protected final int CONTENT_INDEX = 0;
  372 + protected final int HEADERS_INDEX = 1;
  373 + protected final int CODE_INDEX = 2;
  374 + protected final Map<String, Object[]> cache = new HashMap<String, Object[]>();
  375 +
  376 + @Override
  377 + public HttpGETCacheEntry get(final String key) {
  378 + if (cache.containsKey(key)) {
  379 + return new RestClient.HttpGETCacheEntry() {
  380 +
  381 + @Override
  382 + public int getResponseCode() {
  383 + return ((Integer) cache.get(key)[CODE_INDEX]).intValue();
  384 + }
  385 +
  386 + @Override
  387 + public Map<String, List<String>> getHeaders() {
  388 + return (Map<String, List<String>>) cache.get(key)[HEADERS_INDEX];
  389 + }
  390 +
  391 + @Override
  392 + public byte[] getContent() {
  393 + return (byte []) cache.get(key)[CONTENT_INDEX];
  394 + }
  395 + };
  396 + }
  397 +
  398 + return null;
  399 + }
  400 +
  401 + /**
  402 + * Clear the map
  403 + */
  404 + public void clear() {
  405 + cache.clear();
  406 + }
  407 +
  408 + @Override
  409 + public void put(String key, HttpGETCacheEntry entry) {
  410 + if (entry != null) {
  411 + Object [] ov = new Object[3];
  412 + ov[CONTENT_INDEX] = entry.getContent();
  413 + ov[HEADERS_INDEX] = entry.getHeaders();
  414 + ov[CODE_INDEX] = entry.getResponseCode();
  415 +
  416 + cache.put(key, ov);
  417 + } else {
  418 + cache.remove(key);
  419 + }
  420 + }
  421 + }
  422 +
  423 + /**
368 424 * Used to specify a file to upload in a multipart POST.
369 425 *
370 426 */
@@ -679,8 +735,8 @@ public boolean removeConnectionInitializer(ConnectionInitializer initializer) {
679 735
680 736 validateArguments(method, url);
681 737
682   - String httpUrl = url;
683   - if (!url.toLowerCase().startsWith("http://"))
  738 + String httpUrl = url.toLowerCase();
  739 + if (!httpUrl.startsWith("http://") && !httpUrl.startsWith("https://"))
684 740 httpUrl = "http://" + url;
685 741
686 742 StringBuilder debugBuffer = null;
@@ -691,7 +747,10 @@ public boolean removeConnectionInitializer(ConnectionInitializer initializer) {
691 747
692 748 HttpGETCacheEntry cacheEntry = null;
693 749 if (method == HttpMethod.GET && contentCache != null && (cacheEntry = contentCache.get(url)) != null) {
694   - connection = new CachedConnectionProvider(cacheEntry.getInputStream(), cacheEntry.getHeaders(), cacheEntry.getResponseCode());
  750 + connection = new CachedConnectionProvider(cacheEntry.getContent(), cacheEntry.getHeaders(), cacheEntry.getResponseCode());
  751 +
  752 + if (debugStream != null)
  753 + debugMid(debugBuffer, "[CACHE HIT]");
695 754 } else {
696 755 connection = connectionProvider.getConnection(httpUrl);
697 756 connection.setRequestMethod(method.toString());
@@ -834,30 +893,43 @@ public T getContent() throws IOException {
834 893 return null;
835 894 }
836 895
837   - final InputStream inputStream = connection.getInputStream();
  896 + InputStream inputStream = connection.getInputStream();
838 897 final int responseCode = connection.getResponseCode();
839 898 final Map<String, List<String>> headerFields = connection.getHeaderFields();
840   -
  899 + HttpGETCacheEntry entry = null;
841 900 if (contentCache != null) {
842   - contentCache.put(url, new HttpGETCacheEntry() {
  901 + final byte[] buf = readStream(connection.getInputStream());
  902 +
  903 +
  904 + entry = new HttpGETCacheEntry() {
843 905
844 906 @Override
845 907 public int getResponseCode() {
846 908 return responseCode;
847   - }
848   -
849   - @Override
850   - public InputStream getInputStream() {
851   - return inputStream;
852   - }
  909 + }
853 910
854 911 @Override
855 912 public Map<String, List<String>> getHeaders() {
856 913 return headerFields;
857 914 }
858   - });
  915 +
  916 + @Override
  917 + public byte[] getContent() {
  918 + return buf;
  919 + }
  920 + };
  921 + contentCache.put(url, entry);
  922 +
  923 + inputStream = new ByteArrayInputStream(buf);
  924 +
  925 + if (responseBuffer != null)
  926 + debugMid(responseBuffer, "[CACHED RESPONSE]");
  927 + } else if (responseBuffer != null) {
  928 + debugMid(responseBuffer, "[NOT CACHING, INVALID RESPONSE]");
859 929 }
860 930
  931 + if (entry != null)
  932 +
861 933 if (deserializer == null) {
862 934 // If no deserializer is specified, use String.
863 935 T response = (T) RestClient.STRING_DESERIALIZER.deserialize(inputStream, 0, null);
@@ -1663,11 +1735,11 @@ public URLBuilder emitDomain(boolean value) {
1663 1735 */
1664 1736 private final class CachedConnectionProvider extends HttpURLConnection {
1665 1737
1666   - private final InputStream content;
  1738 + private final byte[] content;
1667 1739 private final Map<String, List<String>> headers;
1668 1740 private final int responseCode;
1669 1741
1670   - public CachedConnectionProvider(InputStream content, Map<String, List<String>> headerFields, int responseCode) {
  1742 + public CachedConnectionProvider(byte[] content, Map<String, List<String>> headerFields, int responseCode) {
1671 1743 super(null);
1672 1744 this.content = content;
1673 1745 this.headers = headerFields;
@@ -1703,7 +1775,7 @@ public int getResponseCode() throws IOException {
1703 1775
1704 1776 @Override
1705 1777 public InputStream getInputStream() throws IOException {
1706   - return content;
  1778 + return new ByteArrayInputStream(content);
1707 1779 }
1708 1780
1709 1781 @Override
7 org.touge.restclient/src/test/java/org/touge/restclient/test/CacheTestCase.java
... ... @@ -1,7 +1,6 @@
1 1 package org.touge.restclient.test;
2 2
3 3 import java.io.IOException;
4   -import java.io.InputStream;
5 4 import java.io.PrintWriter;
6 5 import java.util.HashMap;
7 6 import java.util.List;
@@ -123,8 +122,8 @@ public int getResponseCode() {
123 122 }
124 123
125 124 @Override
126   - public InputStream getInputStream() {
127   - return (InputStream) cache.get(key)[CONTENT_INDEX];
  125 + public byte[] getContent() {
  126 + return (byte[]) cache.get(key)[CONTENT_INDEX];
128 127 }
129 128
130 129 @Override
@@ -147,7 +146,7 @@ public void clear() {
147 146 public void put(String key, HttpGETCacheEntry entry) {
148 147 addCalled = true;
149 148 Object [] ov = new Object[3];
150   - ov[CONTENT_INDEX] = entry.getInputStream();
  149 + ov[CONTENT_INDEX] = entry.getContent();
151 150 ov[HEADERS_INDEX] = entry.getHeaders();
152 151 ov[CODE_INDEX] = entry.getResponseCode();
153 152

No commit comments for this range

Something went wrong with that request. Please try again.