-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFrontCacheAgent.java
More file actions
198 lines (161 loc) · 5.97 KB
/
FrontCacheAgent.java
File metadata and controls
198 lines (161 loc) · 5.97 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Copyright 2017 Eternita LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.frontcache.agent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.RedirectStrategy;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.TrustStrategy;
public class FrontCacheAgent {
private String frontCacheURL;
private String frontCacheURI;
private String siteKey = "";
private final static String IO_URI = "frontcache-io";
private final static String INVALIDATE = "invalidate";
private HttpClient client;
public FrontCacheAgent(String frontcacheURL) {
final RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(10000)
.setConnectTimeout(3000)
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.build();
ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator
(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase
("timeout")) {
return Long.parseLong(value) * 1000;
}
}
return 10 * 1000;
}
};
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = null;
try {
sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
} catch (Exception e) {
e.printStackTrace();
}
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
client = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false))
.setKeepAliveStrategy(keepAliveStrategy)
.setRedirectStrategy(new RedirectStrategy() {
@Override
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
return false;
}
@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
return null;
}
})
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.setSSLSocketFactory(csf)
.build();
this.frontCacheURL = frontcacheURL;
if (frontcacheURL.endsWith("/"))
this.frontCacheURI = frontcacheURL + IO_URI;
else
this.frontCacheURI = frontcacheURL + "/" + IO_URI;
}
public FrontCacheAgent(String frontcacheURL, String siteKey) {
this(frontcacheURL);
this.siteKey = siteKey;
}
/**
*
* @param filter
* @return
*/
public String removeFromCache(String filter)
{
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("action", INVALIDATE));
urlParameters.add(new BasicNameValuePair("filter", filter));
try {
return requestFrontCache(urlParameters);
} catch (Exception e) {
e.printStackTrace();
return "ERROR " + e.getMessage();
}
}
/**
*
* @param urlParameters
* @return
* @throws IOException
*/
protected String requestFrontCache(List<NameValuePair> urlParameters) throws IOException
{
HttpPost post = new HttpPost(frontCacheURI);
// post.addHeader("Accept-Encoding", "gzip");
if (null != siteKey)
post.addHeader("X-frontcache.site-key", siteKey);
post.setEntity(new UrlEncodedFormEntity(urlParameters));
StringBuffer result = new StringBuffer();
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
return result.toString();
}
public String getFrontCacheURL() {
return frontCacheURL;
}
@Override
public String toString() {
return "FrontCacheAgent [" + frontCacheURL + "]";
}
}