use of org.apache.http.client.methods.CloseableHttpResponse in project elasticsearch-analysis-ik by medcl.
the class Dictionary method getRemoteWords.
/**
* 从远程服务器上下载自定义词条
*/
private static List<String> getRemoteWords(String location) {
List<String> buffer = new ArrayList<String>();
RequestConfig rc = RequestConfig.custom().setConnectionRequestTimeout(10 * 1000).setConnectTimeout(10 * 1000).setSocketTimeout(60 * 1000).build();
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response;
BufferedReader in;
HttpGet get = new HttpGet(location);
get.setConfig(rc);
try {
response = httpclient.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
String charset = "UTF-8";
// 获取编码,默认为utf-8
if (response.getEntity().getContentType().getValue().contains("charset=")) {
String contentType = response.getEntity().getContentType().getValue();
charset = contentType.substring(contentType.lastIndexOf("=") + 1);
}
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
String line;
while ((line = in.readLine()) != null) {
buffer.add(line);
}
in.close();
response.close();
return buffer;
}
response.close();
} catch (ClientProtocolException e) {
logger.error("getRemoteWords {} error", e, location);
} catch (IllegalStateException e) {
logger.error("getRemoteWords {} error", e, location);
} catch (IOException e) {
logger.error("getRemoteWords {} error", e, location);
}
return buffer;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project jersey by jersey.
the class ApacheConnector method apply.
@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
final HttpUriRequest request = getUriHttpRequest(clientRequest);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request);
try {
final CloseableHttpResponse response;
final HttpClientContext context = HttpClientContext.create();
if (preemptiveBasicAuth) {
final AuthCache authCache = new BasicAuthCache();
final BasicScheme basicScheme = new BasicScheme();
authCache.put(getHost(request), basicScheme);
context.setAuthCache(authCache);
}
response = client.execute(getHost(request), request, context);
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());
final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null ? Statuses.from(response.getStatusLine().getStatusCode()) : Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
final ClientResponse responseContext = new ClientResponse(status, clientRequest);
final List<URI> redirectLocations = context.getRedirectLocations();
if (redirectLocations != null && !redirectLocations.isEmpty()) {
responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1));
}
final Header[] respHeaders = response.getAllHeaders();
final MultivaluedMap<String, String> headers = responseContext.getHeaders();
for (final Header header : respHeaders) {
final String headerName = header.getName();
List<String> list = headers.get(headerName);
if (list == null) {
list = new ArrayList<>();
}
list.add(header.getValue());
headers.put(headerName, list);
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
if (headers.get(HttpHeaders.CONTENT_LENGTH) == null) {
headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength()));
}
final Header contentEncoding = entity.getContentEncoding();
if (headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) {
headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue());
}
}
try {
responseContext.setEntityStream(new HttpClientResponseInputStream(getInputStream(response)));
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, null, e);
}
return responseContext;
} catch (final Exception e) {
throw new ProcessingException(e);
}
}
use of org.apache.http.client.methods.CloseableHttpResponse in project pinot by linkedin.
the class AutoLoadPinotMetricsUtils method getAllTablesFromPinot.
public JsonNode getAllTablesFromPinot() throws IOException {
HttpGet tablesReq = new HttpGet(PINOT_TABLES_ENDPOINT);
LOG.info("Retrieving datasets: {}", tablesReq);
CloseableHttpResponse tablesRes = pinotControllerClient.execute(pinotControllerHost, tablesReq);
JsonNode tables = null;
try {
if (tablesRes.getStatusLine().getStatusCode() != 200) {
throw new IllegalStateException(tablesRes.getStatusLine().toString());
}
InputStream tablesContent = tablesRes.getEntity().getContent();
tables = new ObjectMapper().readTree(tablesContent).get("tables");
} catch (Exception e) {
LOG.error("Exception in loading collections", e);
} finally {
if (tablesRes.getEntity() != null) {
EntityUtils.consume(tablesRes.getEntity());
}
tablesRes.close();
}
return tables;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project pinot by linkedin.
the class AutoLoadPinotMetricsUtils method getSchemaFromTableConfig.
private Schema getSchemaFromTableConfig(String dataset) throws IOException {
Schema schema = null;
HttpGet schemaReq = new HttpGet(String.format(PINOT_SCHEMA_ENDPOINT_TEMPLATE, URLEncoder.encode(dataset, UTF_8)));
LOG.info("Retrieving schema: {}", schemaReq);
CloseableHttpResponse schemaRes = pinotControllerClient.execute(pinotControllerHost, schemaReq);
try {
if (schemaRes.getStatusLine().getStatusCode() != 200) {
LOG.error("Schema {} not found, {}", dataset, schemaRes.getStatusLine().toString());
} else {
InputStream schemaContent = schemaRes.getEntity().getContent();
schema = new org.codehaus.jackson.map.ObjectMapper().readValue(schemaContent, Schema.class);
}
} catch (Exception e) {
LOG.error("Exception in retrieving schema collections, skipping {}", dataset);
} finally {
if (schemaRes.getEntity() != null) {
EntityUtils.consume(schemaRes.getEntity());
}
schemaRes.close();
}
return schema;
}
use of org.apache.http.client.methods.CloseableHttpResponse in project pinot by linkedin.
the class AutoLoadPinotMetricsUtils method getSchemaFromSchemaEndpoint.
private Schema getSchemaFromSchemaEndpoint(String dataset) throws IOException {
Schema schema = null;
HttpGet schemaReq = new HttpGet(String.format(PINOT_SCHEMA_ENDPOINT, URLEncoder.encode(dataset, UTF_8)));
LOG.info("Retrieving schema: {}", schemaReq);
CloseableHttpResponse schemaRes = pinotControllerClient.execute(pinotControllerHost, schemaReq);
try {
if (schemaRes.getStatusLine().getStatusCode() != 200) {
LOG.error("Schema {} not found, {}", dataset, schemaRes.getStatusLine().toString());
} else {
InputStream schemaContent = schemaRes.getEntity().getContent();
schema = new org.codehaus.jackson.map.ObjectMapper().readValue(schemaContent, Schema.class);
}
} catch (Exception e) {
LOG.error("Exception in retrieving schema collections, skipping {}", dataset);
} finally {
if (schemaRes.getEntity() != null) {
EntityUtils.consume(schemaRes.getEntity());
}
schemaRes.close();
}
return schema;
}
Aggregations