use of org.apache.http.HeaderElement in project dubbo by alibaba.
the class RestProtocol method doRefer.
@Override
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
// TODO more configs to add
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
// 20 is the default maxTotal of current PoolingClientConnectionManager
connectionManager.setMaxTotal(url.getParameter(CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXTOTAL));
connectionManager.setDefaultMaxPerRoute(url.getParameter(CONNECTIONS_KEY, HTTPCLIENTCONNECTIONMANAGER_MAXPERROUTE));
if (connectionMonitor == null) {
connectionMonitor = new ConnectionMonitor();
connectionMonitor.start();
}
connectionMonitor.addConnectionManager(connectionManager);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(url.getParameter(CONNECT_TIMEOUT_KEY, DEFAULT_CONNECT_TIMEOUT)).setSocketTimeout(url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT)).build();
SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).setKeepAliveStrategy((response, 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_KEY)) {
return Long.parseLong(value) * 1000;
}
}
return HTTPCLIENT_KEEPALIVEDURATION;
}).setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig).build();
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient);
ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
clients.add(client);
client.register(RpcContextFilter.class);
for (String clazz : COMMA_SPLIT_PATTERN.split(url.getParameter(EXTENSION_KEY, ""))) {
if (!StringUtils.isEmpty(clazz)) {
try {
client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
} catch (ClassNotFoundException e) {
throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
}
}
}
// TODO protocol
ResteasyWebTarget target = client.target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
return target.proxy(serviceType);
}
use of org.apache.http.HeaderElement in project uPortal by Jasig.
the class PortletResourceResponseContextImpl method handleResourceHeader.
/**
* Handles resource response specific headers. Returns true if the header was consumed by this
* method and requires no further processing
*
* @return
*/
protected boolean handleResourceHeader(String key, String value) {
if (ResourceResponse.HTTP_STATUS_CODE.equals(key)) {
this.portletResourceOutputHandler.setStatus(Integer.parseInt(value));
return true;
}
if ("Content-Type".equals(key)) {
final ContentType contentType = ContentType.parse(value);
final Charset charset = contentType.getCharset();
if (charset != null) {
this.portletResourceOutputHandler.setCharacterEncoding(charset.name());
}
this.portletResourceOutputHandler.setContentType(contentType.getMimeType());
return true;
}
if ("Content-Length".equals(key)) {
this.portletResourceOutputHandler.setContentLength(Integer.parseInt(value));
return true;
}
if ("Content-Language".equals(key)) {
final HeaderElement[] parts = BasicHeaderValueParser.parseElements(value, null);
if (parts.length > 0) {
final String localeStr = parts[0].getValue();
final Locale locale = LocaleUtils.toLocale(localeStr);
this.portletResourceOutputHandler.setLocale(locale);
return true;
}
}
return false;
}
use of org.apache.http.HeaderElement in project disconf by knightliao.
the class HttpClientKeepAliveStrategy method getKeepAliveDuration.
@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")) {
try {
return Long.parseLong(value) * 1000;
} catch (NumberFormatException ignore) {
}
}
}
return keepAliveTimeOut * 1000;
}
use of org.apache.http.HeaderElement in project coprhd-controller by CoprHD.
the class NTLMEncryptedEntity method getEncoding.
/**
* Retrieves the character encoding from the entity.
*
* @param entity
* the entity
* @return the encoding
*/
private String getEncoding(HttpEntity entity) {
String encoding = null;
Header header = wrappedEntity.getContentType();
if (header != null) {
for (HeaderElement he : header.getElements()) {
for (NameValuePair nvp : he.getParameters()) {
if (nvp.getName().equals(CHARSET)) {
encoding = nvp.getValue();
}
}
}
}
// The encoding must be UTF-16 or UTF-8 as specified by the NTLM spec.
if (CharEncoding.UTF_16.equals(encoding) || CharEncoding.UTF_8.equals(encoding)) {
return encoding;
} else {
LOG.warn(encoding + " is not a valid character set for NTLM communcation.");
return NTLMUtils.DEFAULT_CHARSET.displayName();
}
}
use of org.apache.http.HeaderElement in project coprhd-controller by CoprHD.
the class NTLMDecryptedEntity method processHeaders.
/**
* Processes the section of the multipart message that is headers.
*
* @param headerBytes
* the header bytes
*/
private void processHeaders(byte[] headerBytes) {
try {
int start = NTLMUtils.indexOf(NTLMConstants.ORIGINAL_CONTENT_AS_BYTES, headerBytes);
int end = NTLMUtils.indexOf(NTLMConstants.NEWLINE_AS_BYTES, headerBytes, start);
String type = null;
String encoding = null;
Header header = BasicLineParser.parseHeader(new String(Arrays.copyOfRange(headerBytes, start, end), NTLMUtils.DEFAULT_CHARSET), null);
for (HeaderElement element : header.getElements()) {
if (element.getName().equals(NTLMConstants.TYPE)) {
type = element.getValue();
}
for (NameValuePair nvp : element.getParameters()) {
if (nvp.getName().equals(NTLMConstants.CHARSET)) {
encoding = nvp.getValue();
} else if (nvp.getName().equals(NTLMConstants.LENGTH)) {
length = Integer.parseInt(nvp.getValue());
}
}
}
if (type == null || encoding == null || length == null) {
throw new Exception("OriginalContent does not contain the necessary values. " + header.getValue());
}
contentType = buildContentTypeHeader(type, encoding);
} catch (Exception e) {
throw new RuntimeException("There was an error extracting values from the oringial content header.", e);
}
}
Aggregations