-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
ETagWebRequestor.java
158 lines (141 loc) · 5.29 KB
/
ETagWebRequestor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Copyright (c) 2010-2021 Mark Allen, Norbert Bartels.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.restfb;
import static java.net.HttpURLConnection.HTTP_NOT_MODIFIED;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.Map;
import java.util.function.Supplier;
import com.restfb.util.SoftHashMap;
/**
* WebRequestor with ETag-support.
*
* <p>
* The {@link ETagWebRequestor} caches all <tt>GET</tt>-requests with an ETag header field in a {@link SoftHashMap} and
* uses the ETag on the next request as <code>If-None-Match</code> header field if the same URL is requested.
* </p>
*
* <p>
* Is the response status code 304 (NOT MODIFIED) the old response from cache is used.
* </p>
*
* <p>
* <strong>Attention:</strong> even 304 responses count as request at Facebook and so they count against the throttling
* limits. Facebook suggests to use them for data that change only frequently
* </p>
*
* <p>
* Further information regarding ETag at Facebook can be found here:
* <a href="https://developers.facebook.com/blog/post/627/">https://developers.facebook.com/blog/post/627/</a>
* </p>
*
* <p>
* <strong>Attention 2</strong>: If excessively used with a lot of URLs, the {@link SoftHashMap} can lead to a
* performance degradation
* </p>
*/
public class ETagWebRequestor extends DefaultWebRequestor {
private static Supplier<Map<String, ETagResponse>> mapBuilder = SoftHashMap::new;
final Map<String, ETagResponse> etagCache = Collections.synchronizedMap(mapBuilder.get());
private final ThreadLocal<ETagResponse> currentETagRespThreadLocal = new ThreadLocal<>();
private volatile boolean useCache = true;
@Override
protected void customizeConnection(HttpURLConnection connection) {
if (isUseCache() && connection.getRequestMethod().equals(HttpMethod.GET.name())) {
ETagResponse resp = etagCache.get(connection.getURL().toString());
if (resp != null) {
currentETagRespThreadLocal.set(resp);
connection.addRequestProperty("If-None-Match", resp.getEtag());
}
}
}
@Override
protected Response fetchResponse(HttpURLConnection httpUrlConnection) throws IOException {
try {
if (httpUrlConnection.getRequestMethod().equals(HttpMethod.GET.name())) {
if (httpUrlConnection.getResponseCode() == HTTP_NOT_MODIFIED && currentETagRespThreadLocal.get() != null) {
ETagResponse etagResp = currentETagRespThreadLocal.get();
return new Response(httpUrlConnection.getResponseCode(), etagResp.getBody());
} else {
Response resp = super.fetchResponse(httpUrlConnection);
if (httpUrlConnection.getHeaderField("ETag") != null) {
etagCache.put(httpUrlConnection.getURL().toString(),
new ETagResponse(httpUrlConnection.getHeaderField("ETag"), resp.getBody()));
}
return resp;
}
} else {
return super.fetchResponse(httpUrlConnection);
}
} finally {
currentETagRespThreadLocal.remove();
}
}
/**
* return if cache is used.
*
* @return <code>true</code> if ETag-Cache is used, <code>false</code> if not
*/
public boolean isUseCache() {
return this.useCache;
}
/**
* activate/deactivate the ETag-Cache for the next request.
*
* <p>
* when deactivated, the ETag-Cache is *not* deleted
* </p>
*
* @param useCache
* flag to dis/enable the cache during runtime
*/
public void setUseCache(boolean useCache) {
this.useCache = useCache;
}
/**
* Override the mapSupplier, it needs to be some implementation of the {@link Map} interface.
*
* You have to set this before the {@link ETagWebRequestor} object is created. While building it, the mapSupplier is
* used
*
* @param mapSupplier
* the supplier, that returns a new Map,
*/
public static void setMapSupplier(Supplier<Map<String, ETagResponse>> mapSupplier) {
ETagWebRequestor.mapBuilder = mapSupplier;
}
static class ETagResponse {
public ETagResponse(String etag, String body) {
this.etag = etag;
this.body = body;
}
private final String etag;
private final String body;
public String getEtag() {
return etag;
}
public String getBody() {
return body;
}
}
}