use of org.apache.http.client.protocol.HttpClientContext in project opennms by OpenNMS.
the class RestSessionIT method queryUri.
private Header[] queryUri(final String uri, final String header) throws IOException {
final HttpGet httpGet = new HttpGet(getBaseUrl() + uri);
final HttpHost targetHost = new HttpHost(httpGet.getURI().getHost(), httpGet.getURI().getPort(), httpGet.getURI().getScheme());
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("admin", "admin"));
final AuthCache authCache = new BasicAuthCache();
final BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
final CloseableHttpResponse closeableHttpResponse = HttpClients.createDefault().execute(targetHost, httpGet, context);
closeableHttpResponse.close();
return closeableHttpResponse.getHeaders(header);
}
use of org.apache.http.client.protocol.HttpClientContext in project lucene-solr by apache.
the class HttpSolrClient method executeMethod.
protected NamedList<Object> executeMethod(HttpRequestBase method, final ResponseParser processor) throws SolrServerException {
method.addHeader("User-Agent", AGENT);
org.apache.http.client.config.RequestConfig.Builder requestConfigBuilder = HttpClientUtil.createDefaultRequestConfigBuilder();
if (soTimeout != null) {
requestConfigBuilder.setSocketTimeout(soTimeout);
}
if (connectionTimeout != null) {
requestConfigBuilder.setConnectTimeout(connectionTimeout);
}
if (followRedirects != null) {
requestConfigBuilder.setRedirectsEnabled(followRedirects);
}
method.setConfig(requestConfigBuilder.build());
HttpEntity entity = null;
InputStream respBody = null;
boolean shouldClose = true;
try {
// Execute the method.
HttpClientContext httpClientRequestContext = HttpClientUtil.createNewHttpClientRequestContext();
final HttpResponse response = httpClient.execute(method, httpClientRequestContext);
int httpStatus = response.getStatusLine().getStatusCode();
// Read the contents
entity = response.getEntity();
respBody = entity.getContent();
Header ctHeader = response.getLastHeader("content-type");
String contentType;
if (ctHeader != null) {
contentType = ctHeader.getValue();
} else {
contentType = "";
}
// handle some http level checks before trying to parse the response
switch(httpStatus) {
case HttpStatus.SC_OK:
case HttpStatus.SC_BAD_REQUEST:
case // 409
HttpStatus.SC_CONFLICT:
break;
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_MOVED_TEMPORARILY:
if (!followRedirects) {
throw new SolrServerException("Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ").");
}
break;
default:
if (processor == null || "".equals(contentType)) {
throw new RemoteSolrException(baseUrl, httpStatus, "non ok status: " + httpStatus + ", message:" + response.getStatusLine().getReasonPhrase(), null);
}
}
if (processor == null || processor instanceof InputStreamResponseParser) {
// no processor specified, return raw stream
NamedList<Object> rsp = new NamedList<>();
rsp.add("stream", respBody);
rsp.add("closeableResponse", response);
// Only case where stream should not be closed
shouldClose = false;
return rsp;
}
String procCt = processor.getContentType();
if (procCt != null) {
String procMimeType = ContentType.parse(procCt).getMimeType().trim().toLowerCase(Locale.ROOT);
String mimeType = ContentType.parse(contentType).getMimeType().trim().toLowerCase(Locale.ROOT);
if (!procMimeType.equals(mimeType)) {
// unexpected mime type
String msg = "Expected mime type " + procMimeType + " but got " + mimeType + ".";
Header encodingHeader = response.getEntity().getContentEncoding();
String encoding;
if (encodingHeader != null) {
encoding = encodingHeader.getValue();
} else {
// try UTF-8
encoding = "UTF-8";
}
try {
msg = msg + " " + IOUtils.toString(respBody, encoding);
} catch (IOException e) {
throw new RemoteSolrException(baseUrl, httpStatus, "Could not parse response with encoding " + encoding, e);
}
throw new RemoteSolrException(baseUrl, httpStatus, msg, null);
}
}
NamedList<Object> rsp = null;
String charset = EntityUtils.getContentCharSet(response.getEntity());
try {
rsp = processor.processResponse(respBody, charset);
} catch (Exception e) {
throw new RemoteSolrException(baseUrl, httpStatus, e.getMessage(), e);
}
if (httpStatus != HttpStatus.SC_OK) {
NamedList<String> metadata = null;
String reason = null;
try {
NamedList err = (NamedList) rsp.get("error");
if (err != null) {
reason = (String) err.get("msg");
if (reason == null) {
reason = (String) err.get("trace");
}
metadata = (NamedList<String>) err.get("metadata");
}
} catch (Exception ex) {
}
if (reason == null) {
StringBuilder msg = new StringBuilder();
msg.append(response.getStatusLine().getReasonPhrase()).append("\n\n").append("request: ").append(method.getURI());
reason = java.net.URLDecoder.decode(msg.toString(), UTF_8);
}
RemoteSolrException rss = new RemoteSolrException(baseUrl, httpStatus, reason, null);
if (metadata != null)
rss.setMetadata(metadata);
throw rss;
}
return rsp;
} catch (ConnectException e) {
throw new SolrServerException("Server refused connection at: " + getBaseURL(), e);
} catch (SocketTimeoutException e) {
throw new SolrServerException("Timeout occured while waiting response from server at: " + getBaseURL(), e);
} catch (IOException e) {
throw new SolrServerException("IOException occured when talking to server at: " + getBaseURL(), e);
} finally {
if (shouldClose) {
Utils.consumeFully(entity);
}
}
}
use of org.apache.http.client.protocol.HttpClientContext in project lucene-solr by apache.
the class SolrHttpRequestRetryHandler method retryRequest.
@Override
public boolean retryRequest(final IOException exception, final int executionCount, final HttpContext context) {
log.debug("Retry http request {} out of {}", executionCount, this.retryCount);
if (executionCount > this.retryCount) {
log.debug("Do not retry, over max retry count");
return false;
}
if (this.nonRetriableClasses.contains(exception.getClass())) {
log.debug("Do not retry, non retriable class {}", exception.getClass().getName());
return false;
} else {
for (final Class<? extends IOException> rejectException : this.nonRetriableClasses) {
if (rejectException.isInstance(exception)) {
log.debug("Do not retry, non retriable class {}", exception.getClass().getName());
return false;
}
}
}
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final HttpRequest request = clientContext.getRequest();
if (requestIsAborted(request)) {
log.debug("Do not retry, request was aborted");
return false;
}
if (handleAsIdempotent(clientContext)) {
log.debug("Retry, request should be idempotent");
return true;
}
log.debug("Do not retry, no allow rules matched");
return false;
}
use of org.apache.http.client.protocol.HttpClientContext in project sling by apache.
the class TopologyConnectorClient method ping.
/** ping the server and pass the announcements between the two **/
void ping(final boolean force) {
if (autoStopped) {
// then we suppress any further pings!
logger.debug("ping: autoStopped=true, hence suppressing any further pings.");
return;
}
if (force) {
backoffPeriodEnd = -1;
} else if (backoffPeriodEnd > 0) {
if (System.currentTimeMillis() < backoffPeriodEnd) {
logger.debug("ping: not issueing a heartbeat due to backoff instruction from peer.");
return;
} else {
logger.debug("ping: backoff period ended, issuing another ping now.");
}
}
final String uri = connectorUrl.toString() + "." + clusterViewService.getSlingId() + ".json";
if (logger.isDebugEnabled()) {
logger.debug("ping: connectorUrl=" + connectorUrl + ", complete uri=" + uri);
}
final HttpClientContext clientContext = HttpClientContext.create();
final CloseableHttpClient httpClient = createHttpClient();
final HttpPut putRequest = new HttpPut(uri);
// setting the connection timeout (idle connection, configured in seconds)
putRequest.setConfig(RequestConfig.custom().setConnectTimeout(1000 * config.getSocketConnectTimeout()).build());
Announcement resultingAnnouncement = null;
try {
String userInfo = connectorUrl.getUserInfo();
if (userInfo != null) {
Credentials c = new UsernamePasswordCredentials(userInfo);
clientContext.getCredentialsProvider().setCredentials(new AuthScope(putRequest.getURI().getHost(), putRequest.getURI().getPort()), c);
}
Announcement topologyAnnouncement = new Announcement(clusterViewService.getSlingId());
topologyAnnouncement.setServerInfo(serverInfo);
final ClusterView clusterView;
try {
clusterView = clusterViewService.getLocalClusterView();
} catch (UndefinedClusterViewException e) {
// SLING-5030 : then we cannot ping
logger.warn("ping: no clusterView available at the moment, cannot ping others now: " + e);
return;
}
topologyAnnouncement.setLocalCluster(clusterView);
if (force) {
logger.debug("ping: sending a resetBackoff");
topologyAnnouncement.setResetBackoff(true);
}
announcementRegistry.addAllExcept(topologyAnnouncement, clusterView, new AnnouncementFilter() {
public boolean accept(final String receivingSlingId, final Announcement announcement) {
// filter out announcements that are of old cluster instances
// which I dont really have in my cluster view at the moment
final Iterator<InstanceDescription> it = clusterView.getInstances().iterator();
while (it.hasNext()) {
final InstanceDescription instance = it.next();
if (instance.getSlingId().equals(receivingSlingId)) {
// all fine then
return true;
}
}
// then I should also not propagate that announcement anywhere
return false;
}
});
final String p = requestValidator.encodeMessage(topologyAnnouncement.asJSON());
if (logger.isDebugEnabled()) {
logger.debug("ping: topologyAnnouncement json is: " + p);
}
requestValidator.trustMessage(putRequest, p);
if (config.isGzipConnectorRequestsEnabled()) {
// tell the server that the content is gzipped:
putRequest.addHeader("Content-Encoding", "gzip");
// and gzip the body:
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
gzipOut.write(p.getBytes("UTF-8"));
gzipOut.close();
final byte[] gzippedEncodedJson = baos.toByteArray();
putRequest.setEntity(new ByteArrayEntity(gzippedEncodedJson, ContentType.APPLICATION_JSON));
lastRequestEncoding = "gzip";
} else {
// otherwise plaintext:
final StringEntity plaintext = new StringEntity(p, "UTF-8");
plaintext.setContentType(ContentType.APPLICATION_JSON.getMimeType());
putRequest.setEntity(plaintext);
lastRequestEncoding = "plaintext";
}
// independent of request-gzipping, we do accept the response to be gzipped,
// so indicate this to the server:
putRequest.addHeader("Accept-Encoding", "gzip");
final CloseableHttpResponse response = httpClient.execute(putRequest, clientContext);
if (logger.isDebugEnabled()) {
logger.debug("ping: done. code=" + response.getStatusLine().getStatusCode() + " - " + response.getStatusLine().getReasonPhrase());
}
lastStatusCode = response.getStatusLine().getStatusCode();
lastResponseEncoding = null;
if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) {
final Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue() != null && contentEncoding.getValue().contains("gzip")) {
lastResponseEncoding = "gzip";
} else {
lastResponseEncoding = "plaintext";
}
// limiting to 16MB, should be way enough
final String responseBody = requestValidator.decodeMessage(putRequest.getURI().getPath(), response);
if (logger.isDebugEnabled()) {
logger.debug("ping: response body=" + responseBody);
}
if (responseBody != null && responseBody.length() > 0) {
Announcement inheritedAnnouncement = Announcement.fromJSON(responseBody);
final long backoffInterval = inheritedAnnouncement.getBackoffInterval();
if (backoffInterval > 0) {
// then reset the backoffPeriodEnd:
/* minus 1 sec to avoid slipping the interval by a few millis */
this.backoffPeriodEnd = System.currentTimeMillis() + (1000 * backoffInterval) - 1000;
logger.debug("ping: servlet instructed to backoff: backoffInterval=" + backoffInterval + ", resulting in period end of " + new Date(backoffPeriodEnd));
} else {
logger.debug("ping: servlet did not instruct any backoff-ing at this stage");
this.backoffPeriodEnd = -1;
}
if (inheritedAnnouncement.isLoop()) {
if (logger.isDebugEnabled()) {
logger.debug("ping: connector response indicated a loop detected. not registering this announcement from " + inheritedAnnouncement.getOwnerId());
}
if (inheritedAnnouncement.getOwnerId().equals(clusterViewService.getSlingId())) {
if (config.isAutoStopLocalLoopEnabled()) {
// results in connected -> false and representsloop -> true
inheritedAnnouncement = null;
// results in isAutoStopped -> true
autoStopped = true;
}
}
} else {
inheritedAnnouncement.setInherited(true);
if (announcementRegistry.registerAnnouncement(inheritedAnnouncement) == -1) {
if (logger.isDebugEnabled()) {
logger.debug("ping: connector response is from an instance which I already see in my topology" + inheritedAnnouncement);
}
statusDetails = "receiving side is seeing me via another path (connector or cluster) already (loop)";
return;
}
}
resultingAnnouncement = inheritedAnnouncement;
statusDetails = null;
} else {
statusDetails = "no response body received";
}
} else {
statusDetails = "got HTTP Status-Code: " + lastStatusCode;
}
// SLING-2882 : reset suppressPingWarnings_ flag in success case
suppressPingWarnings_ = false;
} catch (IOException e) {
// SLING-2882 : set/check the suppressPingWarnings_ flag
if (suppressPingWarnings_) {
if (logger.isDebugEnabled()) {
logger.debug("ping: got IOException: " + e + ", uri=" + uri);
}
} else {
suppressPingWarnings_ = true;
logger.warn("ping: got IOException [suppressing further warns]: " + e + ", uri=" + uri);
}
statusDetails = e.toString();
} catch (JsonException e) {
logger.warn("ping: got JSONException: " + e);
statusDetails = e.toString();
} catch (RuntimeException re) {
logger.warn("ping: got RuntimeException: " + re, re);
statusDetails = re.toString();
} finally {
putRequest.releaseConnection();
lastInheritedAnnouncement = resultingAnnouncement;
lastPingedAt = System.currentTimeMillis();
try {
httpClient.close();
} catch (IOException e) {
logger.error("disconnect: could not close httpClient: " + e, e);
}
}
}
use of org.apache.http.client.protocol.HttpClientContext in project sling by apache.
the class TopologyConnectorClient method disconnect.
/** Disconnect this connector **/
public void disconnect() {
final String uri = connectorUrl.toString() + "." + clusterViewService.getSlingId() + ".json";
if (logger.isDebugEnabled()) {
logger.debug("disconnect: connectorUrl=" + connectorUrl + ", complete uri=" + uri);
}
if (lastInheritedAnnouncement != null) {
announcementRegistry.unregisterAnnouncement(lastInheritedAnnouncement.getOwnerId());
}
final HttpClientContext clientContext = HttpClientContext.create();
final CloseableHttpClient httpClient = createHttpClient();
final HttpDelete deleteRequest = new HttpDelete(uri);
// setting the connection timeout (idle connection, configured in seconds)
deleteRequest.setConfig(RequestConfig.custom().setConnectTimeout(1000 * config.getSocketConnectTimeout()).build());
try {
String userInfo = connectorUrl.getUserInfo();
if (userInfo != null) {
Credentials c = new UsernamePasswordCredentials(userInfo);
clientContext.getCredentialsProvider().setCredentials(new AuthScope(deleteRequest.getURI().getHost(), deleteRequest.getURI().getPort()), c);
}
requestValidator.trustMessage(deleteRequest, null);
final CloseableHttpResponse response = httpClient.execute(deleteRequest, clientContext);
if (logger.isDebugEnabled()) {
logger.debug("disconnect: done. code=" + response.getStatusLine().getStatusCode() + " - " + response.getStatusLine().getReasonPhrase());
}
// ignoring the actual statuscode though as there's little we can
// do about it after this point
} catch (IOException e) {
logger.warn("disconnect: got IOException: " + e);
} catch (RuntimeException re) {
logger.error("disconnect: got RuntimeException: " + re, re);
} finally {
deleteRequest.releaseConnection();
try {
httpClient.close();
} catch (IOException e) {
logger.error("disconnect: could not close httpClient: " + e, e);
}
}
}
Aggregations