Search in sources :

Example 26 with HeaderElement

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);
}
Also used : TIMEOUT_KEY(org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY) DEFAULT_CONNECT_TIMEOUT(org.apache.dubbo.remoting.Constants.DEFAULT_CONNECT_TIMEOUT) AbstractProxyProtocol(org.apache.dubbo.rpc.protocol.AbstractProxyProtocol) INTERFACE_KEY(org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) SERVER_KEY(org.apache.dubbo.remoting.Constants.SERVER_KEY) SocketConfig(org.apache.http.config.SocketConfig) RequestConfig(org.apache.http.client.config.RequestConfig) COMMA_SPLIT_PATTERN(org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN) BootstrapListener(org.apache.dubbo.remoting.http.servlet.BootstrapListener) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget) ProtocolServer(org.apache.dubbo.rpc.ProtocolServer) StringUtils(org.apache.dubbo.common.utils.StringUtils) URL(org.apache.dubbo.common.URL) Map(java.util.Map) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) GetRestful(org.jboss.resteasy.util.GetRestful) LinkedList(java.util.LinkedList) HttpBinder(org.apache.dubbo.remoting.http.HttpBinder) HTTP(org.apache.http.protocol.HTTP) HeaderElementIterator(org.apache.http.HeaderElementIterator) DEFAULT_TIMEOUT(org.apache.dubbo.common.constants.CommonConstants.DEFAULT_TIMEOUT) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) EXTENSION_KEY(org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY) ApplicationModel(org.apache.dubbo.rpc.model.ApplicationModel) CONNECTIONS_KEY(org.apache.dubbo.remoting.Constants.CONNECTIONS_KEY) RpcException(org.apache.dubbo.rpc.RpcException) HeaderElement(org.apache.http.HeaderElement) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CONNECT_TIMEOUT_KEY(org.apache.dubbo.remoting.Constants.CONNECT_TIMEOUT_KEY) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) ServletManager(org.apache.dubbo.remoting.http.servlet.ServletManager) ProcessingException(javax.ws.rs.ProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) ServletContext(javax.servlet.ServletContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Collections(java.util.Collections) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ResteasyClient(org.jboss.resteasy.client.jaxrs.ResteasyClient) SocketConfig(org.apache.http.config.SocketConfig) HeaderElement(org.apache.http.HeaderElement) ApacheHttpClient4Engine(org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) HeaderElementIterator(org.apache.http.HeaderElementIterator) RpcException(org.apache.dubbo.rpc.RpcException) ResteasyWebTarget(org.jboss.resteasy.client.jaxrs.ResteasyWebTarget)

Example 27 with HeaderElement

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;
}
Also used : Locale(java.util.Locale) ContentType(org.apache.http.entity.ContentType) HeaderElement(org.apache.http.HeaderElement) Charset(java.nio.charset.Charset)

Example 28 with HeaderElement

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;
}
Also used : BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) HeaderElementIterator(org.apache.http.HeaderElementIterator) HeaderElement(org.apache.http.HeaderElement) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator)

Example 29 with HeaderElement

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();
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HeaderElement(org.apache.http.HeaderElement)

Example 30 with HeaderElement

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);
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) BasicHeaderElement(org.apache.http.message.BasicHeaderElement) HeaderElement(org.apache.http.HeaderElement) IOException(java.io.IOException)

Aggregations

HeaderElement (org.apache.http.HeaderElement)58 Header (org.apache.http.Header)17 NameValuePair (org.apache.http.NameValuePair)14 HeaderElementIterator (org.apache.http.HeaderElementIterator)10 BasicHeaderElementIterator (org.apache.http.message.BasicHeaderElementIterator)10 HttpResponse (org.apache.http.HttpResponse)9 MalformedCookieException (org.apache.http.cookie.MalformedCookieException)9 ParserCursor (org.apache.http.message.ParserCursor)9 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 Cookie (org.apache.http.cookie.Cookie)5 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)5 HttpContext (org.apache.http.protocol.HttpContext)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ConnectionKeepAliveStrategy (org.apache.http.conn.ConnectionKeepAliveStrategy)4 HashSet (java.util.HashSet)3 NoSuchElementException (java.util.NoSuchElementException)3 ParseException (org.apache.http.ParseException)3 ProtocolException (org.apache.http.ProtocolException)3