Search in sources :

Example 1 with LaxRedirectStrategy

use of org.apache.http.impl.client.LaxRedirectStrategy in project Signal-Android by WhisperSystems.

the class LegacyMmsConnection method constructHttpClient.

protected CloseableHttpClient constructHttpClient() throws IOException {
    RequestConfig config = RequestConfig.custom().setConnectTimeout(20 * 1000).setConnectionRequestTimeout(20 * 1000).setSocketTimeout(20 * 1000).setMaxRedirects(20).build();
    URL mmsc = new URL(apn.getMmsc());
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    if (apn.hasAuthentication()) {
        credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()), new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
    }
    return HttpClients.custom().setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4()).setRedirectStrategy(new LaxRedirectStrategy()).setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT)).setConnectionManager(new BasicHttpClientConnectionManager()).setDefaultRequestConfig(config).setDefaultCredentialsProvider(credsProvider).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) NoConnectionReuseStrategyHC4(org.apache.http.impl.NoConnectionReuseStrategyHC4) AuthScope(org.apache.http.auth.AuthScope) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) URL(java.net.URL) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 2 with LaxRedirectStrategy

use of org.apache.http.impl.client.LaxRedirectStrategy in project opennms by OpenNMS.

the class HttpClientWrapper method getClient.

public CloseableHttpClient getClient() {
    if (m_httpClient == null) {
        final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        if (!m_reuseConnections) {
            httpClientBuilder.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
        }
        if (m_usePreemptiveAuth) {
            enablePreemptiveAuth(httpClientBuilder);
        }
        if (m_useSystemProxySettings) {
            httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()));
        }
        if (!isEmpty(m_cookieSpec)) {
            requestConfigBuilder.setCookieSpec(m_cookieSpec);
        }
        if (m_cookieStore != null) {
            httpClientBuilder.setDefaultCookieStore(m_cookieStore);
        }
        if (m_username != null) {
            setCredentials(httpClientBuilder, m_username, m_password);
        }
        if (m_socketTimeout != null) {
            requestConfigBuilder.setSocketTimeout(m_socketTimeout);
        }
        if (m_connectionTimeout != null) {
            requestConfigBuilder.setConnectTimeout(m_connectionTimeout);
        }
        if (m_retries != null) {
            httpClientBuilder.setRetryHandler(new HttpRequestRetryOnExceptionHandler(m_retries, false));
        }
        if (m_sslContext.size() != 0) {
            configureSSLContext(httpClientBuilder);
        }
        for (final HttpRequestInterceptor interceptor : m_requestInterceptors) {
            httpClientBuilder.addInterceptorLast(interceptor);
        }
        for (final HttpResponseInterceptor interceptor : m_responseInterceptors) {
            httpClientBuilder.addInterceptorLast(interceptor);
        }
        if (m_useLaxRedirect) {
            httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
        }
        httpClientBuilder.setDefaultRequestConfig(requestConfigBuilder.build());
        m_httpClient = httpClientBuilder.build();
    }
    return m_httpClient;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) HttpResponseInterceptor(org.apache.http.HttpResponseInterceptor) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner)

Example 3 with LaxRedirectStrategy

use of org.apache.http.impl.client.LaxRedirectStrategy in project opennms by OpenNMS.

the class DiscoveryIT method canDiscoverRemoteNodes.

