use of org.apache.http.client.methods.HttpHead in project elasticsearch-analysis-ik by medcl.
the class Monitor method runUnprivileged.
/**
* 监控流程:
* ①向词库服务器发送Head请求
* ②从响应中获取Last-Modify、ETags字段值,判断是否变化
* ③如果未变化,休眠1min,返回第①步
* ④如果有变化,重新加载词典
* ⑤休眠1min,返回第①步
*/
public void runUnprivileged() {
// 超时设置
RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10 * 1000).setConnectTimeout(10 * 1000).setSocketTimeout(15 * 1000).build();
HttpHead head = new HttpHead(location);
head.setConfig(rc);
// 设置请求头
if (last_modified != null) {
head.setHeader("If-Modified-Since", last_modified);
}
if (eTags != null) {
head.setHeader("If-None-Match", eTags);
}
CloseableHttpResponse response = null;
try {
response = httpclient.execute(head);
// 返回200 才做操作
if (response.getStatusLine().getStatusCode() == 200) {
if (((response.getLastHeader("Last-Modified") != null) && !response.getLastHeader("Last-Modified").getValue().equalsIgnoreCase(last_modified)) || ((response.getLastHeader("ETag") != null) && !response.getLastHeader("ETag").getValue().equalsIgnoreCase(eTags))) {
// 远程词库有更新,需要重新加载词典,并修改last_modified,eTags
Dictionary.getSingleton().reLoadMainDict();
last_modified = response.getLastHeader("Last-Modified") == null ? null : response.getLastHeader("Last-Modified").getValue();
eTags = response.getLastHeader("ETag") == null ? null : response.getLastHeader("ETag").getValue();
}
} else if (response.getStatusLine().getStatusCode() == 304) {
// 没有修改,不做操作
// noop
} else {
logger.info("remote_ext_dict {} return bad code {}", location, response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
logger.error("remote_ext_dict {} error!", e, location);
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
}
use of org.apache.http.client.methods.HttpHead in project indy by Commonjava.
the class IndyClientHttp method exists.
public boolean exists(final String path, Supplier<Map<String, String>> querySupplier, final int... responseCodes) throws IndyClientException {
HttpHead request = newJsonHead(buildUrl(baseUrl, querySupplier, path));
ClientMetrics metrics = metricManager.register(request);
connect();
CloseableHttpResponse response = null;
CloseableHttpClient client = null;
try {
client = newClient();
addLoggingMDCToHeaders(request);
response = client.execute(request, newContext());
final StatusLine sl = response.getStatusLine();
if (validResponseCode(sl.getStatusCode(), responseCodes)) {
return true;
} else if (sl.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
return false;
}
metrics.registerErr(sl);
throw new IndyClientException(sl.getStatusCode(), "Error checking existence of: %s.\n%s", path, new IndyResponseErrorDetails(response));
} catch (final IOException e) {
metrics.registerErr(e);
throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
} finally {
metrics.registerEnd(response);
cleanupResources(request, response, client, metrics);
}
}
use of org.apache.http.client.methods.HttpHead in project indy by Commonjava.
the class IndyClientHttp method head.
public Map<String, String> head(final String path, final int... responseCodes) throws IndyClientException {
HttpHead request = newJsonHead(buildUrl(baseUrl, path));
connect();
CloseableHttpResponse response = null;
CloseableHttpClient client = null;
ClientMetrics metrics = metricManager.register(request);
try {
addLoggingMDCToHeaders(request);
client = newClient();
response = client.execute(request, newContext());
final StatusLine sl = response.getStatusLine();
if (!validResponseCode(sl.getStatusCode(), responseCodes)) {
if (sl.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
return null;
}
metrics.registerErr(sl);
throw new IndyClientException(sl.getStatusCode(), "Error executing HEAD: %s. Status was: %d %s (%s)", path, sl.getStatusCode(), sl.getReasonPhrase(), sl.getProtocolVersion());
}
final Map<String, String> headers = new HashMap<>();
for (final Header header : response.getAllHeaders()) {
final String name = header.getName().toLowerCase();
if (!headers.containsKey(name)) {
headers.put(name, header.getValue());
}
}
return headers;
} catch (final IOException e) {
metrics.registerErr(e);
throw new IndyClientException("Indy request failed: %s", e, e.getMessage());
} finally {
metrics.registerEnd(response);
cleanupResources(request, response, client, metrics);
}
}
use of org.apache.http.client.methods.HttpHead in project indy by Commonjava.
the class AutoCreateRepoAndRetrievePomTest method run.
@Test
public void run() throws Exception {
final String testRepo = "test";
final PomRef pom = loadPom("simple.pom", Collections.<String, String>emptyMap());
final String url = server.formatUrl(testRepo, pom.path);
server.expect(url, 200, pom.pom);
final HttpGet get = new HttpGet(url);
CloseableHttpClient client = proxiedHttp();
CloseableHttpResponse response = null;
InputStream stream = null;
try {
response = client.execute(get, proxyContext(USER, PASS));
stream = response.getEntity().getContent();
final String resultingPom = IOUtils.toString(stream);
assertThat(resultingPom, notNullValue());
assertThat(resultingPom, equalTo(pom.pom));
} finally {
IOUtils.closeQuietly(stream);
HttpResources.cleanupResources(get, response, client);
}
final RemoteRepository remoteRepo = this.client.stores().load(new StoreKey(GENERIC_PKG_KEY, StoreType.remote, "httprox_127-0-0-1_" + server.getPort()), RemoteRepository.class);
assertThat(remoteRepo, notNullValue());
assertThat(remoteRepo.getUrl(), equalTo(server.getBaseUri()));
assertThat(remoteRepo.isPassthrough(), equalTo(true));
String pomUrl = this.client.content().contentUrl(remoteRepo.getKey(), testRepo, pom.path) + "?cache-only=true";
HttpHead head = new HttpHead(pomUrl);
client = HttpClients.createDefault();
try {
response = client.execute(head);
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
} finally {
HttpResources.cleanupResources(head, response, client);
}
}
use of org.apache.http.client.methods.HttpHead in project jackrabbit by apache.
the class BindTest method testRebind.
public void testRebind() throws Exception {
String testcol = this.root + "testRebind/";
String subcol1 = testcol + "bindtest1/";
String testres1 = subcol1 + "res1";
String subcol2 = testcol + "bindtest2/";
String testres2 = subcol2 + "res2";
int status;
try {
HttpMkcol mkcol = new HttpMkcol(testcol);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
mkcol = new HttpMkcol(subcol1);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
mkcol = new HttpMkcol(subcol2);
status = this.client.execute(mkcol, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
// create new resource R with path bindtest1/res1
HttpPut put = new HttpPut(testres1);
put.setEntity(new StringEntity("foo", ContentType.create("text/plain", "UTF-8")));
status = this.client.execute(put, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
// enabling version control always makes the resource referenceable
HttpVersionControl versioncontrol = new HttpVersionControl(testres1);
status = this.client.execute(versioncontrol, this.context).getStatusLine().getStatusCode();
assertTrue("status: " + status, status == 200 || status == 201);
URI r1 = this.getResourceId(testres1);
HttpGet get = new HttpGet(testres1);
HttpResponse resp = this.client.execute(get, this.context);
status = resp.getStatusLine().getStatusCode();
assertEquals(200, status);
assertEquals("foo", EntityUtils.toString(resp.getEntity()));
// rebind R with path bindtest2/res2
HttpRebind rebind = new HttpRebind(subcol2, new RebindInfo(testres1, "res2"));
status = this.client.execute(rebind, this.context).getStatusLine().getStatusCode();
assertEquals(201, status);
URI r2 = this.getResourceId(testres2);
get = new HttpGet(testres2);
resp = this.client.execute(get, this.context);
status = resp.getStatusLine().getStatusCode();
assertEquals(200, status);
assertEquals("foo", EntityUtils.toString(resp.getEntity()));
// make sure that rebind did not change the resource-id
assertEquals(r1, r2);
// verify that the initial binding is gone
HttpHead head = new HttpHead(testres1);
status = this.client.execute(head, this.context).getStatusLine().getStatusCode();
assertEquals(404, status);
} finally {
delete(testcol);
}
}
Aggregations