@Test
public void canDiscoverRemoteNodes() throws ClientProtocolException, IOException {
    Date startOfTest = new Date();
    final String tomcatIp = minionSystem.getContainerInfo(ContainerAlias.TOMCAT).networkSettings().ipAddress();
    final InetSocketAddress opennmsHttp = minionSystem.getServiceAddress(ContainerAlias.OPENNMS, 8980);
    final HttpHost opennmsHttpHost = new HttpHost(opennmsHttp.getAddress().getHostAddress(), opennmsHttp.getPort());
    HttpClient instance = HttpClientBuilder.create().setRedirectStrategy(// Ignore the 302 response to the POST
    new LaxRedirectStrategy()).build();
    Executor executor = Executor.newInstance(instance).auth(opennmsHttpHost, "admin", "admin").authPreemptive(opennmsHttpHost);
    // Configure Discovery with the specific address of our Tomcat server
    // No REST endpoint is currently available to configure the Discovery daemon
    // so we resort to POSTin nasty form data
    executor.execute(Request.Post(String.format("http://%s:%d/opennms/admin/discovery/actionDiscovery?action=AddSpecific", opennmsHttp.getAddress().getHostAddress(), opennmsHttp.getPort())).bodyForm(Form.form().add("specificipaddress", tomcatIp).add("specifictimeout", "2000").add("specificretries", "1").add("initialsleeptime", "30000").add("restartsleeptime", "86400000").add("foreignsource", "NODES").add("location", "MINION").add("retries", "1").add("timeout", "2000").build())).returnContent();
    executor.execute(Request.Post(String.format("http://%s:%d/opennms/admin/discovery/actionDiscovery?action=SaveAndRestart", opennmsHttp.getAddress().getHostAddress(), opennmsHttp.getPort())).bodyForm(Form.form().add("initialsleeptime", "1").add("restartsleeptime", "86400000").add("foreignsource", "NODES").add("location", "MINION").add("retries", "1").add("timeout", "2000").build())).returnContent();
    InetSocketAddress pgsql = minionSystem.getServiceAddress(ContainerAlias.POSTGRES, 5432);
    HibernateDaoFactory daoFactory = new HibernateDaoFactory(pgsql);
    EventDao eventDao = daoFactory.getDao(EventDaoHibernate.class);
    Criteria criteria = new CriteriaBuilder(OnmsEvent.class).eq("eventUei", EventConstants.NEW_SUSPECT_INTERFACE_EVENT_UEI).ge("eventTime", startOfTest).toCriteria();
    await().atMost(1, MINUTES).pollInterval(10, SECONDS).until(DaoUtils.countMatchingCallable(eventDao, criteria), greaterThan(0));
}
Also used : CriteriaBuilder(org.opennms.core.criteria.CriteriaBuilder) HibernateDaoFactory(org.opennms.smoketest.utils.HibernateDaoFactory) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Executor(org.apache.http.client.fluent.Executor) EventDao(org.opennms.netmgt.dao.api.EventDao) InetSocketAddress(java.net.InetSocketAddress) HttpHost(org.apache.http.HttpHost) HttpClient(org.apache.http.client.HttpClient) Criteria(org.opennms.core.criteria.Criteria) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) Date(java.util.Date) Test(org.junit.Test)

Example 4 with LaxRedirectStrategy

use of org.apache.http.impl.client.LaxRedirectStrategy in project cloudstack by apache.

the class HttpClientHelper method createHttpClient.

public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
    final BasicCookieStore cookieStore = new BasicCookieStore();
    return HttpClientBuilder.create().setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry)).setRedirectStrategy(new LaxRedirectStrategy()).setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).setMaxRedirects(maxRedirects).build()).setDefaultCookieStore(cookieStore).setRetryHandler(new StandardHttpRequestRetryHandler()).build();
}
Also used : ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) StandardHttpRequestRetryHandler(org.apache.http.impl.client.StandardHttpRequestRetryHandler)

Aggregations

LaxRedirectStrategy (org.apache.http.impl.client.LaxRedirectStrategy)4 RequestConfig (org.apache.http.client.config.RequestConfig)2 InetSocketAddress (java.net.InetSocketAddress)1 URL (java.net.URL)1 Date (java.util.Date)1 HttpHost (org.apache.http.HttpHost)1 HttpRequestInterceptor (org.apache.http.HttpRequestInterceptor)1 HttpResponseInterceptor (org.apache.http.HttpResponseInterceptor)1 AuthScope (org.apache.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 HttpClient (org.apache.http.client.HttpClient)1 Executor (org.apache.http.client.fluent.Executor)1 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)1 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)1 NoConnectionReuseStrategy (org.apache.http.impl.NoConnectionReuseStrategy)1 NoConnectionReuseStrategyHC4 (org.apache.http.impl.NoConnectionReuseStrategyHC4)1 BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1